Achieving state of the art result with fast.ai
Creating an AI web application that detects diseases in plants using FastAi which built on the top of Facebook’s deep learning platform: PyTorch. According to the Food and Agriculture Organization of the United Nations (UN), transboundary plant pests and diseases affect food crops, causing significant losses to farmers and threatening food security.
Achieved 99.654% Accuracy with Resnet34 model
For this challenge, I used the “” dataset. This dataset contains an open access repository of images on plant health to enable the development of mobile disease diagnostics. The dataset contains 54, 309 images. The images span 14 crop species: Apple, Blueberry, Cherry, Grape, Orange, Peach, Bell Pepper, Potato, Raspberry, Soybean, Squash, Strawberry, and Tomato. It contains images of 17 fundal diseases, 4 bacterial diseases, 2 molds (oomycete) diseases, 2 viral diseases, and 1 disease caused by a mite. 12 crop species also have images of healthy leaves that are not visibly affected by a disease.
I have used fastai which is built on top of Pytorch. Dataset consists of 38 disease classes from PlantVillage dataset and 1 background class from Stanford’s open dataset of background images DAGS. 80% of the dataset is used for training and 20% for validation.
We will use the pre-trained resnet34 model to solve the issue with the training.
Start every notebook with the following three lines:
The lines in jupyter notebook that start with ‘%’ are called . These are not instructions for Python to execute but to Jupyter notebook.
The first two lines will ensure automatic reload whenever you make any changes in the library. And the third line to show charts and graphs in the notebook.
Import all the import libraries:
Now, give the path of the dataset (in my case it is in the root directory):
Batch size means we will feed x images at once to update parameters of our deep learning model. Set batch size to 64, if smaller GPU use 16 or 32 instead of 64.
ImageDataBunch is used to do classification based on images.
ImageDataBunch.from_folder gets the label names from the folder name automatically. fastai library has awesome documentation to navigate through their library functions with live examples on how to use them. Once the data is loaded, we can also normalize the data by using .normalize to ImageNet parameters.
To look at a random sample of images, we can use .show_batch() function ImageDataBunch class.
Let’s print all the data classes present in the data. In total, we have images in 39 classes as mentioned above!
To create the transfer learning model we will need to use the function
cnn_learner that takes the data, network and the
metrics . The
metrics is just used to print out how the training is performing.
We will train for 5 epochs.
As we can see above by just running five epochs with the default setting our accuracy for this fine-grained classification task is around ~99.10%.
Let’s train it for two more epochs.
This time 99.2% Accuracy!
Now save the model with
We can also plot the confusion matrix.
We use the
lr_find method to find the optimum learning rate. Learning Rate is an important hyper-parameter to look for. We traditionally use αα to denote this parameter. If the Learning rate is too slow, we take more time to reach the most accurate result. If it is too high, we might not even end up reaching the accurate result. Learning Rate Finder was the idea of automatically getting the magic number (which is near perfect), to get this optimum learning rate. This was introduced in last year’s Fast AI course and continues to be useful.
After we run the finder, we plot the graph between loss and learning rate. We see a graph and typically choose a higher learning rate for which the loss is minimal. The higher learning rate makes sure that the machine ends up learning faster.
Considering the fact that we are using a pre-trained model of Resnet34, we know for sure that our previous layers of this neural network would learn to detect the edges and the later layers would learn complicated shapes. We don’t want to ruin out the earlier layers which presumably does a good job of detecting the edges. But would like to improve the model in narrowing down classifying the images.
So, we will set a lower learning rate for earlier layers and a higher one for the last layers.
slice is used to provide the learning rate wherein, we just provide the range of learning rates (its min and max). The learning rate is set gradually higher as we move from the earlier layer to the latest layers.
Let’s unfreeze all the layers so that we can train the entire model using unfreeze() function.
Accuracy close to 99.63%. Save the Model!
Freeze the model, find the learning rate, and fine-tune the model:
Finally, we achieved the state of the art results close to 99.654% Accuracate using Resnet34.
Fork or Star it on GitHub:
This content was originally published here.