廊坊新聞網(wǎng)-主流媒體,廊坊城市門戶

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

    2023-08-27 10:50:57 來源:博客園


    (資料圖)

    問題描述:

    # 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]

    所以?。。≡谟行蜃值渲袑獔箦e內(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 ...               ...

    成功解決問題~

    關鍵詞:

    熱點

    亚洲第一页综合图片自拍| 亚洲第一黄片大全| 亚洲成av人在片观看| 亚洲欧美日韩中文字幕一区二区三区 | 亚洲欧美黑人猛交群| 精品亚洲成A人无码成A在线观看 | 亚洲AV无码成人网站在线观看| 国产亚洲中文日本不卡二区| 亚洲一本之道高清乱码| 亚洲春色在线观看| 亚洲大香伊人蕉在人依线| 亚洲一区在线视频| 国产亚洲精aa在线看| 亚洲日本一线产区和二线| 亚洲国产成人综合精品| 亚洲AV性色在线观看| 国产精品亚洲色图| 午夜亚洲国产成人不卡在线| 亚洲精品tv久久久久| 国产综合亚洲专区在线| 亚洲精品无码MV在线观看| 亚洲va久久久噜噜噜久久天堂| 久久久久亚洲av无码专区蜜芽 | 亚洲人成电影亚洲人成9999网 | 亚洲综合一区二区精品导航 | 亚洲日韩乱码中文无码蜜桃| 亚洲午夜精品在线| 亚洲最大无码中文字幕| 亚洲爆乳无码专区www| 国产亚洲午夜精品| 国产啪亚洲国产精品无码| 亚洲精品无码高潮喷水在线| 亚洲精品高清视频| 亚洲一级片在线观看| 亚洲欧美日韩中文字幕在线一区 | 处破女第一次亚洲18分钟| 亚洲色偷偷狠狠综合网| 亚洲精品无码国产| 亚洲的天堂av无码| 亚洲人片在线观看天堂无码| 日韩亚洲综合精品国产|