state_dict 内の予期しないキー: “model”

問題の説明: 事前トレーニングの重みをロードするとエラーが発生します。state_dict に予期しないキーがあります: "model"

Weight_dic_path = 'convnext_base_1k_224.pth'
state_dic=torch.load(weight_dic_path,map_location=device)
model.load_state_dict(state_dic)

解決:

Weight_dic_path = 'convnext_base_1k_224.pth' 
state_dic=torch.load(weight_dic_path, map_location=device) 
new_state={} 
for k,v in state_dic.items(): #去除关键字”model" 
    new_state=v 
for k,v in new_state .items(): 
    print(k) 
model1.load_state_dict(new_state)

操作結果:

ノルム.バイアス
ヘッド.ウェイト
ヘッド.バイアス

プロセスは終了コード 0 で終了しました
 

完璧に動作します

おすすめ

転載: blog.csdn.net/weiyuangong/article/details/124986691