AI for farmingBeginnerLesson 1207 min read

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.

Lesson in motion

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…

1/5
In simple words
Imagine teaching a child to spot a rotten mango. You show many good mangoes and many rotten ones. Soon the child notices dark soft patches. But if you only ever showed mangoes on a white plate in sunlight, the child may get confused by a mango on a dark cloth in the evening. Photo apps learn the same way, and get confused the same way.
A crop disease app uses an image classifier. It is a program that looks at a photo and picks one label from a list, such as "healthy", "leaf spot" or "yellowing". It gives a score for how well the photo matches each label.

How a classifier learns

  1. 1

    Collect photos

    Thousands of leaf photos are gathered.
  2. 2

    Label them

    Experts mark each photo with the right label: healthy, or which disease.
  3. 3

    Train

    The program looks at the photos again and again, and adjusts itself to match the labels better.
  4. 4

    Test

    It is checked on photos it has never seen, to measure how often it is right.
  5. 5

    Use

    A farmer's new photo goes in, and the app gives its best guess with a score.
Inside, the program learns features: patterns of colour, shape and texture. Brown round spots, yellow edges or a white powder can all be features. Modern apps learn these features by themselves from the photos.

Why the app can be wrong

ProblemWhat happensWhat helps
LightEvening light or harsh sun changes colours. A healthy leaf can look yellow.Take photos in soft daylight, without strong shadows
BackgroundIf 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 varietiesThe 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 listThe 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 problemsLack 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
Note
Many research projects use a well-known public dataset of plant leaf photos. Such photos are often taken one leaf at a time on a plain background. A model can score very well on photos like these, and still do much worse on messy photos taken in a real field. Always ask: was it tested on photos like mine?

A tiny classifier you can run

Real apps look at millions of pixel values. To see the idea, we can describe each leaf with just three made-up numbers: how much of it is green, how much is yellow, and how much is covered with brown spots. Then we sort a new leaf by finding which group it is closest to.
Python: which group is this leaf closest to?python
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.")
Watch out
A score like "92% leaf spot" is not the chance that you are right to spray. It only says how much this photo looks like the training photos of that label. Apps can be very confident and still wrong.
Danger
Never choose a pesticide or its dose from an app's photo result alone. Confirm the problem and the treatment with your KVK or agriculture officer, and follow the product label and safety instructions.
Try this. In the code above, change 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.

~20 min

The problem

This runs in your browser. All numbers are made up: each leaf is described by [green share, yellow share, brown-spot share].

  1. Run the starter. The classify function is "lazy": it always says "healthy". Look at its accuracy.
  2. Rewrite classify(features) so it returns the label whose centre (in the centres dictionary) is closest to the leaf. Use the distance from the example code in the lesson.
  3. Run it again. Accuracy on test_leaves should now be much higher.
  4. 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.

Output

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.

Please sign in to continue.

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.

Sign in to take the test and save your progress