亚洲国内精品自在线影视,国产成人色美女av网址,日本老太婆XXXB视频,浓逼毛美女掰逼

您的位置:首頁 > 體育 >

strict=False 但還是size mismatch 的解決辦法

2023-08-26 23:12:51 來源:博客園


(資料圖片)

問題描述:

# 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]...
出現(xiàn)兩個參數(shù)的不匹配。

具體內(nèi)容如下:

model = GAN(opt)loaded = torch.load(model_path)assert (opt.epoch == loaded["epoch"])model.load_state_dict(loaded["model"], strict=False)   # 這里爆出上述Error,定位到下面的函數(shù)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,忽略不匹配參數(shù),仍有上述問題        elif k ...               ...

在參考 這里后,如果只是pop()掉fg_decoder.0.weightbg_decoder.0.weight,會有新的問題出現(xiàn)(一般問題通過pop掉能解決問題),即

KeyError: "fg_decoder.0.weight,bg_decoder.0.weight"

即不能識別上述兩個鍵值,這時可以通過打印模型參數(shù)具體內(nèi)容查看:

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 ...               ...

打印結(jié)果

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")....

可以發(fā)現(xiàn)fg_decoder.0.weight和bg_decoder.0.weight都在里面,并且對應為pretrained_dict[k][u]

所以?。?!在有序字典中將對應報錯內(nèi)容刪除后,就能解決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 ...               ...

成功解決問題~

關(guān)鍵詞:

[責任編輯:xwzkw]

相關(guān)閱讀

丰台区| 望都县| 兴文县| 娱乐| 墨竹工卡县| 互助| 万州区| 商水县| 台州市| 新疆| 沛县| 中宁县| 涡阳县| 神木县| 肥城市| 阜新| 依安县| 和平县| 蓝山县| 桃江县| 进贤县| 龙陵县| 麻栗坡县| 子长县| 汉中市| 湘潭县| 抚顺市| 囊谦县| 老河口市| 锦州市| 沙坪坝区| 抚顺县| 清新县| 治多县| 上思县| 武清区| 沅江市| 中江县| 岫岩| 蒲城县| 朔州市|