73  Convolutional Neural Networks

Feeding a raw image into a feedforward network directly, one input neuron per pixel, throws away something important: the fact that nearby pixels are related, and that a meaningful visual pattern, the edge of a diseased leaf lesion, the boundary between a healthy and a stressed patch of a field, can appear anywhere in the frame. A Convolutional Neural Network (CNN) (Yann LeCun et al., 1998) is built specifically for grid-structured data like images, using a specialized operation, convolution, that scans a small learned filter across the whole image, detecting the same local pattern wherever it happens to occur.

73.1 Convolution, Filters, and Feature Maps

A filter (also called a kernel) is a small grid of weights, commonly 3x3 or 5x5. Convolution slides this filter across the input image one position at a time; at each position, it multiplies the filter’s weights against the pixel values underneath it and sums the result into a single output value. The complete grid of these output values is called a feature map, and it highlights wherever in the image the pattern that particular filter is tuned to detect shows up strongly.

In a trained CNN, filter weights are learned by backpropagation, exactly the algorithm from the earlier section in this topic, just applied to convolutional weights rather than fully connected ones, so the network discovers on its own which local patterns, edges, textures, color transitions, are actually useful for the task at hand. A typical CNN stacks several convolution layers, each followed by a nonlinear activation (ReLU is standard) and a pooling step, most commonly max pooling, which shrinks each feature map by keeping only the strongest activation in each small block, reducing the amount of data flowing forward while keeping the strongest detected signals. After several such conv-and-pool stages, the resulting compact set of features is flattened and passed into an ordinary feedforward classifier head, of exactly the kind built in the first section of this topic, to produce a final prediction.

73.2 Worked Example

Ten small 6-by-6 grids representing simplified NDVI patches from ten different field sections, five uniformly healthy and five containing a small, sharply bounded low-NDVI patch simulating localized crop stress. The task: detect that boundary using a convolution filter, and classify each patch as Healthy or Stressed from the resulting features.

73.3 Convolutional Networks in R

No CNN package is part of R’s base or recommended set (production CNNs are normally built with keras or torch, unavailable here). The pipeline below implements 2D convolution, ReLU, and max pooling directly as base R functions, using a hand-designed edge-detecting filter rather than a learned one, then trains a single dense output neuron, using the same gradient-descent logic from the first section of this topic, on the resulting pooled features. This mirrors, in simplified form, exactly how a full CNN’s convolutional base and dense classifier head work together.

73.4 Reading the Result

The two printed feature maps make the whole point visible directly: the healthy patch’s feature map should stay close to zero everywhere (a Laplacian filter responds to sharp local changes, and a uniform patch has none), while the stressed patch’s feature map should show a strong response right at the boundary of the inserted low-NDVI region, exactly the local pattern the filter was designed to catch. Max pooling then compresses that feature map down to its strongest signals before the values ever reach the classifier, which is why the final dense neuron, working from only a handful of pooled numbers per image rather than the original 36 pixels, still separates the two classes cleanly. A production CNN differs from this pipeline mainly in scale and in one further respect: it learns the filter itself through backpropagation rather than using a hand-designed one, and it typically stacks several convolution-and-pooling stages, each learning progressively higher-level patterns, before the classifier head ever sees the data.


Summary

Concept Description
Foundations
Convolutional Neural Network (CNN) Scans small learned filters across an image, detecting local patterns wherever they occur in the frame
Filters (Kernels) and Feature Maps A filter's weighted local sum, computed at every position, produces a feature map highlighting that pattern
Why Convolution Suits Grid Data Nearby pixels are related, and a useful pattern can appear anywhere, both handled naturally by convolution
ReLU Activation and Max Pooling ReLU introduces nonlinearity; max pooling shrinks each feature map to its strongest local activations
Stacking Conv-and-Pool Layers Multiple conv-and-pool stages let a CNN learn progressively higher-level patterns from raw pixels
Worked Example
Worked Example: Detecting a Stressed Patch Ten 6x6 NDVI patches, five uniform and five with a localized stressed region, classified from their edges
Implementing Convolution and Pooling in Base R conv2d(), relu(), and maxpool2d() built directly from their definitions using base R matrix operations
The Conv-then-Dense Pipeline A hand-designed edge filter extracts features; a dense output neuron, trained by gradient descent, classifies them