Skip to content

bwolfson97/plant_pathology

Repository files navigation

Plant Pathology Classifier

A neural network classifies images of leaves as healthy or diseased!

A picture of a leaf

Recently, I've been learning about Fast.ai and PyTorch in my free time and wanted to apply my knowledge. I'm trying to learn how to win Kaggle competitions, so I decided to build a model for the completed Kaggle Plant Pathology Competition.

I built this model using Nbdev, which provides an literate programming environment as originally envisioned by Donald Knuth. This means the notebooks in the nbks folder are the library's "source code". They get converted into regular python files, a full documentation site, and contain unit and functional tests, all in one place.

Install

pip install plant-pathology

How to use

Inference example

from plant_pathology.pretrained_models import get_model

model = get_model("resnet18_2021-04-08")
prediction = model.predict_leaf("images/leaf.jpg")
prediction
{'predicted_class': 'rust',
 'probabilities': {'healthy': 0.05,
  'multiple_diseases': 0.04,
  'rust': 0.88,
  'scab': 0.03}}

Training

Run training script

❯ python -m plant_pathology.train -h
usage: train.py [-h] [--epochs EPOCHS] [--lr LR] [--frz FRZ] [--pre PRE [PRE ...]] [--re RE] [--bs BS] [--smooth] [--arch ARCH] [--dump] [--log] [--save] [--mixup MIXUP] [--tta] [--fp16] [--do_eval] [--val_fold VAL_FOLD] [--pseudo PSEUDO] [--export]
                path

positional arguments:
  path                 Path to data dir

optional arguments:
  -h, --help           show this help message and exit
  --epochs EPOCHS      Number of unfrozen epochs (default: 1)
  --lr LR              Initial learning rate (default: 0.0003)
  --frz FRZ            Number of frozen epochs (default: 1)
  --pre PRE [PRE ...]  Image presize (default: (682, 1024))
  --re RE              Image resize (default: 256)
  --bs BS              Batch size (default: 256)
  --smooth             Label smoothing? (default: False)
  --arch ARCH          Architecture (default: resnet18)
  --dump               Don't train, just print model (default: False)
  --log                Log w/ W&B (default: False)
  --save               Save model based on RocAuc (default: False)
  --mixup MIXUP        Mixup (0.4 is good) (default: 0.0)
  --tta                Test-time augmentation (default: False)
  --fp16               Mixed-precision training (default: False)
  --do_eval            Evaluate model and save predictions CSV (default: False)
  --val_fold VAL_FOLD  Don't go cross-validation, just do 1 fold (or pass 9 to train on all data)
  --pseudo PSEUDO      Path to pseudo labels to train on
  --export             Export learner(s) to export_val_on_{fold}.pkl (default: False)

Testing

To run all the tests in the notebooks in parallel, just run nbdev_test_nbs from the terminal! :)

Web App

I deployed the classifier as a simple web app using Streamlit and Heroku. Note, it may take a few minutes to start up.

The code for the app is here.

About

A convolutional neural network that predicts diseases of plants based on leaf images!

Resources

License

Stars

Watchers

Forks

Packages

No packages published