Nothing Special   »   [go: up one dir, main page]

Skip to content

使用训练过的模型运行demo时发生运行警告 #592

Answered by vic01512
vic01512 asked this question in Q&A
Discussion options

You must be logged in to vote

再次按照#320更改了代码,成功运行了,建议阅读如下的源代码

def load_pretrain(model, pretrained_path): # 加载模型,一般五步
logger.info('load pretrained model from {}'.format(pretrained_path))
# 1.初始化device
device = torch.cuda.current_device() # 返回当前设备索引,直接使用GPU
# 2.加载模型 载入GPU
pretrained_dict = torch.load(pretrained_path,
map_location=lambda storage, loc: storage.cuda(device)) # 进行模型加载
# 3.移除module前缀(一般用于多GPU分布式训练后的模型)
if "state_dict" in pretrained_dict.keys():
pretrained_dict = remove_prefix(pretrained_dict['state_dict'],
'module.')
else:
pretrained_dict = remove_prefix(pretrained_dict, 'module.')
# 4.检查键完整性:即判断当前构建的模型参数与待加载的模型参数是否匹配
try:
check_keys(model, pretrained_dict)
except:
logger.info('[Warning]: using pretrain as f…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by vic01512
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants