CycleGan训练报错总结:There is no latest_net_G.pth in the program-generated model

文章描述了一位用户在使用CycleGan训练模型时遇到的错误,即缺少latest_net_G.pth文件。用户尝试将latest_net_G_A.pth重命名为latest_net_G.pth,但导致新的错误。通过修改base_model.py文件中的代码,特别是将net.load_state_dict()的strict参数设为False,问题最终得到解决,模型成功运行。
摘要由CSDN通过智能技术生成

CycleGan训练报错总结:There is no latest_net_G.pth in the program-generated model

1,All errors are reported as follows:
Traceback (most recent call last):
File “C:/Users/Administrator/Desktop/pytorch-CycleGAN-and-pix2pix-master/test.py”, line 52, in
model.setup(opt) # regular setup: load and print networks; create schedulers
File “C:\Users\Administrator\Desktop\pytorch-CycleGAN-and-pix2pix-master\models\base_model.py”, line 88, in setup
self.load_networks(load_suffix)
File “C:\Users\Administrator\Desktop\pytorch-CycleGAN-and-pix2pix-master\models\base_model.py”, line 192, in load_networks
state_dict = torch.load(load_path, map_location=str(self.device))
File “E:\Anaconda3\envs\pytorch-gpu-Gan\lib\site-packages\torch\serialization.py”, line 594, in load
with _open_file_like(f, ‘rb’) as opened_file:
File “E:\Anaconda3\envs\pytorch-gpu-Gan\lib\site-packages\torch\serialization.py”, line 230, in _open_file_like
return _open_file(name_or_buffer, mode)
File “E:\Anaconda3\envs\pytorch-gpu-Gan\lib\site-packages\torch\serialization.py”, line 211, in init
super(_open_file, self).init(open(name, mode))
FileNotFoundError: [Errno 2] No such file or directory: ‘./checkpoints\maps_cyclegan\latest_net_G.pth’

Here is the folder where I generated the model
C:\Users\Administrator\Desktop\pytorch-CycleGAN-and-pix2pix-master\checkpoints\maps_cyclegan:
-latest_net_D_A.pth
-latest_net_D_B.pth
-latest_net_G_A.pth
-latest_net_G_B.pth
There is no latest_net_G.pth in the program-generated model?

修改:latest_net_G_A.pth直接修改为latest_net_G.pth 从A生成B

2,修改后出现如下错误
在这里插入图片描述
参考:http://www.taodudu.cc/news/show-863207.html?action=onClick

经查找后,在base_model.py文件中199行

196 # patch InstanceNorm checkpoints prior to 0.4
197  for key in list(state_dict.keys()):  # need to copy keys here because we mutate in loop
198  self.__patch_instance_norm_state_dict(state_dict, net, key.split('.'))
199  net.load_state_dict(state_dict,strict=False)  #qcy 添加了strict=False

修改后正常运行

根据引用\[1\],在程序生成的模型中没有名为"latest_net_G.pth"的文件。根据引用\[2\],可以尝试使用绝对路径加载模型文件,以避免手动更改路径的麻烦。在model.py中,可以使用以下代码来构造模型文件的绝对路径: ``` abs_file = os.path.abspath(__file__) # 获取model.py文件的绝对路径 abs_dir = abs_file\[:abs_file.rfind('\\')\] if os.name == 'nt' else abs_file\[:abs_file.rfind(r'/')\] # 找到绝对路径的同级目录 model_dir = os.path.join(abs_dir, 'checkpoints/Epoch99.pth') ``` 根据引用\[3\],在base_model.py文件的199行,添加了`strict=False`参数来加载模型状态字典。这可能是为了兼容旧版本的patch InstanceNorm checkpoints。所以,如果你遇到了"\[Errno 2\] No such file or directory: 'checkpoints/G_net_50.pth'"的错误,你可以尝试在加载模型时添加`strict=False`参数。 #### 引用[.reference_title] - *1* *3* [CycleGan训练报错总结:There is no latest_net_G.pth in the program-generated model](https://blog.csdn.net/tjnkyqcy/article/details/130791704)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [解决模型加载的路径问题“No such file or directory”](https://blog.csdn.net/qq_36560894/article/details/111302578)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值