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

jit.trace() not working #57

Closed
shehroz010 opened this issue Sep 24, 2020 · 6 comments
Closed

jit.trace() not working #57

shehroz010 opened this issue Sep 24, 2020 · 6 comments

Comments

@shehroz010
Copy link

I am using detecto model which is trained on custom data set, but the problem is when i try to use that model with jit.trace it throws an error.

Code:

from detecto.core import Model
import torch

model = Model.load(model_path, classes)

example = torch.rand(1, 3, 224, 224)
model_trace = torch.jit.trace(model, example)

Error:

Traceback (most recent call last):
File “c:/Users/shehr/Desktop/Android Project/Android_Model.py”, line 21, in
model_trace = torch.jit.script(model, example)
File “C:\Users\shehr\Anaconda3\envs\tf_gpu\lib\site-packages\torch\jit_init_.py”, line 1257, in script
qualified_name = _qualified_name(obj)
File “C:\Users\shehr\Anaconda3\envs\tf_gpu\lib\site-packages\torch_jit_internal.py”, line 682, in _qualified_name
name = obj.name
AttributeError: ‘Model’ object has no attribute ‘name’

Any idea why m getting this ? Any help will be appreciated.

@alankbi
Copy link
Owner

alankbi commented Sep 25, 2020

It looks like you're trying to use a PyTorch function on a Detecto object - I think the following should hopefully fix the error:

model_trace = torch.jit.trace(model.get_internal_model(), example)

@shehroz010
Copy link
Author

I have tried that too, but in that case it throws another error

"ValueError: In training mode, targets should be passed"

But if you see my code above m not training a model m trying to trace model already trained on custom data set with custom classes

@alankbi
Copy link
Owner

alankbi commented Sep 26, 2020

Maybe try adding the following line:

internal_model = model.get_internal_model()
internal_model.eval()  # add this line
model_trace = torch.jit.trace(internal_model, example)

@shehroz010
Copy link
Author

Okay I tried that too but m getting another error now

RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _thnn_conv2d_forward

Even cuda is available on my machine as torch.cuda.is_available() returns True

There is one more thing that is confusing me, if we are extract the internal model, doesn't it only returns the official pretrained version on of faster rcnn ? I mean will it still be able to detect the objects with the classes i have trained the model ?
I am new to the PyTorch and I found detecto easy for fatster rcnn but now m stuck on this. I just want the traced file to deploy it on the android. But it keeps giving me errors.

@alankbi
Copy link
Owner

alankbi commented Sep 30, 2020

When you train the Detecto model, it applies transfer learning on the internal PyTorch model, so the internal model will still be able to do everything (and more) that the Detecto model does. The Detecto model is mainly a wrapper around the PyTorch model to make things simpler to use.

As for the issue you're getting, I'm not too sure as that seems to be getting more into PyTorch's domain than Detecto's. I would suggest maybe trying to manually send the internal model to the GPU (maybe internal_model.cuda() or the other suggestions here) or seeing if there's an error elsewhere in the program.

@shehroz010
Copy link
Author

Currently RCNN models are not supported by jit.trace() in pytorch and even wit jit.script() there is an issue on which they are working on.

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

2 participants