AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

AI for Agriculture: Crop Prediction and Pest Detection

📚 AI Applications⏱️ 28 min read🎓 Grade 12
✍️ AI Computer Institute Editorial Team Updated: September 2026 CBSE-aligned · Peer-reviewed · 28 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

In 2017, cotton farmers across Maharashtra and Telangana faced a pest that had already beaten India's biotechnology defenses once: the pink bollworm (Pectinophora gossypiella), a moth larva that bores into cotton bolls and had developed resistance to Bt cotton's built-in toxin. The standard defense is a pheromone trap — a small container baited with synthetic female moth scent that lures and catches males, so an extension officer can count how many moths a field is producing that week. The counting itself was the bottleneck: India has over 120 million hectares under cultivation and nowhere near enough entomologists to walk every field. Wadhwani AI, a Mumbai-based nonprofit AI research institute, built a computer-vision pipeline that lets a farmer photograph the inside of a trap on their own phone and submit it through CottonAce, a purpose-built offline-capable mobile app that also gives voice-based advisory in multiple Indian languages, to get back an automated moth count within minutes — replacing a manual count that used to take a trained scout a full day per cluster of farms. The system doesn't decide anything mystical; it runs a convolutional neural network trained to detect and count moth bodies against a cluttered, low-resolution background, then compares the count against an economic threshold that agricultural extension guidelines define for that pest and crop stage. Cross the threshold, and the system recommends a targeted spray; stay under it, and it tells the farmer to save the pesticide and the money.

That system sits next to a second, quieter one running across Indian agriculture: yield prediction. Microsoft Research's FarmBeats project, piloted with ICRISAT in Andhra Pradesh, combined low-cost soil-moisture sensors, weather station feeds, and satellite imagery to forecast how much a field would actually produce, weeks before harvest — information insurers, input suppliers, and the farmers themselves all need and historically got only from post-harvest surveys. These two systems — a photo turned into a pest verdict, and a stack of numbers turned into a yield forecast — look like they belong to the same chapter only because both say "AI for agriculture." They are, in fact, two structurally different machine learning problems wearing the same headline, and understanding why they're different, and how each one is actually built, is the point of this chapter.

Two problems, two kinds of data, two kinds of model

Crop yield prediction is a regression problem over structured, largely tabular (or time-indexed) data: rainfall in millimetres, mean temperature in degrees Celsius, soil nitrogen-phosphorus-potassium concentration, a satellite-derived vegetation index, historical yield for that plot. The output is a continuous number — quintals per hectare. Pest and disease detection is an image classification problem: the input is a photograph — pixels, three color channels, no inherent units — and the output is a category (which pest, which disease, or "healthy") plus a confidence score. The first problem is naturally solved with models built for structured, tabular, and sequential data: linear and polynomial regression as a baseline, gradient-boosted decision tree ensembles (XGBoost, LightGBM) for the nonlinear interactions real agronomy has (rainfall matters far more at flowering stage than at sowing), and recurrent or 1D-convolutional architectures when you want to model the whole growing season as a sequence rather than a single snapshot. The second problem is a job for convolutional neural networks, almost always started from a network pretrained on a large generic image corpus and then fine-tuned — because no agricultural institute owns anywhere near the tens of millions of labelled images needed to train a modern CNN's early layers from scratch, but ImageNet-scale pretraining has already taught those early layers to detect edges, textures, and color gradients that transfer directly to leaf lesions and insect bodies.

Conflating the two is the single most common design mistake a student makes on this topic: feeding pixel images into a gradient-boosted tree (which has no notion of spatial locality and would have to memorize every pixel position independently), or feeding tabular weather statistics into a CNN (which has nothing to convolve over). The right model follows from the right question: is the input a photograph, or a table of measurements?

Turning satellite light into a number: NDVI

Before any regression model can predict yield, raw satellite imagery has to become a feature. The workhorse feature in agricultural remote sensing is the Normalized Difference Vegetation Index (NDVI), computed from two reflectance bands that satellites like Sentinel-2 or Landsat measure separately: near-infrared (NIR) and red (Red) light reflected off the ground.

NDVI = (ρ_NIR − ρ_Red) / (ρ_NIR + ρ_Red)

where ρ denotes surface reflectance (a fraction between 0 and 1) in each band. The physics behind this formula is what makes it useful rather than arbitrary: healthy chlorophyll-rich leaf tissue absorbs red light for photosynthesis (low ρ_Red) but strongly scatters near-infrared light because of the internal cell structure of a leaf (high ρ_NIR). A wilting, sparse, or diseased canopy does the opposite — it reflects more red light back (less chlorophyll absorption) and scatters less NIR (less leaf structure to bounce it off). NDVI turns that physical asymmetry into a single number between −1 and 1, with healthy dense vegetation typically landing between 0.6 and 0.9, and stressed or sparse vegetation dropping well below that.

Work the arithmetic through with two concrete satellite readings. A healthy wheat canopy at tillering stage might reflect ρ_NIR = 0.45 and ρ_Red = 0.08:

NDVI_healthy = (0.45 − 0.08) / (0.45 + 0.08) = 0.37 / 0.53 = 0.698

A drought-stressed patch of the same field, with a thinner and yellowing canopy, might reflect ρ_NIR = 0.25 and ρ_Red = 0.15:

NDVI_stressed = (0.25 − 0.15) / (0.25 + 0.15) = 0.10 / 0.40 = 0.25

Both numbers were computed directly from the formula above — no model, no training data, just two reflectance readings and a ratio. That is exactly why NDVI is the first feature every crop-yield pipeline computes: it is cheap (derivable from any multispectral satellite pass, revisited every few days, no ground sensors required), physically interpretable, and — critically for a regression model — it correlates with canopy biomass and, indirectly, with yield potential across the growing season. It is a correlate, not the yield itself: a field can show high NDVI from vigorous vegetative growth and still underperform at grain-fill if late-season rainfall fails, which is exactly why serious yield models track NDVI as a time series across the season rather than a single reading, and combine it with rainfall, temperature, and soil features rather than relying on it alone.

From features to a forecast: regression, worked by hand

Once features exist, yield prediction becomes ordinary (if agriculturally flavoured) regression. Take the simplest possible case — one feature, monsoon-season total rainfall in millimetres, predicting wheat yield in quintals per hectare — from four seasons of district-level data:

Rainfall (mm)4006008001000
Yield (quintal/ha)12182529

Ordinary least squares finds the line ŷ = slope·x + intercept that minimizes squared error. The slope is the ratio of the covariance of x and y to the variance of x, both computed around their means:

x̄ = (400+600+800+1000)/4 = 700       ȳ = (12+18+25+29)/4 = 21

Sxy = Σ(xᵢ−x̄)(yᵢ−ȳ) = (−300)(−9) + (−100)(−3) + (100)(4) + (300)(8)
    = 2700 + 300 + 400 + 2400 = 5800

Sxx = Σ(xᵢ−x̄)² = 300² + 100² + 100² + 300² = 90000+10000+10000+90000 = 200000

slope     = Sxy / Sxx = 5800 / 200000 = 0.029
intercept = ȳ − slope·x̄ = 21 − 0.029×700 = 21 − 20.3 = 0.700

So the fitted model is ŷ = 0.029·rainfall + 0.700. Every 100 mm of additional monsoon rainfall predicts roughly 2.9 additional quintals per hectare of wheat yield in this (illustrative, four-point) dataset. Plugging in a fifth season with 900 mm of rainfall:

ŷ(900) = 0.029 × 900 + 0.700 = 26.1 + 0.700 = 26.8 quintal/hectare

This is verifiable directly in code — the same arithmetic, no shortcuts:

import numpy as np

rainfall  = np.array([400, 600, 800, 1000])   # mm, monsoon-season total
yield_qha = np.array([12, 18, 25, 29])        # quintal/hectare, wheat

x_bar = rainfall.mean()
y_bar = yield_qha.mean()

Sxy = np.sum((rainfall - x_bar) * (yield_qha - y_bar))
Sxx = np.sum((rainfall - x_bar) ** 2)

slope = Sxy / Sxx
intercept = y_bar - slope * x_bar

print(f"slope = {slope:.3f}, intercept = {intercept:.3f}")
prediction = slope * 900 + intercept
print(f"predicted yield at 900 mm: {prediction:.1f} quintal/hectare")

This prints exactly slope = 0.029, intercept = 0.700 followed by predicted yield at 900 mm: 26.8 quintal/hectare — matching the hand computation line for line, because it is the same formula. A real deployed system replaces this single-feature toy with dozens of features (weekly NDVI, rainfall, temperature, soil nutrients) fed into a gradient-boosted tree ensemble, or — when the goal is to model the whole growing season rather than one snapshot — a recurrent or convolutional network over the time series. You et al. (AAAI, 2017) built exactly this for U.S. county-level soybean yield: they summarized MODIS satellite bands (surface reflectance and land-surface temperature) into per-week histograms, fed the histogram sequence through a CNN and an LSTM to extract a season-long representation, and used that representation to predict final yield — the same idea as the four-point regression above, scaled up from one feature to a learned representation of an entire season's remote-sensing history.

Turning a photo into a diagnosis: convolution, traced by hand

Pest and disease detection starts from a different kind of number: a grid of pixel intensities. A convolutional layer scans a small learned filter (a kernel) across that grid, computing a dot product at every position — this is the operation that lets a CNN respond to local patterns (an edge, a lesion boundary, the dark body of a moth) regardless of where in the image they appear.

Take a simplified 5×5 grayscale patch from a leaf photograph, where pixel values run from 10 (dark, necrotic lesion tissue) to 80 (lighter, healthy tissue), with a sharp boundary running down the middle:

Patch (5×5):
10 10 10 80 80
10 10 10 80 80
10 10 10 80 80
10 10 10 80 80
10 10 10 80 80

Kernel (3×3, vertical-edge detector):
 1  0 -1
 1  0 -1
 1  0 -1

A convolutional layer slides this 3×3 kernel over the 5×5 patch, stopping at every position where it fits fully inside — with stride 1 and no padding, that is 3 positions across and 3 down, giving a 3×3 output (the general formula, used constantly in CNN architecture design, is output size = (W − F)/S + 1, where W is input width, F is kernel size, and S is stride: (5 − 3)/1 + 1 = 3). At each position the layer takes the element-wise product of the kernel with the patch underneath it and sums the results. For the top-left window (columns 0–2, all value 10):

(1×10 + 0×10 + −1×10) + (1×10 + 0×10 + −1×10) + (1×10 + 0×10 + −1×10)
= (10 + 0 − 10)×3 = 0

Uniform region, zero response — the kernel found no edge because there's nothing to the left of column 0 versus column 2 of that window to contrast. Slide one column right, so the window now spans columns 1–3 (values 10, 10, 80):

(1×10 + 0×10 + −1×80) × 3 rows = (10 + 0 − 80)×3 = −70×3 = −210

A large-magnitude response, −210, exactly where the window straddles the boundary between dark lesion tissue and healthy tissue. Running this across all nine positions gives the full 3×3 output feature map:

  0  −210  −210
  0  −210  −210
  0  −210  −210

Zero wherever the window sits inside a uniform region, large magnitude wherever it crosses the boundary — this is precisely the mechanism by which a convolutional layer turns "a photograph" into "a map of where the interesting boundaries are." A real, trained network does not use this hand-designed edge kernel; backpropagation learns dozens of kernels per layer directly from labelled data, and early layers converge on edge- and texture-detectors like this one anyway because they are genuinely useful, while deeper layers combine them into detectors for lesion shapes, moth-body silhouettes, or leaf-vein patterns. Stack several such layers, interleave them with pooling (which downsamples the feature map and adds a degree of position-invariance), and finish with a fully connected layer that outputs a probability over the target classes — healthy, pink bollworm, leaf rust, and so on — via a softmax.

Training a CNN like this from raw pixels requires far more labelled images than any single agricultural project can collect, so the standard approach is transfer learning: start from a CNN already trained on a huge generic image dataset (ImageNet, roughly 1.2 million photographs across 1,000 everyday object categories), keep its early convolutional layers — which have already learned general-purpose edge and texture detectors — and retrain only the final layers on a much smaller, domain-specific dataset. The dataset of choice for plant disease work is PlantVillage, an open-access repository of over 50,000 labelled leaf images across 14 crop species and 26 diseases, released by Hughes and Salathé (2015). A minimal transfer-learning setup in PyTorch:

import torch
import torch.nn as nn
from torchvision import models

# Start from a network pretrained on ImageNet; its convolutional
# base already knows general edge/texture features.
base_model = models.efficientnet_b0(weights="IMAGENET1K_V1")

# Freeze the pretrained convolutional layers — only the new
# classification head will be trained on the small pest dataset.
for param in base_model.parameters():
    param.requires_grad = False

num_classes = 5  # e.g. healthy, pink bollworm, leaf rust, aphid, blight
in_features = base_model.classifier[1].in_features
base_model.classifier[1] = nn.Linear(in_features, num_classes)

optimizer = torch.optim.Adam(base_model.classifier.parameters(), lr=1e-3)
loss_fn = nn.CrossEntropyLoss()

# train_loader: (assumed helper, not shown) yields (image_batch, label_batch)
# pairs of shape (batch_size, 3, 224, 224) and (batch_size,) respectively,
# drawn from a labelled folder of trap or leaf photographs.
for images, labels in train_loader:
    optimizer.zero_grad()
    outputs = base_model(images)
    loss = loss_fn(outputs, labels)
    loss.backward()
    optimizer.step()

Only base_model.classifier[1], the new final linear layer, has requires_grad=True left on by the freeze loop above it, so optimizer.step() updates only those weights — the pretrained convolutional base stays fixed, which is what makes transfer learning practical with a dataset of a few thousand field images instead of a few million.

The diagram: two pipelines, one advisory

AI for Agriculture: two pipelines, one advisory CROP YIELD PREDICTION — structured / time-series data Satellite NIR/Red bands (Sentinel-2) + IMD rainfall data + soil N-P-K sensors Feature extraction: NDVI = (NIR−Red) /(NIR+Red) + soil, rainfall Regression model: Gradient-Boosted Trees / CNN-LSTM over the season Yield estimate: quintal/hectare, block or district level Example: healthy canopy NDVI ≈ 0.70  •  drought-stressed patch NDVI ≈ 0.25 PEST & DISEASE DETECTION — image data Farmer's phone photo of leaf or pheromone trap (via CottonAce app) CNN: conv + pool layers learn lesion / moth-body edge features Classifier head: softmax over disease / pest class probabilities Pest class + confidence, e.g. "pink bollworm, 94% confidence" Example: edge-kernel response of magnitude 210 flags a lesion boundary; softmax then ranks the classes Farmer advisory (SMS / app) "Spray now — pest count crossed threshold" OR "Irrigate — predicted yield falling, soil moisture low" OR "No action — crop healthy, on track for average yield"

Why the confusion matrix, not accuracy, decides whether a pest model is trustworthy

A pest-detection classifier is not judged the way a general-purpose photo classifier is. Suppose a pink-bollworm trap-image classifier is evaluated on 200 held-out trap photographs, of which 45 actually show an infested trap (crossing the economic threshold) and 155 do not. The classifier flags 50 of the 200 as "infested." A confusion matrix records the four possible outcomes:

Predicted infestedPredicted not infested
Actually infestedTP = 42FN = 3
Actually not infestedFP = 8TN = 147

From these four counts:

precision = TP / (TP + FP) = 42 / 50        = 0.840
recall    = TP / (TP + FN) = 42 / 45        = 0.933
F1        = 2·precision·recall / (precision+recall) = 0.884
accuracy  = (TP + TN) / 200                 = 189 / 200 = 0.945

Accuracy looks reassuring at 94.5%, but it is the wrong number to optimize here, because the two error types have wildly different real-world costs. A false positive (FP = 8) means a farmer sprays pesticide on a field that didn't need it — money and chemical wasted, but recoverable. A false negative (FN = 3) means an actual infestation goes unflagged, cotton bolls are lost to larvae that were never sprayed against, and by the time the damage is visually obvious the pest has already completed a generation. In agricultural pest alerts, missing a real outbreak is categorically worse than one unnecessary spray, so a well-designed system deliberately biases its decision threshold toward higher recall even at the cost of some precision — accepting more false alarms in exchange for catching more real ones. This is the same precision–recall tradeoff taught generically in Class 12 machine-learning evaluation, but agriculture is a genuinely good setting to see why the "right" balance point is not 50-50 accuracy maximization but a decision driven by the asymmetric cost of the two mistakes.

The misconception to correct: lab accuracy is not field accuracy

A student who has just built a 99%-accurate leaf-disease classifier on the PlantVillage dataset will naturally assume that number describes how the model will perform in a real Indian field. It does not, and the gap is large and well documented. Mohanty, Hughes, and Salathé (Frontiers in Plant Science, 2016) trained deep CNNs on PlantVillage and reported 99.35% accuracy on a held-out test split — but PlantVillage images are taken under near-identical, controlled conditions: a single leaf, laid flat, against a plain uniform background, consistent lighting. When the same authors tested their trained model on a separate set of leaf images pulled from ordinary online sources — real backgrounds, real shadows, multiple leaves, phone-camera lighting — accuracy collapsed to roughly the 30% range. The model had not learned "what leaf rust looks like"; it had partly learned "what a PlantVillage photograph looks like," and the uniform gray background had become an accidental, unintended feature.

This is why a serious agricultural CNN deployment — Wadhwani AI's included — cannot simply fine-tune on a lab dataset and ship: it needs training and validation images collected under the same messy conditions the model will actually see (cluttered fields, monsoon lighting, farmer camera-shake, multiple overlapping leaves), plus explicit testing for this specific failure mode before any accuracy number is trusted. "High accuracy on the benchmark dataset" and "reliable in the field" are different claims, and the entire point of the PlantVillage-to-web-photo experiment above is that a model can score almost perfectly on the first while failing badly at the second.

Active recall

Attempt each question before reading its answer.

1. A Sentinel-2 pass over a sugarcane field records ρ_NIR = 0.55 and ρ_Red = 0.10. Compute the NDVI, and state whether this reading is more consistent with a healthy or a stressed canopy.

2. A convolutional layer receives a 7×7 input feature map. It applies a 3×3 kernel with stride 2 and no padding. What is the spatial size of the output feature map?

3. Using the four-point rainfall/yield dataset from the worked example (400→12, 600→18, 800→25, 1000→29 quintal/ha), a fifth data point is discovered: 500 mm of rainfall produced 22 quintal/hectare — well above what the fitted line predicts. Without recomputing the full regression, name two agricultural or data-quality explanations for why this single point might be a legitimate outlier rather than evidence the model is wrong.

4. In the pest-detection confusion matrix worked example (TP=42, FP=8, FN=3, TN=147), suppose the classifier's decision threshold is lowered so that FN drops from 3 to 1, but FP rises from 8 to 20 (TP correspondingly rises to 44, TN drops to 135, total still 200). Recompute precision and recall, and state whether this threshold change is agriculturally desirable given the cost asymmetry discussed above.

5. Explain, in terms of what a convolutional kernel actually computes, why the hand-worked edge-detection example in this chapter produced a response of magnitude 210 exactly at the columns where the patch transitions from value 10 to value 80, and zero everywhere else.

6. A team fine-tunes an ImageNet-pretrained CNN entirely on PlantVillage images and reports 99% validation accuracy, then deploys it directly to farmer phone uploads. Name the specific risk this chapter identifies, and describe one concrete change to the training data that would address it.

Answers

1. NDVI = (0.55 − 0.10)/(0.55 + 0.10) = 0.45/0.65 ≈ 0.692. This is close to the healthy-wheat example (0.698) computed earlier and well within the 0.6–0.9 range typical of dense, chlorophyll-rich vegetation — consistent with a healthy canopy, not a stressed one.

2. Output size = (W − F)/S + 1 = (7 − 3)/2 + 1 = 4/2 + 1 = 2 + 1 = 3. The output feature map is 3×3.

3. Two legitimate explanations: (a) rainfall totals alone don't capture timing — 500 mm concentrated during flowering and grain-fill can outperform 800 mm poorly distributed across the season, since crop water demand is stage-dependent, not uniform; (b) the single-feature model omits soil quality, seed variety, and fertilizer application, any of which could independently push that season's yield above what rainfall alone predicts — a reminder that a one-feature regression is a teaching simplification, and a real system's feature set (soil N-P-K, NDVI time series, variety) is what absorbs exactly this kind of "outlier."

4. New precision = TP/(TP+FP) = 44/64 = 0.688. New recall = TP/(TP+FN) = 44/45 = 0.978. Precision fell noticeably (0.840→0.688) while recall rose only slightly (0.933→0.978, since FN was already small). Whether this is desirable depends on the actual cost ratio, but given that a missed infestation (FN) causes irreversible crop loss while a false alarm (FP) only wastes one unnecessary spray, trading a dozen extra false alarms for one fewer missed outbreak is generally a reasonable threshold shift in this domain — recall matters more than precision when the false-negative cost is asymmetrically higher.

5. The kernel's three columns are [1, 0, −1]. When the 3×3 window sits entirely inside a uniform region, the "+1×value" and "−1×value" contributions from the outer columns cancel exactly (both columns hold the same pixel value), leaving zero regardless of what that uniform value is — which is why the leftmost window (all 10s) and, by the same logic, an all-80s window would both give zero. When the window straddles the boundary, the left column holds the low value (10) and the right column holds the high value (80), so the subtraction no longer cancels: 3×(1×10 − 1×80) = 3×(−70) = −210. The kernel is, by construction, a horizontal-difference operator — it responds only to a difference between a window's left and right edges, which is exactly where an intensity boundary (a lesion edge, a leaf margin) sits.

6. The risk is the lab-to-field domain-shift documented by Mohanty, Hughes, and Salathé (2016): a model trained only on PlantVillage's uniform-background, controlled-lighting images can learn incidental features of the dataset's photography setup rather than the disease itself, causing accuracy to collapse — reported around 30% in their own out-of-distribution test — on real, cluttered field photographs. The concrete fix is to add training and validation images captured under the deployment conditions (real field backgrounds, varied lighting, phone-camera quality, multiple leaves per frame) rather than relying on PlantVillage accuracy alone, and to report a separate held-out accuracy number measured specifically on field-condition images before trusting the model's real-world performance.

Think About It

Think about this: How would you explain ai for agriculture: crop prediction and pest detection to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

Practice Exercises

Now it is time to practice! Complete these challenges to solidify your understanding:

  • Exercise 1: Write a short program that demonstrates the core concept from this chapter. Test it with at least 3 different inputs.
  • Exercise 2: Find a real-world example where ai for agriculture: crop prediction and pest detection is used in an Indian company (like TCS, Infosys, Flipkart, or ISRO). Write a paragraph explaining the connection.
  • Exercise 3: Create a mind-map connecting ai for agriculture: crop prediction and pest detection to at least 3 other topics you have studied.

Key Takeaways — Summary and Recap

Let us recap what we covered: the core ideas behind ai for agriculture: crop prediction and pest detection, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.

← AI for Climate: Weather Prediction and Carbon TrackingAI for Education: Adaptive Learning and Tutoring Systems →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn