File size: 8,671 Bytes
e17e8cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""This code is taken from <https://github.com/alexandre01/deepsvg>
by Alexandre Carlier, Martin Danelljan, Alexandre Alahi and Radu Timofte
from the paper >https://arxiv.org/pdf/2007.11301.pdf>
"""

import shutil
import torch
import torch.nn as nn
import os
import random
import numpy as np
import glob


def save_ckpt(checkpoint_dir, model, cfg=None, optimizer=None, scheduler_lr=None, scheduler_warmup=None,
              stats=None, train_vars=None):
    if is_multi_gpu(model):
        model = model.module

    state = {
        "model": model.state_dict()
    }

    if optimizer is not None:
        state["optimizer"] = optimizer.state_dict()
    if scheduler_lr is not None:
        state["scheduler_lr"] = scheduler_lr.state_dict()
    if scheduler_warmup is not None:
        state["scheduler_warmup"] = scheduler_warmup.state_dict()
    if cfg is not None:
        state["cfg"] = cfg.to_dict()
    if stats is not None:
        state["stats"] = stats.to_dict()
    if train_vars is not None:
        state["train_vars"] = train_vars.to_dict()

    checkpoint_path = os.path.join(checkpoint_dir, "{:06d}.pth.tar".format(stats.step))

    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
    torch.save(state, checkpoint_path)

    if stats.is_best():
        best_model_path = os.path.join(checkpoint_dir, "best.pth.tar")
        shutil.copyfile(checkpoint_path, best_model_path)


def save_ckpt_list(checkpoint_dir, model, cfg=None, optimizers=None, scheduler_lrs=None, scheduler_warmups=None,
              stats=None, train_vars=None):
    if is_multi_gpu(model):
        model = model.module

    state = {
        "model": model.state_dict()
    }

    if optimizers is not None:
        state["optimizers"] = [optimizer.state_dict() if optimizer is not None else optimizer for optimizer in optimizers]
    if scheduler_lrs is not None:
        state["scheduler_lrs"] = [scheduler_lr.state_dict() if scheduler_lr is not None else scheduler_lr for scheduler_lr in scheduler_lrs]
    if scheduler_warmups is not None:
        state["scheduler_warmups"] = [scheduler_warmup.state_dict() if scheduler_warmup is not None else None for scheduler_warmup in scheduler_warmups]
    if cfg is not None:
        state["cfg"] = cfg.to_dict()
    if stats is not None:
        state["stats"] = stats.to_dict()
    if train_vars is not None:
        state["train_vars"] = train_vars.to_dict()

    checkpoint_path = os.path.join(checkpoint_dir, "{:06d}.pth.tar".format(stats.step))

    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
    torch.save(state, checkpoint_path)

    if stats.is_best():
        best_model_path = os.path.join(checkpoint_dir, "best.pth.tar")
        shutil.copyfile(checkpoint_path, best_model_path)


def load_ckpt(checkpoint_dir, model, cfg=None, optimizer=None, scheduler_lr=None, scheduler_warmup=None,
              stats=None, train_vars=None):
    if not os.path.exists(checkpoint_dir):
        return False

    if os.path.isfile(checkpoint_dir):
        checkpoint_path = checkpoint_dir
    else:
        ckpts_paths = sorted(glob.glob(os.path.join(checkpoint_dir, "./[0-9]*.pth.tar")))
        if not ckpts_paths:
            return False
        checkpoint_path = ckpts_paths[-1]

    state = torch.load(checkpoint_path, map_location=torch.device('cpu'))

    if is_multi_gpu(model):
        model = model.module
    model.load_state_dict(state["model"], strict=False)

    if optimizer is not None:
        optimizer.load_state_dict(state["optimizer"])
    if scheduler_lr is not None:
        scheduler_lr.load_state_dict(state["scheduler_lr"])
    if scheduler_warmup is not None:
        scheduler_warmup.load_state_dict(state["scheduler_warmup"])
    if cfg is not None:
        cfg.load_dict(state["cfg"])
    if stats is not None:
        stats.load_dict(state["stats"])
    if train_vars is not None:
        train_vars.load_dict(state["train_vars"])

    return True


def load_ckpt_list(checkpoint_dir, model, cfg=None, optimizers=None, scheduler_lrs=None, scheduler_warmups=None,
              stats=None, train_vars=None):
    if not os.path.exists(checkpoint_dir):
        return False

    if os.path.isfile(checkpoint_dir):
        checkpoint_path = checkpoint_dir
    else:
        ckpts_paths = sorted(glob.glob(os.path.join(checkpoint_dir, "./[0-9]*.pth.tar")))
        if not ckpts_paths:
            return False
        checkpoint_path = ckpts_paths[-1]

    state = torch.load(checkpoint_path, map_location=torch.device('cpu'))

    if is_multi_gpu(model):
        model = model.module
    model.load_state_dict(state["model"], strict=False)

    for optimizer, scheduler_lr, scheduler_warmup, optimizer_sd, scheduler_lr_sd, scheduler_warmups_sd in zip(optimizers, scheduler_lrs, scheduler_warmups, state["optimizers"], state["scheduler_lrs"], state["scheduler_warmups"]):
        if optimizer is not None and optimizer_sd is not None:
            optimizer.load_state_dict(optimizer_sd)
        if scheduler_lr is not None and scheduler_lr_sd is not None:
            scheduler_lr.load_state_dict(scheduler_lr_sd)
        if scheduler_warmup is not None and scheduler_warmups_sd is not None:
            scheduler_warmup.load_state_dict(scheduler_warmups_sd)
    if cfg is not None and state["cfg"] is not None:
        cfg.load_dict(state["cfg"])
    if stats is not None and state["stats"] is not None:
        stats.load_dict(state["stats"])
    if train_vars is not None and state["train_vars"] is not None:
        train_vars.load_dict(state["train_vars"])

    return True


def load_model(checkpoint_path, model):
    state = torch.load(checkpoint_path, map_location=torch.device('cpu'))

    if is_multi_gpu(model):
        model = model.module
    model.load_state_dict(state["model"], strict=False)


def is_multi_gpu(model):
    return isinstance(model, nn.DataParallel)


def count_parameters(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)


def pad_sequence(sequences, batch_first=False, padding_value=0, max_len=None):
    r"""Pad a list of variable length Tensors with ``padding_value``

    ``pad_sequence`` stacks a list of Tensors along a new dimension,
    and pads them to equal length. For example, if the input is list of
    sequences with size ``L x *`` and if batch_first is False, and ``T x B x *``
    otherwise.

    `B` is batch size. It is equal to the number of elements in ``sequences``.
    `T` is length of the longest sequence.
    `L` is length of the sequence.
    `*` is any number of trailing dimensions, including none.

    Example:
        >>> from torch.nn.utils.rnn import pad_sequence
        >>> a = torch.ones(25, 300)
        >>> b = torch.ones(22, 300)
        >>> c = torch.ones(15, 300)
        >>> pad_sequence([a, b, c]).size()
        torch.Size([25, 3, 300])

    Note:
        This function returns a Tensor of size ``T x B x *`` or ``B x T x *``
        where `T` is the length of the longest sequence. This function assumes
        trailing dimensions and type of all the Tensors in sequences are same.

    Arguments:
        sequences (list[Tensor]): list of variable length sequences.
        batch_first (bool, optional): output will be in ``B x T x *`` if True, or in
            ``T x B x *`` otherwise
        padding_value (float, optional): value for padded elements. Default: 0.

    Returns:
        Tensor of size ``T x B x *`` if :attr:`batch_first` is ``False``.
        Tensor of size ``B x T x *`` otherwise
    """

    # assuming trailing dimensions and type of all the Tensors
    # in sequences are same and fetching those from sequences[0]
    max_size = sequences[0].size()
    trailing_dims = max_size[1:]

    if max_len is None:
        max_len = max([s.size(0) for s in sequences])
    if batch_first:
        out_dims = (len(sequences), max_len) + trailing_dims
    else:
        out_dims = (max_len, len(sequences)) + trailing_dims

    out_tensor = sequences[0].data.new(*out_dims).fill_(padding_value)
    for i, tensor in enumerate(sequences):
        length = tensor.size(0)
        # use index notation to prevent duplicate references to the tensor
        if batch_first:
            out_tensor[i, :length, ...] = tensor
        else:
            out_tensor[:length, i, ...] = tensor

    return out_tensor


def set_seed(_seed=42):
    random.seed(_seed)
    np.random.seed(_seed)
    torch.manual_seed(_seed)
    torch.cuda.manual_seed(_seed)
    torch.cuda.manual_seed_all(_seed)
    os.environ['PYTHONHASHSEED'] = str(_seed)


def infinite_range(start_idx=0):
    while True:
        yield start_idx
        start_idx += 1