‫In the last video, we used data augmentation techniques to increase our validation accuracy to 82 to
‫83%.
‫In this video, we will use Vgg 16 model architecture to further increase our validation accuracy above
‫90%.
‫Vgg, 16, was the runner up of 2014in VRC competition.
‫The problem is statement of that competition was to categorize millions of pictures into thousand different
‫categories.
‫The pictures were of animals, humans, etcetera.
‫And the categories were of different animal species and many other.
‫So the problem we are trying to solve, you can consider it as a subset of original 2014 VRC competition
‫data.
‫As we discussed in our theory lecture.
‫We can use convolutional part of this pre-trained model architectures in our problems.
‫These models consist of two parts convolutional base and then a fully connected neural network base.
‫The convolutional base is used to identify features from the images and then.
‫The fully connected neural base is used to classify those features.
‫So for any similar kind of problem, we can easily use Pre-trained convolutional base to extract features
‫from our images and then we can add two three layers of fully connected neural network to classify the
‫result of this conv basis.
‫In this video.
‫The idea is same.
‫We will use the conv base of vgg16 model.
‫And then we will add one fully connected hidden layer and one output layer to classify the features
‫extracted from our vgg16 common base.
‫So let's start.
‫First, we will be creating two object train generator and validation generator.
‫We have already used the same generators in our previous cases also.
‫So we are using the same setting.
‫We are using rescaling of one by 255 to convert our RGB values.
‫From zero 2 to 55 to 0 to 1.
‫Then we have rotation ranges, width shift height, shift shear range, zoom range and horizontal flip.
‫To create dummy augmented data.
‫And our target size of images is 150 by 150 and we are using a batch size of 20.
‫We have already discussed this in detail in our previous videos, so we are not going to discuss this
‫here.
‫Just like in our previous case, we have around 2000 images.
‫For training and 1000 images for validation.
‫Now, the second step is to create architecture for our model.
‫Now our idea is to first use a base of vgg16 and then use two dense layer.
‫So to use con base of vgg16, you can directly import that from Keras.
‫There is no need to manually build all the con layers in that base.
‫So to import vgg16 you can just right from tensorflow keras dot application import vgg16 and then give
‫these three different parameters.
‫So we are creating our com base object and we are using vgg16 and these are the three parameters that
‫we are passing.
‫First, we need to provide weights.
‫So in any convolutional neural network, first we provide randomized weight and then our convolutional
‫network tries to optimize those weights.
‫Since.
‫V 16 was used in that competition and we can use the final weights of that model.
‫So to use those weights, we have to write weights equal to ImageNet.
‫ImageNet is the competition I vrc competition.
‫So to use pre-trained weights, we just have to write weight equal to image net.
‫And then there were two parts of that Vgg 16 model.
‫First was the base and then the fully connected neural network base.
‫We only want the conv base from that model since cone bases are reusable.
‫Those are mainly used to extract features and not to categorize the images.
‫So we will be only using the cone base and we only need to import cone base.
‫That's why we are using include underscore top equal to false.
‫If we want to import the whole model along with the fully connected dense layers, then you have to
‫change it to true.
‫But in our case, since we are only importing the convolutional base, we are providing false here.
‫Then next parameter is to give the input shape.
‫The input shape of our images are 150 by one, 50 by three.
‫That's why we are providing this tuple here.
‫Let's run this.
‫So we have imported our corn base from Vgg16 model.
‫Now to look at this.
‫You can just write cone base dot summary.
‫Well, if you run this, you will get details of all the layers of this vgg16 corn base.
‫Now as we discussed in our theory lecture.
‫Which is the 16 have this convolutional blocks.
‫So here you can see.
‫First convolutional block.
‫Then second convolutional block.
‫In each block there are multiple conv layers.
‫So in first and second blocks there are two conv layers and then a max pooling layer in third, fourth
‫and fifth block.
‫There are three conv layers and then a max pooling layer.
‫So in a way, by importing Vgg 16, we avoided creating these many layers and we have already imported
‫the final weights of that model.
‫So there is no need to randomly provide weights and optimize those weights.
‫We already have the final model weights with us in this model.
‫Now the next step is to add fully connected dense layer and output layer in front of this Conv base.
‫Now this is similar to creating any CNN model.
‫We just have to create our model first.
‫We are using models dot sequential and then just like you add any other layer, you can add the corn
‫base that we have imported.
‫So we will write model dot Add.
‫And here you can just write the variable in which we have stored this vgg16.
‫So our variable name was con base.
‫So first we can add this con base.
‫Next we have to use of Latin layer and then include a fully connected dense layer and then output layer.
‫So first we are adding flatten layer, then a dense layer with 256 neurons and then an output layer
‫with a single neuron.
‫The activation is Relu in the dense layer and the activation is sigmoid in the output layer.
‫You can run this and then you can look at the model summary as well.
‫So if you see this is our model summary, our first layer is vgg16.
‫We have around 14 million trainable parameter in this 16 layer.
‫Then we have a flatten layer.
‫Then we have a dense layer with around 2 million trainable parameters.
‫And then finally an output layer with 257 trainable parameters.
‫The total trainable parameter in our model is around 16 million.
‫Now, as I have told you earlier, we were using the weights of the final Vgg16 model.
‫So the weights are already optimized in this vgg16 layer.
‫Now if you don't want to train those weights, you can just freeze that layer.
‫To freeze that, you can use con base dot trainable, equal to false.
‫In that case, the trainable parameter here will turn to zero and our model will not try to optimize
‫the weights of this layer.
‫In that way, we can significantly reduce the number of trainable parameters in our model and significantly
‫improve our execution time.
‫So if you run this con based, trainable, equal to false.
‫Our number of trainable parameters will reduce from 16 million to just 2.1 million.
‫But here we are not running this and we are training all the 16 million parameters.
‫But in case you want to save time, you can run this on base dot trainable, equal to false.
‫Now the next step is to compile our model.
‫We will be using a loss function of binary cross entropy.
‫Since we have two classes, then we are using Rmsprop as our optimizer and a learning rate of two into
‫ten raise to power minus five.
‫We are using somewhat a smaller learning rate here just because we want to fine tune our already trained
‫model.
‫So the weights of this convolutional layers are already optimized and we just want to optimize it in
‫little steps according to our problem.
‫So since we are fine tuning it, we are not training it from randomly assigned weights.
‫We can use a smaller learning rate.
‫That's why we are using two into ten raise to power minus five.
‫And the metrics we want to calculate is of accuracy.
‫So training these models will take somewhere between 8 to 10 hours.
‫So it is better to use callbacks to save our model after each epoch.
‫So we are creating our checkpoint callback and we are saving our model for each epoch.
‫You can also use save based on the parameter here if you don't want to save 30 different models.
‫And if you give say best equal to true, we will save the model with the best validation score.
‫Now the next step is to fit the training data.
‫The step is similar to the last time We will use fitgenerator and then train generator with steps per
‫epoch and we will also give validation generator and validation steps for our validation data.
‫And here we are also providing callback just to save our model after each epoch.
‫So I have already executed this and these are the results.
‫So if you see the validation accuracies are in the range of 90 to 97.
‫So at the end of 30 epoch, we were getting a training accuracy of around 98% and the validation accuracy
‫of 98% as well.
‫You can see each epoch is taking around 15 minutes to train.
‫So just remember, this may take up to 8 to 10 hours to train our model.
‫Now let's look at how accuracies and losses are changing with each epoch.
‫The orange line here is for training accuracy.
‫The red line here is for validation accuracy.
‫And similarly, we have validation loss in green and training, loss in blue.
‫You can see that validation accuracy is oscillating between 97 to 98 and there is no further improvement
‫in accuracy as we move from lower epoch value to higher epoch value.
‫So we can say that we have achieved a convergence in our model and it is not possible to further improve
‫this validation accuracy with increase in number of epochs.
‫So if you compare the validation accuracy with our last CNN model and the last CNN model, we were getting
‫a maximum accuracy of 84%.
‫But in this, by using Vgg 16 Pre-trained model, we are achieving up to 97 to 98% of validation accuracy.
‫And it is very easy to train our model using this pre-trained models.
‫So there is no need to create your own con basis.
‫You can just use any one of these pre-trained con bases.
‫If the problem statement is somewhat similar to the image net problem statements.
‫Now I am also saving this history variable into a CSV file.
‫There is no need to do this step here now.
‫Till now we were only calculating the accuracies on our validation sets.
‫But now it's time to use our test set to see how this model performs on our test set.
‫Now we have to follow the same steps to evaluate our model performance.
‫Again, we will be using test generator.
‫So we are creating another generator.
‫We are calling it test generator.
‫We are using the test underscore data gen.
‫This is the same object we used for validation as well.
‫So here in this object we are just reshaping our data from 0 to 255 to 0 to 1.
‫And then we are using flow from directory and here we are providing test directory instead of validation
‫directory.
‫So our test generator is ready.
‫Now, normally if we have data in the form of array or dataframe, we use evaluate method.
‫But since we have our data flowing from our directory, that's why we have to use evaluate underscore
‫generator.
‫So there is a simple pattern for it.
‫We were using fit generator.
‫Similarly for evaluate, we are using evaluate generator and here also we have to provide our test generator
‫object and the number of steps.
‫We have a batch size of 20.
‫We have total test data size of around 1000 images.
‫That's why we need 50 steps, 1000 divided by 20 equal to 50.
‫So we will be able to cover all our test images in 50 steps.
‫So if you run this just like evaluate method, you will get two values.
‫First is the loss value and second is the accuracy value.
‫And here you can see that the accuracy we are getting is around 97%.
‫So just to revise, we started with a simple convolutional model.
‫At that time, we were getting accuracy of around 73 to 74%.
‫Then we use data augmentation techniques to create dummy data and avoid overfitting.
‫In that case, we were getting accuracy of around 83 to 84%.
‫And in this case, we used a pre-trained vgg16 model.
‫For our problem.
‫And in this case, we are getting an accuracy of around 97%.
‫So we have increased our validation accuracy from 73% to 98% during this project.
‫That's all for this project.
‫Thank you.