🎨 Tomato Ripeness Classifier — YOLO26s-cls
Component 2 of the Tomato Ripeness Detection & Counting pipeline. Given a crop of one tomato, says whether it is unripe, half-ripe, ripe or rotten, plus a small rotten check that removes false "rotten" calls.
| Class | Label |
|---|---|
| 🟢 unripe | raw |
| 🟠 half-ripe | half_ripe |
| 🔴 ripe | ripe |
| 🟣 rotten | rotten |
| Architecture | YOLO26s-cls, 160×160 input, 5.4 M parameters |
| Trained on | 50,776 tomato crops (rotten ×4 with flips / rotations) · validated on 8,579 |
| Size | 11 MB · <1 ms / crop on a T4 |
Results (Tomato Defects test crops / matched tomatoes)
| Accuracy | |
|---|---|
| 4 classes | 80.8% |
| ripe / unripe / rotten | 88.7% |
| sound vs rotten | 98.6% |
Errors are almost only between neighbouring stages (ripe ↔ half-ripe ↔ unripe); ripe is never taken for unripe.
Rotten check (rotten_check.npz)
The classifier learnt "rotten" mostly from dark-red fruit, so on a dark conveyor it called healthy deep-red tomatoes rotten. rotten_check.npz is a logistic-regression head on the classifier's 512-d embedding, trained with conveyor tomatoes as sound examples. Keep a "rotten" call only when the check's probability is ≥ 0.5:
| Without | With | |
|---|---|---|
| Healthy conveyor tomatoes called rotten | 32 | 1 |
| Healthy sxsc7 tomatoes called rotten | 20 | 3 |
| Truly rotten kept (validation) | — | 97.5% |
Use
Crop each tomato with 10% padding around its box and resize to 160×160 (the training rule).
import numpy as np
from huggingface_hub import hf_hub_download
from ultralytics import YOLO
repo = "ParsaVictor/tomato-ripeness-classifier-yolo26s"
cls = YOLO(hf_hub_download(repo, "ripeness_yolo26s-cls_best.pt"))
emb = YOLO(hf_hub_download(repo, "ripeness_yolo26s-cls_best.pt")) # separate instance for embeddings
z = np.load(hf_hub_download(repo, "rotten_check.npz"))
p = cls.predict("tomato_crop.jpg", imgsz=160, verbose=False)[0].probs
label = cls.names[p.top1]
if label == "rotten":
e = emb.embed("tomato_crop.jpg", imgsz=160, verbose=False)[0].cpu().numpy()
e = e / np.linalg.norm(e)
if 1 / (1 + np.exp(-(e @ z["coef"] + z["intercept"]))) < 0.5:
probs = p.data.cpu().numpy().copy()
probs[p.top1] = 0
label = cls.names[int(probs.argmax())]
print(label)
Related
- 🍅 Full pipeline (detection + ripeness + tracking + counting): ParsaVictor/tomato-ripeness-yolo26
- 🔍 Component 1, tomato detector: ParsaVictor/tomato-detector-yolo26s
- 🗂️ Dataset: ParsaVictor/tomato-ripeness-detection
- 💻 Code: github.com/ParsaVictor/tomato-ripeness-detection
License: AGPL-3.0 (Ultralytics). Closed-source commercial use needs an Ultralytics Enterprise license.