← All blogs

Computer vision

Knowledge distillation for fine-grained image classification

In a research experiment, we trained a large vision model to name 353 cat and dog breeds and then needed something small enough to serve cheaply. Knowledge distillation took us from a 95.11M-parameter teacher to a YOLO26 classifier with 5,895,201 parameters and a 23.58 MB ONNX file. The student kept most of the teacher's top-five behaviour and lost a good chunk of top-1. Then a packaging step quietly took more away.

Rubix Labs5 min read
Distillation diagram: a 95.11M-parameter DINOv3 teacher scores 63.34% cat and 80.50% dog top-1 on our development panel. Its cached outputs train a 5,895,201-parameter YOLO26s classifier that scores 52.87% and 71.83%. On 6,305 held-out single-animal photos the student's pipeline reaches 75.78% cat and 79.17% dog top-1, and 96.24% and 96.99% top-5. Packaging it as a detector dropped cat top-1 to 48.13% until the original head was restored.
Numbers from our research experiment. The development panel and the held-out photos are different sets with different difficulty; neither measures real-world accuracy.

The teacher and the data split

Our teacher shares one DINOv3 Base encoder between a Faster R-CNN detector and whole-image breed heads. On our development panel of 401 cats and 646 dogs, it names 63.34% of cats and 80.50% of dogs correctly first time, counting retakes as failures. It also needs about 4.5 GiB of process memory on CPU.

The data split had 59,064 training, 7,680 validation and 4,707 test records, and the test split stayed sealed for this whole experiment. Each student fit read 62,418 training rows per epoch, which includes some supplementary views on top of the base training records. We ran the teacher over those rows once, at 384 pixels, and cached its outputs. Students then trained on 224-pixel letterboxed versions of the same views without the teacher running at all.

The recipe: half labels, half teacher

In our matched distillation runs, each student's loss was half ordinary cross-entropy against the true breed and half KL divergence against the teacher's softened outputs at temperature 2. The teacher's probabilities carry information a hard label throws away: if it spreads its belief across two look-alike breeds, the student learns that those two are close.

The YOLO26 small classifier used the same cached teacher outputs, training rows and 224-pixel inputs. It started from Ultralytics' ImageNet weights and trained for 30 epochs; we kept epoch 19. We chose checkpoints by how much of the teacher's top-1 the student kept for cats and for dogs, taking the worse of the two, so a student couldn't score well by giving up one species.

Did distillation itself help? We can only answer where we trained a matched label-only control. MobileNetV4 trained with the teacher named 198 cats and 432 dogs; the same model on labels alone named 174 and 417. A DINOv3 Small student tied its control on cats at 216 and gained two dogs. We didn't train a label-only YOLO26 control, so we can't separate the teacher's contribution there.

The recipe: half labels, half teacher
ModelSizeCats of 401Dogs of 646
Teacher: DINOv3 Base with detector and heads95.11M parameters254520
DINOv3 ConvNeXt Tiny, distilled112.49 MB ONNX224486
FastViT-SA12, distilled43.63 MB ONNX202466
MobileNetV4, distilled35.47 MB ONNX198432
MobileNetV4, labels only35.47 MB ONNX174417
YOLO26s classifier, distilled23.58 MB ONNX212464

What the student kept on photos it never saw

The development panel is deliberately hard. For a second read, we took 6,449 validation photos that weren't on that panel and confirmed that none shared a byte hash, perceptual hash or duplicate group with the student's 61,451 training photos. On the 6,305 that contain one animal, the full pipeline of detector, crop and student named 75.78% of cats and 79.17% of dogs correctly first time. The right breed was among its five suggestions 96.24% and 96.99% of the time.

Those held-out photos come from the same kinds of sources as the training photos, so they're easier than a stranger's phone photo. Most mistakes were between look-alike pairs, such as collie and border collie, Pembroke and Cardigan Welsh Corgi, or Birman and Ragdoll, and the right answer was nearly always in the five.

On the harder development panel the student still trails the teacher: 212 cats against 254, and 464 dogs against 520. Top-five holds up better there too, at 81.05% of cats for the student against 86.78% for the teacher.

The packaging step that dropped cat accuracy

To host the student on a detection-oriented serving platform, we first converted the classifier into a detector-shaped model. That conversion dropped the trained 1,280-channel global projection, pooling and linear breed head and put a detector neck and spatial class heads in their place. Even after a short recovery fit, cat top-1 on the development panel sat at 48.13% instead of 52.87%, and dogs at 64.40% instead of 71.83%.

Reattaching the original global head on the same saved inputs brought cats back up, which pointed at the missing head rather than the backbone. The fix kept the complete backbone and the original head and expressed its linear layer as a 1×1 convolution read at a single centre position. With zero training updates, every ordered top-1, top-3 and top-5 output, species decision and action matched the original classifier across all 1,492 saved scenes.

The hosted package has 7,426,962 parameters and a 29.96 MB FP32 checkpoint because it carries a second branch alongside the classifier. When you repackage a distilled model, compare its outputs with the original's on every saved case, as well as its headline accuracy.

Distilling your own model

Distillation took us from a model that needed a 4.5 GiB process to one we could host for a fraction of a credit per image. It didn't close the gap to the teacher, and on this task closing it means more training: more distillation data or a larger student. If you have a large model that's accurate and too expensive to serve, Rubix can help plan the student, the matched controls and the parity checks. Our pricing page lists custom vision models and on-device AI under custom models.