qfgaohao / pytorch-ssd

MobileNetV1, MobileNetV2, VGG based SSD/SSD-lite implementation in Pytorch 1.0 / Pytorch 0.4. Out-of-box support for retraining on Open Images dataset. ONNX and Caffe2 support. Experiment Ideas like CoordConv.
https://medium.com/@smallfishbigsea/understand-ssd-and-implement-your-own-caa3232cd6ad
MIT License
1.4k stars 534 forks source link

runtime error mobillenet-ssd-v2 #183

Open DaiGuard opened 2 years ago

DaiGuard commented 2 years ago

Hello

I'm trying to use this repo to run live demo to use mobielnet-ssd-v2. I tried to run python run_ssd_live_demo.py mb2-ssd-lite models/mb2-ssd-lite-mp-0_686.pth models/voc-model-labels.txt.

I got this error as output

$ python run_ssd_live_demo.py mb2-ssd-lite models/mb2-ssd-lite-mp-0_686.pth models/voc-model-labels.txt 
Traceback (most recent call last):
  File "run_ssd_live_demo.py", line 70, in <module>
    boxes, labels, probs = predictor.predict(image, 10, 0.4)
  File "/home/dai_guard/Workspaces/apps/pytorch-ssd/vision/ssd/predictor.py", line 37, in predict
    scores, boxes = self.net.forward(images)
  File "/home/dai_guard/Workspaces/apps/pytorch-ssd/vision/ssd/ssd.py", line 93, in forward
    locations, self.priors, self.config.center_variance, self.config.size_variance
  File "/home/dai_guard/Workspaces/apps/pytorch-ssd/vision/utils/box_utils.py", line 108, in convert_locations_to_boxes
    locations[..., :2] * center_variance * priors[..., 2:] + priors[..., :2],
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
DaiGuard commented 2 years ago

From wath I can tell, of the two valiables (locations, priors), priors seems to be in cuda mode.

# add this code in box_utils.py line: 104
print(locations.is_cuda)
print(priors.is_cuda)
---
False
True

Therefore, the following code could be added for use


    # priors can have one dimension less.
    if priors.dim() + 1 == locations.dim():
        priors = priors.unsqueeze(0)

+    if priors.is_cuda:
+       priors = priors.to('cpu')

    return torch.cat([
        locations[..., :2] * center_variance * priors[..., 2:] + priors[..., :2],
        torch.exp(locations[..., 2:] * size_variance) * priors[..., 2:]
    ], dim=locations.dim() - 1)
bryanbocao commented 1 year ago

https://github.com/qfgaohao/pytorch-ssd/blob/master/vision/ssd/ssd.py#L38

priors is saved in cuda device if you run with GPUs.

Another walkaway solution would be simply to run by

CUDA_VISIBLE_DEVICES='' python3 run_ssd_live_demo2.py mb2-ssd-lite models/mb2-ssd-lite-mp-0_686.pth models/voc-model-labels.txt <TEST_IMG>.jpg
bryanbocao commented 1 year ago

I simply commented it out

self.priors = config.priors #.to(self.device)

https://github.com/qfgaohao/pytorch-ssd/pull/189 https://github.com/qfgaohao/pytorch-ssd/pull/189/commits/f9de9738f56a6844adc8375f217deabb7767dd20

hidetoshi-kinoshita commented 1 year ago

Hello. With the modification below, I was able to get it to work as expected on both CPU and GPU environments. MobileNetV1 has the same implementation and runs without problems in the GPU environment. I think it was probably a simple mistake.

imgC1

DaiGuard commented 1 year ago

I simply commented it out

self.priors = config.priors #.to(self.device)

189 f9de973

Thank you very much. It worked fine.