6 minute read

What if, instead of hand-coding an image filter, we let a population of small programs evolve one?

That was the premise of this experiment. I used genetic programming (GP) to identify roads in satellite images, then tested the evolved filter on a different road network to see whether it had learned something general rather than simply memorizing the training image.

The problem

The training image was a 200 × 149 satellite view of the Queen Elizabeth Way and Highway 406 area in St. Catharines, Ontario:

Satellite image of the QEW and Highway 406 interchange in St. Catharines

The goal was to classify each pixel as either road or non-road. Roads, however, are not uniform. They vary in width, brightness, shading, curvature, and surrounding scenery. A useful detector would therefore need to combine a pixel’s local intensity with information from its neighbourhood.

I created a ground-truth mask in GIMP. Road pixels are shown in blue:

Ground-truth mask showing road pixels in blue

Of the 29,800 pixels, 3,117 represented roads and 26,683 represented non-road features. That imbalance mattered: a classifier could score well by mostly predicting “not road” while still failing at the actual objective.

Representing an image as a GP problem

For each sampled pixel, the system calculated features from 3 × 3, 7 × 7, and 11 × 11 neighbourhoods:

  • average intensity;
  • standard deviation;
  • minimum intensity;
  • maximum intensity.

These values became the terminal set—the leaves of a GP expression tree. The function set combined terminals using operations such as addition, subtraction, multiplication, protected division, inversion, minimum, and maximum. I also tested trigonometric and logarithmic operations, but they did not meaningfully improve the best candidates and were removed.

The population contained 512 programs. Each run used tournament selection, 90% crossover, 5% mutation, and 5% reproduction. Programs were rewarded for minimizing false positives and false negatives.

To keep training manageable, I sampled 160 pixels: 80 road pixels and 80 non-road pixels. The sampler distributed selections across the image rather than concentrating them in one region.

From image features to GP trees

The implementation used ECJ, with a separate Java class for each GP terminal and function. The expensive image calculations were completed before evolution. That meant the evaluator could load an array value during each fitness calculation instead of repeatedly scanning the surrounding pixels.

For example, the neighbourhood average walks an odd-sized window and skips coordinates outside the image:

private static Float mAvg(float[][] pixels, int row, int col, int size) {
    float sum = 0;
    int firstRow = row - (int) Math.floor(size / 2);
    int firstCol = col - (int) Math.floor(size / 2);

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            int r = firstRow + i;
            int c = firstCol + j;
            if (isElementExists(pixels, r, c)) {
                sum += pixels[r][c];
            }
        }
    }

    return sum / (size * size);
}

IOFactory used the same structure for minimum, maximum, and standard-deviation features at different window sizes. A terminal node then exposed one precomputed value to the evolved tree:

public void eval(final EvolutionState state,
    final int thread, final GPData input,
    final ADFStack stack, final GPIndividual individual,
    final Problem problem) {

    DoubleData data = (DoubleData) input;
    data.x = ((CoscCompVissionImgAnalysis) problem).avg3x3;
}

Function nodes combined terminal values. Their implementations had to be “closed”: every legal child value had to produce another valid number. Protected division was especially important because evolved trees could otherwise divide by zero:

children[0].eval(state, thread, input, stack, individual, problem);
double numerator = data.x;

children[1].eval(state, thread, input, stack, individual, problem);
data.x = (data.x == 0) ? 1 : numerator / data.x;

During fitness evaluation, the program was executed once for every sampled pixel. A non-negative result represented road and a negative result represented non-road. The result was compared with the ground-truth image, then recorded in the confusion matrix:

((GPIndividual) ind).trees[0].child.eval(
    state, threadnum, input, stack, (GPIndividual) ind, this);

boolean isRoad = IOInit.compareTwoPixels(
    3,
    IOInit.train_ggl_sat_img_tgi,
    new int[] {30, 30, 246},
    x,
    y
);

if (input.x >= 0) {
    if (isRoad) {
        confusionMatrix[0][0]++; // true positive
    } else {
        confusionMatrix[1][0]++; // false positive
    }
} else {
    if (isRoad) {
        confusionMatrix[1][1]++; // false negative
    } else {
        confusionMatrix[0][1]++; // true negative
    }
}

Finally, the standardized Koza fitness was simply the number of misses—false positives plus false negatives. During testing, the same classifier was evaluated across the complete image and a colour map was produced: green for true positives, black for true negatives, red for false positives, and yellow for false negatives.

The parameter files also show the practical experimentation around the main run: different population and generation sizes, plus an island-model setup that distributed four populations across a local server and clients. Those variants were attempts to exchange useful candidates and increase the amount of search available in the time budget.

What evolution learned

The fitness curve below shows the best and mean adjusted fitness for the strongest run:

Chart showing progressive fitness improvement over 60 generations

Across 20 runs, the programs generally improved from one generation to the next. The best experiment on the training image correctly classified 84.33% of its pixels overall. For that run:

  • 70.00% of actual road pixels were detected;
  • 86.01% of actual non-road pixels were rejected correctly;
  • 30.00% of actual road pixels were missed;
  • 13.99% of actual non-road pixels were falsely classified as road.

The evolved output was noisy, but it recovered much of the road network:

Road classification produced by the best evolved program

Some runs detected more than 95% of road pixels, but those gains came with poor non-road classification. That reinforced an important lesson: a high score on the target class is not enough when the classes are imbalanced. The useful solution was the one that balanced true positives and true negatives.

Testing on a different road network

The more interesting question was whether the evolved rule generalized. I applied the programs to a satellite image from Nürburgring, Germany:

Satellite image of a road network near Nürburgring, Germany

The second image contained visually similar roads but very different background textures:

Classification output from the test on the Nürburgring image

The results were noticeably weaker. Roads were only partly recovered, and the output contained substantial false positives and false negatives. The evolved filter had learned useful local intensity patterns from the first image, but those patterns did not transfer cleanly to the second environment.

This is a familiar machine-learning problem. The rule was exposed to only 160 training samples from one location, and many background features in the test image resembled the road signal. More diverse training data, larger samples, improved neighbourhood filters, and additional runs would likely have produced a more robust detector.

What I took away

This experiment demonstrated that genetic programming can evolve image-classification rules without a designer manually specifying the final filter. It also exposed several practical constraints:

  • The training set has to represent the problem. Eighty non-road samples were not enough to capture all background variation.
  • Balanced metrics matter. High road recall can hide unacceptable false-positive behaviour.
  • Evolution improves with evidence, not just generations. The 200-generation trials improved training performance, but the broader generalization problem remained.
  • Generalization is the real test. A rule that works well on one satellite image is still a narrow solution until it succeeds on unseen environments.

The full configuration took roughly 3,024 minutes to execute, which also shaped the scope of the experiment. Future work would include a larger and more varied training set, more runs, additional fitness strategies, and automatically defined functions to simplify reusable parts of evolved programs.

The central result was encouraging: evolution found a useful road detector from local image statistics. The harder lesson was just as valuable—good performance on a familiar image is only the beginning of a useful vision system.