Path: blob/master/site/en-snapshot/hub/tutorials/cropnet_on_device.ipynb
25118 views
Copyright 2021 The TensorFlow Hub Authors.
Licensed under the Apache License, Version 2.0 (the "License");
Fine tuning models for plant disease detection
This notebook shows you how to fine-tune CropNet models from TensorFlow Hub on a dataset from TFDS or your own crop disease detection dataset.
You will:
Load the TFDS cassava dataset or your own data
Enrich the data with unknown (negative) examples to get a more robust model
Apply image augmentations to the data
Load and fine tune a CropNet model from TF Hub
Export a TFLite model, ready to be deployed on your app with Task Library, MLKit or TFLite directly
Imports and Dependencies
Before starting, you'll need to install some of the dependencies that will be needed like Model Maker and the latest version of TensorFlow Datasets.
Load a TFDS dataset to fine-tune on
Lets use the publicly available Cassava Leaf Disease dataset from TFDS.
Or alternatively load your own data to fine-tune on
Instead of using a TFDS dataset, you can also train on your own data. This code snippet shows how to load your own custom dataset. See this link for the supported structure of the data. An example is provided here using the publicly available Cassava Leaf Disease dataset.
Visualize samples from train split
Let's take a look at some examples from the dataset including the class id and the class name for the image samples and their labels.
Add images to be used as Unknown examples from TFDS datasets
Add additional unknown (negative) examples to the training dataset and assign a new unknown class label number to them. The goal is to have a model that, when used in practice (e.g. in the field), has the option of predicting "Unknown" when it sees something unexpected.
Below you can see a list of datasets that will be used to sample the additional unknown imagery. It includes 3 completely different datasets to increase diversity. One of them is a beans leaf disease dataset, so that the model has exposure to diseased plants other than cassava.
The UNKNOWN datasets are also loaded from TFDS.
Apply augmentations
For all the images, to make them more diverse, you'll apply some augmentation, like changes in:
Brightness
Contrast
Saturation
Hue
Crop
These types of augmentations help make the model more robust to variations in image inputs.
To apply the augmentation, it uses the map
method from the Dataset class.
Wrap the data into Model Maker friendly format
To use these dataset with Model Maker, they need to be in a ImageClassifierDataLoader class.
Run training
TensorFlow Hub has multiple models available for Transfer Learning.
Here you can choose one and you can also keep experimenting with other ones to try to get better results.
If you want even more models to try, you can add them from this collection.
To fine tune the model, you will use Model Maker. This makes the overall solution easier since after the training of the model, it'll also convert it to TFLite.
Model Maker makes this conversion be the best one possible and with all the necessary information to easily deploy the model on-device later.
The model spec is how you tell Model Maker which base model you'd like to use.
One important detail here is setting train_whole_model
which will make the base model fine tuned during training. This makes the process slower but the final model has a higher accuracy. Setting shuffle
will make sure the model sees the data in a random shuffled order which is a best practice for model learning.
Evaluate model on test split
To have an even better understanding of the fine tuned model, it's good to analyse the confusion matrix. This will show how often one class is predicted as another.
Evaluate model on unknown test data
In this evaluation we expect the model to have accuracy of almost 1. All images the model is tested on are not related to the normal dataset and hence we expect the model to predict the "Unknown" class label.
Print the confusion matrix.
Export the model as TFLite and SavedModel
Now we can export the trained models in TFLite and SavedModel formats for deploying on-device and using for inference in TensorFlow.
Next steps
The model that you've just trained can be used on mobile devices and even deployed in the field!
To download the model, click the folder icon for the Files menu on the left side of the colab, and choose the download option.
The same technique used here could be applied to other plant diseases tasks that might be more suitable for your use case or any other type of image classification task. If you want to follow up and deploy on an Android app, you can continue on this Android quickstart guide.