廊坊新聞網-主流媒體,廊坊城市門戶

    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]...
    出現兩個參數的不匹配。

    具體內容如下:

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

    成功解決問題~

    關鍵詞:

    熱點

    亚洲AV永久无码精品一百度影院| 亚洲综合激情六月婷婷在线观看| 久久亚洲sm情趣捆绑调教| 亚洲Av综合色区无码专区桃色| 日韩在线视精品在亚洲| 亚洲大尺度无码无码专区| 亚洲精品免费在线视频| 亚洲av最新在线网址| 中文字幕不卡亚洲 | 亚洲最新中文字幕| 亚洲AV无码久久| 亚洲精品卡2卡3卡4卡5卡区| 亚洲色大成网站www永久| 久久综合图区亚洲综合图区| 亚洲性猛交XXXX| 亚洲欧美成aⅴ人在线观看| 亚洲精品无码久久久久sm| 亚洲欧洲日产国码高潮αv| 亚洲蜜芽在线精品一区| 久久精品国产亚洲AV高清热| 久久久久久亚洲av成人无码国产| 国产l精品国产亚洲区在线观看| 最新亚洲成av人免费看| 亚洲精品线路一在线观看| 亚洲av成人一区二区三区在线观看| 亚洲国产成人久久综合| 中文字幕亚洲综合精品一区| 亚洲综合精品一二三区在线| 亚洲国产精品无码久久SM| 久久亚洲精品中文字幕三区| 亚洲一区二区中文| 亚洲人成高清在线播放| 亚洲综合国产精品| 亚洲人和日本人jizz| 亚洲欧美第一成人网站7777| 亚洲成av人片不卡无码久久| 亚洲综合熟女久久久30p| 亚洲国产精品久久久久| 亚洲一区在线免费观看| 亚洲国产成人精品无码区二本| 亚洲AV永久无码精品一区二区国产|