-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
327 lines (265 loc) · 15.2 KB
/
engine.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
323
324
325
326
327
import copy
import math
import pytorch_lightning as pl
import torch
import torch.nn as nn
from pytorch_lightning.utilities import rank_zero_only
from transformers import DetrConfig, DetrForObjectDetection
from torchmetrics import Accuracy
from torchvision import transforms
from unittest import result
from utils import CocoEvaluator
class Detr(pl.LightningModule):
def __init__(self, args, dataset_val_coco, feature_extractor):
super().__init__()
if args.mscoco_pretrained:
self.model = DetrForObjectDetection.from_pretrained(
"facebook/detr-resnet-50",
num_labels=args.num_labels,
ignore_mismatched_sizes=True
)
else:
self.source_model = DetrForObjectDetection.from_pretrained(
"facebook/detr-resnet-50",
num_labels=args.num_labels,
ignore_mismatched_sizes=True
)
self.model = DetrForObjectDetection(DetrConfig())
self.model.config.num_labels = args.num_labels
self.model.class_labels_classifier = copy.deepcopy(self.source_model.class_labels_classifier)
self.model.model.backbone.load_state_dict(self.source_model.model.backbone.state_dict())
self.source_model = None
if args.encoder_init_ckpt != 'none':
encoder_state_dict = torch.load(args.encoder_init_ckpt)['state_dict']
encoder_state_dict_backbone = {'.'.join(k.split('.')[2:]):v for k,v in encoder_state_dict.items() if k.split('.')[1] == 'backbone'}
encoder_state_dict_input_projection= {'.'.join(k.split('.')[2:]):v for k,v in encoder_state_dict.items() if k.split('.')[1] == 'input_projection'}
encoder_state_dict_encoder = {'.'.join(k.split('.')[2:]):v for k,v in encoder_state_dict.items() if k.split('.')[1] == 'encoder'}
self.model.model.backbone.load_state_dict(encoder_state_dict_backbone)
self.model.model.input_projection.load_state_dict(encoder_state_dict_input_projection)
self.model.model.encoder.load_state_dict(encoder_state_dict_encoder)
print(f'Weights loaded from {args.encoder_init_ckpt} for backbone, input_projection and encoder')
if args.freeze_backbone:
for n, p in self.model.named_parameters():
if "backbone" in n:
p.requires_grad_(False)
if args.freeze_encoder:
for n, p in self.model.named_parameters():
if "encoder" in n:
p.requires_grad_(False)
self.min_image_size = args.min_image_size
self.max_image_size = args.max_image_size
self.lr = args.lr
self.lr_backbone = args.lr_backbone
self.weight_decay = args.weight_decay
self.dataset_val_coco = dataset_val_coco
self.coco_evaluator = CocoEvaluator(self.dataset_val_coco, ['bbox'])
self.feature_extractor = feature_extractor
self.ssl_patch_size = args.ssl_patch_size
self.ssl_task = args.ssl_task
self.ssl_task_ratio = args.ssl_task_ratio
self.ssl_loss_only_for_transformed = args.ssl_loss_only_for_transformed
self.ssl_loss_weight = args.ssl_loss_weight
if self.ssl_task == 'none':
self.return_dict = False
else:
self.return_dict = True
if args.ssl_task == 'jigsaw-discrete':
self.ssl_pre_prediction_head = nn.Sequential(
nn.Linear(self.model.config.d_model, self.model.config.d_model),
nn.ReLU(),
)
assert self.min_image_size == self.max_image_size
self.ssl_prediction_head = nn.Linear(self.model.config.d_model, (self.max_image_size//32)**2)
self.ssl_criterion = nn.CrossEntropyLoss(reduction='none')
self.ssl_metric = Accuracy()
elif args.ssl_task == 'mim-discrete':
self.ssl_pre_prediction_head = nn.Sequential(
nn.Linear(self.model.config.d_model, self.model.config.d_model),
nn.ReLU(),
)
self.ssl_prediction_head = nn.Linear(self.model.config.d_model, 8192)
self.ssl_criterion = nn.CrossEntropyLoss(reduction='none')
self.ssl_metric = Accuracy()
elif args.ssl_task == 'mim-continuous' or args.ssl_task == 'jigsaw-continuous':
#self.ssl_prediction_head = nn.Linear(self.model.config.d_model, 32*32*3)
self.ssl_pre_prediction_head = nn.Sequential(
nn.Linear(self.model.config.d_model, self.model.config.d_model),
nn.ReLU(),
)
self.ssl_prediction_head = nn.ConvTranspose2d(
self.model.config.d_model,
3,
kernel_size=(self.ssl_patch_size, self.ssl_patch_size),
stride=(self.ssl_patch_size, self.ssl_patch_size)
)
self.ssl_criterion = nn.L1Loss(reduction='none')
self.inv_trans = transforms.Compose([transforms.Normalize(mean = [ 0., 0., 0. ], std = [ 1/0.229, 1/0.224, 1/0.225 ]),
transforms.Normalize(mean = [ -0.485, -0.456, -0.406 ], std = [ 1., 1., 1. ])
])
def forward(self, pixel_values, pixel_mask):
outputs = self.model(pixel_values=pixel_values, pixel_mask=pixel_mask)
return outputs
def common_step(self, batch, batch_idx):
pixel_values = batch["pixel_values"]
pixel_mask = batch["pixel_mask"]
labels = [{k: v.to(self.device) for k, v in t.items()} for t in batch["labels"]]
outputs = self.model(pixel_values=pixel_values, pixel_mask=pixel_mask, labels=labels, return_dict=True)
loss, loss_dict = outputs.loss, outputs.loss_dict
if self.ssl_task != 'none':
ssl_detr_outputs = self.model(pixel_values=batch['ssl_patches'], pixel_mask=pixel_mask, labels=labels, return_dict=True)
encoder_output = ssl_detr_outputs.encoder_last_hidden_state # [batch_size, num_patches, hidden_dim]
if self.ssl_task == 'jigsaw-discrete' or self.ssl_task == 'mim-discrete':
ssl_output = self.ssl_pre_prediction_head(encoder_output) # [batch_size, num_patches, hidden_dim]
ssl_output = self.ssl_prediction_head(ssl_output) # [batch_size, num_patches, num_classes]
ssl_output = ssl_output.permute(0, 2, 1) # [batch_size, num_patches, num_classes] -> [batch_size, num_classes, num_patches]
ssl_loss = self.ssl_criterion(ssl_output, batch['ssl_target'])
ssl_pred = torch.argmax(ssl_output, dim=1) # [batch_size, num_patches]
elif self.ssl_task == 'jigsaw-continuous' or self.ssl_task == 'mim-continuous':
#ssl_output = self.ssl_prediction_head(encoder_output) # [batch_size, num_patches, num_pixel_locations]
ssl_output = self.ssl_pre_prediction_head(encoder_output) # [batch_size, num_patches, hidden_dim]
ssl_output = ssl_output.transpose(1, 2) # [batch_size, hidden_dim, num_patches]
num_patch_rows = int(math.sqrt(ssl_output.size()[2]))
ssl_output = ssl_output.unflatten(2, (num_patch_rows, num_patch_rows)) # [batch_size, hidden_dim, num_patch_rows(or)h, num_patch_cols(or)w]
ssl_output = self.ssl_prediction_head(ssl_output) # [batch_size, 3, H, W]
batch_size, C, H, W = ssl_output.shape
ssl_output = ssl_output.unfold(2, self.ssl_patch_size, self.ssl_patch_size).unfold(3, self.ssl_patch_size, self.ssl_patch_size) # [batch_size, 3, h, w, patch_size_h, patch_size_w]
ssl_output = ssl_output.contiguous().view(batch_size, C, -1, self.ssl_patch_size, self.ssl_patch_size) # [batch_size, 3, num_patches=hw, patch_size_h, patch_size_w]
ssl_output = ssl_output.permute(0, 2, 3, 4, 1) # [batch_size, num_patches=hw, patch_size_h, patch_size_w, 3]
_, num_patches, _, _, _ = ssl_output.shape
ssl_output = ssl_output.reshape(batch_size, num_patches, -1) # [batch_size, num_patches, patch_size*patch_size*3]
ssl_loss = self.ssl_criterion(ssl_output, batch['ssl_target']).mean(dim=2)
ssl_pred = ssl_output
else:
raise ValueError()
if self.ssl_loss_only_for_transformed:
ssl_loss = (ssl_loss * batch['ssl_patches_mask']).sum() / batch['ssl_patches_mask'].sum()
else:
ssl_loss = ssl_loss.mean()
else:
ssl_loss = 0
return loss, loss_dict, ssl_loss
def training_step(self, batch, batch_idx):
loss, loss_dict, ssl_loss = self.common_step(batch, batch_idx)
if self.ssl_task == "none":
total_loss = loss
else:
total_loss = loss + self.ssl_loss_weight * ssl_loss
self.log("train/loss", loss)
for k,v in loss_dict.items():
self.log("train/" + k, v.item())
self.log("train/ssl_loss", ssl_loss)
self.log("train/total_loss", total_loss)
return total_loss
def validation_step(self, batch, batch_idx):
# get the inputs
pixel_values = batch["pixel_values"].to(self.device)
pixel_mask = batch["pixel_mask"].to(self.device)
labels = [{k: v.to(self.device) for k, v in t.items()} for t in batch["labels"]]
# forward pass
outputs = self.model(pixel_values=pixel_values, pixel_mask=pixel_mask, labels=labels, return_dict=True)
loss, loss_dict = outputs.loss, outputs.loss_dict
# ssl loss
if self.ssl_task != 'none':
ssl_detr_outputs = self.model(pixel_values=batch['ssl_patches'], pixel_mask=pixel_mask, labels=labels, return_dict=True)
encoder_output = ssl_detr_outputs.encoder_last_hidden_state # [batch_size, num_patches, hidden_dim]
if self.ssl_task == 'jigsaw-discrete' or self.ssl_task == 'mim-discrete':
ssl_output = self.ssl_pre_prediction_head(encoder_output) # [batch_size, num_patches, hidden_dim]
ssl_output = self.ssl_prediction_head(ssl_output) # [batch_size, num_patches, num_classes]
ssl_output = ssl_output.permute(0, 2, 1) # [batch_size, num_patches, num_classes] -> [batch_size, num_classes, num_patches]
ssl_loss = self.ssl_criterion(ssl_output, batch['ssl_target'])
ssl_pred = torch.argmax(ssl_output, dim=1) # [batch_size, num_patches]
elif self.ssl_task == 'jigsaw-continuous' or self.ssl_task == 'mim-continuous':
#ssl_output = self.ssl_prediction_head(encoder_output) # [batch_size, num_patches, num_pixel_locations]
ssl_output = self.ssl_pre_prediction_head(encoder_output) # [batch_size, num_patches, hidden_dim]
ssl_output = ssl_output.transpose(1, 2) # [batch_size, hidden_dim, num_patches]
num_patch_rows = int(math.sqrt(ssl_output.size()[2]))
ssl_output = ssl_output.unflatten(2, (num_patch_rows, num_patch_rows)) # [batch_size, hidden_dim, num_patch_rows(or)h, num_patch_cols(or)w]
ssl_output = self.ssl_prediction_head(ssl_output) # [batch_size, 3, H, W]
batch_size, C, H, W = ssl_output.shape
ssl_output = ssl_output.unfold(2, self.ssl_patch_size, self.ssl_patch_size).unfold(3, self.ssl_patch_size, self.ssl_patch_size) # [batch_size, 3, h, w, patch_size_h, patch_size_w]
ssl_output = ssl_output.contiguous().view(batch_size, C, -1, self.ssl_patch_size, self.ssl_patch_size) # [batch_size, 3, num_patches=hw, patch_size_h, patch_size_w]
ssl_output = ssl_output.permute(0, 2, 3, 4, 1) # [batch_size, num_patches=hw, patch_size_h, patch_size_w, 3]
_, num_patches, _, _, _ = ssl_output.shape
ssl_output = ssl_output.reshape(batch_size, num_patches, -1) # [batch_size, num_patches, patch_size*patch_size*3]
ssl_loss = self.ssl_criterion(ssl_output, batch['ssl_target']).mean(dim=2) # [batch_size, num_patches]
ssl_pred = ssl_output
else:
raise ValueError()
if self.ssl_loss_only_for_transformed:
ssl_loss = (ssl_loss * batch['ssl_patches_mask']).sum() / batch['ssl_patches_mask'].sum()
else:
ssl_loss = ssl_loss.mean()
else:
ssl_loss = 0
if self.ssl_task == "none":
total_loss = loss
else:
total_loss = loss + self.ssl_loss_weight * ssl_loss
# logging
self.log("validation/loss", loss, sync_dist=True)
for k,v in loss_dict.items():
self.log("validation/" + k, v.item(), sync_dist=True)
self.log("validation/ssl_loss", ssl_loss)
self.log("validation/total_loss", total_loss)
orig_target_sizes = torch.stack([target["orig_size"] for target in labels], dim=0)
results = self.feature_extractor.post_process(outputs, orig_target_sizes) # convert outputs of model to COCO api
res = {target['image_id'].item(): output for target, output in zip(labels, results)}
self.coco_evaluator.update(res)
# ssl evaluation
if self.ssl_task.endswith('discrete'):
if self.ssl_loss_only_for_transformed:
mask = batch['ssl_patches_mask'] == 1
ssl_acc = self.ssl_metric(ssl_pred[mask], batch['ssl_target'][mask])
else:
ssl_acc = self.ssl_metric(ssl_pred, batch['ssl_target'])
self.log("validation/ssl_acc", ssl_acc)
elif self.ssl_task.endswith('continuous') and batch_idx == 0:
max_images = 8
input_imgs = self.inv_trans(batch['ssl_patches'][:max_images]) # [batch_size, 3, H, W]
self.logger.log_image("images/ssl_input", [x for x in input_imgs])
pred_imgs = ssl_pred[:max_images] # [batch_size, num_patches, patch_size*patch_size*3]
_, num_patches, num_patch_pixels = pred_imgs.shape
patch_size = int((num_patch_pixels // 3)**0.5)
height = int(num_patches**0.5)
pred_imgs = pred_imgs.reshape(max_images, num_patches, patch_size, patch_size, 3) # [batch_size, num_patches, patch_h, patch_w, 3]
pred_imgs = pred_imgs.reshape(max_images, height, height , patch_size, patch_size, 3) # [batch_size, h, w, patch_h, patch_w, 3]
pred_imgs = pred_imgs.permute(0, 5, 1, 3, 2, 4) # [batch_size, 3, h, patch_h, w, patch_w]
pred_imgs = pred_imgs.reshape(max_images, 3, patch_size*height, patch_size*height) # [batch_size, 3, H, W]
pred_imgs = self.inv_trans(pred_imgs)
self.logger.log_image("images/ssl_pred", [x for x in pred_imgs])
return total_loss
def validation_epoch_end(self, validation_step_outputs):
self.coco_evaluator.synchronize_between_processes()
self.coco_evaluator.accumulate()
self.coco_evaluator.summarize()
results = self.coco_evaluator.coco_eval['bbox'].stats
self.log("result/all_ap_50_95", results[0], sync_dist=True)
self.log("result/all_ap_50", results[1], sync_dist=True)
self.log("result/all_ap_75", results[2], sync_dist=True)
self.log("result/small_ap_50_95", results[3], sync_dist=True)
self.log("result/medium_ap_50_95", results[4], sync_dist=True)
self.log("result/large_ap_50_95", results[5], sync_dist=True)
self.log("result/all_ar_50_95_1d", results[6], sync_dist=True)
self.log("result/all_ar_50_95_10d", results[7], sync_dist=True)
self.log("result/all_ar_75_95", results[8], sync_dist=True)
self.log("result/small_ar_50_95", results[9], sync_dist=True)
self.log("result/medium_ar_50_95", results[10], sync_dist=True)
self.log("result/large_ar_50_95", results[11], sync_dist=True)
self.coco_evaluator = CocoEvaluator(self.dataset_val_coco, ['bbox'])
if self.ssl_task.endswith('discrete'):
ssl_acc = self.ssl_metric.compute()
self.log("result/ssl_acc", ssl_acc)
self.ssl_metric.reset()
def configure_optimizers(self):
param_dicts = [
{"params": [p for n, p in self.named_parameters() if "backbone" not in n and p.requires_grad]},
{
"params": [p for n, p in self.named_parameters() if "backbone" in n and p.requires_grad],
"lr": self.lr_backbone,
},
]
optimizer = torch.optim.AdamW(param_dicts, lr=self.lr, weight_decay=self.weight_decay)
return optimizer
def create_model(args, dataset_val_coco, feature_extractor):
model = Detr(args=args, dataset_val_coco=dataset_val_coco, feature_extractor = feature_extractor)
return model