Paddy Disease Classification using Azure AI
Tags: azure, azurecustomvision, computervision
Published: 2022-07-23 06:13:00

**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](https://www.kaggle.com/competitions/paddy-disease-classification) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5xub5dyo7puiokd7hx6.png)
 
# Add Images        

We upload the **bacterial_leaf_blight** images and also tag them. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6lacyv76ex6pqrr7bkq.png)             

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/im8jjvq6loaycul8sczs.png)    
          
# Training 

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32km85fygpc0u86uvdo2.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80flotqb852xlpk8ficu.png)
   
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrf0qkjyws0ugitvvlsa.png)         

# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s89d2bp3tn1fqtzwjr9k.png)
 
# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6er1p35dogdpfkhxhnq7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nauaapo4dq5gd783cic.png)             

# Prediction

## Import the libraries      

```python
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

```


## Set the parameters   

```python   
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"

```

## Complete the prediction

```python
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))


```

# References 

1. [Paddy Disease Classsification Dataset](https://www.kaggle.com/competitions/paddy-disease-classification/overview)        

2.[Azure Custom Vision](https://docs.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/quickstarts/image-classification?tabs=visual-studio&pivots=programming-language-python)