Plant Disease Detection Web Application using Fastai
Achieving state of the art result with fast.ai
by Shubham Kumar
Demystifying Artificial Intelligence! Follow him for amazing cool stuffs related to technology! http://shubhamkumar.live
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 “PlanVillage” 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 Google Cloud Platform with the base platform called,
n1-highmem-8, and costs $0.12 per hour. Attaching a P4 GPU costs $0.26 per hour so both together amount to $0.38 per hour. And the suggested 200GB Standard Disk storage size, there will be an additional charge of $9.60 a month. For a full walkthrough, setup visit here!
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:
%reload_ext autoreload %autoreload 2 %matplotlib inline
The lines in jupyter notebook that start with ‘%’ are called Line Magics. 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:
from fastai import * from fastai.vision import * from fastai.metrics import error_rate, accuracy
Now, give the path of the dataset (in my case it is in the root directory):
PATH_IMG = Path('PlantVillage/')
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.
bs = 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.
img_data = ImageDataBunch.from_folder(path=PATH_IMG, train='train', valid='val', ds_tfms=get_transforms(), size=224, bs=bs)img_data.normalize(imagenet_stats)
paththe path of the images directory.
ds_tfmsthe transformations that are needed for the image. This includes centering, cropping and zooming of the images.
sizethe size to which the image is to be resized. This is usually a square image. This is done because of the limitation in the GPU that the GPU performs faster only when it has to do similar computations (such as matrix multiplication, addition and so on) on all the images.
To look at a random sample of images, we can use .show_batch() function ImageDataBunch class.
To continue reading click here