Histopathology Classification and Knowledge Distillation
Two small, self-contained PyTorch experiments from 2024, each one script that downloads its own data and runs end to end. The first asks whether architecture capacity helps on a small medical-imaging benchmark: a two-block CNN against an ImageNet-pretrained ResNet18 on PathMNIST, colorectal-cancer histology tiles in nine tissue classes. The second asks whether knowledge distillation helps a small network: a teacher CNN on MNIST, then a half-width student trained twice, once against the teacher and once without. Both are in the repository under MIT.
Two questions, one design
Both experiments ask the same kind of question—where the accuracy of a small network actually comes from—and both answer it the same way: change exactly one thing, hold everything else fixed, and read the gap. On PathMNIST the thing that changes is the architecture and what it was initialised from. On MNIST the architecture is held constant and what changes is the signal the loss is computed against. In each pair the data, the preprocessing, the optimiser, the batch size and the number of epochs are identical, so a difference in the final number has one candidate explanation rather than several.
That constraint is what shaped the code. Each experiment is a single script that downloads its own data, builds both models, trains them under one budget defined in one place, and prints the two numbers side by side. There is no configuration surface for the two arms of a comparison to drift apart through, and no state carried between runs: the result on the page is what the script produces from an empty directory.
The two experiments
| Experiment | Models | Training | Test accuracy |
|---|---|---|---|
| PathMNIST, 9 classes, 28 × 28 RGB | SimpleCNN, two conv blocks, ~0.42 M parameters, from scratch ResNet18, ~11.2 M, ImageNet weights, fully fine-tuned |
Adam 1e-3, batch 64, 5 epochs, no augmentation, no scheduler, no seed | 77.23% 81.91% |
| MNIST distillation | Teacher CNN, ~1.2 M Student at half width, ~0.30 M, with and without a distillation loss ( T = 5, α = 0.7) |
Adam 1e-3, batch 128, 5 epochs each, no seed | 98.81% 98.72% / 98.64% |
The four networks
SimpleCNN is two convolution blocks and a fully connected head, about 0.42 M parameters, initialised from nothing. It sets the floor: what a model small enough to train from scratch in minutes can reach on 28 × 28 tissue tiles.
ResNet18 sets the other end of the same budget: a 7 × 7 stem, four residual stages, global average pooling, and a new nine-way head on about 11.2 M parameters carrying ImageNet weights. Every layer is fine-tuned rather than frozen, so the comparison is between two models that both spent the whole budget learning this data—one starting from random weights, one starting from features learned on photographs. Whether those features are worth anything on stained tissue, which looks nothing like ImageNet, is the question the run answers.
Teacher and student on MNIST are the same design at two widths: a convolutional feature stack and a classifier head at about 1.20 M parameters for the teacher and 0.30 M for the student, a quarter of the size. Keeping the shape fixed and moving only the width is what makes the student's two runs comparable to each other and to the teacher.
What distillation moves across
A one-hot label says which class an image belongs to and nothing else. A trained network's output says more than that: a four that leans towards nine scores nine higher than it scores seven, and that ordering is a statement about the shape of the input, learned from the whole training set. Under an ordinary softmax the winning class takes almost all the probability mass and the rest of that structure is numerically invisible.
Temperature is what makes it visible. Dividing the logits by T = 5 before the softmax flattens the distribution until the relative sizes of the losing classes matter, and the student is asked to match that flattened distribution rather than the label. The loss puts both signals side by side: α = 0.7 on the divergence from the teacher's softened output, and the remaining 0.3 on the ordinary cross entropy against the hard label, so the student is pulled towards the teacher's judgement without being allowed to drift away from the ground truth.
The control is the same student, the same data and the same budget with the first term removed. Two runs that differ in one term of one loss are what turn “distillation helps” from a claim into a measurement.
What the numbers show
PathMNIST. A 4.7-point gap after an identical, short budget on a test split of about 7,180 images is well outside noise: the pre-trained ResNet18 separates cleanly from the two-block CNN trained from scratch, and the comparison is set up so that both models see exactly the same data for exactly the same number of steps.
MNIST. The same student architecture is trained twice, once against the teacher's temperature-softened outputs and once on the labels alone, so the only thing that differs between the two runs is the distillation term. With it, the 0.30 M student closes part of the distance to a teacher four times its size.