-
Notifications
You must be signed in to change notification settings - Fork 16
/
engine.py
246 lines (189 loc) · 10.3 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
import math
import sys
from typing import Iterable
import torch
import torch.nn.functional as F
import utils
from sklearn.metrics import average_precision_score
import numpy as np
import cv2
import os
from pathlib import Path
palette = [0, 0, 0, 128, 0, 0, 0, 128, 0, 128, 128, 0, 0, 0, 128, 128, 0, 128, 0, 128, 128, 128, 128, 128,
64, 0, 0, 192, 0, 0, 64, 128, 0, 192, 128, 0, 64, 0, 128, 192, 0, 128, 64, 128, 128, 192, 128, 128,
0, 64, 0, 128, 64, 0, 0, 192, 0, 128, 192, 0, 0, 64, 128, 255, 255, 255, 128, 64, 128, 0, 192, 128, 128, 192, 128,
64, 64, 0, 192, 64, 0, 64, 192, 0, 192, 192, 0]
def train_one_epoch(model: torch.nn.Module, data_loader: Iterable,
optimizer: torch.optim.Optimizer, device: torch.device,
epoch: int, loss_scaler, max_norm: float = 0,
set_training_mode=True, args=None):
model.train(set_training_mode)
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
print_freq = 10
for samples, targets in metric_logger.log_every(data_loader, print_freq, header):
samples = samples.to(device, non_blocking=True)
targets = targets.to(device, non_blocking=True)
patch_outputs = None
c_outputs = None
with torch.cuda.amp.autocast():
outputs = model(samples)
if len(outputs) == 2:
outputs, patch_outputs = outputs
elif len(outputs) == 3:
outputs, c_outputs, patch_outputs = outputs
loss = F.multilabel_soft_margin_loss(outputs, targets)
metric_logger.update(mct_loss=loss.item())
if c_outputs is not None:
c_outputs = c_outputs[-args.num_cct:]
output_cls_embeddings = F.normalize(c_outputs, dim=-1) # 12xBxCxD
scores = output_cls_embeddings @ output_cls_embeddings.permute(0, 1, 3, 2) # 12xBxCxC
ground_truth = torch.arange(targets.size(-1), dtype=torch.long, device=device) # C
ground_truth = ground_truth.unsqueeze(0).unsqueeze(0).expand(c_outputs.shape[0], c_outputs.shape[1],
c_outputs.shape[2]) # 12xBxC
regularizer_loss = torch.nn.CrossEntropyLoss(reduction='none')(scores.permute(1, 2, 3, 0),
ground_truth.permute(1, 2, 0)) # BxCx12
regularizer_loss = torch.mean(
torch.mean(torch.sum(regularizer_loss * targets.unsqueeze(-1), dim=-2), dim=-1) / (
torch.sum(targets, dim=-1) + 1e-8))
metric_logger.update(attn_loss=regularizer_loss.item())
loss = loss + args.loss_weight*regularizer_loss
if patch_outputs is not None:
ploss = F.multilabel_soft_margin_loss(patch_outputs, targets)
metric_logger.update(pat_loss=ploss.item())
loss = loss + ploss
loss_value = loss.item()
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
sys.exit(1)
optimizer.zero_grad()
is_second_order = hasattr(optimizer, 'is_second_order') and optimizer.is_second_order
loss_scaler(loss, optimizer, clip_grad=max_norm,
parameters=model.parameters(), create_graph=is_second_order)
torch.cuda.synchronize()
metric_logger.update(loss=loss_value)
metric_logger.update(lr=optimizer.param_groups[0]["lr"])
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}
@torch.no_grad()
def evaluate(data_loader, model, device):
criterion = torch.nn.MultiLabelSoftMarginLoss()
mAP = []
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Test:'
# switch to evaluation mode
model.eval()
for images, target in metric_logger.log_every(data_loader, 10, header):
images = images.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
batch_size = images.shape[0]
with torch.cuda.amp.autocast():
output = model(images)
if len(output) == 2:
output, patch_output = output
elif len(output) == 3:
output, c_output, patch_output = output
loss = criterion(output, target)
output = torch.sigmoid(output)
mAP_list = compute_mAP(target, output)
mAP = mAP + mAP_list
metric_logger.meters['mAP'].update(np.mean(mAP_list), n=batch_size)
metric_logger.update(loss=loss.item())
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print('* mAP {mAP.global_avg:.3f} loss {losses.global_avg:.3f}'
.format(mAP=metric_logger.mAP, losses=metric_logger.loss))
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}
def compute_mAP(labels, outputs):
y_true = labels.cpu().numpy()
y_pred = outputs.cpu().numpy()
AP = []
for i in range(y_true.shape[0]):
if np.sum(y_true[i]) > 0:
ap_i = average_precision_score(y_true[i], y_pred[i])
AP.append(ap_i)
return AP
@torch.no_grad()
def generate_attention_maps_ms(data_loader, model, device, args):
metric_logger = utils.MetricLogger(delimiter=" ")
header = 'Generating attention maps:'
if args.attention_dir is not None:
Path(args.attention_dir).mkdir(parents=True, exist_ok=True)
if args.cam_npy_dir is not None:
Path(args.cam_npy_dir).mkdir(parents=True, exist_ok=True)
model.eval()
img_list = open(os.path.join(args.img_list, 'train_aug_id.txt')).readlines()
index = 0
for image_list, target in metric_logger.log_every(data_loader, 10, header):
images1 = image_list[0].to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
batch_size = images1.shape[0]
img_name = img_list[index].strip()
index += 1
img_temp = images1.permute(0, 2, 3, 1).detach().cpu().numpy()
orig_images = np.zeros_like(img_temp)
orig_images[:, :, :, 0] = (img_temp[:, :, :, 0] * 0.229 + 0.485) * 255.
orig_images[:, :, :, 1] = (img_temp[:, :, :, 1] * 0.224 + 0.456) * 255.
orig_images[:, :, :, 2] = (img_temp[:, :, :, 2] * 0.225 + 0.406) * 255.
w_orig, h_orig = orig_images.shape[1], orig_images.shape[2]
with torch.cuda.amp.autocast():
cam_list = []
vitattn_list = []
cam_maps = None
for s in range(len(image_list)):
images = image_list[s].to(device, non_blocking=True)
w, h = images.shape[2] - images.shape[2] % args.patch_size, images.shape[3] - images.shape[3] % args.patch_size
w_featmap = w // args.patch_size
h_featmap = h // args.patch_size
if 'MCTformerV1' in args.model:
output, cls_attentions, patch_attn = model(images, return_att=True, n_layers=args.layer_index)
cls_attentions = cls_attentions.reshape(batch_size, args.nb_classes, w_featmap, h_featmap)
patch_attn = torch.sum(patch_attn, dim=0)
else:
output, cls_attentions, patch_attn = model(images, return_att=True, n_layers=args.layer_index,
attention_type=args.attention_type)
patch_attn = torch.sum(patch_attn, dim=0)
if args.patch_attn_refine:
cls_attentions = torch.matmul(patch_attn.unsqueeze(1), cls_attentions.view(cls_attentions.shape[0],cls_attentions.shape[1], -1, 1)).reshape(cls_attentions.shape[0],cls_attentions.shape[1], w_featmap, h_featmap)
cls_attentions = F.interpolate(cls_attentions, size=(w_orig, h_orig), mode='bilinear', align_corners=False)[0]
cls_attentions = cls_attentions.cpu().numpy() * target.clone().view(args.nb_classes, 1, 1).cpu().numpy()
if s % 2 == 1:
cls_attentions = np.flip(cls_attentions, axis=-1)
cam_list.append(cls_attentions)
vitattn_list.append(cam_maps)
sum_cam = np.sum(cam_list, axis=0)
sum_cam = torch.from_numpy(sum_cam)
sum_cam = sum_cam.unsqueeze(0).to(device)
output = torch.sigmoid(output)
if args.visualize_cls_attn:
for b in range(images.shape[0]):
if (target[b].sum()) > 0:
cam_dict = {}
norm_cam = np.zeros((args.nb_classes, w_orig, h_orig))
for cls_ind in range(args.nb_classes):
if target[b,cls_ind]>0:
cls_score = format(output[b, cls_ind].cpu().numpy(), '.3f')
cls_attention = sum_cam[b,cls_ind,:]
cls_attention = (cls_attention - cls_attention.min()) / (cls_attention.max() - cls_attention.min() + 1e-8)
cls_attention = cls_attention.cpu().numpy()
cam_dict[cls_ind] = cls_attention
norm_cam[cls_ind] = cls_attention
if args.attention_dir is not None:
fname = os.path.join(args.attention_dir, img_name + '_' + str(cls_ind) + '_' + str(cls_score) + '.png')
show_cam_on_image(orig_images[b], cls_attention, fname)
if args.cam_npy_dir is not None:
np.save(os.path.join(args.cam_npy_dir, img_name + '.npy'), cam_dict)
# gather the stats from all processes
metric_logger.synchronize_between_processes()
return
def show_cam_on_image(img, mask, save_path):
img = np.float32(img) / 255.
heatmap = cv2.applyColorMap(np.uint8(255 * mask), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / 255
cam = heatmap + img
cam = cam / np.max(cam)
cam = np.uint8(255 * cam)
cv2.imwrite(save_path, cam)