Spotting crop disease from a photo
Leaf-photo apps use image classifiers that learned from many labelled photos. Learn how they decide, why light, background and local varieties can fool them, and why an expert must confirm.
In 60 seconds
Spotting crop disease from a photo
Leaf-photo apps use image classifiers that learned from many labelled photos. Learn how they decide, why light, background and local varieties can fool them, and why an expert…
How a classifier learns
- 1
Collect photos
Thousands of leaf photos are gathered. - 2
Label them
Experts mark each photo with the right label: healthy, or which disease. - 3
Train
The program looks at the photos again and again, and adjusts itself to match the labels better. - 4
Test
It is checked on photos it has never seen, to measure how often it is right. - 5
Use
A farmer's new photo goes in, and the app gives its best guess with a score.
Why the app can be wrong
| Problem | What happens | What helps |
|---|---|---|
| Light | Evening light or harsh sun changes colours. A healthy leaf can look yellow. | Take photos in soft daylight, without strong shadows |
| Background | If the training photos were on a plain background, a leaf still on the plant with soil behind it may confuse the app. | Fill the photo with the leaf; take several photos |
| Local varieties | The app may never have seen your local variety, whose healthy leaves have a different colour or shape. | Prefer apps trained or tested on crops from your region |
| Diseases not in its list | The app can only choose from the labels it knows. It will still give an answer, even if the real problem is new to it. | Treat a strange result as a reason to ask an expert |
| Look-alike problems | Lack of a nutrient, too much water, and some diseases can look similar in a photo. | An expert can check the soil, roots and whole plant |
A tiny classifier you can run
import numpy as np
# Made-up "average leaf" for each group: [green share, yellow share, brown-spot share]
groups = {
"healthy": np.array([0.85, 0.10, 0.02]),
"leaf spot": np.array([0.60, 0.10, 0.25]),
"yellowing": np.array([0.40, 0.50, 0.03]),
}
new_leaf = np.array([0.62, 0.12, 0.20])
for name, centre in groups.items():
distance = np.linalg.norm(new_leaf - centre)
print(f"{name:10s} distance {distance:.3f}")
print("Closest group is the app's guess. A smaller distance means more similar.")new_leaf to [0.45, 0.42, 0.05]. Which group is closest now? Then imagine the same leaf photographed in evening light, which makes everything look more yellow. How could that change the answer?Watch and read more
Lab
A working nearest-centre leaf classifier on made-up colour numbers, and a note on what changes in bad light.
The problem
This runs in your browser. All numbers are made up: each leaf is described by [green share, yellow share, brown-spot share].
- Run the starter. The
classifyfunction is "lazy": it always says "healthy". Look at its accuracy. - Rewrite
classify(features)so it returns the label whose centre (in thecentresdictionary) is closest to the leaf. Use the distance from the example code in the lesson. - Run it again. Accuracy on
test_leavesshould now be much higher. - Look at the printout for
evening_leaves: the same kind of healthy leaves, photographed in yellow evening light. Write two lines on what happened and why.
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1The classifier gets 100% on test_leaves but fails on evening_leaves. Why is 100% on the test set misleading here?Reveal
The test leaves were made in the same way and the same "light" as the training leaves, so they were easy. The evening leaves come from different conditions, and the model has never seen those. Accuracy only means something for photos like the test photos. To trust a real app, it must be tested on photos from real fields, in different light, with local varieties.
Q2How could you make a classifier cope better with different light?Reveal
Add training photos taken in many kinds of light, time of day and background, all correctly labelled. Some systems also adjust colours before classifying, or add changed copies of photos (brighter, darker, warmer) during training. Also teach the app to say "not sure" when a photo is far from every group, and to ask for a better photo.
Questions people ask
How good are these apps in real fields?
It depends on the app, the crop and the photo. Some do well on common problems of common crops. Results in research papers are often measured on clean photos and can be higher than what you get in the field. Test an app on problems your KVK has already confirmed before you rely on it.
Why does the app give a different answer for two photos of the same leaf?
Small changes in light, angle, focus or background change the pixel values. If the model is unsure between two labels, a small change can flip the answer. That is a sign to get an expert opinion.
Can I help make these apps better?
Yes. Some projects collect photos from farmers, labelled by experts, to cover local varieties and real field conditions. Before sharing, ask how your photos and location will be used, and whether you can say no.
What should a good app show besides its guess?
How sure it is, a "not sure" answer when the photo is unclear, tips for taking a better photo, and a clear message to confirm with an expert before treatment.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.