Transfer Learning, how to use pre-trained neural networks to apply to your own
Juan David Tuta Botero
Data Science | Machine Learning | Artificial Intelligence
Abstract
This review is provided a detailed overview of how to develop a Neural network able to recognize and categorize all the different classes presented in the database CIFAR-10, which consists of 60000 32x32 color images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images. Using a pre-trained model called InceptionV3 found in Keras.applications instance which function specialized in image detection. Adding it two dense layers and one adam optimizer that were trained using a batch size of 100 and 2 epochs with an ending accuracy of 89.7%.
Introduction
Object detection is a computer vision technique that allows us to identify and locate objects in an image or video. With this kind of identification and localization, object detection can be used to count objects in a scene and determine and track their precise locations, all while accurately labeling them.
Imagine, for example, an image that contains two cats and a person. Object detection allows us to at once classify the types of things found while also locating instances of them within the image.
This technology can have countless applications and as time pass we see more in our day by day. But there is a technological wall the common people and business find and it is as more complex the computer power and time to train the model increase. So to surpass this problem we must rely on human collaboration to generate knowledge to this process we call Transfer Learning. The first thing to remember here is that transfer learning is not a new concept that is very specific to deep learning. There is a stark difference between the traditional approach of building and training machine learning models and using a methodology following transfer learning principles.
Applying the transfer learning we can use pre-trained models from universities or technological enterprises that were developed by highly skillfully people and trained during weeks and even months to accomplish specific tasks such as self-driving technologies, medical illness prediction, finances model and so more. We can import those and apply them to our model to save time and have really good predictions.
For a better understanding of the problem, I'm going to use a pre-trained model from Toronto university and modified it to be able to recognize 10 different objects found in the CIFAR 10 dataset that consists of 60000 32x32 color images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.
How to solve the problem
Now that we know we have a problem with image identification the next thing is to find a neural network developed to solve this problem. Fortunately the keras.aplication instance has a list of deep learning models that are made available alongside pre-trained weights. These models can be used for prediction, feature extraction, and fine-tuning. Between those models, we are going to use the InceptionV3 model that is suited perfectly for the task we are looking for. Thes model was developed to recognize 1000 different objects and we only want to be able to recognize 10 so it is perfectly fine. So let's start with the code and the first thing we are going to do is import our machine learning framework and the model we are going to use.
import tensorflow.keras as K
inception = K.applications.InceptionV3(include_top=False,
weights="imagenet"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? input_shape=(128, 128, 3))
Now that we have successfully imported the model we must "freeze" it. This concept is used when in the process of backpropagation the optimizable parameters stay the same and don't change because we are exporting an entire model using weights="imagenet" we exported with the parameter already optimize. If we don't freeze it we face the problem to find them again and can take a lot of time.
领英推荐
inception.trainable?=?False #freeze the layer
Now we have an exported model and freeze, we must add some extra layers to be able to adapt our new modified deep neural network to solve the problem of the CIFAR data. As we see in previous articles this NN is convolutional and to be connected to a normal network first we must be flattened then we create two extra layers one with 128 nodes and activation function "relu" and the other one as the dataset only have 10 classes we want to have only 10 outputs and as activation function use "softmax", finally in the compile process we use the optimization process of Adam to speed up the process of backpropagation, the code should look like this.
model?=?K.Sequential()
model.add(inception)
model.add(K.layers.Flatten())
model.add(K.layers.Dense(units=128,
?????????????????????????activation='relu',
?????????????????????????kernel_initializer='he_normal')
model.add(K.layers.Dense(units=10,
?????????????????????????activation='softmax',
?????????????????????????kernel_initializer='he_normal')
model.compile(optimizer='adam',
??????????????loss='categorical_crossentropy',
??????????????metrics=['accuracy'])
Results
Now that the code was running using 2 epoch and a batch size of 100 we found a pretty good result of 89,7%.
Discussion
As we can see the accuracy is pretty good with a value of 89,7%, offcourse if we want we can keep trained for more time or adding more layers to the model, but for the poorpuse of this article is really good enough. So in this way we can create a very powerful network able to recognise images in a little time respecting of coding and training aspects, so this could be a really good approximation werever you face a complicate problem and you want a fast result to compare or follow a totally new framework. I f you want to check the code by your sel you can find it here.
Biblyography
https://keras.io/api/applications/inceptionv3/
https://github.com/Juand0145/holbertonschool-machine_learning/blob/main/supervised_learning/0x09-transfer_learning/Manual_review.ipynb
https://www.cs.toronto.edu/~kriz/cifar.html
https://towardsdatascience.com/a-comprehensive-hands-on-guide-to-transfer-learning-with-real-world-applications-in-deep-learning-212bf3b2f27a