Image Classification

Image classification maps one supplied image crop to scores over a fixed label set, then selects the largest score. In this real SqueezeNet run, a full Samoyed crop wins at 61.6%; shrinking it at the same center leaves Samoyed first at 33.4%; shifting that same-size crop right makes chow win at 19.3%.

The crop is the question on the exam. Show the classifier the whole subject and you ask one question; hand it a tighter square of face, fur, trees, and harness and you ask another. The model gets the pixels in the square. It does not get your memory of the original photograph.

How Image Classification Works

A single-label image classifier receives one fixed-size tensor and returns one score for every class in its vocabulary. This lesson's SqueezeNet head has 1,000 ImageNet classes. The model does not first locate "the dog" as a separate object, and it has no field for what the photographer intended. One crop goes in; one ranked list comes out.

The class with the largest score is the argmax, so one label becomes top-1 even when several alternatives are close. That winner is a prediction, not ground truth. Here the photograph's pinned ground-truth label is Samoyed; chow winning the ranking does not rewrite it.

The preprocessing contract determines which pixels reach the model. A square is cut from a 1280×853 CC0 Samoyed photograph. Its side is cropSize percent of the source height, its vertical center stays fixed, and focusX sets its horizontal center before boundary clamping. The square is resized directly to 224×224 RGB pixels, scaled to [0, 1], and normalized with ImageNet mean [0.485, 0.456, 0.406] and standard deviation [0.229, 0.224, 0.225].

SqueezeNet 1.1 then produces 1,000 logits. Stable softmax converts them into a distribution:

p_i = exp(z_i - max(z)) / Σ_j exp(z_j - max(z))

Subtracting the largest logit prevents numerical overflow without changing the probabilities. The chart keeps those probabilities on a fixed 0–1 scale. It shows the top five and combines the rest as "995 other labels"; it never renormalizes the visible five, because that would hide real probability mass and inflate every displayed value.

All 25 legal crop combinations were evaluated offline with the same image, preprocessing, model, and label list. The browser replays those exported results. It does not guess scores between samples.

Image Classification Step by Step

The default run ends at { focusX: 60, cropSize: 40 }. It reaches that crop through two controls, one variable at a time.

  1. Define the task. One supplied crop becomes 1,000 class scores. Argmax selects the largest score as the top label. The ground truth stays Samoyed throughout.
  2. Build the reference input. The reference { focusX: 50, cropSize: 100 } cuts an 853×853 square from the source, with horizontal bounds x = 213.5 to x = 1066.5. Pixels outside that square never reach the model.
  3. Score the full-context crop. Samoyed ranks first at 61.6%, followed by white wolf at 33.9%. The top five contain 98.3% of the distribution; the other 995 labels share 1.7%.
  4. Change crop size only. Set cropSize = 40 while holding focusX = 50. The side becomes 0.40 × 853 = 341.2 pixels. This is a smaller square around the same center, so the experiment can attribute the next difference to lost context rather than horizontal motion.
  5. Score the centered smaller crop. Samoyed remains top-1 at 33.4%. white wolf reaches 30.0%, and Arctic fox reaches 17.4%. Shrinking changed the distribution and cut the winner's score, but it did not flip the winner.
  6. Change crop position only. Keep cropSize = 40 and move focusX from 50% to 60%. The final square has source bounds [597.4, 255.9, 938.6, 597.1]. Size, vertical center, preprocessing, model, and labels remain fixed.
  7. Score the shifted crop. The ranking becomes chow 19.3%, Great Pyrenees 17.3%, Eskimo dog 16.2%, Samoyed 16.0%, and white wolf 13.8%. The top label is now the wrong breed, while the true label has fallen to rank 4.
  8. Compare the two arrows. Full context → centered smaller crop changed confidence but kept the winner. Centered smaller crop → same-size shifted crop changed the winner. Each arrow has one independent crop change, so those are two defensible observations rather than one muddled before/after jump.
  9. Keep the conclusion narrow. This run shows that one single-label classifier answers the supplied crop. It does not prove that every smaller crop hurts, that every shift flips a label, or that another model will produce these percentages.

On the canvas, the purple square is the input contract. The green chip names ground truth; the prediction may agree or disagree with it. The final three-card rail holds the inputs and results still, with shrink only and shift only written between them.

What the Crop Experiment Proves

The first comparison rules out a lazy story: "small crop means wrong answer." That is not what happened. The centered 40% crop lowered Samoyed from 61.6% to 33.4%, but Samoyed still won. The second comparison then kept that 40% size fixed and moved the crop right; only then did chow take top-1.

This does not tell you which individual pixels caused the flip. It tells you something more modest and more useful: input geometry belongs to the system you are evaluating. A model score is conditional on the entire preprocessing pipeline that produced its tensor.

Softmax percentages require the same restraint. They sum to one, but one prediction cannot establish calibration. A 61.6% score means Samoyed won this model's normalized ranking on this crop. Calibration asks whether predictions near 61.6% are correct about 61.6% of the time across a dataset.

Using Image Classification in Production

Match deployment preprocessing to the pipeline you evaluated: resize rule, crop policy, color-channel order, numeric range, and normalization constants. A model can be unchanged while the product silently asks it a different question.

Then evaluate the crop policy over a representative dataset. Measure more than one attractive demo image, and inspect cases where plausible crops change the winner or push the true label down the ranking. If the product needs to locate objects, use object detection. If one image can legitimately carry several independent tags, use a multi-label formulation. A single argmax cannot do either job by politeness.

Edge Cases

  • A full-height crop near an edge. At cropSize = 100, moving focusX to 30% or 70% would push the square beyond the photograph. The bounds clamp to the image edge rather than padding with invented pixels.
  • No size change. At cropSize = 100, the shrink-only control equals the reference. The trace says the size is unchanged instead of pretending a resize occurred.
  • No position change. At focusX = 50, the final crop equals the centered size-only control. The shift beat remains honest and reports that the center stayed fixed.
  • The true label falls outside top five. Samoyed receives a separate ground-truth row with its actual rank and probability. It is not inserted into the top five by displacing another class.
  • A different image or model. These values belong to one 2016 SqueezeNet model and one photograph. Other models, label sets, crop policies, and images may behave differently.

Common Mistakes

  • Treating prediction as ground truth. Argmax names the largest model score. It does not change the known label when the model is wrong.
  • Changing size and position together. A single jump from {50,100} to {60,40} cannot tell you which crop change produced which effect. Insert the centered {50,40} control.
  • Reading cropSize as zoom. cropSize = 40 means the square side is 40% of source height. A smaller value admits fewer source pixels; it is not a 40% enlargement.
  • Renormalizing the top five. Dividing the five displayed values by their own sum erases the 995-label tail and overstates confidence.
  • Expecting one label to locate several objects. Whole-image single-label classification returns one distribution for the supplied crop. Detection and multi-label classification solve different tasks.

A Note on Simplification

This is an input-contract experiment, not a vision benchmark or a substitute for formal model evaluation. It uses one compact 2016 classifier, one image, square crops with a fixed vertical center, and 25 precomputed runs. It omits detection boxes, multi-label output, calibration measurement, data augmentation, modern model families, and arbitrary uploaded images.