File size: 10,835 Bytes
3ce19a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# PyTorch StudioGAN: https://github.com/POSTECH-CVLab/PyTorch-StudioGAN
# The MIT License (MIT)
# See license file or visit https://github.com/POSTECH-CVLab/PyTorch-StudioGAN for details

# src/utils/ckpt.py

from os.path import join
import os
import glob

import torch
import numpy as np

import utils.log as log
try:
    import utils.misc as misc
except AttributeError:
    pass

blacklist = ["CCMGAN2048-train-2021_06_22_06_11_37"]


def make_ckpt_dir(ckpt_dir):
    if not os.path.exists(ckpt_dir):
        os.makedirs(ckpt_dir)
    return ckpt_dir


def load_ckpt(model, optimizer, ckpt_path, load_model=False, load_opt=False, load_misc=False, is_freezeD=False):
    ckpt = torch.load(ckpt_path, map_location=lambda storage, loc: storage, weights_only=False)
    if load_model:
        if is_freezeD:
            mismatch_names = misc.load_parameters(src=ckpt["state_dict"],
                                                  dst=model.state_dict(),
                                                  strict=False)
            print("The following parameters/buffers do not match with the ones of the pre-trained model:", mismatch_names)
        else:
            # Inference helpers (paper viz dumpers) set STUDIOGAN_LOAD_STRICT=0 so that
            # state_dicts saved by an older code revision (e.g. extra mapping.rtm_*
            # buffers) still load. Default behaviour is unchanged.
            _strict = os.environ.get("STUDIOGAN_LOAD_STRICT", "1") != "0"
            if _strict:
                model.load_state_dict(ckpt["state_dict"], strict=True)
            else:
                missing, unexpected = model.load_state_dict(ckpt["state_dict"], strict=False)
                if missing or unexpected:
                    print("[ckpt] non-strict load: missing={n_m} unexpected={n_u}".format(
                        n_m=len(missing), n_u=len(unexpected)))

    if load_opt:
        optimizer.load_state_dict(ckpt["optimizer"])
        for state in optimizer.state.values():
            for k, v in state.items():
                if isinstance(v, torch.Tensor):
                    state[k] = v.cuda()

    if load_misc:
        seed = ckpt["seed"]
        run_name = ckpt["run_name"]
        step = ckpt["step"]
        try:
            aa_p = ckpt["aa_p"]
        except:
            aa_p = ckpt["ada_p"]
        best_step = ckpt["best_step"]
        best_fid = ckpt["best_fid"]

        try:
            epoch = ckpt["epoch"]
        except:
            epoch = 0
        try:
            topk = ckpt["topk"]
        except:
            topk = "initialize"
        try:
            best_ckpt_path = ckpt["best_fid_checkpoint_path"]
        except:
            best_ckpt_path = ckpt["best_fid_ckpt"]
        try:
            lecam_emas = ckpt["lecam_emas"]
        except:
            lecam_emas = None
        return seed, run_name, step, epoch, topk, aa_p, best_step, best_fid, best_ckpt_path, lecam_emas


def load_StudioGAN_ckpts(ckpt_dir, load_best, Gen, Dis, g_optimizer, d_optimizer, run_name, apply_g_ema, Gen_ema, ema,
                         is_train, RUN, logger, global_rank, device, cfg_file):
    when = "best" if load_best is True else "current"
    x = join(ckpt_dir, "model=G-{when}-weights-step=".format(when=when))
    y = join(ckpt_dir, "model=D-{when}-weights-step=".format(when=when))
    Gen_glob = glob.glob(glob.escape(x) + '*.pth')
    Dis_glob = glob.glob(glob.escape(y) + '*.pth')

    # Inference-only fallback for shipped checkpoint dirs that contain
    # *only* G_ema-* (e.g. StudioGAN's pretrained baseline tarballs).
    # Triggered only when both the Gen and Dis files are absent and the
    # caller is not training. Returns dummy step/epoch metadata.
    if not Gen_glob and not Dis_glob and not is_train and apply_g_ema:
        z_pat = join(ckpt_dir, "model=G_ema-{when}-weights-step=".format(when=when))
        z_glob = glob.glob(glob.escape(z_pat) + '*.pth')
        if not z_glob:
            z_glob = glob.glob(join(ckpt_dir, "model=G_ema-current-weights-step=*.pth"))
        if z_glob:
            Gen_ema_ckpt_path = sorted(z_glob)[-1]
            print("[ckpt] G/D ckpts missing -> G_ema-only inference load:", Gen_ema_ckpt_path)
            os.environ.setdefault("STUDIOGAN_LOAD_STRICT", "0")
            load_ckpt(model=Gen_ema, optimizer=None, ckpt_path=Gen_ema_ckpt_path,
                      load_model=True, load_opt=False, load_misc=False)
            try:
                load_ckpt(model=Gen, optimizer=None, ckpt_path=Gen_ema_ckpt_path,
                          load_model=True, load_opt=False, load_misc=False)
            except Exception as _e:
                print("[ckpt] (skipping Gen mirror; not needed for sampling):", _e)
            ema.source, ema.target = Gen, Gen_ema
            return run_name, 0, 0, "initialize", 0, 0, 0.0, "", None, logger

    Gen_ckpt_path = Gen_glob[0]
    Dis_ckpt_path = Dis_glob[0]

    prev_run_name = torch.load(Dis_ckpt_path, map_location=lambda storage, loc: storage, weights_only=False)["run_name"]
    is_freezeD = True if RUN.freezeD > -1 else False

    load_ckpt(model=Gen,
              optimizer=g_optimizer,
              ckpt_path=Gen_ckpt_path,
              load_model=True,
              load_opt=False if prev_run_name in blacklist or is_freezeD or not is_train else True,
              load_misc=False,
              is_freezeD=is_freezeD)

    seed, prev_run_name, step, epoch, topk, aa_p, best_step, best_fid, best_ckpt_path, lecam_emas =\
        load_ckpt(model=Dis,
                  optimizer=d_optimizer,
                  ckpt_path=Dis_ckpt_path,
                  load_model=True,
                  load_opt=False if prev_run_name in blacklist or is_freezeD or not is_train else True,
                  load_misc=True,
                  is_freezeD=is_freezeD)

    if apply_g_ema:
        z = join(ckpt_dir, "model=G_ema-{when}-weights-step=".format(when=when))
        Gen_ema_ckpt_path = glob.glob(glob.escape(z) + '*.pth')[0]
        load_ckpt(model=Gen_ema,
                  optimizer=None,
                  ckpt_path=Gen_ema_ckpt_path,
                  load_model=True,
                  load_opt=False,
                  load_misc=False,
                  is_freezeD=is_freezeD)

        ema.source, ema.target = Gen, Gen_ema

    if is_train and RUN.seed != seed:
        RUN.seed = seed + global_rank
        misc.fix_seed(RUN.seed)

    if device == 0:
        if not is_freezeD:
            logger = log.make_logger(RUN.save_dir, prev_run_name, None)

        logger.info("Generator checkpoint is {}".format(Gen_ckpt_path))
        if apply_g_ema:
            logger.info("EMA_Generator checkpoint is {}".format(Gen_ema_ckpt_path))
        logger.info("Discriminator checkpoint is {}".format(Dis_ckpt_path))

    if is_freezeD:
        prev_run_name, step, epoch, topk, aa_p, best_step, best_fid, best_ckpt_path =\
            run_name, 0, 0, "initialize", None, 0, None, None
    return prev_run_name, step, epoch, topk, aa_p, best_step, best_fid, best_ckpt_path, lecam_emas, logger


def load_best_model(ckpt_dir, Gen, Dis, apply_g_ema, Gen_ema, ema):
    Gen, Dis, Gen_ema = misc.peel_models(Gen, Dis, Gen_ema)
    Gen_glob = glob.glob(join(ckpt_dir, "model=G-best-weights-step*.pth"))
    Dis_glob = glob.glob(join(ckpt_dir, "model=D-best-weights-step*.pth"))
    if not Gen_glob and not Dis_glob and apply_g_ema:
        z_glob = glob.glob(join(ckpt_dir, "model=G_ema-best-weights-step*.pth"))
        if not z_glob:
            z_glob = glob.glob(join(ckpt_dir, "model=G_ema-current-weights-step*.pth"))
        if z_glob:
            Gen_ema_ckpt_path = sorted(z_glob)[-1]
            print("[ckpt] G/D best ckpts missing -> G_ema-only inference load:", Gen_ema_ckpt_path)
            os.environ.setdefault("STUDIOGAN_LOAD_STRICT", "0")
            load_ckpt(model=Gen_ema, optimizer=None, ckpt_path=Gen_ema_ckpt_path,
                      load_model=True, load_opt=False, load_misc=False)
            try:
                load_ckpt(model=Gen, optimizer=None, ckpt_path=Gen_ema_ckpt_path,
                          load_model=True, load_opt=False, load_misc=False)
            except Exception as _e:
                print("[ckpt] (skipping Gen mirror; not needed for sampling):", _e)
            ema.source, ema.target = Gen, Gen_ema
            try:
                step_str = os.path.basename(Gen_ema_ckpt_path).split("step=")[1].split(".")[0]
                return int(step_str)
            except Exception:
                return 0
    Gen_ckpt_path = Gen_glob[0]
    Dis_ckpt_path = Dis_glob[0]

    load_ckpt(model=Gen,
              optimizer=None,
              ckpt_path=Gen_ckpt_path,
              load_model=True,
              load_opt=False,
              load_misc=False,
              is_freezeD=False)


    _, _, _, _, _, _, best_step, _, _, _ = load_ckpt(model=Dis,
                                                  optimizer=None,
                                                  ckpt_path=Dis_ckpt_path,
                                                  load_model=True,
                                                  load_opt=False,
                                                  load_misc=True,
                                                  is_freezeD=False)

    if apply_g_ema:
        Gen_ema_ckpt_path = glob.glob(join(ckpt_dir, "model=G_ema-best-weights-step*.pth"))[0]
        load_ckpt(model=Gen_ema,
                  optimizer=None,
                  ckpt_path=Gen_ema_ckpt_path,
                  load_model=True,
                  load_opt=False,
                  load_misc=False,
                  is_freezeD=False)

        ema.source, ema.target = Gen, Gen_ema
    return best_step


def load_prev_dict(directory, file_name):
    return np.load(join(directory, file_name), allow_pickle=True).item()


def check_is_pre_trained_model(ckpt_dir, GAN_train, GAN_test):
    assert GAN_train*GAN_test == 0, "cannot conduct GAN_train and GAN_test togather."
    if GAN_train:
        mode = "fake_trained"
    else:
        mode = "real_trained"

    ckpt_list = glob.glob(join(ckpt_dir, "model=C-{mode}-best-weights.pth".format(mode=mode)))
    if len(ckpt_list) == 0:
        is_pre_train_model = False
    else:
        is_pre_train_model = True
    return is_pre_train_model, mode


def load_GAN_train_test_model(model, mode, optimizer, RUN):
    ckpt_path = join(RUN.ckpt_dir, "model=C-{mode}-best-weights.pth".format(mode=mode))
    ckpt = torch.load(ckpt_path, map_location=lambda storage, loc: storage, weights_only=False)

    model.load_state_dict(ckpt["state_dict"])
    optimizer.load_state_dict(ckpt["optimizer"])
    epoch_trained = ckpt["epoch"]
    best_top1 = ckpt["best_top1"]
    best_top5 = ckpt["best_top5"]
    best_epoch = ckpt["best_epoch"]
    return epoch_trained, best_top1, best_top5, best_epoch