TensorFlow Tutorial 09 - Transfer Learning

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys and welcome to a new tensorflow tutorial today we will continue where we left off last time and applied transfer learning to get a model with a good performance so i highly recommend that you watch the last tutorial first if you haven't already and as a quick recap so we used a data set from kaggle with lego star wars minifigures and we applied our own convolutional neural net and then we had the problem that for the training we got a very good accuracy so this was close to 100 percent but it didn't perform well on our validation data set and also for the final evaluation with the test data set we only got an accuracy of 40 so this is not very good so now what we want to do is we want to apply transfer learning to improve our model and transfer learning is a very nice simple but very powerful technique um so the concept is that we use a model that has been already trained and this is probably a very good model that with a lot of features and that also has been trained on a lot of data and now what we do here is so we we take this model and then we only modify the last layers so we cut them out and then apply our own classification layers at the end and train only these layers at the end so with this our training can be very quick but we can also get the power of the rest of the neural network that is already pre-trained so this is the whole concept behind transfer learning and now i show you how we do that with keras so the first thing we want to do is to load a pre-trained model so there are already some models available in keras for example the popular vgg g16 um model so you get this by saying tensorflow keras applications and then the name of our model so if we download if we call this then it's downloading this model and saves it in your folder so let's run this and print the summary and here we have the architecture so you can have a look at that and now what we want to do here is as i said we want to delete the last layer so you can delete only the very last layer or you can also delete more and then apply your own so in this example we only take out the last layer so this dense layer and then apply our own dense layer with five different outputs because in our example we have five different classes and right now um this is a keras functional model so i already talked about the functional api and if you haven't then i also recommend to watch this one first and i will put the link in the description but um i also said that we can easily convert this to a um sequential model so um we can do this by setting up a new model and say this is a sequential model and then we iterate over all the layers um except the last one so this is not included and say model at layer so now let's run this and print the summary and then this should be the same except that you don't see this layer so um oh sorry so here um this is the original summary and here we have this as last layer with a thousand outputs and now in our case this should be the last layer so let's have a look and yes here it is this is the last layer and now what we want to do here is we want to set all of those layers to trainable equals false because we don't have to retrain these again we only have to train our new classification layer so we loop over all layers and say layer trainable equals false and for example here you see the trainable parameters and the non-trainable parameters and now if we do this and print the summary again then we see that now all of our parameters are non-trainable and now we add a last dense layer and by default now this is trainable again and then again we set up a loss and a optimizer and compile our model and then so last time i told you how we can use this um image data generator and then call this dot flow from directory function and this very easily loads the images from a directory and we can also apply um pre-processing and rescaling and even image augmentation here so last time we applied this rescaling here and now this time what we want to do we want to apply the same pre-processing function as in our base model so in this vgg net so and we also get this by saying this is available in tensorflow keras applications than the name of our model and then dot pre-process input so this is our pre-processing function and then we can give this to our image data generator for with the argument preprocessing function so this is the same for our training validation and test set and then we call this flow from directory for each one and this is these are the same arguments as last time and now this is loading them the images from the different models i didn't run this cell so let's run this and this again and now we have it and now again this is the same as last time so we fitted our model and we applied this early stopping callback so if our validation loss does not increase for or improve for um five epochs then it does an early stopping so let's apply this again here and now let's fit our data so this is giving an error you must compile your model before training and testing so i think i already did this in this cell so let's run this again and run this and this and now our training so now it's working i guess i didn't run the cell the first time so now let's see how our transfer learning model is doing all right and now our training is done and again we had an early stopping so we see that our accuracy on the test data set is 100 percent so perfect and the validation accuracy is almost 94 so yeah i think this is now very good and it's a lot better than last time and now um we specified 30 epochs but then our validation loss had the lowest value here and then it didn't get better over the next five epochs so we said patience is five and that's why it did the early stopping so we already had a validation accuracy of 100 percent in the second epoch but this might be the case because we don't have so many images available in our validation data set so i think this result is not very reliable but yeah i think we can see how powerful this transfer learning is because last time we had a problem um with overfitting so that we had a good accuracy on the test on the training data but not on the validation data and not on the test data and now with a transfer learning technique we can get this after only two epochs and even in this example where we don't have so many images available and this is very good so now let's evaluate our model on the test data set and we see in this case we got a hundred percent on accuracy on the test data so i guess i i'm a little bit lucky this time but yeah we see how powerful this technique is again and how simple it is so you load a pre-trained model and um so so this one and then we converted in this case to a sequential model so you don't have to do this but this makes it a little bit simpler for you because i know the sequential api is easier to understand and then we excluded the last layer and added our own dense layer and then we also set layers strainable to false except for our last dense layer and then we did a new training and as a homework you can try out other pre-trained nets so for example the mobile net version 2 is another very popular net so you can go to this link and then here you have all the different uh available nets so here's our vg16 you also have vgt19 you have the mobilenet and the mobilenet version 2 and the resonet so yeah try them out for yourselves and i can tell you that you might run into a problem when you try to convert your model to a sequential model because this only works if the architecture of your model is linear and if you don't know how you do this if your architecture is not linear then you can watch my tutorial about the functional api because there i gave you a tip at the end and showed you how you can still do transfer learning with a functional model so yeah check that out and let me know if you can also get a accuracy of 100 and i hope to see you in the next video then and if you enjoyed this video please hit the like button and consider subscribing to the channel bye
Info
Channel: Patrick Loeber
Views: 14,874
Rating: undefined out of 5
Keywords: Python, Machine Learning, ML, Deep Learning, DL, Python DL Tutorial, Neural Net, TensorFlow, TensorFlow Course, CNN, Convolutional Neural Net, Star Wars, Kaggle, Transfer Learning
Id: 8cN0PiZQl18
Channel Id: undefined
Length: 12min 20sec (740 seconds)
Published: Sun Oct 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.