Build a Fine-Tuned Neural Network with TensorFlow's Keras API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey I'm maybe from beef lizard in this episode we'll demonstrate how we can fine-tune a pre-trained model to classify images using tensor flows Karis API the pre trained model that we'll be working with is called VG g16 and this is the model that won the 2014 image net competition in the image net competition multiple teams compete against each other to build a model that best classifies images within the image net library and the image net library is made up of thousands of images that belong to one thousand different classes using Kerris we'll import this vgg 16 model and then fine tune it to not classify on one of the 1000 categories for which it was originally trained but instead only on two categories cat and dog note however that cats and dogs were included in the original image net library for which vgg 16 was trained on and because of this we won't have to do much tuning to change the model from classifying from 1000 classes to just the two cat and dog classes so the overall fine-tuning that we'll do will be very minimal in later episodes though we'll do more involved fine-tuning and use transfer learning to transfer what a model has learned on an original data set to completely new data in a new custom data set that we'll be using later to understand fine tuning and transfer learning at a fundamental level check out the corresponding episode for fine tuning in the deep learning fundamentals course on V Blizzard comm before we actually start building our model let's quickly talk about the vgg 16 pre-processing that is done now recall we are here in our jupiter notebook this is from last time when we plotted a batch from our test data set and a few episodes ago we discussed the fact that this color data was skewed as a result of the vgg 16 pre-processing function that we were calling well now we're going to discuss what exactly this pre-processing does so as we can see we can still make out the images as these here being all cats but it's just the data the color data itself that appears to be distorted so if we look at the paper that the authors of vgg 16 detailed the model in under the architecture section let's blow this up a bit we can see that they state that the only pre-processing we do is subtract the mean RGB value computing on the trait computed on the training set from each pixel so what does that mean that means that they computed the mean red value pixel from all of the training data and then once they had that mean value across each image in the training set they subtracted that mean value from the red value the red pixel each pixel in the image and then they did the same thing for the green and blue pixel values as well so they found the green picks the mean green pixel value among the entire training set and then for each sample in the training set every green pixel they subtracted that green value from it so the same thing for the blue of course so that's what they did for the pre-processing so given that that's how bgg 16 was originally trained that means now whenever new data is passed to the model that it needs to be processed in the same exact way as the original training set so Kerris already has functions built in for popular models like vgg 16 where they have that pre-processing in place that matches for the corresponding model so that's why we were calling that model whenever we process our cat-and-dog images earlier so that we could go ahead and get those images processed in such a way that matched how the original training set was processed when vgg was originally trained all right so that is what that color distortion is all about so now let's jump back into the code now that we know what that's about we have an understanding of the pre-processing now let's now get back to actually building the fine-tuned model so the first thing that we need to do is download the model and when you call this for the first you will need an internet connection because it's going to be downloading the model from the Internet but after this subsequent calls to this function will just be grabbing this model from the downloaded copy on your machine alright so the models been downloaded now we are just running this summary and we can see that this model is much more complex than what we have worked with up to this point so total there are almost 140 million parameters in this model and on disk it is over 500 megabytes so it is quite large now recall I said that this vgg 16 model originally was predicting for 1000 different image net classes so here we can see our output layer of the vgg 16 model has 1000 different outputs so our objective is going to simply be to change this last output layer to predict only two output classes corresponding to cat and dog along with a couple other details regarding the fine-tuning process that we'll get to in just a moment so now we're actually going to just skip these two cells here these are just for me to be able to make sure that I've imported the model correctly but it is not relevant for any code here it's just checking that the trainable parameters and non trainable parameters are what I expect them to be after importing but this is not of concern at the moment for us so if we scroll down here we're now going to build a new model called sequential alright now before we run this code we're actually just going to look at the type of model das's so this is returning a model called model so this is actually a model from the Charis functional API we have been previously working with sequential models so we are in a later episode going to discuss the functional API in depth it's a bit more complicated and more sophisticated than the sequential model for now since we're not ready to bring in the functional model again we are going to convert the original vgg 16 model into a sequential model and we're going to do that by creating a new variable called model here and setting this equal to an instance of sequential object and we are going to then loop through every layer in vgg 16 except for the last output layer we're leaving that out we're going to loop through every layer and then add each layer into our new sequential model so now we'll look at a summary of our new model and by looking at this summary if you take the time to compare the previous summary to this summary what you will notice is that they are exactly the same except for the last layer has been not included in this new model so this layer here we have this fully connected to layer as what this is here with the output shape of 4096 here if we scroll back up we can see that this is this layer here so the predictions layer has not been included because when we were iterating over our for loop we went all the way up to the second-to-last layer we did not include the last layer of vgg 16 all right so now let's scroll back down and we're now going to iterate over all of the layers within our new sequential model and we are going to set each of these layers to not be trainable by setting layer dot trainable to false and what this is going to do is going to freeze the trainable parameters or the weights and biases from all the layers in the model so that they're not going to be retrained whenever we go through the training process for cats and dogs because vgg 16 has already learned features of cats and dogs in its original training we don't want it to have to go through more training again since it's already learned those features so that's why we are freezing the weights here we're now going to add our own output layer to this model so remember we've removed the previous output layer that had ten output curve that had 1000 output classes rather and now we are going to add our own output layer that has only two output classes for cat and dog so we add that now since we have set all of the previous layers to not be trainable we can see that actually only our last output layer that's going to be the only trainable layer in the entire model and like I said before that's because we already know that vgg 16 has already learned the features of cats and dogs during its original training so we only need to retrain this output layer to classify to output classes so now if we look at a new summary of our model then we'll see that everything is the same except for now we have this new dense layer as our output layer which only has two classes instead of 1000 from the original vgg 16 model we can also see that our model now only has 8,000 trainable parameters and those are all within our output layer as I said that our output layer is our only trainable layer so before actually all of our layers were trainable if we go take a look at our original vgg 16 model we see that we have 138 million total parameters all of which are trainable none of which are non trainable so if we didn't freeze those layers and then they would be getting retrained during the training process for our cat and dog images so just to scroll back down again and check this out we can see that now we still have quite a bit of learn Bowl parameter of total parameters 134 million but only 8,000 of which are trainable the rest are non trainable in the next episode we'll see how we can train this modified model on our images of cats and dogs by the way we are currently in Vietnam filming this episode if you didn't know we also have a vlog channel where we document our travels and share a little bit more about ourselves so check that out at people's our vlog on YouTube also be sure to check out the corresponding blog for this episode along with other resources available on deep loser calm and check out the people's r-type mine where you can gain exclusive access to perks and rewards thanks for contributing to collective intelligence I'll see you next time [Music] [Music]
Info
Channel: deeplizard
Views: 23,551
Rating: undefined out of 5
Keywords: deep learning, pytorch, cuda, gpu, cudnn, nvidia, training, train, activation function, AI, artificial intelligence, artificial neural network, autoencoders, batch normalization, clustering, CNN, convolutional neural network, data augmentation, education, Tensorflow.js, fine-tune, image classification, Keras, learning, machine learning, neural net, neural network, Python, relu, Sequential model, SGD, supervised learning, Tensorflow, transfer learning, tutorial, unsupervised learning, TFJS
Id: 3ou0KYtDlOI
Channel Id: undefined
Length: 11min 48sec (708 seconds)
Published: Tue Aug 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.