-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgn.py
304 lines (251 loc) · 12 KB
/
pgn.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
from pathlib import Path
import logging
import torch
from torch import nn
import torch.nn.functional as F
from archs.resnet_generator import ResnetGenerator
from archs.unet_custom import UNetCustom
from utils import \
mse_loss_batchwise, l1_loss_batchwise, logcosh_loss_batchwise, \
MseLogitLossBatchwise, LogcoshLogitLossBatchwise, \
Normalizer
def read_checkpoint(checkpoint_path: Path, backbone_type):
logging.debug(f'Loading PGN checkpoint from {checkpoint_path}...')
checkpoint = torch.load(checkpoint_path, map_location='cpu')
checkpoint_version = checkpoint.get('version', 0)
logging.debug(f'Loaded PGN checkpoint with version {checkpoint_version}')
logging.debug(f'Fields that are not Tensors: {[k for k, v in checkpoint.items() if not isinstance(v, torch.Tensor)]}')
if checkpoint_version == 0:
backbone_params_saved = {k: v for k, v in checkpoint.items() if not isinstance(v, torch.Tensor)}
backbone_to_grad_params_saved = {}
if 'grad_scale' in backbone_params_saved:
grad_scale = backbone_params_saved.pop('grad_scale')
if backbone_type == 'unet' and grad_scale == 1e-3:
# The constant 48 is due to the fact that I used to not have batch reduction in the MSE gradient
# computation, and I have a whole lot of checkpoints trained with this batch size.
grad_scale = 0.048
backbone_to_grad_params_saved['grad_scale'] = grad_scale
return {
'state_dict': {k: v for k, v in checkpoint.items() if isinstance(v, torch.Tensor)},
'backbone_type': None,
'backbone_params': backbone_params_saved,
'backbone_to_grad_type': None,
'backbone_to_grad_params': backbone_to_grad_params_saved,
}
elif checkpoint_version == 1:
return {
'state_dict': checkpoint['state_dict'],
'backbone_type': None,
'backbone_params': checkpoint['params'],
'backbone_to_grad_type': None,
'backbone_to_grad_params': {},
}
elif checkpoint_version == 2:
return checkpoint
else:
raise RuntimeError(f"Checkpoint version {checkpoint_version} is not supported.")
class Pgn(nn.Module):
def __init__(self, normalizer=None,
backbone_type=None, backbone_params=None,
backbone_to_grad_type=None, backbone_to_grad_params=None,
ignore_grad_scale_mismatch=False,
checkpoint_path=None):
super().__init__()
if normalizer is None:
normalizer = Normalizer.make('vgg')
if backbone_params is None:
backbone_params = {}
if backbone_to_grad_params is None:
backbone_to_grad_params = {}
logging.debug('Args contain the following parameters:\n' + '\n'.join([
f' backbone_type: {backbone_type}',
f' backbone_params: {backbone_params}',
f' backbone_to_grad_type: {backbone_to_grad_type}',
f' backbone_to_grad_params: {backbone_to_grad_params}',
]))
model_state_dict = None
if checkpoint_path is not None:
checkpoint = read_checkpoint(checkpoint_path, backbone_type)
logging.debug('Read checkpoint with the following parameters:\n' + '\n'.join([
f' backbone_type: {checkpoint["backbone_type"]}',
f' backbone_params: {checkpoint["backbone_params"]}',
f' backbone_to_grad_type: {checkpoint["backbone_to_grad_type"]}',
f' backbone_to_grad_params: {checkpoint["backbone_to_grad_params"]}',
]))
if backbone_type is None:
backbone_type = checkpoint['backbone_type']
elif checkpoint['backbone_type'] is not None:
assert backbone_type == checkpoint['backbone_type'], (backbone_type, checkpoint['backbone_type'])
if backbone_to_grad_type is None:
backbone_to_grad_type = checkpoint['backbone_to_grad_type']
elif checkpoint['backbone_to_grad_type'] is not None:
assert backbone_to_grad_type == checkpoint['backbone_to_grad_type'], (backbone_to_grad_type, checkpoint['backbone_to_grad_type'])
for key in (set(checkpoint['backbone_params'].keys()) & set(backbone_params)):
value_ckpt = checkpoint['backbone_params'][key]
value_args = backbone_params[key]
assert value_args == value_ckpt, (key, value_args, value_ckpt)
backbone_params.update(checkpoint['backbone_params'])
for key in (set(checkpoint['backbone_to_grad_params'].keys()) & set(backbone_to_grad_params)):
value_ckpt = checkpoint['backbone_to_grad_params'][key]
value_args = backbone_to_grad_params[key]
if key == 'grad_scale' and value_args != value_ckpt and ignore_grad_scale_mismatch:
logging.warning(f'grad_scale mismatch: provided {value_args}, but checkpoint has {value_ckpt}')
checkpoint['backbone_to_grad_params'].pop('grad_scale') # safe since we're iterating over a copy
else:
assert value_args == value_ckpt, (key, value_args, value_ckpt)
backbone_to_grad_params.update(checkpoint['backbone_to_grad_params'])
logging.debug('Final checkpoint parameters:\n' + '\n'.join([
f' backbone_type: {backbone_type}',
f' backbone_params: {backbone_params}',
f' backbone_to_grad_type: {backbone_to_grad_type}',
f' backbone_to_grad_params: {backbone_to_grad_params}',
]))
model_state_dict = checkpoint['state_dict']
assert backbone_type is not None
assert backbone_to_grad_type is not None
self.backbone = {
'unet': UNetCustom,
'resnet': ResnetGenerator,
}[backbone_type](**backbone_params)
proxy_type = backbone_to_grad_params['type']
proxy_params = backbone_to_grad_params[proxy_type]
make_proxy = {
'raw': ProxyRaw,
'sigmoid': ProxyAsSigmoid,
'warped_target': ProxyAsWarpedTarget,
}[proxy_type](normalizer, **proxy_params)
if backbone_to_grad_type == 'direct':
self.backbone_to_grad = PgnPredictGrad(
make_proxy,
backbone_to_grad_params['out_scale'],
backbone_to_grad_params['grad_scale'],
)
elif backbone_to_grad_type == 'proxy':
batchwise_loss_func = {
'mse': mse_loss_batchwise,
'l1': l1_loss_batchwise,
'logcosh': logcosh_loss_batchwise,
'mse_logit': MseLogitLossBatchwise(normalizer),
'logcosh_logit': LogcoshLogitLossBatchwise(normalizer),
}[backbone_to_grad_params['grad_type']]
self.backbone_to_grad = PgnProxyToGrad(
make_proxy,
batchwise_loss_func,
backbone_to_grad_params['grad_scale'],
)
else:
assert False
self.backbone_type = backbone_type
self.backbone_params = backbone_params
self.backbone_to_grad_type = backbone_to_grad_type
self.backbone_to_grad_params = backbone_to_grad_params
if model_state_dict is not None:
self.backbone.load_state_dict(model_state_dict)
def forward(self, input, target, **kwargs):
pred = self.backbone(input, target, **kwargs)
pred.update(self.backbone_to_grad(pred['out'], input, target))
return pred
def get_checkpoint(self):
return {
'state_dict': self.backbone.state_dict(),
'backbone_type': self.backbone_type,
'backbone_params': self.backbone_params,
'backbone_to_grad_type': self.backbone_to_grad_type,
'backbone_to_grad_params': self.backbone_to_grad_params,
'version': 2,
}
class PgnPredictGrad(nn.Module):
def __init__(self, make_proxy, out_scale=None, grad_scale=None):
super().__init__()
self.make_proxy = make_proxy
self.out_scale = out_scale
self.grad_scale = grad_scale
def forward(self, out, input, target):
if self.out_scale is not None and self.out_scale != 1.0:
out = out / self.out_scale
result = self.make_proxy(input - out, target)
proxy = result['proxy']
_, c, h, w = input.shape
grad = input - proxy # MSE gradient
grad_coef = 2 / (c * h * w)
if self.grad_scale is not None and self.grad_scale != 1.0:
grad_coef /= self.grad_scale
grad = grad * grad_coef
result['grad'] = grad
return result
class PgnProxyToGrad(nn.Module):
def __init__(self, make_proxy, batchwise_loss_func, grad_scale=None):
super().__init__()
self.make_proxy = make_proxy
self.batchwise_loss_func = batchwise_loss_func
self.grad_scale = grad_scale
def forward(self, out, input, target):
result = self.make_proxy(out, target)
proxy = result['proxy']
with torch.enable_grad():
input_ = input.detach().requires_grad_()
batchwise_loss = self.batchwise_loss_func(proxy, input_)
grad = torch.autograd.grad(batchwise_loss.sum(dim=0), input_, create_graph=True)[0]
if self.grad_scale is not None and self.grad_scale != 1.0:
grad = grad / self.grad_scale
result['grad'] = grad
return result
class ProxyRaw(nn.Module):
def __init__(self, normalizer):
super().__init__()
pass
def forward(self, out, target):
return {
'proxy': out,
}
class ProxyAsSigmoid(nn.Module):
def __init__(self, normalizer, scale: float):
super().__init__()
self.normalizer = normalizer
self.scale = scale
def forward(self, out, target):
return {
'proxy': self.scale * self.normalizer(torch.sigmoid(out)),
}
class ProxyAsWarpedTarget(nn.Module):
def __init__(self, normalizer, scale: float, additive: bool, downscale_by: float, additive_scale: float):
super().__init__()
self.normalizer = normalizer
self.scale = scale
self.use_additive = additive
self.downscale_by = downscale_by
self.additive_scale = additive_scale
def forward(self, out, target):
b, _, h, w = target.shape
grid_identity = torch.stack(torch.meshgrid(
torch.linspace(-1, 1, h, device=target.device, dtype=target.dtype),
torch.linspace(-1, 1, w, device=target.device, dtype=target.dtype),
)[::-1], dim=-1).unsqueeze(0).repeat(b, 1, 1, 1)
if self.downscale_by is not None:
ch, cw = out.shape[2:]
nh, nw = round(ch / self.downscale_by), round(cw / self.downscale_by)
out = F.interpolate(out, (nh, nw), mode='bilinear', align_corners=False)
ch, cw = out.shape[2:]
if (ch, cw) != (h, w):
out = F.interpolate(out, (h, w), mode='bilinear', align_corners=False)
if self.use_additive:
grid_correction = out[:, :2, :, :]
additive = out[:, 2:, :, :]
additive = self.additive_scale * self.normalizer(torch.sigmoid(additive))
else:
grid_correction = out
grid_correction = self.scale * torch.tanh(grid_correction)
grid_correction = grid_correction.permute(0, 2, 3, 1)
grid = torch.clamp(grid_identity + grid_correction, min=-1, max=1)
proxy = F.grid_sample(target, grid, align_corners=False)
if self.use_additive:
proxy = proxy + additive
# Here, I could choose to clamp the proxy to the range of the target image,
# but I don't do that for the same reason as introducing --pgn-proxy-sigmoid-scale.
result = {
'proxy': proxy,
'grid': grid_correction,
}
if self.use_additive:
result['additive'] = additive
return result