ChristianMarzahl / ObjectDetection

Some experiments with object detection in PyTorch
136 stars 43 forks source link

Inference Error #12

Open Jermanis opened 4 years ago

Jermanis commented 4 years ago

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 commented 4 years ago

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

NikolayTV commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Work on it

ChristianMarzahl commented 4 years ago

Moin,

please take a look at this example:

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

NikolayTV commented 4 years ago

Predictions are very different from show_results_side_by_side

ChristianMarzahl commented 4 years ago

You have to Normalize your input data.

Uncomment:

image = transforms.Normalize(mean, std)(image)

NikolayTV commented 4 years ago

Works. But I retrained my model without normalization