Training machine learning models to recognize fruits
As part of a learning exercise, I've trained a couple of machine learning models to detect and recognize a wide variety of fruits. The models are loaded locally in the browser via WebGPU, and all detection is done client
As part of a learning exercise, I've trained a couple of machine learning models to detect and recognize a wide variety of fruits. The models are loaded locally in the browser via WebGPU, and all detection is done client side, so no data gets sent outside of the client.
The fruitlens web app
Architecture
I split the problem of recognizing fruits in a scene into 2 smaller separate problems.
- Is there a fruit in the scene?
- If there is a fruit, what is the fruit?
For the first problem, a detector model reads each frame of the video feed and attempts to find any fruit-like object in the scene. Once found, we then move on to solve the second problem by using a classifier model to attempt to recognize the fruit object.
There are two separate pipelines that train the detector and classifier models. Once trained, the models are then uploaded to a web server. The web app downloads the models to the client when a user visits the web app.
Evaluation Set
Before talking about the training pipelines, a more pertinent question to be asked is: "How do we know if the detector and classifier are any good?"
To answer that question, I scored the classifier and detector against a set of 20 real photos of fruit. These photos are marked manually to indicate exactly where each piece of fruit is, and what fruit it is. These photos form the evaluation set, which every version of the classifier and detector will be scored against. This will help inform me if a newly trained version of a model is "better" than the previous version.
Some examples of the images in the evaluation set
The models are scored against the evaluation set using these criteria:
- Did they find all the fruit?
- How many did they miss?
- How many fake identifications (false positives) did they make?
- Of the fruits found, how often did they get the name right?
Twenty photos might seem small for an evaluation set, but that's intentional. The size of the evaluation set has to be manageable for one person to look at every single mistake. I can then analyze what is happening, instead of solely relying on metrics.
For people familiar with ML, these are the actual metrics for the detector and classifier models:
| Number | The question it answers | Score |
|---|---|---|
| Detector mAP | How closely do its boxes line up with the real fruit, and is it most confident about the right ones? | 0.33 |
| Detector recall | Of all the real fruit in the photos, how much did it find? | 65% |
| Detector precision | Of all the boxes it drew, how many had fruit in them? | 74% |
| False positives per image | How many boxes per photo landed on something that isn't fruit? | 2.4 |
| Classification accuracy | When it named a fruit, how often was the name right? | 81% |
| Classifier F1 | Fruit by fruit, how well does it catch each one without handing its name to the wrong fruit? | 0.74 |
| Coverage | Of the fruit it found, how much was it confident enough to name at all? | 75% |
How the detector works
The detector has one job, to look at a video frame and see if there's a fruit in the scene. It doesn't try to identify the fruit, that's the job of the classifier.
It's built on top of YOLO (You Only Look Once), a CNN designed for real-time object detection. By default, a YOLO model is trained to detect a wide variety of objects; I fine tuned it against a corpus of fruit images to tailor it specifically on fruits.
To teach it what fruit looks like, I fed it:
- Real photos of fruit from a giant public photo collection, with boxes already drawn around each piece.
- Extra photos of fruit piled up in big messy groups (like a crate of apples), so it also learns to spot fruit in a crowd.
- Photos of things that look like fruit but aren't: balls, hands, phones, with no boxes drawn, so it learns what to ignore.
All three sources point at the same detector. It never sees which dataset a photo came from, only the fruit boxes (or the lack of any) in it.
How the classifier works
The detector determines if there is a fruit in the scene. If there is, the classifier then attempts to identify the fruit using the cropped piece of the scene.
The classifier is built on MobileNet, a small mobile friendly image recognition model. Similar to the detector, it is fine tuned specifically on images of fruit.
There is one valued learning from fine tuning the classifier. The usual data augmentation techniques that is applied on the corpus had to be applied carefully for this use case. For fruits, the color is often the main clue as to what fruit it is (green vs red apple, ripe vs unripe banana, etc.). Twist the colors too much during fine tuning, and you'll accidentally teach the model the wrong lesson.
Quantization
For performance optimization, the models are quantized to fp16 before deployment to the web server.
One lesson learnt from the quantization of these models, if I quantize every layer of the model equally, the models perform quite badly. The problem was the first layer (the stem, which reads the raw pixels) and the last layer (the head, which produces the boxes or class scores). Both are very sensitive to rounding. Mess with those and the whole thing falls apart. Quantize everything in between, though, is totally fine. Once I figured out to leave the first and last layers alone, the quantized models performed almost exactly as well as the full-size ones.
Loading it all client side
When you visit https://fruitlens.app, it loads the detector and classifier models into your browser. The loading process is in a separate background thread so the page remains responsive.
Then, while your camera is on, roughly 10 times a second, it grabs a snapshot and runs it through four steps:
- Find - spot the fruit-shaped boxes in the frame.
- Track - match each box to the one from a split second ago, so it's not treating the same banana as a brand new banana every single frame.
- Name - crop out each box and figure out what fruit it is. If there's a bunch of fruit in frame, it bundles all the crops together and asks the model once, instead of asking one at a time (which would be slower).
- Draw - paint the boxes on screen and show the nutrition info for whichever fruit is selected.
The drawing code runs at 60 frames per second, separate from the Vue UI. Updating the UI is expensive, so it only hears about detections when they change.
Labels are also kept from flickering. A tracked fruit only changes its name after two classifications in a row agree on the new one.
Try to break it
Open fruitlens.app and point it at whatever fruit you have lying around. If it gets something wrong, I'd like to hear about it. A photo it fails on is the most useful thing you can send me.
I hope you got some useful pointers from this article. If you are interested in more articles about tech, be sure to subscribe to get notified whenever I publish a new article.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.







