Guide
Building a Mobile Crop Disease Detection System with MobileNetV2, TFLite and Flutter
In short
A mobile crop disease detector photographs a leaf, classifies it with a CNN and returns a diagnosis with treatment guidance. Bishal Neupane's system uses MobileNetV2 transfer learning in TensorFlow, converts the model to TFLite for offline use in a Flutter app, adds a FastAPI and PostgreSQL backend, cites FAO/USDA treatments, and contributes GPS-tagged field data to the Nepal Agricultural Disease Dataset.
- Model
- TensorFlow MobileNetV2 (CNN), transfer learning
- On-device inference
- Offline TFLite
- App and backend
- Flutter, FastAPI, PostgreSQL
- Recommendations
- FAO/USDA treatment citations
- Data contribution
- GPS-tagged field data to the Nepal Agricultural Disease Dataset (NADD)
What is the crop disease detection system?
Bishal Neupane developed a research-oriented mobile AI system that identifies crop diseases from photographs of leaves and returns treatment recommendations backed by FAO and USDA citations. The methods page lists its components: CNN-based disease detection, offline TensorFlow Lite (TFLite) support, GPS-tagged research data, and a stack of Flutter, FastAPI, TensorFlow with MobileNetV2, TFLite and PostgreSQL. According to his CV, the project also contributed GPS-tagged field data to the Nepal Agricultural Disease Dataset (NADD), which the system is helping to build. The source code is on GitHub.
This guide explains how the pieces of such a system fit together and how transfer learning with MobileNetV2 works, so that students and researchers can understand or reproduce the approach. It does not report accuracy figures, because none are published on the site for this system.
| Component | Technology (as listed) | Typical role in a system like this |
|---|---|---|
| Mobile app | Flutter | Camera capture, results display, works on Android and iOS from one codebase |
| Model | TensorFlow, MobileNetV2 (CNN) | Classifies leaf images into disease or healthy classes |
| On-device inference | TFLite | Runs the model offline on the phone |
| Backend API | FastAPI | Server-side API that the app can call |
| Database | PostgreSQL | Structured storage, for example of research records |
| Recommendations | FAO/USDA citations | Links each diagnosis to referenced treatment guidance |
Why build crop disease detection as a mobile app?
Disease diagnosis in the field depends on expert eyes, and extension staff cannot visit every plot when symptoms first appear. A phone app puts a first-pass diagnosis in the hands of farmers and field technicians at the moment they see a symptom. Two design choices follow from conditions in rural Nepal. First, connectivity in hill and remote areas is unreliable, so the model has to run on the device rather than depend on a server; that is the purpose of the offline TFLite support. Second, a diagnosis is only useful if it leads to a sound action, which is why recommendations are tied to FAO and USDA sources rather than generated freely.
Why MobileNetV2?
MobileNetV2, introduced by Sandler and colleagues in 2018, was designed for mobile and resource-constrained devices. Its building block, the inverted residual with a linear bottleneck, uses lightweight depthwise convolutions to filter features in an expanded intermediate layer, then projects them back into a narrow layer without a non-linearity. The result is a network that gives good accuracy for its size and computational cost, which matters when inference has to run on a mid-range phone without draining the battery.
How transfer learning works for leaf disease images
Training a CNN from scratch needs very large labelled datasets. Transfer learning avoids that by starting from a network already trained on ImageNet, whose early layers have learned general visual features such as edges, textures and colour patterns. Those features transfer well to leaf lesions, spots, mildew and discolouration. The usual recipe, described in the TensorFlow transfer-learning tutorial, has two stages:
- Feature extraction. Load MobileNetV2 without its ImageNet classification head, freeze its weights, and train only a small new head (global average pooling, dropout and a dense softmax layer with one output per disease class).
- Fine-tuning. Unfreeze the upper layers of the base network and train them together with the head at a much lower learning rate, so the pretrained features adapt to leaf images without being destroyed.
import tensorflow as tf
IMG_SIZE = (224, 224)
NUM_CLASSES = 10 # set to the number of disease + healthy classes in your data
base = tf.keras.applications.MobileNetV2(input_shape=IMG_SIZE + (3,),
include_top=False, weights='imagenet')
base.trainable = False # stage 1: freeze the pretrained feature extractor
inputs = tf.keras.Input(shape=IMG_SIZE + (3,))
x = tf.keras.applications.mobilenet_v2.preprocess_input(inputs) # scales pixels to [-1, 1]
x = base(x, training=False) # keep BatchNormalization in inference mode
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer=tf.keras.optimizers.Adam(1e-4),
loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_ds, validation_data=val_ds, epochs=10)
Two details from the tutorial matter. MobileNetV2 expects pixel values in the range −1 to 1, which preprocess_input handles; and the base model is called with training=False so its BatchNormalization layers stay in inference mode during fine-tuning.
From Keras model to offline TFLite on the phone
# Stage 2 (optional): fine-tune the top of the base network with a lower learning rate
base.trainable = True
for layer in base.layers[:100]:
layer.trainable = False
model.compile(optimizer=tf.keras.optimizers.Adam(1e-5),
loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_ds, validation_data=val_ds, epochs=10)
# Convert for offline use on the phone
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT] # post-training quantization
tflite_model = converter.convert()
open('crop_disease.tflite', 'wb').write(tflite_model)
The TFLite (LiteRT) conversion guide notes that post-training quantization can reduce model size and latency with minimal loss in accuracy. In a typical setup, the resulting .tflite file is bundled with the mobile app and run through a TFLite interpreter, so a diagnosis works with no network connection, while a backend API handles tasks that need a server when a connection is available. For the exact implementation in Bishal's app, see the repository.
Why a Nepal-specific, GPS-tagged dataset matters
Most published leaf-disease models are trained on public datasets photographed under controlled conditions. In the widely cited study by Mohanty, Hughes and Salathé (2016), a model trained on 54,306 leaf images reached 99.35% accuracy on a held-out test set from the same dataset, but accuracy fell to just above 31% when tested on images taken under different conditions. Field photos differ in background, lighting, leaf angle and disease stage, and local crop varieties and pathogens differ from those in global datasets.
That is the case for collecting local data. GPS-tagged images do more than add training examples: they record where and when a disease was observed, which turns individual diagnoses into spatial data. Mapped over time, such records could show where outbreaks cluster and how they relate to elevation, season or crop, linking the app to the GIS and remote-sensing side of precision agriculture discussed in GIS, remote sensing and ML as one system.
Limitations and responsible use
- A classifier can only recognise classes it was trained on; an unfamiliar disease or nutrient deficiency may be confidently misclassified.
- Accuracy reported on a random test split overstates field performance. Evaluation on images from different farms, seasons and phones is a stricter test.
- Several diseases share visible symptoms; laboratory or expert confirmation remains important before costly treatment.
- Treatment advice must follow local registration and safety rules for any pesticide recommended.
For remote-sensing approaches to crop health at field scale, see the Sentinel-2 vegetation indices guide. Researchers and supervisors interested in field-ready AI for crop protection can reach Bishal through the contact page.
Frequently asked questions
Why use MobileNetV2 for a crop disease app?
MobileNetV2 was designed for mobile and embedded devices. Its inverted residual blocks with linear bottlenecks and depthwise convolutions keep the model small and fast while retaining good accuracy, so it can run on an ordinary smartphone. Pretrained ImageNet weights are available in Keras, which makes transfer learning to leaf-disease images straightforward.
What is transfer learning in plant disease detection?
Transfer learning reuses a network already trained on a large image dataset such as ImageNet. Its learned features, like edges, textures and colour patterns, are kept, and only a new classification head is trained on leaf-disease images. Optionally, the upper layers are then fine-tuned at a low learning rate. This needs far fewer labelled images than training from scratch.
How does a crop disease app work without internet?
The trained TensorFlow model is converted to a TensorFlow Lite (.tflite) file, often with post-training quantization to make it smaller and faster, and bundled inside the app. A TFLite interpreter on the phone runs the prediction locally, so diagnosis works in fields without network coverage. Data can be synced to a server later.
Why do plant disease models perform worse in the field?
Many models are trained on images taken under controlled conditions with plain backgrounds. Mohanty and colleagues (2016) reported 99.35% accuracy on a held-out set from their training dataset but just above 31% on images taken under different conditions. Field photos vary in lighting, background and disease stage, which is why locally collected data matters.
What is the Nepal Agricultural Disease Dataset (NADD)?
NADD is a dataset of crop disease records from Nepal that Bishal Neupane's crop disease detection project is helping to build. According to his CV, he contributed GPS-tagged field data to it. Location-tagged images can improve models for local conditions and also allow disease observations to be mapped and analysed spatially.
Sources
- Sandler et al. (2018) MobileNetV2: Inverted Residuals and Linear Bottlenecks
- TensorFlow tutorial: Transfer learning and fine-tuning (MobileNetV2)
- LiteRT / TensorFlow Lite: Convert TensorFlow models
- Mohanty, Hughes and Salathé (2016) Using Deep Learning for Image-Based Plant Disease Detection. Frontiers in Plant Science
- Crop disease detection app source code (GitHub)