---
title: "NeCNN — Case study — Fabian Amhof"
description: "My bachelor's thesis in Computer Science at the University of Innsbruck: neuroevolution for image classification. On top of a frozen, pretrained CNN, the NEAT algorithm evolves the structure of the classifier and PyTorch trains its weights. Tested on MNIST, CIFAR-10, Imagenette and smallNORB."
canonical_url: "https://fabbo.net/projects/necnn"
meta:
  author: "Fabian Amhof"
  "og:description": "My bachelor's thesis in Computer Science at the University of Innsbruck: neuroevolution for image classification. On top of a frozen, pretrained CNN, the NEAT algorithm evolves the structure of the classifier and PyTorch trains its weights. Tested on MNIST, CIFAR-10, Imagenette and smallNORB."
  "og:image": "https://fabbo.net/og.png"
  "og:image:alt": "Fabian Amhof — Full-Stack Developer & Team Lead. A line-drawn mountain landscape."
  "og:image:height": "630"
  "og:image:width": "1200"
  "og:title": "NeCNN — Case study"
---

> Index of every page on this site: [llms.txt](https://fabbo.net/llms.txt)

[Overview](https://fabbo.net/#work)Case study 03

(03)Case studyBachelor's thesis — 2022

# NeCNN

My bachelor's thesis: can evolution design the classifier of a neural network?

<dl>

<dt>Client</dt>
<dd>Bachelor's thesis, University of Innsbruck</dd>

<dt>Role</dt>
<dd>Research &amp; code</dd>

<dt>Year</dt>
<dd>2022</dd>

<dt>Stack</dt>
<dd>Python, PyTorch, neat-python</dd>

<dt>Code</dt>
<dd><a href="https://github.com/fabianamhof/NeCNN">github.com /fabianamhof /NeCNN</a></dd>

</dl>

*Fig. 01 — The feature extractor stays frozen, the classifier evolves.2022*

01

## Overview

Convolutional neural networks recognise images in two steps: convolutional layers extract features, and a classifier at the end decides what is in the picture. In my bachelor’s thesis I let evolution build that classifier.

02

## Question

Usually a person decides how the classifier is built: how many layers, how many neurons. Can evolution do that instead — and how close does it get?

NEAT (NeuroEvolution of Augmenting Topologies) starts with a few connections and lets networks grow: mutations add neurons and connections, crossover combines two parents, and only the fittest candidates pass on their genes.

03

## Approach

The feature extractor is a pretrained network and stays frozen. Only the classifier on top evolves: NEAT decides its structure, PyTorch its weights.

Each generation has 50 candidates. Every candidate is turned into a PyTorch network, trained for two epochs with gradient descent and judged by its loss: fitness = 1 / (1 + loss). After 50 generations, the best one is tested on images it has never seen. Below is a real run — CIFAR-10 with ResNet-18 — generation by generation.

**Input**512 image features**Hidden neurons**added by evolution**Output**one per class

GenerationGeneration 50 / 50

NeuronConnectionNew in this generation

What you see

One run from the thesis. Every generation, 50 networks are trained and scored; the picture shows the best one. On the left are the 512 features the frozen ResNet-18 extracts from an image, on the right the ten classes. Everything in between was built by evolution.

1.
2.
3.
4.

Generation 50 — Growth

The network keeps growing, to 135 neurons and 1,163 connections — yet its fitness only rises from 0.988 to 0.991. A fully connected layer would have 5,120 connections.

<dl>

<dt>Hidden neurons</dt>
<dd>135</dd>

<dt>Connections</dt>
<dd>1,163</dd>
<dd>fully connected: 5,120</dd>

<dt>Fitness</dt>
<dd>0.991</dd>
<dd>1 = perfect on the training images</dd></dl>

1.00.4Gen. 150

Best networkAverage of all 50Fitness = 1 / (1 + loss)

*CIFAR-10 with a frozen ResNet-18, 50 networks per generation, 50 generations. Data: the NEAT statistics of this run in the repository.*

04

## Implementation

NEAT describes a network as a list of genes — nodes and connections, wired however evolution likes. PyTorch expects layers.

A converter sorts the nodes of a genome by depth into layers and turns each layer into a linear layer with a mask, so only the connections that exist in the genome are trained. Because the feature extractor never changes, its outputs are computed once per dataset and stored; each candidate only trains its own small classifier. I also changed NEAT’s crossover: a child can inherit connections from both parents, not only from the fitter one, and takes along just the nodes those connections need.

*Fig. 02 — Fitness as a landscape: generation by generation, the population climbs higher.*

*Fig. 03 — No two candidates are wired alike; each one is trained on its own.*

05

## Result

The evolved classifiers came close to the original ones but did not beat them — with far fewer connections.

On MNIST and CIFAR-10 they stayed within half a percentage point, on smallNORB within one and a half; on Imagenette they fell three to five points short. The classifier evolved for CIFAR-10 on ResNet-18 uses 1,163 connections — a fully connected layer has 5,120 — and reaches 92.61% instead of 92.62%.

| Dataset | Network | Original | Evolved |
| --- | --- | --- | --- |
| MNIST | ResNet-18 | 99.16% | 98.80% |
| MNIST | ResNet-50 | 99.09% | 98.68% |
| CIFAR-10 | ResNet-18 | 92.62% | 92.61% |
| CIFAR-10 | ResNet-50 | 93.43% | 93.40% |
| Imagenette | ResNet-18 | 97.71% | 92.31% – 93.53% |
| Imagenette | ResNet-50 | 98.96% | 96.05% – 96.08% |
| smallNORB | ResNet-18 | 87.83% | 86.99% |
| smallNORB | ResNet-50 | 88.92% | 87.61% |

Test accuracy from the logs in the repository: one run per configuration, two for Imagenette. The network is the frozen feature extractor; the original classifier is its single linear layer.

Four datasetsMNIST, CIFAR-10, Imagenette and smallNORB, each with ResNet-18 and ResNet-50.

2,500 networks per run50 generations of 50 candidates, each one trained with PyTorch.

Open sourceCode, configurations and logs of every run are on GitHub.

Next projectThis website

[Form / Function](https://fabbo.net/projects/form-function)

Personal website — 2026

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://fabbo.net/#website","@type":"WebSite","inLanguage":"en-US","name":"Fabian Amhof","url":"https://fabbo.net/","publisher":{"@id":"https://fabbo.net/#identity"},"workTranslation":{"@id":"https://fabbo.net/de#website"}},{"@id":"https://fabbo.net/projects/necnn#webpage","@type":"WebPage","description":"My bachelor's thesis in Computer Science at the University of Innsbruck: neuroevolution for image classification. On top of a frozen, pretrained CNN, the NEAT algorithm evolves the structure of the classifier and PyTorch trains its weights. Tested on MNIST, CIFAR-10, Imagenette and smallNORB.","name":"NeCNN — Case study","url":"https://fabbo.net/projects/necnn","isPartOf":{"@id":"https://fabbo.net/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://fabbo.net/projects/necnn"]}]},{"@id":"https://fabbo.net/#identity","@type":"Person","name":"Fabian Amhof","url":"https://fabbo.net","image":{"@id":"https://fabbo.net/#/schema/image/1"},"sameAs":["https://github.com/fabianamhof"]},{"@id":"https://fabbo.net/#/schema/image/1","@type":"ImageObject","contentUrl":"https://fabbo.net/fabian.jpg","inLanguage":"en-US","url":"https://fabbo.net/fabian.jpg"}]}
```

## Sitemap

See the full [sitemap](https://fabbo.net/sitemap.md) for all pages.
