Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting MegaDetector model v5a #90

Closed
agentmorris opened this issue May 20, 2023 · 3 comments
Closed

Scripting MegaDetector model v5a #90

agentmorris opened this issue May 20, 2023 · 3 comments

Comments

@agentmorris
Copy link
Owner

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
Copy link
Owner Author

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
Copy link
Owner Author

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
Copy link
Owner Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant