预训练模型测试
1、根据原论文作者所提供的代码进行下载
EDSR-PyTorch
2、配置环境
官方提供的环境配置要求如下:
创建环境,并根据要求进行配置。(我这边的pytorch版本为pytorch,需要根据服务器上cuda的版本匹配进行配置)
基本上的库使用conda install xxx就能进行安装。
3、下载预训练模型
在README.md文件中下载预训练模型,并放置到EDSR-PyTorch/experiment/test/model中(并不是一定要按这个位置存放)
4、修改EDSR-PyTorch/src/option.py中相关内容
将–pre_train 后的地址改为存放官方所提供的预训练模型的地址
5、运行main.py
cd src
python main.py --data_test Demo --scale 4 --n_resblocks 32 --n_feats 256 --res_scale 0.1 --pre_train ../experiment/test/model/EDSR_x4.pt --test_only --chop --save_results --self_ensemble
6、使用预训练测试成功
终端会出现这样的提示
在项目文件中能够看到测试生成的结果在results-Demo下
问题记录
1、问题1:AttributeError: module ‘torch.backends’ has no attribute ‘mps’
解决办法:将model/init.py和src/trainer.py中有关mps的内容进行修改。
init.py原代码
if self.args.cpu:
device = torch.device('cpu')
else:
if torch.backends.mps.is_available():
device = torch.device('mps')
elif torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
init.py修改后为
if self.args.cpu:
device = torch.device('cpu')
else:
if int(torch.__version__.split('.')[0]) >= 1 and int(torch.__version__.split('.')[1]) >= 10:
if torch.backends.mps.is_available():
self.device = torch.device('mps')
elif torch.cuda._available():
self.device = torch.device('cuda')
else:
self.device = torch.device('cpu')
else:#当前版本不支持mps
if torch.cuda.is_available():
self.device = torch.device('cuda')
else:
self.device = torch.device('cpu')
trainer.py原代码
if self.args.cpu:
device = torch.device('cpu')
else:
if torch.backends.mps.is_available():
device = torch.device('mps')
elif torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
trainer.py修改后
if self.args.cpu:
device = torch.device('cpu')
else:
if int(torch.__version__.split('.')[0]) >= 1 and int(torch.__version__.split('.')[1]) >= 10:
if torch.backends.mps.is_available():
self.device = torch.device('mps')
elif torch.cuda._available():
self.device = torch.device('cuda')
else:
self.device = torch.device('cpu')
else:#当前版本不支持mps
if torch.cuda.is_available():
self.device = torch.device('cuda')
else:
self.device = torch.device('cpu')
2、问题2:NameError: free variable ‘device’ referenced before assignment in enclosing scope
解决办法:修改trainer.py中的__pepare函数,修改为
def _prepare(tensor, device):
if self.args.precision == 'half':
tensor = tensor.half()
return tensor.to(device)
lr = _prepare(lr, self.device)
hr = _prepare(hr, self.device)
return lr, hr
参考博客
EDSR-pytorch复现anaconda环境配置
基于pytorch的EDSR代码复现
EDSR-PyTorch复现