(資料圖)
問題描述:
# RuntimeError: Error(s) in loading state_dict for Fusion_Generator: size mismatch for fg_decoder.0.weight: copying a param with shape torch.Size([4096, 1024]),g_decoder.0.weight: copying a param with shape torch.Size([4096, 1024]...
出現兩個參數的不匹配。
具體內容如下:
model = GAN(opt)loaded = torch.load(model_path)assert (opt.epoch == loaded["epoch"])model.load_state_dict(loaded["model"], strict=False) # 這里爆出上述Error,定位到下面的函數def load_state_dict(self, pretrained_dict, strict=False): for k in pretrained_dict: if k ... ... ... elif k == "generator": self.generator.load_state_dict(pretrained_dict[k], strict=strict) # 這里雖然strict傳入的是False,忽略不匹配參數,仍有上述問題 elif k ... ...
在參考 這里后,如果只是pop()掉fg_decoder.0.weight和bg_decoder.0.weight后,會有新的問題出現(一般問題通過pop掉能解決問題),即
KeyError: "fg_decoder.0.weight,bg_decoder.0.weight"
即不能識別上述兩個鍵值,這時可以通過打印模型參數具體內容查看:
def load_state_dict(self, pretrained_dict, strict=False): for k in pretrained_dict: if k ... ... ... elif k == "fusion_generator": for u in pretrained_dict[k].keys(): print(u," ",pretrained_dict[k][u]) self.fusion_generator.load_state_dict(pretrained_dict[k], strict=strict) # elif k ... ...
打印結果
fg_decoder.0.weight xxxxxx tensor([0., 0., 0., ..., 0., 0., 0.], device="cuda:0")
fg_decoder.0.bias xxxxxx tensor([0., 0., 0., ..., 0., 0., 0.], device="cuda:0") fg_decoder.1.weight xxxxxx tensor([1.0362, 0.9969, 0.9892, ..., 0.9939, 1.0122, 1.0190], device="cuda:0") fg_decoder.1.bias xxxxxx tensor([0., 0., 0., ..., 0., 0., 0.], device="cuda:0") fg_decoder.1.running_mean xxxxxx tensor([ 0.1915, -0.5510, 0.5370, ..., -0.1265, 0.8344, 1.4391], device="cuda:0") fg_decoder.1.running_var xxxxxx tensor([0.9402, 0.7382, 0.0167, ..., 0.3988, 0.1081, 0.4470], device="cuda:0") fg_decoder.1.num_batches_tracked xxxxxx tensor(3880, device="cuda:0") fg_decoder.3.weight xxxxxx tensor([[ 0.0211, -0.0072, 0.0030, ..., 0.0090, 0.0120, 0.0043], [ 0.0221, -0.0320, -0.0050, ..., 0.0239, 0.0035, 0.0438], [ 0.0246, -0.0091, 0.0146, ..., -0.0003, 0.0257, -0.0025], ..., [ 0.0077, -0.0209, -0.0017, ..., 0.0135, 0.0418, 0.0052], [ 0.0109, 0.0066, -0.0093, ..., 0.0048, -0.0019, -0.0381], [ 0.0145, -0.0165, 0.0095, ..., 0.0252, -0.0184, 0.0178]], device="cuda:0")....
bg_decoder.0.weight xxxxxx tensor([0., 0., 0., ..., 0., 0., 0.], device="cuda:0")....
可以發現fg_decoder.0.weight和bg_decoder.0.weight都在里面,并且對應為pretrained_dict[k][u]
所以!?。≡谟行蜃值渲袑獔箦e內容刪除后,就能解決size mismatch問題
def load_state_dict(self, pretrained_dict, strict=False): for k in pretrained_dict: if k ... ... ... elif k == "fusion_generator": for u in list(pretrained_dict[k].keys()):# (小坑)加list防止同時讀寫報錯 if u == "fg_decoder.0.weight" or u == "bg_decoder.0.weight": pretrained_dict[k].pop(u) self.fusion_generator.load_state_dict(pretrained_dict[k], strict=strict) # elif k ... ...
成功解決問題~
關鍵詞: