/
averaged_model.py
322 lines (279 loc) · 14.5 KB
/
averaged_model.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright (c) OpenMMLab. All rights reserved.
import itertools
import warnings
from typing import List, Optional
import torch
import torch.nn as nn
from mmengine.model import BaseAveragedModel
from torch import Tensor
from mmgen.registry import MODELS
# NOTICE: Since mmengine do not support loading ``state_dict`` without wrap
# ema module with ``BaseAveragedModel`` currently, we rewrite
# ``ExponentialMovingAverage`` and add ``_load_from_state_dict`` temporarily
@MODELS.register_module()
class ExponentialMovingAverage(BaseAveragedModel):
r"""Implements the exponential moving average (EMA) of the model.
All parameters are updated by the formula as below:
.. math::
Xema_{t+1} = (1 - momentum) * Xema_{t} + momentum * X_t
Args:
model (nn.Module): The model to be averaged.
momentum (float): The momentum used for updating ema parameter.
Defaults to 0.0002.
Ema's parameter are updated with the formula
:math:`averaged\_param = (1-momentum) * averaged\_param +
momentum * source\_param`.
interval (int): Interval between two updates. Defaults to 1.
device (torch.device, optional): If provided, the averaged model will
be stored on the :attr:`device`. Defaults to None.
update_buffers (bool): if True, it will compute running averages for
both the parameters and the buffers of the model. Defaults to
False.
""" # noqa: W605
def __init__(self,
model: nn.Module,
momentum: float = 0.0002,
interval: int = 1,
device: Optional[torch.device] = None,
update_buffers: bool = False) -> None:
super().__init__(model, interval, device, update_buffers)
assert 0.0 < momentum < 1.0, 'momentum must be in range (0.0, 1.0)'\
f'but got {momentum}'
self.momentum = momentum
def avg_func(self, averaged_param: Tensor, source_param: Tensor,
steps: int) -> None:
"""Compute the moving average of the parameters using exponential
moving average.
Args:
averaged_param (Tensor): The averaged parameters.
source_param (Tensor): The source parameters.
steps (int): The number of times the parameters have been
updated.
"""
averaged_param.mul_(1 - self.momentum).add_(
source_param, alpha=self.momentum)
def _load_from_state_dict(self, state_dict: dict, prefix: str,
local_metadata: dict, strict: bool,
missing_keys: list, unexpected_keys: list,
error_msgs: List[str]) -> None:
"""Overrides ``nn.Module._load_from_state_dict`` to support loading
``state_dict`` without wrap ema module with ``BaseAveragedModel``.
In OpenMMLab 1.0, model will not wrap ema submodule with
``BaseAveragedModel``, and the ema weight key in `state_dict` will
miss `module` prefix. Therefore, ``BaseAveragedModel`` need to
automatically add the ``module`` prefix if the corresponding key in
``state_dict`` misses it.
Args:
state_dict (dict): A dict containing parameters and
persistent buffers.
prefix (str): The prefix for parameters and buffers used in this
module
local_metadata (dict): a dict containing the metadata for this
module.
strict (bool): Whether to strictly enforce that the keys in
:attr:`state_dict` with :attr:`prefix` match the names of
parameters and buffers in this module
missing_keys (List[str]): if ``strict=True``, add missing keys to
this list
unexpected_keys (List[str]): if ``strict=True``, add unexpected
keys to this list
error_msgs (List[str]): error messages should be added to this
list, and will be reported together in
:meth:`~torch.nn.Module.load_state_dict`.
"""
for key, value in list(state_dict.items()):
# To support load the pretrained model, which does not wrap ema
# module with `BaseAveragedModel`, `BaseAveragedModel` will
# automatically add `module` prefix to the `state_dict` which
# key starts with the custom prefix. For example, the old
# checkpoint with `state_dict` with keys:
# ['layer.weight', 'layer.bias', 'ema.steps', 'ema.weight', 'ema.bias'] # noqa: E501
# will be replaced with:
# ['layer.weight', 'layer.bias', 'ema.steps', 'ema.module.weight', 'ema.module.bias'] # noqa: E501
# The key added with `module` prefix needs to satisfy
# three conditions.
# 1. key starts with current prefix, such as `model.ema`.
# 2. The content after the prefix does not start with the `module`
# 3. Key does not end with steps.
if key.startswith(prefix) and not key[len(prefix):].startswith(
'module') and not key.endswith('steps'):
new_key = key[:len(prefix)] + 'module.' + key[len(prefix):]
state_dict[new_key] = value
state_dict.pop(key)
state_dict.setdefault(prefix + 'steps', torch.tensor(0))
super()._load_from_state_dict(state_dict, prefix, local_metadata,
strict, missing_keys, unexpected_keys,
error_msgs)
def sync_buffers(self, model: nn.Module) -> None:
"""Copy buffer from model to averaged model.
Args:
model (nn.Module): The model whose parameters will be averaged.
"""
# if not update buffer, copy buffer from orig model
if self.update_buffers:
warnings.warn(
'`update_buffers` is set to True in this ema model, and '
'buffers will be updated in `update_parameters`.')
avg_buffer = itertools.chain(self.module.buffers())
orig_buffer = itertools.chain(model.buffers())
for b_avg, b_orig in zip(avg_buffer, orig_buffer):
b_avg.data.copy_(b_orig.data)
def sync_parameters(self, model: nn.Module) -> None:
"""Copy buffer and parameters from model to averaged model.
Args:
model (nn.Module): The model whose parameters will be averaged.
"""
# before ema, copy weights from orig
avg_param = (
itertools.chain(self.module.parameters(), self.module.buffers()))
src_param = (itertools.chain(model.parameters(), model.buffers()))
for p_avg, p_src in zip(avg_param, src_param):
p_avg.data.copy_(p_src.data)
@MODELS.register_module()
class RampUpEMA(BaseAveragedModel):
r"""Implements the exponential moving average with ramping up momentum.
Ref: https://github.com/NVlabs/stylegan3/blob/master/training/training_loop.py # noqa
Args:
model (nn.Module): The model to be averaged.
interval (int): Interval between two updates. Defaults to 1.
ema_kimg (int, optional): EMA kimgs. Defaults to 10.
ema_rampup (float, optional): Ramp up rate. Defaults to 0.05.
batch_size (int, optional): Global batch size. Defaults to 32.
eps (float, optional): Ramp up epsilon. Defaults to 1e-8.
start_iter (int, optional): EMA start iter. Defaults to 0.
device (torch.device, optional): If provided, the averaged model will
be stored on the :attr:`device`. Defaults to None.
update_buffers (bool): if True, it will compute running averages for
both the parameters and the buffers of the model. Defaults to
False.
""" # noqa: W605
def __init__(self,
model: nn.Module,
interval: int = 1,
ema_kimg: int = 10,
ema_rampup: float = 0.05,
batch_size: int = 32,
eps: float = 1e-8,
start_iter: int = 0,
device: Optional[torch.device] = None,
update_buffers: bool = False) -> None:
"""_summary_"""
super().__init__(model, interval, device, update_buffers)
self.interval = interval
self.ema_kimg = ema_kimg
self.ema_rampup = ema_rampup
self.batch_size = batch_size
self.eps = eps
@staticmethod
def rampup(steps, ema_kimg=10, ema_rampup=0.05, batch_size=4, eps=1e-8):
"""Ramp up ema momentum.
Ref: https://github.com/NVlabs/stylegan3/blob/a5a69f58294509598714d1e88c9646c3d7c6ec94/training/training_loop.py#L300-L308 # noqa
Args:
steps:
ema_kimg (int, optional): Half-life of the exponential moving
average of generator weights. Defaults to 10.
ema_rampup (float, optional): EMA ramp-up coefficient.If set to
None, then rampup will be disabled. Defaults to 0.05.
batch_size (int, optional): Total batch size for one training
iteration. Defaults to 4.
eps (float, optional): Epsiolon to avoid ``batch_size`` divided by
zero. Defaults to 1e-8.
Returns:
dict: Updated momentum.
"""
cur_nimg = (steps + 1) * batch_size
ema_nimg = ema_kimg * 1000
if ema_rampup is not None:
ema_nimg = min(ema_nimg, cur_nimg * ema_rampup)
ema_beta = 0.5**(batch_size / max(ema_nimg, eps))
return ema_beta
def avg_func(self, averaged_param: Tensor, source_param: Tensor,
steps: int) -> None:
"""Compute the moving average of the parameters using exponential
moving average.
Args:
averaged_param (Tensor): The averaged parameters.
source_param (Tensor): The source parameters.
steps (int): The number of times the parameters have been
updated.
"""
momentum = self.rampup(self.steps, self.ema_kimg, self.ema_rampup,
self.batch_size, self.eps)
averaged_param.mul_(1 - momentum).add_(source_param, alpha=momentum)
def _load_from_state_dict(self, state_dict: dict, prefix: str,
local_metadata: dict, strict: bool,
missing_keys: list, unexpected_keys: list,
error_msgs: List[str]) -> None:
"""Overrides ``nn.Module._load_from_state_dict`` to support loading
``state_dict`` without wrap ema module with ``BaseAveragedModel``.
In OpenMMLab 1.0, model will not wrap ema submodule with
``BaseAveragedModel``, and the ema weight key in `state_dict` will
miss `module` prefix. Therefore, ``BaseAveragedModel`` need to
automatically add the ``module`` prefix if the corresponding key in
``state_dict`` misses it.
Args:
state_dict (dict): A dict containing parameters and
persistent buffers.
prefix (str): The prefix for parameters and buffers used in this
module
local_metadata (dict): a dict containing the metadata for this
module.
strict (bool): Whether to strictly enforce that the keys in
:attr:`state_dict` with :attr:`prefix` match the names of
parameters and buffers in this module
missing_keys (List[str]): if ``strict=True``, add missing keys to
this list
unexpected_keys (List[str]): if ``strict=True``, add unexpected
keys to this list
error_msgs (List[str]): error messages should be added to this
list, and will be reported together in
:meth:`~torch.nn.Module.load_state_dict`.
"""
for key, value in list(state_dict.items()):
# To support load the pretrained model, which does not wrap ema
# module with `BaseAveragedModel`, `BaseAveragedModel` will
# automatically add `module` prefix to the `state_dict` which
# key starts with the custom prefix. For example, the old
# checkpoint with `state_dict` with keys:
# ['layer.weight', 'layer.bias', 'ema.steps', 'ema.weight', 'ema.bias'] # noqa: E501
# will be replaced with:
# ['layer.weight', 'layer.bias', 'ema.steps', 'ema.module.weight', 'ema.module.bias'] # noqa: E501
# The key added with `module` prefix needs to satisfy
# three conditions.
# 1. key starts with current prefix, such as `model.ema`.
# 2. The content after the prefix does not start with the `module`
# 3. Key does not end with steps.
if key.startswith(prefix) and not key[len(prefix):].startswith(
'module') and not key.endswith('steps'):
new_key = key[:len(prefix)] + 'module.' + key[len(prefix):]
state_dict[new_key] = value
state_dict.pop(key)
state_dict.setdefault(prefix + 'steps', torch.tensor(0))
super()._load_from_state_dict(state_dict, prefix, local_metadata,
strict, missing_keys, unexpected_keys,
error_msgs)
def sync_buffers(self, model: nn.Module) -> None:
"""Copy buffer from model to averaged model.
Args:
model (nn.Module): The model whose parameters will be averaged.
"""
# if not update buffer, copy buffer from orig model
if self.update_buffers:
warnings.warn(
'`update_buffers` is set to True in this ema model, and '
'buffers will be updated in `update_parameters`.')
avg_buffer = itertools.chain(self.module.buffers())
orig_buffer = itertools.chain(model.buffers())
for b_avg, b_orig in zip(avg_buffer, orig_buffer):
b_avg.data.copy_(b_orig.data)
def sync_parameters(self, model: nn.Module) -> None:
"""Copy buffer and parameters from model to averaged model.
Args:
model (nn.Module): The model whose parameters will be averaged.
"""
# before ema, copy weights from orig
avg_param = (
itertools.chain(self.module.parameters(), self.module.buffers()))
src_param = (itertools.chain(model.parameters(), model.buffers()))
for p_avg, p_src in zip(avg_param, src_param):
p_avg.data.copy_(p_src.data)