stn 06 · rec 2024 · universidad de los andes, coursework
Shipping a 120-Breed Dog Classifier End to End
Built a 120-breed dog classifier end to end: a scraper, two transfer-learning architectures, a FastAPI service, a Cloud Function and a Streamlit front end. The training log then showed that the pretrained backbone had already done the work by the end of the first epoch.
sec 01 · brief
The problem
Fine-grained image classification separates classes that look alike. Here the classes are a hundred and twenty breeds of dog, and most of them differ by the set of an ear and the length of a coat. The task is the standard demonstration that a classifier has learned features and not only silhouettes. It has historically needed two things a student does not have: a very large labelled dataset, and enough compute to train a deep network on it from scratch.
Pretrained backbones supply the first and transfer learning removes the need for the second, and that is what makes the task assignable in a course. The published work shows what the combination reaches: three papers in the literature review report 96.75%, 93.53% and 93.2% on this family of problem, all of them with pretrained convolutional networks or vision transformers over the same Stanford Dogs dataset.
The assignment did not ask for a higher figure than those. It asked for one model carried the whole distance, from dataset to deployed endpoint. Most student projects stop at a notebook that reports an accuracy, and the work between that notebook and a service that answers an HTTP request is engineering work. That leaves one question for this report. When a pretrained model is carried from dataset to deployed endpoint without a GPU, what does the training in the middle add?
sec 02 · approach
What I built
I built the whole pipeline around the classifier and answered the question from the training log of the model in the middle of it. The pipeline has six pieces: a Selenium scraper for additional imagery, a side trial of OpenAI's Vision API as a zero-shot baseline, two transfer-learning architectures over Stanford Dogs, a FastAPI service for local inference, a Google Cloud Function that loads its weights lazily from a storage bucket on cold start, and a Streamlit front end that talks to it.
The two backbones carry heads of different sizes, and the reason is hardware. InceptionV3 (2015, 42 layers, ~23.9M parameters) and InceptionResNetV2 (2016, 164 layers, ~55.9M parameters) were both loaded frozen with a small trainable head appended. The paper states why the heads differ: no GPU was available and the larger backbone filled the notebook's memory, so its head was cut from two 240-unit layers to two 120-unit ones.
The Vision API trial measured cost, because accuracy could not be compared. The original intent was to compare a hosted multimodal model against the trained classifier. Without fine-tuning there is no way to constrain the hosted model's output to the 120 Stanford Dogs labels, so the two systems cannot be scored on the same task. Cost could still be measured, so the trial measured token cost.
sec 03 · method
Method
01
Auxiliary dataset and scraper
This stage adds nothing to the classifier's accuracy, because the set it collected never entered training. The classifier was fit on Stanford Dogs alone, and the paper says so. The stage was built for a different test: whether a model could tell two dogs of the same breed apart. For that the target was several hundred images of a small number of individual named dogs, where a breed dataset holds many images of many breeds. Three Selenium functions collected them. One walks a Google Images result set and opens each result to recover the image at original scale in place of the thumbnail, one downloads through Pillow, and one iterates a list of canine Instagram accounts and pulls their photographs.
02
Vision API trial
This stage measures what a hosted model costs in tokens per image, which is what the trial could still measure once the accuracy comparison was ruled out. I called one hosted vision-preview model three ways: a single image at low detail on a 65-token budget, a single image at high detail (where the model generates 512-pixel crops) on a 129-token budget, and a batch of six images against one prompt. The responses came back as paragraphs of hedged prose where a label was wanted, so a second call to a text model parsed each one down to breed names, and the token usage of both calls was written back to the dataframe. The label space could not be constrained, so no accuracy was computed and none is claimed.
03
Dataset and augmentation
This stage fixes the images that every accuracy figure in the report is measured on. Stanford Dogs has 120 categories and 20,580 images at 256×256, built from ImageNet photographs and annotations. A single Keras image generator (rescale 1/255, rotation ±15°, zoom 0.2, width and height shift 0.2, horizontal flip) with a 0.2 validation split was flowed twice from the same directory, which gives 16,508 training and 4,072 validation images across the 120 classes. The batch size was 256, which at integer division gives 64 training steps and 15 validation steps per epoch. The next stage trains on the training subset and reports accuracy on the validation subset.
04
Two transfer-learning heads
This stage is the training the question asks about: a small trainable head fit on top of a frozen backbone. Each backbone was loaded with its ImageNet weights and frozen, followed by global average pooling, flatten, and an alternating stack of dropout and dense layers ending in a 120-way softmax. InceptionV3 took two 240-unit layers and was compiled with SGD at learning rate 0.01 and momentum 0.99 against categorical cross-entropy. InceptionResNetV2 took two 120-unit layers and was compiled with Adam. Both ran 20 epochs. Neither ran on a GPU: the notebook's own device query returned an empty list, so the constraint the paper describes is also recorded in the artifact. The trained model is what the two serving stages load.
05
Local inference service
This stage shows the model answering a request on the machine that holds it. A FastAPI service accepts an uploaded file, decodes it through PIL into a numpy array, expands it to a batch of one, runs prediction, and returns the predicted breed with its confidence. CORS middleware is configured for a browser front end on a different origin. This version holds the weights in process and answers immediately. The next stage moves the same inference path to a hosted function.
06
Cloud deployment and cold start
This stage carries the model the rest of the distance, to a hosted endpoint with a front end. The same inference path was rebuilt as a Google Cloud Function, with one difference: the model is not bundled. On a cold start the function downloads the weights from a Cloud Storage bucket into the runtime's temp directory and caches them in a module-level global, so the first request after a scale-to-zero pays for the download and later requests do not. A Streamlit front end sits in front of it. The user uploads a photograph, the app sends it as an array, and the breed and the confidence come back.
sec 04 · legend
Instruments
legend · modelling
legend · data
legend · serving
legend · trialled
sec 05 · readings
Results
Breeds classified
120
Stanford Dogs, all classes
Images
20,580
16,508 train · 4,072 validation
Best validation accuracy
86.64 %
epoch 10 of 20 · InceptionResNetV2
Validation accuracy, epoch 1
81.04 %
before the head had learned anything
Net change over 20 epochs
−0.01 pt
81.04 % → 81.03 %
Validation accuracy, spread
80.17 – 86.64 %
20 epochs · median 83.62 %
Runs with recoverable metrics
1 of 2
the InceptionV3 log was not retained
Training wall clock
≈ 4 h 24 m
20 epochs × 64 steps · no GPU
Vision prompt tokens, single image
111
1 retained call, low detail
Vision prompt tokens, per image batched
89.3
150 calls · 536 ÷ 6 images
plate 01
Twenty epochs, one flat line
epoch 01
epoch 02
epoch 03
epoch 04
epoch 05
epoch 06
epoch 07
epoch 08
epoch 09
epoch 10
epoch 11
epoch 12
epoch 13
epoch 14
epoch 15
epoch 16
epoch 17
epoch 18
epoch 19
epoch 20
Validation accuracy after each of the 20 epochs, for the one run whose training log survives. It shows what the training added, which is the question the brief asks. Accuracy opens at 81.04% and closes at 81.03%. It passes through 86.64% at epoch 10 and 80.17% at epoch 16 and ends where it started. A frozen backbone with a converged head produces this shape: the features were fit before training began, and the head had nothing left to learn after the first pass.
source
Keras training log retained in the committed notebook, InceptionResNetV2 run. The axis runs the full 0–100, because compressing it to 80–87 would manufacture a trend out of six points of noise. The flatness is the reading, so the axis has to be able to show it.
plate 02
What the literature reports, and what this run did
Borwarnginn 2019
Shah 2020 (a)
Wang · ViT
Shah 2020 (b)
this run
The accuracies the paper's own literature review cites, beside the best epoch of the run measured here. It places the surviving run on the scale the brief set. The comparison is not like for like: those figures come from different splits, different protocols and in two cases different datasets, and 86.64% is a best epoch on a validation split, with no held-out test result behind it. It is charted because the paper's conclusion claims high precision without printing a number, and this plate states that claim with the number the notebook supports.
source
Literature figures as cited in the course paper; the last bar is the notebook's retained training log. The bars are direct-labelled because the four published bars are within six points of each other and length alone cannot separate them.
sec 07 · findings
What the data said
finding · negative result
Twenty epochs of training added nothing to validation accuracy
The training in the middle of the pipeline added nothing measurable. Validation accuracy was 81.04% at epoch 1 and 81.03% at epoch 20. Between them it rose as high as 86.64% at epoch 10 and fell as low as 80.17% at epoch 16, with a median of 83.62% and no trend in either direction. The whole run, roughly four and a half hours of CPU, produced a net change of −0.01 points, and the peak at epoch 10 was neither selected nor saved.
finding · result
The backbone had already solved it, and the two datasets share photographs
The accuracy was in the frozen backbone before training started. An 81% top-1 accuracy across 120 classes after a single epoch means the model did not need to learn the task. The frozen backbone was pretrained on ImageNet, and Stanford Dogs is built out of ImageNet images and annotations, so the features were fit on the same photographs the classifier is scored against. Everything above chance was already present in the frozen layers before the head was initialised.
finding · negative result
The one run with surviving numbers had its loss function misconfigured
The surviving run was trained with a loss function that contradicts its output layer. The model ends in a 120-way softmax and was compiled with a cross-entropy loss set to expect logits. The flag tells Keras the outputs are unnormalised scores and to apply its own softmax, so the loss was computed on a distribution that had been through softmax twice. Gradients still point in a usable direction, which is why the run trains at all, but the reported loss values do not mean what they appear to mean. The 86.64% figure stands. The loss curve beside it cannot be interpreted.
finding · negative result
Neither run is complete: one lost its log and the other its weights
Neither run left both a model and its numbers. The InceptionV3 run's training log was not retained in the committed notebook (its cells were re-executed or cleared), so the paper's claim that both architectures reached similar results cannot be checked. The InceptionResNetV2 run kept its full log but its weights were never committed. The repository therefore ships the model it has no numbers for, and holds the numbers for a model it does not ship.
finding · negative result
The paper reports no result for either of its own models
The paper's results section prints no performance figure. The section is two paragraphs. It states that the models ran for 20 epochs, that the results were similar, and that the gap between the training and validation curves suggests overfitting, smaller for the deeper backbone. It prints no accuracy, loss, precision, recall or confusion matrix. The only percentages anywhere in the document belong to other people's models. Every performance figure on this page had to be recovered from the notebook.
finding · negative result
The scraper works, and the dataset it collected was never used
The auxiliary dataset was never used for anything. Several hundred images of individually named dogs were collected to test whether a model could distinguish two dogs of the same breed. The paper states that the capability was never tested, and the images never entered training either, because the classifier was fit on Stanford Dogs alone. The stage produced a working scraper and an unused dataset.
finding · result
The Vision API trial could not compare accuracy, so it measured token cost
The Vision API trial could not make the accuracy comparison it was planned for. A hosted multimodal model cannot be scored against a 120-class classifier unless its output can be constrained to the same 120 labels, and without fine-tuning it cannot be. The trial returned free prose that needed a second model call to parse into breed names. I re-scoped the trial to the one dimension that could be measured, token cost, and batching six images against one prompt cut prompt tokens per image from 111 to 89.3.
finding · negative result
The deployed endpoint no longer answers
The model reached a deployed endpoint, and that endpoint cannot be shown working today. The Streamlit application the paper links can no longer be reached: the URL returns a 303 redirect to a sign-in page and does not load the app. The Cloud Function behind it and the storage bucket it pulled weights from cannot be verified from outside either. The deployment is documented in the source and in the committed function.
sec 08 · forward
Recommendations
01
Fix the loss configuration and re-run before trusting any curve. The misconfigured loss is a one-line correction: a cross-entropy set to expect logits sits behind a softmax output layer. Until it is corrected, the only defensible number from that run is the accuracy.
02
Checkpoint on best validation accuracy. The run passed through 86.64% at epoch 10 and ended at 81.03%. Saving the best epoch in place of the last would have kept the better model at no extra cost.
03
Unfreeze the top of the backbone, or stop training after one epoch. The flat curve leaves these two options. A frozen backbone whose head has converged by epoch 1 has nothing left to learn, so the compute spent on epochs 2 to 20 should either go to fine-tuning unfrozen layers or not be spent.
04
Report on a held-out test split, not the validation split. The current figures are the best epoch measured on the same split used to watch training, which makes them the most favourable reading the run allows.
05
Commit the training history along with the weights. One run's weights were kept and its numbers were not. A history dump is a few kilobytes and would have kept those numbers recoverable.
06
Use the individual-dog dataset that already exists, or delete it. The scraper collected the right data for a re-identification task, and the task was never attempted. The dataset is either the natural second phase or unused files in the repository.
sec 09 · notes
Notes
disclosure
Nothing on this page is pseudonymized. This is coursework and involves no client: the university, the dataset, the architectures, the three repositories and the deployed application are all named as they are. The student ID printed on the paper and the university email address attached to every commit are excluded, because they identify a person and inform no reader.
limits
- There is no test split. Every accuracy figure on this page is measured on the validation split, which is also the split used to monitor training. The best-epoch figure is the most optimistic reading of a measurement that is already optimistic.
- Validation images were augmented. One image generator carrying rotation, zoom, shift and horizontal flip is flowed twice from the same directory, once per subset, so the validation set was measured through the same random transformations as the training set. That is not standard practice, and it means these figures cannot be compared directly with any published Stanford Dogs number, including the four in plate 02.
- With a batch size of 256 and step counts computed by integer division, each epoch evaluates 3,840 of the 4,072 validation images and trains on 16,384 of the 16,508. The remainders are dropped every epoch.
- Only one of the two runs has any recoverable figures, so nothing here compares the two architectures. The paper's claim that they performed similarly is unverified and is reported as a claim.
- The loss values from the surviving run cannot be interpreted, because the loss function was configured for logits and given probabilities.
- Epoch wall-clock times in the log alternate between roughly 25 minutes and roughly 35 seconds, ten of each, with no corresponding change in step count. The ≈4 h 24 m total is the sum of what the log reports. The alternation is unexplained, and the notebook does not say why.
- The Vision API trial produced no accuracy figure, because the label space could not be constrained. Its token figures rest on 150 calls in one mode and a single call in the other.
- The deployment is documented and cannot currently be reached.