All articles
June 15, 20255 min read

Watching a fruit classifier decide — and where it looks

Computer VisionMobileNetV2Transfer LearningInterpretability

Some computer-vision projects end in a single number on the test set, and that's that. I had something else in mind: a project you can point a camera at. So I put my fruit-and-vegetable classifier online. It's built to show not only what it suspects, but also where it looks while deciding. And everything happens right in the browser, no server, no upload.

Try the live demo →

What's behind it

Technically it's a MobileNetV2 that I adapted to the Fruits-360 dataset via transfer learning. It covers 201 classes, from Apple Golden to Zucchini. The ImageNet-pretrained backbone stays untouched; only the small head on top gets trained — a global-average-pooling layer feeding into a softmax layer. On the studio test set that gets you to about 90 percent.

Getting it into the browser went more smoothly than with my NLP model. All the groundwork, from resizing to MobileNet's typical normalisation to a minus-one-to-one range, is anchored in the network as layers. The browser pushes raw pixels in and TensorFlow.js handles the forward pass on the GPU via WebGL. After 8-bit quantisation of the weights, the bundle weighs in at around 4 MB.

The heat map is almost incidental

One aspect I like: since the head consists of a global average pooling and a dense layer, Class Activation Mapping suggests itself. You take the last convolutional feature maps, weight them with the class's dense weights and sum them up. No gradients, no second pass needed. It's part of the architecture.

So when the demo lights up the pixels that argue for "Lemon", that's no decoration added after the fact, but the evidence the final layer weighed up. Tap one of the top 5 and you see what that class would have keyed on.

Where it struggles — deliberately

Fruits-360 is a studio product: centred fruit against a clean background. In that world the model feels at home. But the demo is useful for leaving it:

Take the fruit in your hand, and fingers and skin compete for its attention. Or aim at a busy scene, and the model doesn't know what it's supposed to name — a question it was never asked in training. Show two fruits, and it still has to find one label for the whole frame, which it cannot do.

Watch how the confidence drops and the heat map drifts off. This distribution shift between the clean test number and the live behaviour is more tangible than just reading about it.

In practice, a supermarket scanner wouldn't simply classify the raw frame. You would detect and segment the object with YOLO or SAM and hand only that crop to the classifier, which would also be trained on real photos rather than studio poses. This model is one honest component of such a system. A model that quietly guesses on unfamiliar inputs carries risk. One whose competence boundary you can see in front of you is one you can put into a pipeline with a clear conscience.

Go poke around the live demo: upload a photo or use the webcam, switch on the heat map and read the top 5.

Code: fruits-360-Classifier