原加载程序 self.net_G.load_state_dict(checkpoint['model_G_state_dict']) 修改后 from collections import OrderedDict # 解决权重加载问题 # 假设checkpoint已经加载 original_state_dict = checkpoint['model_G_state_dict'] # 提取实际的模型状态字典 # 创建一个新字典,给键添加'module.'前缀 new_state_dict = OrderedDict() for key, value in original_state_dict.items(): new_key = 'module.' + key new_state_dict[new_key] = value # 使用新的状态字典加载模型 self.net_G.load_state_dict(new_state_dict)
或者
from collections import OrderedDict # 解决权重加载问题 # 假设checkpoint已经加载 original_state_dict = checkpoint['model_G_state_dict'].pop('model_G_state_dict') # 提取实际的模型状态字典 # 创建一个新字典,给键添加'module.'前缀 new_state_dict = OrderedDict() for key, value in original_state_dict.items(): new_key = 'module.' + key new_state_dict[new_key] = value # 使用新的状态字典加载模型 self.net_G.load_state_dict(new_state_dict)