RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False

username: hitbuyi
cooked: <p>my code see  <a href="https://github.com/weili1457355863/VPS-Net" class="inline-onebox" rel="noopener nofollow ugc">GitHub - weili1457355863/VPS-Net: A vacant parking slot detection method in the around view image based on deep learning.</a><br>
it runs well on pytorch 1.7 with cuda,or with CPU on a laptop<br>
device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)<br>
device = “cpu”<br>
ps_detect =PsDetect(opt.model_def, opt.weights_path_yolo, opt.img_size, device)<br>
vps_classify = vpsClassify(opt.weights_path_vps, device)<br>
when I used cpu to run this code in pytorch 1.8 on the other laptop<br>
<span class="hashtag-raw">#device</span> = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)<br>
device = “cpu”<br>
ps_detect =PsDetect(opt.model_def, opt.weights_path_yolo, opt.img_size, device)<br>
vps_classify = vpsClassify(opt.weights_path_vps, device)</p>
<p>an error happens,<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/2/3249fa108b3bdfb05427abedc1696d6df7773084.png" data-download-href="https://discuss.pytorch.org/uploads/default/3249fa108b3bdfb05427abedc1696d6df7773084" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/2/3249fa108b3bdfb05427abedc1696d6df7773084_2_690x388.png" alt="image" data-base62-sha1="7aSmP2HK1BMgc6ce7mRJcasCpX6" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/2/3249fa108b3bdfb05427abedc1696d6df7773084_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/2/3249fa108b3bdfb05427abedc1696d6df7773084_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/2/3249fa108b3bdfb05427abedc1696d6df7773084_2_1380x776.png 2x" data-dominant-color="18191A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1600×900 146 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>the code attempts to run on GPU:<br>
vpsClassify calls  CustomizedAlexNet()<br>
class vpsClassify(object):<br>
def <strong>init</strong>(self, model_path, device):<br>
self.model_customized = CustomizedAlexNet()<br>
self.model_customized.eval()<br>
self.model_customized.load(model_path)<br>
self.model_customized.to(device)</p>
<p>CustomizedAlexNet() calls  BasicModule<br>
class CustomizedAlexNet(BasicModule):<br>
def <strong>init</strong>(self, num_classes=2):<br>
super(CustomizedAlexNet, self).<strong>init</strong>()</p>
<p>BasicModule is based on nn Module<br>
class BasicModule(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(BasicModule,self).<strong>init</strong>()<br>
def load(self,path):<br>
self.load_state_dict(torch.load(path))</p>
<p>in  load function of BasicModule(), device is no specified, until in init of  vpsClassify( self.model_customized.to(device))</p>
<p>what is the problem for pytorch1.8?  this code runs well on pytorch 1.7</p>

username: ptrblck
cooked: <p>Did you try to add the suggested <code>map_location</code> argument to <code>torch.load</code> as given in the error message?</p>

username: hitbuyi
cooked: <p>Yes I do.  by adding mp_location to torch.load,  the problem is solved.  What puzzled me is that  if I don’t specify mp_location in torch.load while specifying it  in outer class which call torch.load,  Pytorch1.7 run well, Pytorch1.8 report the error,   it implys that torch.load in Pytorch1.8 attemtps to load to GPU, and Pytorch1.7 not.</p>