With Transfer Learning is possible to take a pre-trained network, and use it as a starting point for a new task





It’s not a secret for anyone familiar with image classification that CNN’s need a  considerable amount of images, parameter tuning, and processing time to output a reasonable result.

    The good news is: with Transfer Learning is possible to take a pre-trained network (for a set of images for instance), and use it as a starting point for the training of a new task. For example, A network trained for recognizing motorcycles can be retrained to identify bicycles. Note that the problems are different, yet, related to each other.

    Convolutional Layers usually recognize, edges, intensities, textures, and shapes at some point.  We may utilize that kind of knowledge the network already has, and generalize it for another task.

    The most common way of doing Transfer Learning is using models already present in literature such as VGGInception, and MobileNet. Those networks are trained using images and classes from datasets with thousands of images and classes such as ImageNet or CIFAR.

    By using these datasets, the network will learn the common features of a large number of different objects. So, what would happen if we add some dense layers at the end of the already trained network and use the model to learn about new objects…?  Guess what, we are transferring the network knowledge to a (partially) new one!

    Let’s see how to do that in practice. Code time.

Requirements

  • Python
  • Pillow
  • Numpy
  • Tensorflow
  • Keras
  • Matplotlib

 

Code

    For doing our transfer learning, first, we need to choose an already-trained network. Here, VGG16 is a good choice, because it has already demonstrated state-of-the-art performance in object classification tasks, winning the ILSVRC 2014 (ImageNet Large Scale Visual Recognition Competition) in the classification task. Figure 1 shows the VGG16 architecture.

 

Figure 1 – VGG16 Architecture.

 

    Keras (with Tensorflow backend) has a great set of tools to use VGG16 and also other models, with the option of loading pre-trained weights. The code for loading the model is:

 

    Note that the dense layer is not included, our job is to create and train a new one. That new dense layer will be trained/tested/validated using images after a single pass of each one through all the VGG16 Convolutional Layers.

    There are some functions that have to be implemented. Keras also provides an easy way to load an image and generate batches of tensor image data.  The function below does exactly that, with both test and train datasets.

 

    The code snippet below shows the functions that count folders (classes) and images for the current dataset.

 

    After that, we begin to generate the image data accordingly to VGG16 architecture.

 

All set: time to train the dense layer.

The fit() function does everything. After it finishes, the Network is ready to be used on a new task.

 

    Please note that the code here is to give a detailed explanation on the blog and a better output on Jupyter Notebook. For the working code as it should be, please take a look at my Github page. Have fun!