Computer vision by building a neural network with TensorFlow | Machine Learning Foundations

Google for Developers · Beginner ·📐 ML Fundamentals ·6y ago

Key Takeaways

Builds a neural network with TensorFlow for computer vision tasks

Full Transcript

welcome back to machine learning foundations in the last episode you got an introduction to machine learning and you saw how it works from a programmers perspective by having you create answers and data and letting a computer infer the rules that determine them this is fundamentally different from traditional programming where you have to figure out the rules express them in code and then have them act on data to give answers you created your first neural network a single layer with a single neuron to figure out the relationship between two numbers and then you had an exercise to see if you can extend that to figure out house prices in a simple scenario before we get to this lesson let's take a look at the exercise answer you can find the notebook containing the answer at this URL and I'll step through it next so let's start by looking at the question the idea is for you to build a neural network that will be trained to be able to roughly predict the price of a house if we're saying the cost of a house is a 50k platte fee plus 50k per bedroom so one-bedroom house is a hundred two-bedroom house 150 etc so here's the question so for example we want to build a neural network that can roughly predict the price of a house where the price is a simple formula of 50,000 plus 50,000 per bedroom so one-bedroom house is a hundred a two-bedroom house 150 etc how would we create a neural network that learns that relationship now one way that will make it work better is if you scale the house price down so instead of you trying to figure out like that a one-bedroom house will cost a hundred thousand maybe you could predict that a one-bedroom house will cost one and then you say that's 100 thousand so for example a seven-bedroom house should cost close to four hundred thousand but maybe you can predict a four off of a seven instead of four hundred or four seven so here's the starter code that you were given and let's see what the answer for this was going to look like so here's the answer so you can to define your model in the same way as we did in the lesson a single dense layer that has a single unit with an input shape of one you can compile that with stochastic gradient descent and mean squared error and now we can do an array of just small float values like a one-bedroom house as a hundred thousand two bedroom houses 150,000 three-bedroom houses two and thousand and so on then we can fit the exits to the wise we'll do it over a thousand epochs and we can see what we get for seven so I'll run this now so after a thousand epochs were at 1.2 times 10 to the minus 6 on our loss and our answer is very very close to four it's four point zero zero one six to two so we've trained the neural network now to be able to predict that house price I know it's a very simple formula and house prices aren't really that simple but hopefully this was able to show you how this type of prediction can be done on just a very small amount of data well that was a very simple scenario matching access to wise or getting a house price from an oversimplified formula the key thing here is that the pattern of code that you used creating a neural network compiling it with a loss function and an optimizer and then fitting the data to the labels is the same pattern that you'll use in far more sophisticated scenarios in this video we'll start by looking at computer vision and how a computer can learn to see we'll start simple and we'll build from there so in understanding vision ultimately it's all seeing and recognizing things in a field of view so for example consider the items of clothing here if I were to ask you which ones were shoes you'd probably know right away that it's the high heel and the sneaker but how did you know that how could you teach someone who has never seen a shoe before what a shoe is so that they could then tell the difference what are the rules that make a shoe a shoe looking back at the data how would you come up with the rules to determine a shoe if it's red it's a shoe maybe well that works for this data but we all know that that won't work for all shoes computer vision is the field of taking pixels and recognizing what's in them and the same kind of pattern matching we've been discussing can be used here but in order to train a neural network to recognize the contents of images we need data and we need labelled images fortunately there's an easily available data set called fashion M NIST this has 70,000 images sorted and labeled in ten categories the images are small they're 28 by but despite this they're still recognizable you can tell that this image is a boot and because they're small we can train a neural network quickly using them we'll start by loading the data in care us there's some built-in data sets that can be loaded with a single line of code and here it is Python can return multiple values from a single call and four values are returned here first is a set of training images and training labels and then is a set of test images and test labels you might wonder why our images have been split into two sets there's one for testing and there's one for training the idea here is really simple we'll take a subset of our images in this case 60,000 of the 70,000 that will train the neural network with we'll then use the other 10,000 to see how well it performs on images it has never seen before think about how you would teach someone who has never seen a shoe before what a shoe looks like you'd probably get a bunch of shoes and show them but then there's no point in testing that person on the shoes that you've just shown them because in that case you've already told them that those are shoes instead you get a bunch of stuff that they haven't seen before and have them pick out which ones are shoes it's the same principle here and it's one that you'll commonly see in building neural networks with machine learning so back to our data we have four lists one for training images one for training labels one for test images and one for test labels our images are the 28 by 28 pixel arrays for the clothing items like the boot here and the labels are numbers from 0 to 9 indicating the appropriate item of clothing now think about this for a second why do you think that it would be a number such as 9 and not a piece of text like boot well there's two main answers to this the first is that computers deal better with numbers and we train our neural nets using numbers the second a little more subtle but very important is that by using a string here that said boot would be introducing a bias that the neural network will need to understand the English word for the clothing item but we don't want that bias so we use the number 9 instead of the text for ankle boots which I can then present here a few different languages can you guess what each one is I'll leave that up to you so here's what our neural network definition will look like in the last video you saw a neural network with a single layer containing a single neuron now we have one with three layers the first layer is called a flatten and this is a special type of layer in care us there's no neurons here but the idea of the function is to take the rectangular shape of the data the 28 by 28 and flatten that into one dimensional array that can be processed by the next layer hence the name flattened this is followed by a dense layer now note that there are a hundred and twenty-eight neurons in this layer it's significantly larger than the one neurons single layer that we use last time this is followed in turn by another dense layer this time one with ten neurons in it there's a few important details here that you need to consider and need to remember when designing neural networks to understand what the network is for it's always good to look at the top layer and the bottom layer for their shaping in this case the top layer takes 28 by 28 for its input which is the size of the images in our dataset remember that when building neural networks all data being fed into training has to be the same size in this case it's easy because the data was already in 28 by 28 for us but later you'll see that you often need to pre-process your data to get it ready for training on the output side we have 10 neurons now remember that there were 10 different types of clothing in the data set this is not a coincidence the job of each of these neurons will be to calculate the probability that a piece of clothing is for that particular class you're probably also wondering what these things are well they're another place when neural networks do a lot of math but fortunately most of it is encapsulated in parameters for you by tensorflow layers intensive flow can have an activation function run on them now this is code that executes while the network is learning they can be really useful and can save you a lot of time writing code for yourself the relu function here is pretty simple it basically says that if the output of a neuron is less than zero set it to zero the reason for this is that negative outputs could skew results downstream cancelling out positive outputs elsewhere so we'll just throw them away softmax commonly seen on the final layer if there are multiple categories simply helps you find the most likely candidate remember earlier than I said that each of the neurons on the bottom layer will be a probability that the item of clothing matches that class that could give you something like this and instead of you writing code to look through each value and find the largest softmax we'll set the largest to one and the rest is zero so now to get the correct class you just need to find the one to illustrate the learning a bit think about all of the pixel values which are numbers from 0 to 255 being fed into a neuron called n zero and it's parameters M 0 and C 0 are guests the same happens for every neuron in the network each neuron is initialized with random parameters m and C and when the pixels of each image in the training set and that's all 60,000 of them are fed in one by one and all the totals from all the neurons added up will get an answer the loss function then calculates how good or how bad that answer is and the optimizer then tweaks the MS in the C's of the neurons to give it another try over time the values within the neurons will change so that the answers for the training data are fit as accurately as possible to the labels in the training data this is the same learning that we saw in the single neuron example in the last video for more detail on how this works under the hood check out this excellent video from angie wang and deep learning AI so on the loss function and optimizer we specify them when we compile the model this time we'll use an optimizer called atom which is a particularly good one for a task like this and a loss function called sparse categorical cross entropy note that when you're classifying multiple categories know if 10 here you're gonna need something categorical don't worry if you don't understand what this is what the options are yet just go with it and over time you'll learn good optimizers and loss functions for specifics and areas now next up we just want to fit the training data to its labels but we'll run it for just 5 epochs this time because there's a lot of data there last time when we did the y equals 2x minus 1 we then use model dot predict to test our network now that worked for a single value you can also use model evaluate to pass it in a list of test data it takes the test images gets its prediction compares that to the answer in the test labels and then calculates how many it got right and how many it got wrong returning you an answer it's a nice one line of code to help you test and if you want to see the predictions for yourself even of course do a model dot predict if you pass it in multiple images it will return a list of all of the predictions for the respective images ok so that's a run through your first computer vision neural network let's now see that code in action in a lab and after that you'll be ready to try the next exercise the lab is at this URL and I'll run through it now ok now let's take a look at this fashion amnesty example so here's the collab that you'd be working from the URL was in the video and I'm just gonna step through it so first we can start by importing tensorflow and we'll get this warning that the notebook was not offered by Google that's okay you can run it anyway I've also asked that just to print the tensorflow version so we see what version were working with now at the time I created this tensorflow was at the release candidate to four-tenths flow 2.2 if it takes a little while to run that just keep an eye on this area in the collab and that's where it's assigning you a VM and that will give you status of the VM itself another thing to do when you're training is to take a look at run time and look at the run time type and look at the hardware accelerator that you're using if your training is going to be really really slow this is probably set to none you can actually improve your training by setting that to GPU in this case this is this isn't a particularly sophisticated data set so I'm just going to keep it at the CPU that's run that it's running on okay first of all I'm now going to load the data I'm just going to specify and 1m nest and I'm gonna use the tent the TF Kerris data sets fashion in list I'm going to get my training images and training labels my testing images and testing lay just download them it's pretty quick if we want to take a look at a few of them this code will help you to do that first of all I'm going to take a look at the training labels and then the training images and print them out so we can see what that looks like and if you look kind of closely here you'll see even though it's cropped that it is actually the boot now these images are grayscale so they're stored as values from 0 through 255 so for example the top line of the image here is 28 zeros so that's like all gonna be black this number 9 is the label because that's what we're printing out first the second line is all black the third line is all black etc and when I scroll down and actually look at the image will see the image was there and it's been colorized we can see the dark image is there the dark at the bottom are all these zeros and then this all of these numbers are the numbers representing the pixel values here it is a grayscale but when I'm just color when I'm printing it out here I've colorist it so one thing that you want to do with your data is to normalize it neural networks work best if they're dealing with values between 0 & 1 and as I mentioned the values and the pixels in this image are from 0 to 255 so a simple way to normalize that is just divide by 255 and we'll Python if you have an array as we have here or a list if you just divide the list by 255 it's smart enough to divide all the individual values you don't have to loop through it and do it yourself now we have our neural network as defined with flattening and we have our two dancers 120 8 and 10 so I'll run that and now I will compile my model with my last function my optimizers and then I'll train it and I'm fitting the training images to the training labels and I'm just going to do it for 5 epochs so it's pretty fast we can see even on a CPU it's only taken about 3 seconds per epoch and over here is our accuracy so the first time around it was about 82% accurate which is pretty good because if you think about it it's a one in 10 chance of getting it right you would expect it to be 10% accurate so it started off pretty good the optimizer did a good job of making the first guess but then by measuring the loss and optimizing again we can see that it's increasing so got up to about 89% here once it's done training with those 5 epochs were at 89% varies sometimes are gonna be 91 92 percent depending on the initial initialization but here it's pretty good at 89% but that's at the images that it was trained on so this is where images that it's seen what how will it do with images that it hasn't seen and we can use model dot evaluate to do that so here I've passed that the test images and the test labels it measured its prediction for what I thought it would do and then it measured its against the actual answers and it got a right 86 percent of the time so something here that we've kind of done very simply and in just five epochs it's actually 86 percent accurate with data that had hadn't previously seen so that's pretty good and if you want to work through some other exercises you'll see the slurm there's some exploration exercises further down in the notebook you can work through those yourself and try to figure them out and that will get you ready for the exercise that comes with this video ok so you've now seen how to classify items of clothing from fashion amnesty there's a similar dataset with 28 by 28 grayscale images in ten classes and it's handwriting of the digit 0 through 9 see if you can now build a classifier that recognizes handwriting digits in this exercise don't worry if you're stuck I'll give you the answer in the next video and if you have any questions please leave them in the comments below whatever you do don't forget to hit that subscribe button for more great tensorflow content thank you [Music]

Original Description

Machine Learning Foundations is a free training course where you’ll learn the fundamentals of building machine learned models using TensorFlow. In Episode 2, Google Developers can explore the fundamental patterns of building a neural network and computer vision with Tensorflow. This tutorial has practical exercises for Google Developers on computer vision fundamentals, computer vision foundation, and building a neural network with tensorflow.js. Exercise 1 answer → https://goo.gle/2SAJSay Computer vision example → https://goo.gle/34cHkDk Exercise 2 → https://goo.gle/2xEfFQo TensorFlow is Google’s end-to-end open source machine learning platform. For more videos about TensorFlow, subscribe to the TF YouTube channel → https://goo.gle/TensorFlow Machine Learning Foundations playlist → https://goo.gle/ml-foundations Subscribe to Google Developers → https://goo.gle/developersbuilding neural network with tensorflow
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Google for Developers · Google for Developers · 46 of 60

1 Developer Journey - Sunnyvale DSC Summit ‘19
Developer Journey - Sunnyvale DSC Summit ‘19
Google for Developers
2 How Google is working with students - Sunnyvale DSC Summit ‘19
How Google is working with students - Sunnyvale DSC Summit ‘19
Google for Developers
3 Starting your career in the Cloud - Sunnyvale DSC Summit ‘19
Starting your career in the Cloud - Sunnyvale DSC Summit ‘19
Google for Developers
4 The Solution Challenge  - Sunnyvale DSC Summit ‘19
The Solution Challenge - Sunnyvale DSC Summit ‘19
Google for Developers
5 Firebase - Sunnyvale DSC Summit ‘19
Firebase - Sunnyvale DSC Summit ‘19
Google for Developers
6 Cloud Hero - Sunnyvale DSC Summit ‘19
Cloud Hero - Sunnyvale DSC Summit ‘19
Google for Developers
7 Panel discussion  - Sunnyvale DSC Summit ‘19
Panel discussion - Sunnyvale DSC Summit ‘19
Google for Developers
8 The art of negotiation - Sunnyvale DSC Summit ‘19
The art of negotiation - Sunnyvale DSC Summit ‘19
Google for Developers
9 Courage to care, solve and share - Sunnyvale DSC Summit ‘19
Courage to care, solve and share - Sunnyvale DSC Summit ‘19
Google for Developers
10 Version 9 of Angular, Glass Enterprise Edition 2, path to DX deprecation, & more!
Version 9 of Angular, Glass Enterprise Edition 2, path to DX deprecation, & more!
Google for Developers
11 [DEPRECATING] Introducing a new series (Assistant for Developers Pro Tips)
[DEPRECATING] Introducing a new series (Assistant for Developers Pro Tips)
Google for Developers
12 Detecting memory bugs with HWASan, Bazel 2.1, Next ‘20 session guide, & more!
Detecting memory bugs with HWASan, Bazel 2.1, Next ‘20 session guide, & more!
Google for Developers
13 Why Podcast.app chose a .app domain name
Why Podcast.app chose a .app domain name
Google for Developers
14 Machine Learning Bootcamp Jakarta 2019
Machine Learning Bootcamp Jakarta 2019
Google for Developers
15 Android Studio 3.6, Android 11 Developer Preview, Kubeflow 1.0, & more!
Android Studio 3.6, Android 11 Developer Preview, Kubeflow 1.0, & more!
Google for Developers
16 [DEPRECATING]  Importance of community (Assistant on Air)
[DEPRECATING] Importance of community (Assistant on Air)
Google for Developers
17 Why the Flutter team switched from .io to a .dev domain name
Why the Flutter team switched from .io to a .dev domain name
Google for Developers
18 3 website-building tips from .dev creators
3 website-building tips from .dev creators
Google for Developers
19 Why NimbleDroid chose a .app domain name
Why NimbleDroid chose a .app domain name
Google for Developers
20 Android Platform Codelab, Bazel 2.2, Maps Android Utility Library v1.0, & more!
Android Platform Codelab, Bazel 2.2, Maps Android Utility Library v1.0, & more!
Google for Developers
21 Google for Games Developer Summit: A free, digital experience for game developers
Google for Games Developer Summit: A free, digital experience for game developers
Google for Developers
22 Inspecting Home Graph (Assistant for Developers Pro Tips)
Inspecting Home Graph (Assistant for Developers Pro Tips)
Google for Developers
23 Google for Games Developer Summit Keynote
Google for Games Developer Summit Keynote
Google for Developers
24 Stadia Games & Entertainment presents: Keys to a great game pitch (Google Games Dev Summit)
Stadia Games & Entertainment presents: Keys to a great game pitch (Google Games Dev Summit)
Google for Developers
25 Empowering game developers with Stadia R&D (Google Games Dev Summit)
Empowering game developers with Stadia R&D (Google Games Dev Summit)
Google for Developers
26 Supercharging discoverability with Stadia (Google Games Dev Summit)
Supercharging discoverability with Stadia (Google Games Dev Summit)
Google for Developers
27 Stadia Games & Entertainment presents: Creating for content creators (Google Games Dev Summit)
Stadia Games & Entertainment presents: Creating for content creators (Google Games Dev Summit)
Google for Developers
28 Bringing Destiny to Stadia: A postmortem (Google Games Dev Summit)
Bringing Destiny to Stadia: A postmortem (Google Games Dev Summit)
Google for Developers
29 Live Captioning in Google Slides
Live Captioning in Google Slides
Google for Developers
30 [DEPRECATING]  User engagement for the Google Assistant
[DEPRECATING] User engagement for the Google Assistant
Google for Developers
31 TensorFlow Dev Summit ‘20, Google for Games Dev Summit, Cloud AI Platform Pipelines, & much more!
TensorFlow Dev Summit ‘20, Google for Games Dev Summit, Cloud AI Platform Pipelines, & much more!
Google for Developers
32 Top 5 from the TensorFlow Dev Summit 2020
Top 5 from the TensorFlow Dev Summit 2020
Google for Developers
33 Developer Student Clubs 2019 Turkey Leads Summit
Developer Student Clubs 2019 Turkey Leads Summit
Google for Developers
34 Building simpler payment experiences | Google Pay Plugin for Magento 2
Building simpler payment experiences | Google Pay Plugin for Magento 2
Google for Developers
35 Become A Developer Student Club Lead
Become A Developer Student Club Lead
Google for Developers
36 Firebase Kotlin Extensions, ARM apps on the Android Emulator, Angular v9.1, & more!
Firebase Kotlin Extensions, ARM apps on the Android Emulator, Angular v9.1, & more!
Google for Developers
37 Test suite for Smart Home (Assistant for Developers Pro Tips)
Test suite for Smart Home (Assistant for Developers Pro Tips)
Google for Developers
38 Google Play updates, Bazel 3.0, Business Console for Google Pay, & more!
Google Play updates, Bazel 3.0, Business Console for Google Pay, & more!
Google for Developers
39 How to use error logs (Assistant for Developers Pro Tips)
How to use error logs (Assistant for Developers Pro Tips)
Google for Developers
40 Contact Center AI, Android Studio 4.1 Canary 5, TensorFlow QAT API, & more!
Contact Center AI, Android Studio 4.1 Canary 5, TensorFlow QAT API, & more!
Google for Developers
41 WebView DevTools, Kotlin meets gRPC, Flutter CodePen support, & more! (Episode 200)
WebView DevTools, Kotlin meets gRPC, Flutter CodePen support, & more! (Episode 200)
Google for Developers
42 Offline handling for Smart Home (Assistant for Developers Pro Tips)
Offline handling for Smart Home (Assistant for Developers Pro Tips)
Google for Developers
43 Android 11 Dev Preview 3, Google Fonts for Flutter, Shielded VM, & more!
Android 11 Dev Preview 3, Google Fonts for Flutter, Shielded VM, & more!
Google for Developers
44 Machine Learning Foundations: Ep #1 - What is ML?
Machine Learning Foundations: Ep #1 - What is ML?
Google for Developers
45 Flutter web support updates, BigQuery materialized views, Cloud Spanner emulator, & more!
Flutter web support updates, BigQuery materialized views, Cloud Spanner emulator, & more!
Google for Developers
Computer vision by building a neural network with TensorFlow | Machine Learning Foundations
Computer vision by building a neural network with TensorFlow | Machine Learning Foundations
Google for Developers
47 Machine Learning Foundations: Ep #3 - Convolutions and pooling
Machine Learning Foundations: Ep #3 - Convolutions and pooling
Google for Developers
48 Android 11 Beta plans, Flutter 1.17, Dart 2.8, & much more!
Android 11 Beta plans, Flutter 1.17, Dart 2.8, & much more!
Google for Developers
49 Machine Learning Foundations: Ep #4 - Coding with Convolutional Neural Networks
Machine Learning Foundations: Ep #4 - Coding with Convolutional Neural Networks
Google for Developers
50 Google Developers ML Summit
Google Developers ML Summit
Google for Developers
51 Real-world image classification using convolutional neural networks | Machine Learning Foundations
Real-world image classification using convolutional neural networks | Machine Learning Foundations
Google for Developers
52 Adobe XD support for Flutter, Architecture Framework, temporary closures with Places API, & more!
Adobe XD support for Flutter, Architecture Framework, temporary closures with Places API, & more!
Google for Developers
53 Machine Learning Foundations: Ep #6 - Convolutional cats and dogs
Machine Learning Foundations: Ep #6 - Convolutional cats and dogs
Google for Developers
54 Machine Learning Foundations: Ep #7 - Image augmentation and overfitting
Machine Learning Foundations: Ep #7 - Image augmentation and overfitting
Google for Developers
55 Announcing Firebase Live, Flutter Day, Java 11 on Google Cloud Functions, & more!
Announcing Firebase Live, Flutter Day, Java 11 on Google Cloud Functions, & more!
Google for Developers
56 Machine Learning Foundations: Ep #8 - Tokenization for Natural Language Processing
Machine Learning Foundations: Ep #8 - Tokenization for Natural Language Processing
Google for Developers
57 Android 11 Beta, Google Play Asset Delivery, Firebase Crashlytics SDK, & much more!
Android 11 Beta, Google Play Asset Delivery, Firebase Crashlytics SDK, & much more!
Google for Developers
58 Natural Language Processing: Using sequencing APIs in TensorFlow | Machine Learning Foundations
Natural Language Processing: Using sequencing APIs in TensorFlow | Machine Learning Foundations
Google for Developers
59 Build a sarcasm classifier using NLP and TensorFlow | Machine Learning Foundations
Build a sarcasm classifier using NLP and TensorFlow | Machine Learning Foundations
Google for Developers
60 AR Realism with the ARCore Depth API
AR Realism with the ARCore Depth API
Google for Developers

Related Reads

Up next
Dropout in Deep Learning
AnuTech-CH
Watch →