Training a Plant Disease Classification Model using Fastai LibrarySteve MutuviBlockedUnblockFollowFollowingFeb 11IntroductionOver the past few years, deep learning techniques have dominated computer vision.
One of the computer vision application areas where deep learning excels is image classification with Convolutional Neural Networks (CNNs).
The goal of image classification is to classify a specific image according to a set of possible categories.
The state-of-the-art image classifiers often result from transfer learning approaches based on pre-trained convolutional neural networks.
In this article, we illustrate the training of plant disease classification model using theFastai library.
The Fastai LibraryThe fastai librarysimplifies and enables the training of fast and accurate neural nets using modern best practices.
It is an excellent initiative by Jeremy Howard and his team aimed at democratizing deep learning and by making it easier for everyone to build deep learning models.
Fastai is a wrapper for PyTorch, which makes it easier to access recommended best practices for training deep learning models, while at the same time making all the underlying PyTorch functionality directly available to developers.
The main reason why fastaihas been successful is the use of transfer learning techniques.
Transfer learning involves the use of a pre-trained model, that has been trained on a large collection of data and finally repurposed for ones' specific needs.
Fastai library has awesome documentation to help you navigate through their library functions with live examples on how to use them.
Training a Plant Disease ClassifierThe DatasetThe data used in this article is obtained from the PlantVillage Disease Classification Challenge organized by the CrowdAi.
The goal of this challenge was to develop algorithms that can accurately diagnose a disease based on a plant image.
PlantVillage is a not-for-profit project by Penn State University in the US and EPFL (Ecole polytechnique fédérale de Lausanne) in Switzerland.
The organizations empower smallholder farmers to increase yield by leveraging Artificial Intelligence to provide offline expert level knowledge and extension advice.
PlantVillage has already collected and continue to collect tens of thousands of images of diseased and healthy crops.
The same dataset of diseased plant leaf images and corresponding labels comprising 38 classes of crop disease can also be found in spMohanty’s GitHub account.
Training the ModelWe use the visionmodule of the Fastai library to train an image classification model which can recognize plant diseases at state-of-the-art accuracy.
While training of the model can be done locally using a laptop, we use Google Colab which gives us more compute power, access to a GPU and easy to use Jupiter notebook environment for building machine learning and deep learning models.
We begin by placing the following three lines at the start of the notebook to ensure that any edits made to libraries are reloaded automatically, and also that any charts or images displayed are displayed within the notebook.
These are not python codes but special directives to Jupyter Notebook itself.
The % is one of the magic commands supported in Jupyter lab that adds extract extra functionality to our Jupyter lab notebooks and are not limited to the core language.
%reload_ext autoreload%autoreload 2%matplotlib inlineThe next step is to import the required libraries.
The fastai module and any other module can easily be installed using the pip command.
!pip install fastai #installs the fastai libraryimport numpy as npfrom fastai import *from fastai.
vision import *from pathlib import PathLoading and looking at the DataWe download the colored (original RGB) images using the following commandWhenever we approach a problem, the first thing to do is to take a look at the data in order to very well understand what the problem is and what the data looks like before we can figure out how to solve the problem.
Taking a look at the data means understanding how the data directories are structured, what the labels are and what some sample images look like.
In this particular dataset, the folder name represents the class label of all the images present within that folder.
We need to extract the label names from the folder name automatically.
Fortunately, fastailibrary provides the ImageDataBunch.
from_folder function that enables automatic extraction of the label names from the folder name.
In addition, the ImageDataBunchclass makes it easy to create the training and validation sets with images and labels.
Once the data is loaded, we can also normalize the data by using.
normalizeto ImageNet parameters.
show_batch() function of the ImageDataBunchclass can be used to view a random sample of images from the given data.
Sample plant imagesYou will notice that the images appear to have been zoomed and cropped in a reasonably nice way.
Fastai provides a rich image transformation library, whose main purpose is data augmentation when training computer vision models.
The library can, however, be used for other general transformation tasks such as default center cropping.
DataBunch has a property called c which help determine the number of classes, an important piece of information, especially for classification problems.
We print the class labels and the number of classes as follows;Transfer Learning using a Pre-trained model: Resnet 50Now that our data is ready, it’s time to fit a model.
To create the model we will use the function create_cnn from Learner class and feed a pre-trained model, in this case, ResNet 50, from the models class.
The model we are building will take images as input and output the predicted probability of each category, with accuracy being used as the measure of performance.
We use the methodfit_one_cycleto train the model for 4 epochs (4 cycles through the data).
The set of weights generated from the training process can be saved for re-use using the.
savefunction of thelearner class.
In our case, we save it as ‘stage-1’.
ConclusionTo achieve global food security, society needs to increase food production by an estimated 70% in order to feed an expected population size of over 9 billion people by 2050.
One way of achieving this target is to reduce infectious crop diseases which often result in a reduction of the potential yield by an average of 40% and sometimes even more especially for small-scale farmers in the developing world.
The widespread adoption of smartphone around the globe provides the potential of smartphone turning into a valuable disease diagnostics tool, enabling farmers to recognize diseases from images captured using the device’s camera.
Discuss this post on Hacker News and Reddit.
.. More details