PetervanLunteren / EcoAssist

Simplify camera trap image analysis with ML species recognition models based around the MegaDetector model
MIT License
121 stars 16 forks source link

Ability to use a custom yolov5 model #2

Closed ehallein closed 2 years ago

ehallein commented 2 years ago

Added the ability to use a custom yolov5 model when running inference. e.g. a model trained for specific species or md_v5b.0.0. Currently this needs to be done each time - may be worth saving this to a preference file, then could a have a drop down for various models? We may need to add the ability to use custom classes for when using models trained for a specific speices.

ehallein commented 2 years ago

also only tested on M1 mac os ventura

PetervanLunteren commented 2 years ago

Thanks for collaborating! Good idea to add the option to choose for MDv5b and a custom model. I continued your code and made a dropdown menu. What do you think of this? I tested it on MacOS (intel) and Windows 11. Could you test it on MacOS (M1)?

PetervanLunteren commented 2 years ago

Not sure what you mean with the ability to use custom classes for when using models trained for a specific species. How does that work?

ehallein commented 2 years ago

Not sure what you mean with the ability to use custom classes for when using models trained for a specific species. How does that work?

for example by default megadetector has 3 classes: animal, person, vehicle. I have trained a custom model using transfer learning to find only quokkas, magpies, ravens, humans. To make it work I have been changing the code in cameratraps run_detector.py

# Label mapping for MegaDetector
DEFAULT_DETECTOR_LABEL_MAP = {
    '1': 'animal',
    '2': 'person',
    '3': 'vehicle'  # available in megadetector v4+
}

and separate_detections_into_folders.py friendly_folder_names = {'animal':'animals','person':'people','vehicle':'vehicles'}

A bit of a hack, but it works.

I haven't looked into how your code handles this yet, but I might be possible to use a config file to store these variables so they are loaded at runtime. Then they could be customised as needed. It may be better to add this change to https://github.com/microsoft/CameraTraps though, not here? And then it should just work in EcoAssist with no extra changes? Let me know your thoughts. I am happy to add something to CameraTraps if that's the way to go.

ehallein commented 2 years ago

I'm getting an error on Windows 10 using my custom model. It works fine on my M1 mac and using the cameratrap run_detector_batch.py script on windows 10. So it's strange. Screenshot 2022-11-07 074502 Any ideas?

PetervanLunteren commented 2 years ago

I'm getting an error on Windows 10 using my custom model. It works fine on my M1 mac and using the cameratrap run_detector_batch.py script on windows 10. So it's strange. Screenshot 2022-11-07 074502 Any ideas?

Thanks for testing. That is weird... The error message you're seeing means that the MegaDetector output is not as expected (so probably an error from MD). In the hidden folder C:\ProgramData\EcoAssist_files\EcoAssist\logfiles you'll find session_log.txt. This is the log for the last runtime session. Here you'll find the MegaDetector output and will probably point you in the right direction. Here you can see the batch_command. Do you see anything here that is wrong? Do the MDv5a and MDv5b models work properly?

PetervanLunteren commented 2 years ago

You can upload it or email to petervanlunteren@hotmail.com if you want me to have a look at session_log.txt

PetervanLunteren commented 2 years ago

You can upload it or email to petervanlunteren@hotmail.com if you want me to have a look at session_log.txt

ehallein commented 2 years ago

ok, I found the problem/difference. On windows (and maybe intel Mac?) the install script clones and older version of the Yolov5 code, while m1 is the newer version. I use the newer version with camertraps on windows too, which is why it works. I used the one in the ecoassist directory and it crashed.

The difference is there is a new class name in https://github.com/ultralytics/yolov5/blob/master/models/yolo.py (line 165: class DetectionModel(BaseModel)), so it can't find it and crashes. As the new model is trained on new version, it must need this to be loaded.

I've had no issues running cameratraps with new versions of yolov5 over 100,000s of images (209be932dec9e89b902f0ac2975fa599e9bc676f installed at the moment), so maybe just use a newer version by default?

PetervanLunteren commented 2 years ago

Great work! The only reason I checkout the gits is because I don't know whether everything will still work with future commits.

Definitely, if the newer version of yolov5 works, let's change the default.

I don't have time to do it today, but could have a look at it tomorrow. Or if you have some time and want to go ahead and change the checkout to a new version in the install files, that would be great!

Thanks

PetervanLunteren commented 2 years ago

Great, thanks I'll merge it now!

With regards to the ability to use custom classes, I'll have to admit that I have no idea how to implement that into EcoAssist. I also have never trained a custom model using transfer learning, so I'm afraid I can't help you with that. It might indeed be that the CameraTraps repo is the best place to add this feature, but I don't know. Let me know if I can assist you in any way.