图像识别-pytorch-RuntimeError: Error(s) in loading state_dict for DataParallel:

RuntimeError                              Traceback (most recent call last)
~\ColorRectalCancerClassification_FlyAI - usenet\main.py in 
    144 
    145 print('===============================================================')
--> 146 labels = model.predict_all(x_test)
    147 print(labels)
    148 #f=open("loss.txt",'w')

~\ColorRectalCancerClassification_FlyAI - usenet\model.py in predict_all(self, datas)
     46         # cnn.cuda()
     47         cnn = nn.DataParallel(cnn)
---> 48         cnn.load_state_dict(torch.load(os.path.join(MODEL_PATH, Torch_MODEL_NAME)))
     49         cnn.to(device)
     50         cnn.eval()


RuntimeError: Error(s) in loading state_dict for DataParallel

解决方案:
添加链接描述

model.load_state_dict(state_dict, strict=True)

Copies parameters and buffers from :attr:state_dict into this module and its descendants. If :attr:strict is True, then the keys of :attr:state_dict must exactly match the keys returned by this module’s :meth:~torch.nn.Module.state_dict function
从属性state_dict里面复制参数到这个模块和它的后代。如果strict为True, state_dict的keys必须完全与这个模块的方法返回的keys相匹配。如果为False,就不需要保证匹配。

Arguments:
state_dict (dict): a dict containing parameters and persistent buffers.
strict (bool, optional): whether to strictly enforce that the keys in :attr:state_dict match the keys returned by this module’s:meth:~torch.nn.Module.state_dict function. Default: True

你可能感兴趣的:(图像识别)