DEV Community

Cover image for Paddy Disease Classification using Azure AI
Ambarish Ganguly
Ambarish Ganguly

Posted on • Edited on

Paddy Disease Classification using Azure AI

Rice (Oryza sativa) is one of the staple foods worldwide.

Paddy, the raw grain before removal of husk, is cultivated in tropical climates, mainly in Asian countries. Paddy cultivation requires consistent supervision because several diseases and pests might affect the paddy crops, leading to up to 70% yield loss. Expert supervision is usually necessary to mitigate these diseases and prevent crop loss. With the limited availability of crop protection experts, manual disease diagnosis is tedious and expensive. Thus, it is increasingly important to automate the disease identification process by leveraging computer vision-based techniques that achieved promising results in various domains.

Data

Data is taken from Paddy Disease Classification Dataset from Kaggle We have taken a subset of the data provided in the dataset to demonstrate the power of Azure Cognitive Services

I have made a small dataset [ 1000 images - around 100 images of 10 classes ] from the parent dataset in Kaggle mentioned above for quick experimentation. The data has around 100 images of each of the classes bacterial_leaf_blight, bacterial_leaf_streak , bacterial_panicle_blight , blast , brown spot , dead heart , downy mildew , hispa , normal and tungro

The steps to model and predict for this problem are as follows:

  1. Create a Custom Vision AI project
  2. Add Images to the project
  3. Train on the images and create the model
  4. Publish the model and expose the endpoint for use by other clients
  5. Use the exposed endpoint and predict using new images

Create a Custom Vision AI project

Navigate to https://www.customvision.ai/projects to create a custom vision project.

We created a project with

Name - paddy

Project Type - Classification. Since we are classifying whether the image is having bacterial_leaf_blight, bacterial_leaf_streak , bacterial_panicle_blight , blast , brown spot , dead heart , downy mildew , hispa , normal and tungro

Classification Type - Multiclass. There are 2 choices here, Multiclass and Multilabel. We choose Multiclass since the image is associated with only one class ( bacterial_leaf_blight, bacterial_leaf_streak , bacterial_panicle_blight , blast , brown spot , dead heart , downy mildew , hispa , normal and tungro ). A single image is not associated with multiple classes.

If a single image was associated with multiple classes, then we had to choose the Classification type as Multilabel.

Image description

Add Images

We upload the bacterial_leaf_blight images and also tag them.

Image description

We also add images of other classes

bacterial_leaf_streak , bacterial_panicle_blight , blast , brown spot , dead heart , downy mildew , hispa , normal and tungro

Train the images

We train the images by clicking the Train button in the portal

Image description

Training

We can select Quick Training or Advanced Training for training the images

Image description

We choose Advanced Training to train the images. Each of the 10 classess have around 100 images .

We do model training and we can see the various iterations

Image description

This is the output of the 4th Iteration [ Advanced Training ] . In Advanced Training, we can limit the budget by specifying the time duration

Image description

Publish

We can now Publish the model so that we can use the endpoint of the model for the prediction of unseen images.

Image description

Project details

We display the Azure Cognitive project which has the project id, the published endpoint. This will be used for predicting the unseen test images.

Image description

Image description

Prediction

Import the libraries

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region
from msrest.authentication import ApiKeyCredentials
import os, time, uuid

Enter fullscreen mode Exit fullscreen mode

Set the parameters

ENDPOINT = "YOUR ENDPOINT"
training_key = "YOUR training_key"
prediction_key = "YOUR prediction_key"
prediction_resource_id = "YOUR prediction_resource_id"
project_id = "YOUR project_id"
publish_iteration_name = "YOUR publish_iteration_name"

Enter fullscreen mode Exit fullscreen mode

Complete the prediction

base_image_location = os.path.join (os.path.dirname(__file__), "train_images")


# Now there is a trained endpoint that can be used to make a prediction
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)

with open(os.path.join (base_image_location, "blast/110406.jpg"), "rb") as image_contents:
    results = predictor.classify_image(
        project_id, publish_iteration_name, image_contents.read())

    # Display the results.
    for prediction in results.predictions:
        print("\t" + prediction.tag_name +
              ": {0:.2f}%".format(prediction.probability * 100))


Enter fullscreen mode Exit fullscreen mode

References

  1. Paddy Disease Classsification Dataset

2.Azure Custom Vision

Top comments (0)