Hi all! I am working on a classifier that should be able to tell the difference between a diseased and healthy plant. I am only working with 3 classes for simplicity’s sake: 0: Bacterial infection, 1: Healthy, and 2: Invalid (a class containing random images of the sky or soil that won’t fall into the other classes). I trained the YOLOv8 model and achieved ~94% accuracy and then proceeded to convert the model to TFLite for deployment on a microcontroller. This is where I am running into some trouble, when converting I keep getting this error for my supplied calibration images (for int8 quantization):
ERROR
train: /content/calibration_images_2_split/train… found 1307 images in 3 classes (requires 4 classes, not 3)
ERROR
val: /content/calibration_images_2_split/val… found 329 images in 3 classes (requires 4 classes, not 3)
I believe the background is being read as a fourth class somehow. There are some hints within the confusion matrix that lead me to believe this. Any help would be greatly appreciated!
Is this a detection model or classifier? They are different rhings. It looks like a detection model, not classifier.
What’s the full output of the log?
What was the training and export command used?
The “background” class is essentially the same as your “invalid” class, except it’s not a ‘true’ label. The background label is really just a bucket for all images that have zero labeled objects. When a model mistakenly ‘detects’ an object where there is no object, or when it misses detecting an object, these are labeled in the plot as “background” but there is no actual background label. The only labels provided are the ones you give the model in the data YAML.
Sharing the command used for export, and the full output log after running the command will help a lot for troubleshooting the issue. Additionally, double check that you’re passing the correct data YAML for calibration for your dataset, the exact same one you used for training.
It is a classifier, here are the commands I used for the following…
—————————————————————
Training:
model = YOLO(“yolov8n-cls.pt”)
model.train(data=“C:/Users/aalsa\PycharmProjects\Disease_Detection_v2\.venv\Dataset_7_RGB”,
epochs=100, imgsz=96)
—————————————————————
Export:
model = YOLO(‘/content/best.pt’)
model.export(format=‘tflite’, int8=True, data=‘/content/calibration_images_2’)
—————————————————————
Export output log:
Ultralytics 8.3.234
Python-3.12.12 torch-2.9.0+cu126 CPU (Intel Xeon CPU @ 2.20GHz)
YOLO8n-cls summary (fused): 47 layers, 1,529,867 parameters, 0 gradients, 3.2 GFLOPs
PyTorch: starting from ‘/content/best.pt’ with input shape (1, 3, 96, 96) BCHW and output shape(s) (1, 3) (3.0 MB)
TensorFlow SavedModel: starting export with tensorflow 2.19.0…
TensorFlow SavedModel: collecting INT8 calibration images from ‘data=/content/calibration_images_2’
WARNING
Dataset ‘split=train’ not found at /content/calibration_images_2/train
Found 1107 images in subdirectories. Attempting to split…
Splitting /content/calibration_images_2 (4 classes, 1636 images) into 80% train, 20% val…
Split complete in /content/calibration_images_2_split ![]()
ERROR
train: /content/calibration_images_2_split/train… found 1307 images in 3 classes (requires 4 classes, not 3)
ERROR
val: /content/calibration_images_2_split/val… found 329 images in 3 classes (requires 4 classes, not 3)
test: None…
Fast image access
(ping: 0.0±0.0 ms, read: 107.7±45.1 MB/s, size: 4.4 KB)
Scanning /content/calibration_images_2_split/val/Bacterial… 0 images, 329 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 329/329 1.1Kit/s 0.3s
WARNING
No labels found in /content/calibration_images_2_split/val/Bacterial.cache. See Datasets Overview - Ultralytics YOLO Docs for dataset formatting guidance.
New cache created: /content/calibration_images_2_split/val/Bacterial.cache
WARNING
Labels are missing or empty in /content/calibration_images_2_split/val/Bacterial.cache, training may not work correctly. See Datasets Overview - Ultralytics YOLO Docs for dataset formatting guidance.
ONNX: starting export with onnx 1.19.1 opset 22…
ONNX: slimming with onnxslim 0.1.77…
ONNX: export success
2.5s, saved as ‘/content/best.onnx’ (5.9 MB)
TensorFlow SavedModel: starting TFLite export with onnx2tf 1.28.5…
Saved artifact at ‘/content/best_saved_model’. The following endpoints are available:
…
TensorFlow SavedModel: export success
84.3s, saved as ‘/content/best_saved_model’ (19.6 MB)
TensorFlow Lite: starting export with tensorflow 2.19.0…
TensorFlow Lite: export success
0.0s, saved as ‘/content/best_saved_model/best_int8.tflite’ (1.6 MB)
Export complete (85.1s)
Results saved to /content
Predict: yolo predict task=classify model=/content/best_saved_model/best_int8.tflite imgsz=96 int8
Validate: yolo val task=classify model=/content/best_saved_model/best_int8.tflite imgsz=96 data=C:/Users/aalsa\PycharmProjects\Disease_Detection_v2\.venv\Dataset_7_RGB int8
I am passing a directory of images with the data= argument. Is this incorrect? How would my model access any calibration images if I only pass it a .YAML? Also see my response to Toxite for the full output log!
I think your dataset folder has a hidden IPython Notebook checkpoint folder
You should delete that.
!rm -rf /content/calibration_images_2/.ipynb_checkpoints
Okay so I deleted that checkpoint folder and double checked that there are now only 3 folders within the directory. I am however still getting the same errors:
ERROR
train: /content/calibration_images_split/train… found 1970 images in 3 classes (requires 4 classes, not 3)
ERROR
val: /content/calibration_images_split/val… found 744 images in 3 classes (requires 4 classes, not 3)
I am performing the conversion in google collab, separately from where I trained the model. Should I attempt to perform the whole training to conversion pipeline within the same notebook? Right now there is no YAML file for the program to reference during conversion and I am wondering if that is causing issues… The only files I have currently uploaded to the notebook are the best.pt model and the calibration_images folder.
The hidden directory is probably still present in /content/calibration_images_split/train
find /content/calibration_images_split/ -type d -name ".ipynb_checkpoints" -delete
You should delete using terminal. If you open that directory using the file browser, then it will recreate that hidden directory. You shouldn’t open the folder in file broswer.
