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:
- Create a Custom Vision AI project
- Add Images to the project
- Train on the images and create the model
- Publish the model and expose the endpoint for use by other clients
- 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.
Add Images
We upload the bacterial_leaf_blight images and also tag them.
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
Training
We can select Quick Training or Advanced Training for training the images
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
This is the output of the 4th Iteration [ Advanced Training ] . In Advanced Training, we can limit the budget by specifying the time duration
Publish
We can now Publish the model so that we can use the endpoint of the model for the prediction of unseen images.
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.
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
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"
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))
Top comments (0)