Deployment: Classification¶
The recommended processing pipeline uses a detection model with only one generic class ("insect"). The low input resolution enables a high inference speed, which is necessary to reliably track moving/flying insects. Images of the detected and tracked insects are cropped from synchronized HQ frames in real time.
By using the provided script for automated monitoring, cropped detections of individual insects are saved as .jpg files and relevant metadata is saved to .csv for each recording interval. The insect images can be classified in a subsequent step on your local PC, by using a classification model exported to ONNX format for faster CPU inference. The classification results are added to the merged metadata .csv files for post-processing in the last step.
Installation¶
If you followed the steps in Local Setup, you already have Python installed on your computer.
Optional: Set up an isolated Python environment
You can create a virtual environment before installing the required packages for YOLOv5 to avoid version and dependency conflicts of the packages, especially if you are working on different Python projects.
-
Install
virtualenv
: -
Navigate to the
YOLOv5-cls
folder: -
Create a new Python environment (folder in your current directory):
-
Activate the environment by running:
If you cannot run the activate script because of your Execution Policy settings, open a new PowerShell as administrator and run the following:
-
Now all packages will be installed only in the virtual environment, which you have to activate everytime you want to run a script that depends on these packages.
-
You can deactivate the virtual environment by running:
- Create the new folder
C:\Users\<username>\YOLOv5-cls
. - Download
the custom
yolov5
fork and extract it to theYOLOv5-cls
folder. -
Open a new Terminal (PowerShell) and navigate to the
YOLOv5-cls
folder: -
Update
pip
: -
Install the required packages by running:
If you are running the image classification on a computer with CUDA-enabled GPU, please edit the
requirements.txt
file and changeonnxruntime
toonnxruntime-gpu
before installing the packages.
Run image classification¶
We will use the modified
classify/predict.py
script from the custom yolov5
repo
together with the provided classification model
to classify all insect images in the insect-detect/data
folder (camera trap output)
and add the prediction results to the merged metadata .csv files.
- Download
the
insect-detect-ml
repo and extract it to theYOLOv5-cls
folder. - Copy your
insect-detect/data
folder, saved from the Raspberry Pi's SD card to theYOLOv5-cls
folder. Make sure that only cropped detections are present! If you additionally saved full HQ frames, delete them before running the classification script. -
Navigate to the
YOLOv5-cls
folder and start the classification script by running:py yolov5-master/classify/predict.py --name camtrap1 --source "insect-detect/data/**/" --weights "insect-detect-ml-main/models/efficientnet-b0_imgsz128.onnx" --img 128 --sort-top1 --sort-prob --concat-csv
Change the
--name
of your prediction run accordingly, e.g. by including information about the camera trap location and collection date of the images. Instead of copying theinsect-detect/data
folder to theYOLOv5-cls
folder, you can also insert its full path after--source
.Optional arguments
--sort-top1
sort images to folders with predicted top1 class as folder name and do not write results on to image as text (which is the default configuration)--sort-prob
sort images first by probability and then by top1 class (requires--sort-top1
)--concat-csv
concatenate all metadata .csv files and append classification results to new columns
Image Not Found
While running the classification script, in some cases you might run into the following error:
This error can be caused by corrupt .jpg images, that are rarely generated during image capture. Run the
process_images.py
script to find corrupt images in the data folder and move them to a new folder with:After removing the corrupt .jpg images, the classification script should now run without throwing an error.
All results are saved to yolov5-master/runs/predict-cls/{name}
. The
*metadata_classified.csv
still contains multiple rows for each
tracked insect (= track_ID
). During post-processing
in the last step, the respective class with the highest weighted probability
is calculated for each tracked insect. This will create the final .csv file,
in which each row corresponds to an individual tracked insect.
If you used --sort-top1
as optional argument, the insect images are sorted
to folders with the predicted top1 class as folder name. This allows for a
quick identification of edge cases and these images can be used to
retrain your
classification model. By using --sort-prob
additionally, the images are
sorted by top1 probability first and then by top1 class. This can give you
more information about cases where the model is unsure about its prediction.
Adding images with a low probability to your training dataset can increase
classification accuracy over time.