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

RuntimeError                              Traceback (most recent call last)
~\ColorRectalCancerClassification_FlyAI - usenet\main.py in <module>
    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值