agentmorris / MegaDetector

MegaDetector is an AI model that helps conservation folks spend less time doing boring things with camera trap images.
MIT License
117 stars 26 forks source link

Scripting MegaDetector model v5a #90

Closed agentmorris closed 1 year ago

agentmorris commented 1 year ago

Hello,

I would like to export the MegaDetector model md_v5a.0.0.pt as a TorchScript so I can utilize it more freely. It looks like this capability exists in 'yolov5'/ export.py` but I'm having trouble implementing it. Any advice?


Issue cloned from Microsoft/CameraTraps, original issue posted by r2j2ritson on Apr 19, 2023.

agentmorris commented 1 year ago

export.py is the right tool, but you will likely have to use the same version of PyTorch and the same version of YOLOv5 on which the model was trained. If you follow our instructions for setting up the MegaDetector conda environment, then run export.py in that environment, you should be good to go. My expectation is that the exported TorchScript will also depend on the same version of PyTorch, but I haven't verified that.

If you want to use the current version of PyTorch/YOLOv5, you will need an "updated" MegaDetector model. "updated" is in quotes because as far as we know, there's no way to "update" the model without modifying it just a little, so results are not bit-for-bit identical to the original. But, if you're OK with that, we discuss an unofficial update to the .pt file in this comment. My guess is that if you use that .pt file, the current version of YOLOv5's export.py will work fine.

Be aware that in our experiments, converting to TorchScript yields a model that also doesn't produce exactly the same results as the .pt file. We don't have any reason to believe it's worse, but it's definitely not identical, and we haven't explored this that thoroughly, so you may want to do some head-to-head comparisons.

I'm like 91% sure that the issue was about YOLOv5/PyTorch versions, so I'm going to close this issue for now, but if this doesn't solve your problem, we will re-open.


(Comment originally posted by agentmorris)

agentmorris commented 1 year ago

Thanks, Dan! This got me on the right track. I'll let you know if I have anymore issues. I noticed someone already put together an R package using the tensorflow .pb models, but since it looks like you have moved on to pytorch I figured I'd give a try to getting the v5 models usable in R.


(Comment originally posted by r2j2ritson)

agentmorris commented 1 year ago

If you're referring to this package:

https://github.com/conservationtechlab/animl

...my first thought was also that they're using MDv4, but they report that they've exported MDv5 as a TensorFlow model, so if you want to work in R and you're finding that it's lots of work to wrap MDv5 in R, you may want to check in with them. There are pros and cons to that approach and I haven't tested the package, so, YMMV.


(Comment originally posted by agentmorris)