Description
CSCI585 HW1: (E)ER diagram construction
In this assignment, you need to create a conceptual (Extended) Entity-Relational [ie.
(E)ER] diagram, to model the scenario described below; note that your design is not
going to be at an implementation level, ie. you don’t have to worry about representing
your design using relational tables (including bridges) – you will only need to come up
with entities and relationships, and possibly also use the (E)ER notation for
supertypes/subtypes.
Please submit your work as a single image file in .jpg or .png form that shows the entire
diagram, via (not via Blackboard). ALSO include a README.txt that
contains any design choices you want to highlight, and/or assumptions you made [if the
(E)ER diagram were ‘code’, this would be ‘comments’].
You can create the (E)ER diagram using any software of your choice, including:
yEd ( )
draw.io (browser-based)
Lucidchart (browser-based)
Vertabelo (also entirely online)
DIA
Project Mogwai ( )
E/R Assistant (Windows only:
)
Visio
– browser-based π
…
D2L Dropbox
https://www.yworks.com/products/yed
https://github.com/mirkosertic/MogwaiERDesignerNG
https://highered.mheducation.com/sites/0072942207/student_view0/e_r_assistant.html
https://erdplus.com/standalone
After constructing the (E)ER diagram, save, or take a screengrab snapshot, submit it [as
a .jpg or .png image file].
Note that you can even draw your diagram legibly on paper and take a photo of it and
submit that – but having said that, I’d encourage you to use a diagramming tool, that
will make your result look professional, and have you follow industry practice.
You need to use Crow’s Foot Notation for the (E)ER diagram. For each relationship,
indicate the cardinality (minimum and maximum participation), also via Crow’s Foot
symbols – use infographic (from www.vivekmchawla.com/) as a guide [you don’t
need to denote cardinality as (1,1) etc., instead, you would use the notation shown in
the infographic, ie. use symbols such as |O and ||].
How much detail should your diagram contain? Use sample as a guide (eg. you do
not need to indicate data types for attributes).
Description [created by Vinutha Ananthachandran, one of our CPs :)]
YouTube is one of the most popular video-sharing platforms that allows its users to
upload, watch, search and share video-based content. It is a complex and highly
scalable system that mainly depends on its users to post videos to grow and earn
profits. A small portion of this system can be described using the following
components.
A user is identified with a unique id, name, email, age, address, etc. YouTube users can
be video creators or video consumers or both. YouTube channels are like dashboards
for users to upload and distribute their unique videos. Channels have a name, owner,
subscription count and the details of when it was created. For simplicity, it is safe to
assume that a channel is created by only one video creator but a video creator can
have multiple channels. Video consumers can subscribe to ‘N’ number of channels; the
details of which is available on both ends (subscribers, creators). Subscription can
either be paid or free, and is identified via a subscription type.
YouTube videos can be categorized into informational videos, entertainment videos,
etc. Informational videos are identified based on keywords and entertainment videos
are identified based on tags. When a user uploads a video, YouTubeβs transcoding
server starts the video encoding process before streaming it on to its platform. After
this
this
the completion of the encoding process, the video is stored, and the video metadata
database is updated with the video details. This data includes a URL, title, thumbnails,
category, duration, description, uploader id, upload date, and upload time.
YouTube content creators earn their revenues from the platform based on the
popularity of their channels and videos. Each video has statistics to gauge popularity.
YouTube statistics data includes likes, dislikes, view count, number of shares and
number of comments. Each video can have ‘N’ number of comments in the comments
section where users post their thoughts.
The comments data includes video id, user id,
comment text, likes, sentiment, and the details of when it was commented. As an
optional source of income, YouTubers can use a third-party sponsor for their videos.
For each video the sponsor details include the sponsor id, name, phone, address and
the amount sponsored.
Your task is to create an (E)ER diagram that accurately represents the various entities
and their relationships described in the above specification. You can also make some
reasonable assumptions (which you’d document in your README) while building your
(E)ERD.
Note that there isn’t a single correct/perfect design, or even a single ‘good’ design –
any design involves tradeoffs which determine their efficacy/viability. You are free to
make intelligent choices about what data (attributes) to store where (entities), and
how to connect all the pieces (relations).
Note too that some requirements stated
above, might not be able to be captured in an (E)ER diagram -this is fine. Be sure to
document your design decisions (they would serve to provide rationale for “why you
created your design the way you did”).
Please do not plagiarize, ‘work together’, etc. If we see that your solution resembles
anything else, we will need to report you to SJACS, etc. How to avoid this? Simply do
your own work! This is a design problem, so you have much latitude in coming up with
one that works – enjoy the process!
Submission checklist:
.jpg or .png pic of your (E)ER diagram
README.txt description file
You can post questions (and answer others’ :)) on Piazza, under ‘hw1’. HAVE FUN!
CSCI585 HW2: SQL
In this HW, you are going to be creating relational tables (with your own rows of data),
and writing queries that will make use of the data.
The HW is an ‘extension’ of HW1, in a way – you can reuse the entities you created in
HW1, OR, you can use a different set of entities if you like [there is no req’mt that you
MUST use your HW1’s tables].
ALL the SQL knowledge/commands you need to answer the questions have been
covered in class! You do NOT need to learn more commands or techniques (eg. use of
‘triggers’) etc. on your own in order to do this HW set.
To run SQL code, you can use one of the three ways mentioned in the lecture notes [a
locally installed DB, or a remote server-based DB via an online shell running in a
browser page, or a cloud DB] to do the problems – including using
, , SQLite Browser, Oracle XE, sqlfiddle, etc.
There are 6 questions below, each is worth 1 point.
Description [created by our own Vinutha, who created HW1 as well :)]
Q1. Find the sponsor who has sponsored the highest amount in YouTube. Display the
sponsorβs name, phone number and the total amount sponsored.
Q2. Find the ratio of likes to views of each video belonging to any of the channels
owned by users having the word βMarvel Entertainmentβ in them. Display the Video
Title, channel name and the ratio in the ascending order of the title.
Q3. Find unique user/s with the total number of paid subscribers greater than 100 for
their channel/s created on 01.01.2023. Display the username, email, channel name
and the subscriber count.
Q4. Find the average sentiment score for each keyword category. Display the keyword
name along with average score such that the highest score is displayed first.
Q5. Find the minimum and maximum age of viewers who watched the most
commented on video on Taylor Swiftβs channel. Display the video title, minimum age
and the maximum age.
Q6. Find all the content creators living in the US who have consistently posted at least
1 video each week of the last month. Display their username, channel/s they own and
https://livesql.oracle.com/ https://bit.io
their total subscriber count.
You can hardcode the subjects just for submission purposes, but your query should
work for ANY such table! Using comments in the code, YOU NEED TO EXPLAIN IN
YOUR OWN WORDS, WHAT THE QUERY DOES (how it works); don’t just say things
like “Now I’m using a WHERE condition”, instead explain what it’s for (why you’re
using it).
Bonus(1(+1) point(s)). For Q2, you will get 1 extra point if you can reformulate the
query in a very different way! If you do this, submit a separate text file with the code,
eg. Q2_v2.sql. If you come up with YET ANOTHER very different way, you can get 1
more bonus point (submit yet another file, eg. Q2_v3.sql).
‘Very different’ means just
that – the approaches do have to be totally distinct, eg. you can’t use NOT to invert an
existing solution, or use IN() instead of OR, etc. Sooo… is this actually possible [to do it
in two, or three, different ways]? Yes! Or, as they say in Minne-so-ttta -“yooo betcha!”
What you need to submit are text files with the SQL commands that you come up
with, one file for each question (Q1.sql, Q2.sql.. Q6.sql). PLEASE MENTION AT THE
TOP OF EACH FILE, which database (eg. livesql, Oracle XE, SQLite..) you used for that
question! Your grader(s) will execute the SQL commands from the text files you
submit, using the same software you used, to see if they produce the expected results.
Please be sure to also include your table creation commands and the row insertion
ones – in other words, make sure your query is self-contained and therefore runnable
“out ofthe box” [so that your grader would not need to create a table before running
your code].
To reiterate, everything you need is in the slides (the material we went through in
class) – you do not need to look up more commands online! Look at each relational
operator, each command, each function, each keyword that we covered, and ask
yourself how it could be of use in constructing your query.
Also, you can ‘build up’ each
query by first creating a subquery (or several), then combining the parts – in other
words, ‘divide and conquer’ is a very useful strategy for creating non-trivial SQL
queries!
You can post questions (and answer others’ :)) on Piazza, under ‘hw2’. ENJOY!
CSCI585 HW3: Geospatial data handling
In this homework, you are going to work with spatial data – you will collect/create (generate/sample) your own spatial data,
visualize it, do queries on it, and visualize the query results.. Hope you have fun with this! Geospatial data handling is a
The exercise will give you a taste of working with spatial data, use of a spatial file format and spatial query functions, all of which
are quite useful from a real-world (or job interview) perspective.
What you need to do is described below in sufficient, but not too much, detail – you’d need to do a bit of reading up and
experimenting, to fill in the gaps. Please post on Piazza, or talk to a TA/CP, or me, if you are unable to proceed at any point!
1. You need to create (generate) longitude,latitude pairs (ie. spatial coordinates) for 12 locations on the USC campus, in addition
to where your home/apartment/dorm room is.
The 12, other than your home, would have to be spread out – use your judgment, try to span the campus area, choose locations
inside or outside, nearby – we don’t want to cover a ‘huge’ region, or at the other extreme, have the points be ‘too close’.
The 12 points do need to be as follows:two depts, from six schools. We have a lot of schools you can choose from –
π
If you are not on campus (if you are a DEN student, live abroad/away from campus), feel free to substitute places around where
you live, for the 12 points other than your residence or work (again, make sure they are not too close to each other or too far
apart). You can pick six categories (eg. park, bike rack, coffee shop, museum, restaurant…), and get two locations for each.
How would you obtain (lat,long) spatial coordinates at a location? By walking around!! Please do NOT click on
to acquire your data! You need to submit a selfie of you [if you don’t want to show your full face,
that’s ok, include just a part :)] at each location, to show that you were there. Bring up page on your phone, it will display your
location – write it down/type it up, along with the location’s name; to get (long,lat), you can also your phone’s compass, or a
downloaded app.
Make a simple (text/on paper) table of your long,lat locations and their names, 13 entries total, as a starting point for the analysis
and viz you will be doing with them.
2. Now that you have 13 coordinates and their label strings (ie. text descriptions such as “CS Dept”,”BME”), you are going to
create a KML file (.kml format, which is XML) out of them using a text editor. Specifically, each location will be a ‘placemark’ in
your .kml file (with a label, and coords). is more detail. The .kml file with the 13 placemarks is going to be your starter file, for
doing visualizations and queries. is a .kml skeleton to get you started (just download, rename and edit it to put in your
coords and labels).
NOTE – keep your labels to be 15 characters or less (including spaces). is the same .kml skeleton in .txt
format, if you’d like to RMB save it instead and rename the file extension from .txt to .xml. NOTE too that in .kml, you specify
(long,lat), instead of the expected (lat,long) [after all, longtitude is what corresponds to ‘x’, and latitude, to ‘y’]! Also, place your 12
coords (not your home one) in KML ‘folders’.
You are going to use Google Earth to visualize the data in your KML file (see #3 below). FYI, as a quick check, you can also
visualize it using page – simply copy and paste your KML data into the textbox on the left, and click ‘Show it on the map’ to
have it be displayed on a map on the right π Or you can do a quick check at instead.
3. on your laptop, install it, bring it up. Load your .kml file into it – that should show you your sampled
locations, on Google Earth’s globe π Take a snapshot (screengrab) of this, for submitting.
4. Install Oracle 11g+Oracle Spatial, or Postgres+PostGIS on your laptop, and browse the docs for the spatial functions and
search for basic tutorials (eg. is a good one).
Windows users: please Postgres v.13.3 from , then PostGIS v.3.1.2 after starting Postgres’ Stack Builder UI. Mac users can do
After you install Postgres+PostGIS, is how you can create a DB, create a table, insert data, query it.
OR, you don’t need to install your own copy of Postgres/PostGIS π DO feel free to do the queries this way!
5. You will use the spatial db software to execute the following two spatial queries that you’ll write:
β’ compute the convex hull for your 13 points [a for a set of 2D points is the smallest convex polygon that contains the point set].
If
you use Oracle, see page; if you decide to use Postgres, read and instead. Use the query’s result polygon’s coords, to create a
polygon in your .kml file (edit the .kml file, add relevant XML to specify the KML polygon’s coords). Load this into Google Earth, visually verify
that all your points are on/inside the convex hull, then take a screenshot. Note that even your data points happen to have a concave perimeter
and/or happen to be self-intersecting, the convex hull, by definition, would be a tight, enclosing boundary (hull) that is a simple convex polygon.
The convex hull is a very useful object – eg. see discussion.. Note: be sure to specify your polygon’s coords as ‘…-118,34 -118,34.1…’ for
example, and not ‘…-118, 34 -118, 34.1…’ [in other words, do not separate long,lat with a space after the comma, ie it can’t be long, lat].
β’ compute the four nearest neighbors of your home/apt/dormroom location [look up the spatial function of your DB, to do this]. Use the query’s
results, to create four line segments in your .kml file: line(home,neighbor1), line(home,neighbor2), line(home,neighbor3), line(home,neighbor4).
Verify this looks correct, using Google Earth, take a snapshot.
Note – it *is* OK to hardcode points, in the above queries! Or, you can create and use a table to store your 13 points in it, then
write queries against the table.
6. Use OpenLayers (a JavaScript API) to visualize your location data. The idea is to store your 13 sampled points, via your web
browser, in a browser cache area in your local machine, where the data would persist [even after you close the browser]; then
you’d read back the stored values, and visualize them, using the OpenLayers API.
To store and load points, you’ll use ‘HTML5
localStorage’, which is a key-value based standard WWW
The API calls are in JavaScript, which you would run via an html page, like so:
If you create an html file [OL.html] and run it (in Chrome, preferably), you’d see this (in Chrome, to see the console, use the threedots dropdown menu on the top right: More tools -> Developer tools) :
Now you’re ready to store, retrieve, and plot your points!
The code in this starter is to show you, roughly, how to do it; you’d need to do it ‘for real’ by modifying it π EVEN IF you
don’t know JS or coding, don’t worry – it’s easy, and fun, to figure it out! In other words, start with the OL.html I’m giving you,
mildly EDIT it to ADD IN your sampled locations, submit that [note: you do NOT have to do extensive JavaScript coding at all!].
The OL.html code is also if you want to play with it; a ‘v2’ version is
If you like, you can do the location visualization, using CodePen [ ] or jsfiddle [ ], and just
submit a URL of your code – the grader would simply copy and paste your URL into their browser, and be able to see your code
and the result. If you do this, submit a README file with your link, instead of submitting OL.html.
7. Using Tommy Trojan as the center, compute (don’t/can’t use GPS!) a set (sequence) of lat-long (ie. spatial) co-ordinates that lie
along a pretty π
If you are on campus, you can go visit TT to get his (long,lat); otherwise, please long-press (for just under a second) on him in
, that will make his coords pop up in a little box.
Create a new KML file with Spirograph curve points [see below], convert the KML to an ESRI ‘shapefile’, visualize the shapefile
data using ArcGIS Online.
To convert your .kml into a shapefile, use this online converter: – the result will be
a .zip [which is what we call ‘shapefile’], which will contain within it, shape data (.shp), a relational table (.dbf), and other optional
files (.shx, .prj, .cpg). is a page on shapefiles, and talks about the various components (.shp, .dbf etc) of a shapefile.
Once you have your shapefile, you can upload it to ArcGIS’ online map creator to view your Spirograph curve-shaped points. To
do so, log on to ArcGIS [after creating a free ‘public’ account], at , then use the ‘Map’ tab –
. Do ‘Add -> Add Layer from File’, and upload your shapefile
.zip, you should see your data overlaid on a map. is a screenshot of roughly what to expect.
What if your shapefile is too large, and you get a ‘Dataset too large’ error while using ArcGIS?In that case, you can import your
shapefile into instead, and create a screenshot (this site will handle large shapefiles).
For the Spirograph curve point creation, use the following parametric equations (with R=6, r=1, a=8):
Using the above equations, loop through t from 0.00 to n*Pi (eg. 2*Pi; note that ‘n’ might need to be more than 2, for the curve to
close on itself; and, t is in radians, not degrees), in steps of 0.01.
That will give you the sequence of (x,y) points that make up the
Spiro curve, which would/should look like the curve in the screengrab below, when R=6, r=1, a=8:
Note – your figure MUST resemble the above, ie. it MUST have 6 loops – so please do not change R, r or a!
In order to center the Spirograph at a given location [TT or other], you need to ADD each (x,y) curve point to the (lat,long) of the
centering location – that will give you valid Spiro-based spatial coords for use in your .kml file [note that it means, in actuality,
you’re adding your location’s (long,lat) to the computed curve’s (y,x)].
You can use any coding language you want, to generate (and
visualize) the curve’s coords: JavaScript, C/C++, Java, Python, , MATLAB, Scala, Haskell, Ruby, R.. You can also use Excel, SAS,
SPSS, JMP etc., for computing [and plotting, if you want to check the results visually] the Spirograph curve points. can help
you a lot π
Payoff – when you do this in KML, what you’ll see is the Spirograph curve superposed on the land imagery – pretty!
PS: is MUCH more on Spirograph (hypocycloid and epicycloid) curves if you are curious. Also, for fun, try changing any of R,
r, a in the code for the equations above [you don’t need to submit the results]!
Here is what you need to submit(as a single .zip file):
* 13 selfies, from step 1 above [if you don’t submit these, you will LOSE 2 points!]
* your .kml file from step 5 above – with the placemarks, convex hull and nearest-neighbor line segments (1 point)
* a text file (.txt or .sql) with your two queries from step 5 – table creation commands (if you use Postgres and directly specify points in your
queries, you won’t have table creation commands, in which case you wouldn’t need to worry about this part), and the queries themselves (2
points)
* screengrabs from steps 3,5 (1 point)
* a .html file (with the OpenLayers code) from step 6, or a CodePen/jsfiddle link (1 point)
* your Spirograph point generation code, the resulting .kml file (“spiro.kml”), shapefile (this needs to be a .zip) and a screenshot (1 point)
CSCI585 HW4: NoSQL
This HW is going to get you familiar with working with NoSQL, ie. use JSON data!
While the data we play with are small, the same software, and steps, apply to data that
might be a billion times (!) or more bigger.
Specifically, you will be using MongoDB Atlas – a cloud-based installation of MongoDB,
which means there is nothing to install π You’ll work within the free tier, which gets
you a cluster of 3 nodes, 500G of storage space, etc, which is quite adequate for this
HW and beyond (please DO continue learning more Mongo commands after the
course).
Start by signing up: [‘Try Free’ at the top right,
or, ‘Start free’].
https://www.mongodb.com/cloud/atlas
Once you sign in, you’ll get your cluster set up, like shown above. There is no need to
set up any further clusters at all, just work with the one that just got set up.
Click on ‘COLLECTIONS’, then ‘Create Database’, call it HW4DB (for ex):
Remember – in NoSQL, a ‘database’ holds a set of ‘collections’, and a ‘collection’ holds a
set of ‘documents’ – this is similar to how, in RDBMS, a database contains a set of
tables, each table contains rows. So a NoSQL document (which is a single JSON object)
is equivalent to a table row, and is a complex version of a row in fact (can contain
nested data, multi-valued data). An array of such documents is what constitutes a
collection (ie. a table).
OK, now that you have a blank DB, let’s add collections [of documents] to it, and do
queries π
Create a collection (‘table’) called ‘HW3Data’ – yes, you’re going to (re)use your
locations data from HW3 π The ‘+’ sign to the right of your DB’s name, helps you
create a collection (we will create several for this HW):
All right, we have a blank collection, next we need to “insert rows”(add documents). All
the way over on the right of the page, there is INSERT DOCUMENT, click on it. That
brings up the following popup, for inserting (typing in) just one document (Mongo
inserts a row-key/PK). You’ll be inserting 12 (locations), so, click on the {} JSON icon π
Clean out what’s there, to end up with a BLANK slate π
Insert your data, like so (an array of objects, each object contains a location you
collected):
[
{
“name”: “Tommy Trojan”,
“popularity”: 100,
“loc”:[34.02,-118.28]
},
{
…
},
{
}
]
As you can see above, you’ll supply (create) a ‘popularity’ key for each location – give it
a value between 0-100 π
Type/paste the data, then click ‘Insert’ – voila! Your documents are now in the
collection.
To query, you’d enter a ‘query filter’ – see the example below (the data is NOT the data
you are using):
Read up (there are tutorials right there on Mongo’s site for ex) on writing queries; look
up ‘GeoJSON’ queries too, that’s what you’d use, for Q3, Q5, Q6 below. Mongo queries
may be simple-looking but they are powerful!
Q1 (1 point): write a (simple!) query to output locations (documents) with a popularity
of >=50. Take a screenshot of the query and results.
Next, modify your HW3’s Spirograph code, to output data in the above format, but
without ‘popularity’ and name, ie. like so:
[
{
“loc”: [-118.28886097539089,34.02121762509463]
},
{
“loc”: [-118.28886090156676,34.02121425075701]
},
…
{
“loc”: …
}
]
Create a second collection (in the same DB) called ‘Spiro’, and insert the above data
into it [you can have the step size be 0.1 so that it’s not too much data – not that Mongo
can’t handle it (it can!), it’s just that you don’t need it].
Create a third collection called ‘NPS’ [ ], grab this data, add it (just
the ):
[optionally, you can paste the JSON on the LHS here, to format it, and to
view it as a tree: ]
Now, we’re going to visualize data, and do spatial querying (and see the results
visually). Click on ‘Charts’, then create a dashboard (which will hold your charts, ie
visualizations), select it – now you can ‘ADD CHART’ to create multiple types of
visualizations of your documents/rows π
https://www.nps.gov/
features array https://www.nps.gov/lib/npmap.js/4.0.0/examples/data/nationalparks.geojson
https://bytes.usc.edu/~saty/tools/jsoned/index.html
Q2 (1 point). Visualize your 12 locations (ie the HW3Data collection), take a
screenshot. To visualize (create a chart), you’d pick a chart type, then drag and drop
document columns (keys) on to the chart’s slots, like so:
Q3 (1 point). Write a query (in the chart area) where you would specify a bounding box
(lower-left location, upper-right location) to display just the northern half of your 12
locations. Take a screenshot of the query and the result.
Q4 (1 point). Bring your Spirograph data into a new map π Take a screenshot.
Q5 (1 point). Specify a triangle (three locations) to display just the Spiro coords inside
your triangle. Take a screenshot of the query and result (this is a cool capability, to
bound spatial search results by specifying an arbitrary polygon).
Q6 (1 point). Bring in all the national parks into another map – cool! All 396 are visible,
on a map of the entire US. Write a query to output just the western half locations (west
of the Rockies). Create a bounding box to do this (like in Q3). Take a screenshot.
You’re done! Submit (in a .zip), Q1.{jpg,png} through Q6.{jpg,png}.
Have fun! Now you know how to insert JSON docs into MongoDB, query, visualize, do
spatial queries. Neat π
CSCI585 HW5: ML
This last hw is on supervised machine learning! As you now know, it’s data-related (lots, and lots, and lots of it), after all π
Here is a summary of what you’ll do: on Google’s ,train a neural network on differentiating between a cat pic and
dog pic,then use the trained network to classify a new (cat-like or dog-like) pic into a cat or dog. This is a ‘soup-to-nuts’
(start to finish) assignment that will get your feet wet (or plunge you in!), doing ML – a VERY valuable skill –
, for example, would involve much more complexity, but would be based on the same workflow.
You are going to carry out ‘supervised learning’, as shown in this annotated graphic [from a book on TensorFlow]:
Below are the steps. Have fun!
1. Use your GMail/GDrive account to log in, go to , click on the ‘+ New’ button at the top left of the
page, look for the ‘Colab’ app [after + New, click on More >, then + Connect more apps] and connect it – this will make the
app [which connects to the mighty Google Cloud on the other end!] be able to access (read, write) files and folders in your
GDrive.
2. You’ll notice that the above step created a folder called Colab Notebooks, inside your GDrive – this is good, because we
can keep Colab-related things nicely organized inside that folder. Colab is a cloud environment (maintained by Google), for
executing Jupyter ‘notebooks’. A Jupyter notebook (.ipynb extension, ‘Iron Python Notebook’) is a JSON file that contains a
Colab
training a selfdriving car
https://drive.google.com/
mix of two types of “cells”- text cells that have Markdown-formatted text and images, and code cells that contain, well, code
π The code can be in Julia, Python, or R (or several other languages, including JavaScript, with appropriate language ‘plugins’
(kernels) installed); for this HW, we’ll use Python notebooks.
3. Within the Colab Notebooks subdir/folder, create a folder called cats-vs-dogs, for the hw:
Now we need DATA [images of cats and dogs] for training and validation, and scripts for training+validation and classifying.
4. Download .zip data file (~85MB), unzip it. You’ll see this structure:
data/
live/
train/
cats/
dogs/
validation/
cats/
dogs/
The train/ folder contains 1000 kitteh images under cats/, and 1000 doggo/pupper ones in dogs/. Have fun, looking atthe
adorable furballs π Obviously you know which is which π A neural network is going to start from scratch, and learn the
difference, just based on these 2000 ‘training dataset’ images.
The validation/ folder contains 400 images each, of more cats
and dogs – these are to feed the trained network, compare its classification answers to the actual answers so we can
compute the accuracy of the training (in our code, we do this after each training epoch, to watch the accuracy build up,
mostly monotonically).
And, live/ is where you’d be placing new (to the NN) images of cats and dogs [that are not in the
training or validation datasets], and use their filenames to ask the network to classify them: an output of 0 means ‘cat’, 1
means ‘dog’. Fun!
Simply drag and drop the data/ folder on to your My Drive/Colab Notebooks/cats-vs-dogs/ area, and wait for about a half
hour for the 2800 (2*(1000+400)) images to be uploaded. After that, you should be seeing this [click inside the train/ and
validation/ folders to see that the cats and dogs pics have been indeed uploaded]:
this
5. OK, time to train a network! Download Jupyter notebook. Drag and drop the notebook into cats-vs-dogs/:
Double click on the notebook, that will open it so you can execute the code in the cell(s).
As you can see, it is a VERY short piece of code [not mine, except annotations and mods I made] where a network is set up
[starting with ‘model = Sequential()’], and the training is done using it [model.fit_generator()].
In the last line, the RESULTS
[learned weights, biases, for each neuron in each layer] are stored on disk as a weights.h5 file [a .h5 file is binary, in the
publicly documented (hierarchical, JSON-like, perfect for storing network weights)].
The code uses the , which runs on graph (dataflow) execution backends such TensorFlow(TF), Theano,
CNTK [here we are running it over TF via the Google cloud]. With Keras, it is possible to
– the TF equivalent (or Theano’s etc.) would be more verbose. As a future exercise, you can try coding the model in this hw,
directly in TF or Theano or CNTK – you should get the same results.
Before you run the code to kick off the training, note that you will be using GPU acceleration on the cloud (results in ~10x
speedup) – cool! You’d do this via ‘Edit -> Notebook settings’. In this notebook, this is already set up (by me), but you can
verify that it’s set:
When you click on the circular ‘play’ button at the left of the cell, the training will start – here is a sped-up version of what
you will get (your numerical values will be different):
this
.hd5 file format
Keras NN library
express NN architectures succintly
The backprop loop runs 50 times ( ) through all the training data. The acc: column shows the accuracy [how close the
training is, to the expected validation/ results], which would be a little over 80% – NOT BAD, for having learned from just
1000 input images for each class!
Click the play button to execute the code! The first time you run it (and anytime after logging out and logging back in), you’d
need to authorize Colab to access GDrive – so a message will show up, under the code cell, asking you to click on a link
whereby you can log in and provide authorization, and copy and paste the authorization code that appears.
Once you do
this, the rest of the code (where the training occurs) will start to run.
0:00 / 0:11
‘epochs’
Scroll down to below the code cell, to watch the training happen. As you can see, it is going to take a short while.
After the 50th epoch, we’re all done training (and validating too, which we did 50 times, once at the end of each epoch). What’s the tangible result, atthe end of our training+validating process? It’s a ‘weights.h5’ file! If you look in your cats-vsdogs/ folder, it should be there:
6. Soooo, what exactly [format and content-wise] is in the weights file? You can find out, by downloading HDFView-2.14.0,
from [grab the binary, from the ‘HDFView+Object 2.14’
column on the left]. Install, and bring up the program. Download the .h5 file from GDrive to your local area (eg. desktop),
then drag and drop it into HDView:
https://support.hdfgroup.org/products/java/release/download.html
Right-click on weights.h5 at the top-left, and do ‘Expand All’:
Neat! We can see the NN columns, and the biases and weights (kernels) for each. Double click on the bias and kernel items
in the second (of the two) dense layers [dense_12, in my case – yours might be named something else], and stagger them so
you can see both:
Computing those floating point numbers is WHAT -EVERY FORM- OF NEURAL NETWORK TRAINING IS ALL ABOUT! A
self-driving car, for example, is also trained the same way, resulting in weights that can classify live traffic data (scary, in my
opinion). Here, collectively (taking all layers into account), it’s those floating point numbers that REPRESENT the
network’s “learning” of telling apart cats and dogs! The “learned” numbers (the .h5 weights file, actually) can be sent to
anyone, who can instantiate a new network (with the same architecture as the one in the training step), and simply re/use
the weights in weights.h5 to start classifying cats and dogs right away – no training necessary.
The weight arrays represent
“catness” and “dogness”, in a sense π We would call the network+weights, a ‘pre-trained model’. In a self-driving car, the
weights would be copied to the that resides in the car.
Q1 [1+1=2 points]. Submit your weights.h5 file. Also, create a submittable screengrab similar to the above [showing values
for the second dense layer (eg. dense_12)]. For fun, click around, examine the arrays in the other layers as well.
Again, it’s all
these values that are the end result of training, on account of iterating and minimizing classification errors through those
epochs.
7. Now for the fun part – finding out how well our network has learned! Download Jupyter notebook, and upload it to
your cats-vs-dogs/ Colab area:
processing hardware
this
When you open classify.ipynb, you can see that it contains Keras code to read the weights file and associate the weights
with a new model (which needs to be 100% identical to the one we had set up, to train), then take a new image’s filename as
input, and predict(model.predict()) whether the image is that of a cat[output: 0], or a dog [output: 1]! Why 0 for cat and 1
for dog? Because ‘c’ comes before ‘d’ alphabetically [or ] π
Supply (upload, into live/) a what1.jpg cat image, and what2.jpg dog image, then execute the cell. Hopefully you’d get a 0, and
1 (for what1.jpg and what2.jpg, respectively). The images can be any resolution (size) and aspect ratio (squarishness), but
nearly-square pics would work best. Try this with pics of your pets, your neighbors’, images from a Google search, even your
drawings/paintings… Isn’tthis cool? Our little network can classify!
Just FYI, note that the classification code in classify.ipynb could have simply been inside a new cell in train.ipynb instead.
The advantage of multiple code cells inside a notebook, as opposed to multiple code blocks in a script, is that in a notebook,
code cells can be independently executed one at a time (usually sequentially) – so if both of our programs were in the same
notebook, we would run the training code first (just once), followed by classification (possibly multiple times); a script on the
other hand, can’t be re/executed in parts.
Q2 [2 points]. Create a screenshot that shows the [correct] classification (you’ll also be submitting your what{1,2}.jpg
images with this).
What about misclassification? After all, we trained with “just” 1000 (not 1000000) images each, for about an 80% accurate
prediction. What if we input ‘difficult’ images, of a cat that looks like it could be labeled a dog, and the other way around?:)
Q3 [2 points]. Get a ‘Corgi’ image [the world’s dogs!], and a ‘dog-like’ cat image [hint, it’s all about the ears!], upload
to live/, attempt to (mis)classify, ie. create incorrect results (where the cat pic outputs a 1, and the dog’s, 0), make a
screenshot. Note that you need to edit the code to point myPic and myPic2 to these image filenames.
1 point bonus. MobileNet-based detection!
Click on web page, wait a few sec (for an alert to pop up) – dog detection! Download the html, and edit it – you’ll see a
small bunch of markup:
because
smartest
this
The page loads TensorFlow, and a pre-trained MobileNet model, off a CDN. For the detection, we specify a local image,
using ‘src=’. You can see I’m specifying ‘chih.jpg’, a Chihuahua pic I downloaded off the web.
You are going to get your own Chihuahua CLOSEUP (crop what you download if you need to), and a muffin, put in each (one
after another), load the page in your browser, get screenshots of the detection.
But first, you need to install Miniconda so that you can run a local server – look under ‘Installing Miniconda’. After
Miniconda is installed, bring up a shell, place script in the directory where you are (in the shell) and type the following to
start a webserver (‘python serveit.py ‘):
After starting the webserver, on your (Chrome) browser, go to localhost:8000/, navigate to your .html – you will see that
your pic is loaded, and labeled! Take screenshots of a correct Chihuahua IDing, correct muffin IDing (as ‘bakery, bakeshop’,
not ‘muffin’, because MobileNet isn’t trained on muffins), and a mis-IDing (Chihuahua face closeup misclassified as a muffin
(ie. ‘bakery, bakeshop’), or the other way around, where a muffin gets labeled as a dog (ANY breed)).
Here’s a checklist of what to submit [as a single .zip file]:
β’ weights.h5, and a screenshot from HDFView
β’ your ‘good’ cat and dog pics, and screenshot that shows proper classification
β’ your ‘trick’ cat and dog pics, and screenshot that shows misclassification
β’ if you do the bonus question: three screenshots
All done – hope you had fun, and learned a lot!
Note – you can continue using Colab to run [on Google’s cloud GPUs!], including ones with
TensorFlow, Keras, PyTorch… etc. ML code.
here
this
all sorts of notebooks

