Transfer learning for speeding up your computer vision pipelines


“A fertile soil alone does not carry agriculture to perfection”E. H. DERBY  “A Defence of Agriculture”, Lessons in Modern Farming

Will you learn to play the piano faster if you previously knew how to play the guitar? Probably, yes. At least some notions on music will boost your learning process with the new instrument. This is the idea that is behind transfer learning, a technique widely used in deep learning. Transfer Learning, generally refers to a process where a machine learning model is trained on one problem, and afterwards, it is reused in some way on a second (probably) related problem (Bengio, 2012). Specifically, in deep learning, only some layers of the pre-trained network are fine-tuned. Its promise is that the training will be more efficient and in the best of the cases the performance will be better compared to a model trained from scratch.

When learning a new skill, the brain can reuse some of the knowledge acquired in previous tasks. For instance, this happens while learning how to play a new instrument.

In order to develop your own transfer learning pipelines with Python, Keras library ( provides a rich API on top of TensorFlow that allows easy and consistent implementation. As shown in the figure below, we can reuse the weights that Xception used for obtaining high performances on the ImageNet dataset. In this case, we freeze all the Xception’s layers, so that only the next layers will be updated while training. This way, we are using Xception as a “constant” feature extractor.

How to implement transfer learning with Keras? The code in the red box shows how to reuse the previous knowledge acquired in the ImageNet dataset.

This code canbe used, for instance, to train a neural network, which is able to differentiate between tomatoes and black nightshade (a weed that usually hurts tomato growing). Transfer learning has been used in several types of research on precision agriculture. For instance, in Suh et al., (2018), the authors compared different transfer learning approaches in order to find a suitable approach for weed detection (volunteer potato). Their highest classification accuracy for AlexNet was 98.0%. Comparing different networks, their highest classification accuracy was 98.7%, which was obtained with VGG-19. Additionally, all scenarios and pre-trained networks were feasible for real-time applications (classification time < 0.1 s). Another relevant study has been performed by Kounalakis et al., (2019) where they evaluated transfer learning by a combination of CNN-based feature extraction and linear classifiers to recognize rumex under real-world conditions. Their best system (Inception_v1+L2regLogReg) achieved an accuracy of 96.13 with a false positive rate of 3.62.

In this tutorial, you will train a neural network to differentiate between tomato and black nightshade pictures.