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

Inference Error #12

Open
Jermanis opened this issue Feb 25, 2020 · 8 comments
Open

Inference Error #12

Jermanis opened this issue Feb 25, 2020 · 8 comments

Comments

@Jermanis
Copy link

I have a trained object detection model which I want to use for inference on 1 image at a time, using the following code:

learn = load_learner(Path(myPath), file='myModel.pkl')
img = open_image("myImage.jpg")
prediction = learn.predict(img)

However, I get the following error:

AttributeError: 'list' object has no attribute 'cpu'

AttributeError                            Traceback (most recent call last)
1 prediction = learn.predict(img)
~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/basic_train.py in predict(self, item, return_x, batch_first, with_dropout, **kwargs)
    372         batch = self.data.one_item(item)
    373         res = self.pred_batch(batch=batch, with_dropout=with_dropout)
--> 374         raw_pred,x = grab_idx(res,0,batch_first=batch_first),batch[0]
    375         norm = getattr(self.data,'norm',False)
    376         if norm:

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/torch_core.py in grab_idx(x, i, batch_first)
    321 def grab_idx(x,i,batch_first:bool=True):
    322     "Grab the `i`-th batch in `x`, `batch_first` stating the batch dimension."
--> 323     if batch_first: return ([o[i].cpu() for o in x]   if is_listy(x) else x[i].cpu())
    324     else:           return ([o[:,i].cpu() for o in x] if is_listy(x) else x[:,i].cpu())
    325 

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/torch_core.py in <listcomp>(.0)
    321 def grab_idx(x,i,batch_first:bool=True):
    322     "Grab the `i`-th batch in `x`, `batch_first` stating the batch dimension."
--> 323     if batch_first: return ([o[i].cpu() for o in x]   if is_listy(x) else x[i].cpu())
    324     else:           return ([o[:,i].cpu() for o in x] if is_listy(x) else x[:,i].cpu())
    325 

AttributeError: 'list' object has no attribute 'cpu'
@ChristianMarzahl
Copy link
Owner

Please take a look at FastAI and how to perform inference on a single image.

@NikolayTV
Copy link

NikolayTV commented Mar 1, 2020

Please take a look at FastAI and how to perform inference on a single image.

I looked there. It did not work.
Did you manage to make it work for inference yourself?

@Jermanis
Copy link
Author

Jermanis commented Mar 2, 2020

No I did not - still have errors. Important difference can be that the FastAI example includes Training and Validation data which I wouldn't want to load for an Inference use case. I plan to spend more time on this issue this week. I will share the solution if I get it working. Please do the same.

@NikolayTV
Copy link

Work on it

@ChristianMarzahl
Copy link
Owner

Moin,

please take a look at this example:

https://colab.research.google.com/drive/16un8u65D4oTWiWzpdjMU_cpnVasaQ72n#scrollTo=TY_S_53Pikjt -> Inference example

@NikolayTV
Copy link

Predictions are very different from show_results_side_by_side

@ChristianMarzahl
Copy link
Owner

ChristianMarzahl commented Mar 3, 2020

You have to Normalize your input data.

Uncomment:
#image = transforms.Normalize(mean, std)(image)

@NikolayTV
Copy link

Works. But I retrained my model without normalization

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

3 participants