DEV Community

Cover image for From Deep Learning Model to a Deployed Potato Disease Classifier πŸ₯”
Rose Umutesi
Rose Umutesi

Posted on

From Deep Learning Model to a Deployed Potato Disease Classifier πŸ₯”

From Deep Learning Model to a Deployed Potato Disease Classifier πŸ₯”

Introduction

Potato diseases can significantly affect crop production and farmers' livelihoods. Early identification of diseases can help farmers take action before infections spread and cause greater losses.

As part of my AI and Machine Learning training, I worked on a deep learning project to build an image classification system that can identify three potato leaf conditions:

  • 🌱 Healthy
  • 🟀 Early Blight
  • 🟀 Late Blight

The goal was not only to train a model, but to take the project from dataset preparation and model training to model export, application development, and online deployment.

The final application allows a user to upload a potato leaf image and receive a predicted class together with the model's confidence.

The Dataset

For this project, I used the PlantVillage dataset available through Kaggle.

Potato leaf dataset samples

I focused on three potato categories:

  • Potato___Early_blight
  • Potato___Late_blight
  • Potato___healthy

The selected dataset contained 2,152 potato leaf images.

Before training, I verified the images to make sure that the dataset did not contain corrupted files.

I then created a data pipeline using fastai. The images were resized to 224 Γ— 224 pixels and divided into training and validation sets.

Choosing the Model

For the classification task, I used ResNet18, a convolutional neural network architecture commonly used for image classification.

I used transfer learning rather than training the network completely from scratch. This allowed me to fine-tune a pretrained model for the three potato disease classes.

The model was implemented using fastai and PyTorch.

Baseline Model

I first trained a baseline ResNet18 model using four fine-tuning epochs.

The baseline achieved a validation accuracy of:

98.37%

This provided a strong starting point. I then investigated whether changing the training configuration could improve the validation performance.

Experiment: Improving the Training Configuration

For my second experiment, I kept the ResNet18 architecture unchanged.

Instead, I changed the fine-tuning configuration:

Configuration Baseline Experiment 2
Architecture ResNet18 ResNet18
Fine-tuning epochs 4 6
Learning rate Default 0.001
Validation accuracy 98.37% 98.84%

Experiment 2 achieved a validation accuracy of 99.07%.

This showed how changing the training configuration could affect validation performance without changing the underlying model architecture.

Error Analysis

After training, I performed error analysis using fastai's classification interpretation tools.

I examined the confusion matrix and examples associated with the model's highest-loss predictions.

This helped me look beyond a single accuracy number and understand where the model could still make mistakes.

I also tested the model using images outside the original dataset. This showed an important practical consideration: a model can perform very well on images from the same dataset while finding images from different environments more challenging.

Differences in lighting, background, camera quality, leaf position, and image conditions can affect predictions.


Confusion matrix showing the model's classification performance across Healthy, Early Blight, and Late Blight classes.

Exporting the Model

After training and analysis, I exported the trained PyTorch model so that it could be used outside the notebook environment.

I exported the model to TorchScript.

This allowed me to load the model in a separate Python application without depending on the original training notebook.

I then uploaded the exported model to Hugging Face, where it could be accessed by my deployed application.


Local verification of the exported TorchScript model using test images from the PlantVillage dataset.

Building the Application

The next step was to turn the trained model into an application.

I initially explored a Gradio implementation, but I encountered an inference compatibility issue when loading the exported fastai model locally.

Instead of stopping there, I changed the model export and inference approach by using TorchScript and built the final application with Streamlit.

The application allows a user to:

  1. Upload a potato leaf image.
  2. Process the image using the trained model.
  3. Receive a predicted disease class.
  4. View the probability for each class.

The three possible predictions are:

Healthy | Early Blight | Late Blight

Deploying the Application

The exported model was hosted on Hugging Face, while the Streamlit application was deployed using Streamlit Community Cloud.

The application code was stored in GitHub, while the larger model file was stored separately on Hugging Face.

This allowed the Streamlit application to download the model when it starts.

The final application is publicly available here:

πŸ‘‰ Potato Disease Classifier β€” Live Demo

Challenges I Encountered

One of the most important lessons from this project was that training a model successfully is only one part of machine learning development.

1. Model inference compatibility

The first application approach using the exported fastai model encountered an inference compatibility issue.

I investigated the problem and changed the model export and inference approach by using TorchScript.

2. Large model file

The exported model was approximately 47 MB, which was too large to upload directly through GitHub's browser interface.

I therefore hosted the model separately on Hugging Face.

3. Deployment

I initially explored deploying the application through Hugging Face Spaces. However, the available free option did not provide the type of Python compute environment required for the application.

I therefore deployed the Streamlit application using Streamlit Community Cloud.

These challenges taught me that deploying machine learning models involves more than model accuracy. Model serialization, dependencies, file size, hosting, and the inference environment all matter.

What I Learned

This project helped me understand the complete workflow of taking a deep learning model beyond a notebook.

I learned how to:

  • Work with an image classification dataset.
  • Build a data pipeline using fastai.
  • Use transfer learning with ResNet18.
  • Fine-tune a pretrained model.
  • Compare different training configurations.
  • Perform error analysis.
  • Export a trained PyTorch model to TorchScript.
  • Host a model on Hugging Face.
  • Build an interactive Streamlit application.
  • Connect an application to a remotely hosted model.
  • Deploy a machine learning application online.
  • Troubleshoot model inference and deployment problems.

Most importantly, I learned that a machine learning project does not end when the model reaches a high accuracy. Turning a model into something that another person can use requires additional engineering, testing, and deployment work.

Conclusion

This project took a potato disease classifier from a training notebook to a publicly accessible web application.

The final experiment using ResNet18 achieved 99.07% validation accuracy. The model was then exported to TorchScript, hosted on Hugging Face, integrated into a Streamlit application, and deployed online.

The project gave me practical experience across the machine learning lifecycleβ€”from data preparation and model training to deployment and troubleshooting.

Project Links

You can explore the project and try the deployed application below:

Thank you for reading!

Top comments (0)