forked from longbai1006/Surgical-VQLA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
282 lines (230 loc) · 9.28 KB
/
utils.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
'''
Description : utils implementation of Surgical VQLA model
Paper : Surgical-VQLA: Transformer with Gated Vision-Language Embedding for
Visual Question Localized-Answering in Robotic Surgery
Author : Long Bai, Mobarakol Islam, Lalithkumar Seenivasan, Hongliang Ren
Lab : Medical Mechatronics Lab, The Chinese University of Hong Kong
Acknowledgement : Code adopted from the official implementation of
Surgical VQA (https://github.com/huggingface/transformers.git) and
DETR (https://github.com/facebookresearch/detr).
'''
import torch
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import average_precision_score
from sklearn.metrics import precision_recall_fscore_support
from torchvision.ops.boxes import box_area
import torch.nn.functional as F
from torch import nn
class AverageMeter(object):
"""
Keeps track of most recent, average, sum, and count of a metric.
"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def save_checkpoint(checkpoint_dir, epoch, epochs_since_improvement, model, optimizer, metrics, is_best, final_args):
"""
Saves model checkpoint.
:param data_name: base name of processed dataset
:param epoch: epoch number
:param epochs_since_improvement: number of epochs since last improvement in BLEU-4 score
:param model: model
:param optimizer: optimizer to update model's weights
:param bleu4: validation BLEU-4 score for this epoch
:param is_best: is this checkpoint the best so far?
"""
state = {'epoch': epoch,
'epochs_since_improvement': epochs_since_improvement,
'metrics': metrics,
'model': model,
'optimizer': optimizer,
'final_args': final_args}
filename = checkpoint_dir + 'Best.pth.tar'
torch.save(state, filename)
def save_clf_checkpoint(checkpoint_dir, epoch, epochs_since_improvement, model, optimizer, Acc, final_args):
"""
Saves model checkpoint.
"""
state = {'epoch': epoch,
'epochs_since_improvement': epochs_since_improvement,
'Acc': Acc,
'model': model,
'optimizer': optimizer,
'final_args': final_args}
filename = checkpoint_dir + 'Best.pth.tar'
torch.save(state, filename)
def accuracy(scores, targets, k):
"""
Computes top-k accuracy, from predicted and true labels.
:param scores: scores from the model
:param targets: true labels
:param k: k in top-k accuracy
:return: top-k accuracy
"""
batch_size = targets.size(0)
_, ind = scores.topk(k, 1, True, True)
correct = ind.eq(targets.view(-1, 1).expand_as(ind))
correct_total = correct.view(-1).float().sum() # 0D tensor
return correct_total.item() * (100.0 / batch_size)
def adjust_learning_rate(optimizer, shrink_factor):
"""
Shrinks learning rate by a specified factor.
:param optimizer: optimizer whose learning rate must be shrunk.
:param shrink_factor: factor in interval (0, 1) to multiply learning rate with.
"""
print("\nDECAYING learning rate.")
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * shrink_factor
print("The new learning rate is %f\n" % (optimizer.param_groups[0]['lr'],))
def clip_gradient(optimizer, grad_clip):
"""
Clips gradients computed during backpropagation to avoid explosion of gradients.
:param optimizer: optimizer with the gradients to be clipped
:param grad_clip: clip value
"""
for group in optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def calc_acc(y_true, y_pred):
acc = accuracy_score(y_true, y_pred)
return acc
def calc_classwise_acc(y_true, y_pred):
matrix = confusion_matrix(y_true, y_pred)
classwise_acc = matrix.diagonal()/matrix.sum(axis=1)
return classwise_acc
def calc_map(y_true, y_scores):
mAP = average_precision_score(y_true, y_scores,average=None)
return mAP
def calc_precision_recall_fscore(y_true, y_pred):
precision, recall, fscore, _ = precision_recall_fscore_support(y_true, y_pred, average='macro', zero_division = 1)
return(precision, recall, fscore)
def giou_loss(preds, targets):
'''
Args:
preds: [n,4] ltrb
targets: [n,4]
'''
lt_min=torch.min(preds[:,:2],targets[:,:2])
rb_min=torch.min(preds[:,2:],targets[:,2:])
wh_min=(rb_min+lt_min).clamp(min=0)
overlap=wh_min[:,0]*wh_min[:,1]#[n]
area1=(preds[:,2]+preds[:,0])*(preds[:,3]+preds[:,1])
area2=(targets[:,2]+targets[:,0])*(targets[:,3]+targets[:,1])
union=(area1+area2-overlap)
iou=overlap/union
lt_max=torch.max(preds[:,:2],targets[:,:2])
rb_max=torch.max(preds[:,2:],targets[:,2:])
wh_max=(rb_max+lt_max).clamp(0)
G_area=wh_max[:,0]*wh_max[:,1]#[n]
giou=iou-(G_area-union)/G_area.clamp(1e-10)
loss=1.-giou
return loss.sum()
def mIoU_xyxy(box_a, box_b):
# inter = intersection(box_a, box_b)
assert box_a.shape == box_b.shape
(m, n) = box_a.shape
iou_sum = 0
for i in range(m):
x1 = max(box_a[i, 0], box_b[i, 0])
y1 = max(box_a[i, 1], box_b[i, 1])
x2 = min(box_a[i, 2], box_b[i, 2])
y2 = min(box_a[i, 3], box_b[i, 3])
if x1 >= x2 or y1 >= y2:
inter = 0.0
inter = float((x2 - x1 + 1) * (y2 - y1 + 1))
box_a_area = (box_a[i, 2] - box_a[i, 0] + 1) * (box_a[i, 3] - box_a[i, 1] + 1)
box_b_area = (box_b[i, 2] - box_b[i, 0] + 1) * (box_b[i, 3] - box_b[i, 1] + 1)
union = box_a_area + box_b_area - inter
iou = inter / float(max(union, 1))
iou_sum = iou_sum + iou
m_iou = iou_sum / m
return m_iou
def mIoU_single(box_a, box_b):
# inter = intersection(box_a, box_b)
assert box_a.shape == box_b.shape
x1 = max(box_a[0], box_b[0])
y1 = max(box_a[1], box_b[1])
x2 = min(box_a[2], box_b[2])
y2 = min(box_a[3], box_b[3])
if x1 >= x2 or y1 >= y2:
inter = 0.0
inter = float((x2 - x1 + 1) * (y2 - y1 + 1))
box_a_area = (box_a[2] - box_a[0] + 1) * (box_a[3] - box_a[1] + 1)
box_b_area = (box_b[2] - box_b[0] + 1) * (box_b[3] - box_b[1] + 1)
union = box_a_area + box_b_area - inter
iou = inter / float(max(union, 1))
return iou
def box_cxcywh_to_xyxy(x):
x_c, y_c, w, h = x.unbind(-1)
b = [(x_c - 0.5 * w), (y_c - 0.5 * h),
(x_c + 0.5 * w), (y_c + 0.5 * h)]
return torch.stack(b, dim=-1)
def box_xyxy_to_cxcywh(x):
x0, y0, x1, y1 = x.unbind(-1)
b = [(x0 + x1) / 2, (y0 + y1) / 2,
(x1 - x0), (y1 - y0)]
return torch.stack(b, dim=-1)
# modified from torchvision to also return the union
def box_iou(boxes1, boxes2):
area1 = box_area(boxes1)
area2 = box_area(boxes2)
lt = torch.max(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2]
rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2]
wh = (rb - lt).clamp(min=0) # [N,M,2]
inter = wh[:, :, 0] * wh[:, :, 1] # [N,M]
union = area1[:, None] + area2 - inter
iou = inter / union
return iou, union
def generalized_box_iou(boxes1, boxes2):
"""
Generalized IoU from https://giou.stanford.edu/
The boxes should be in [x0, y0, x1, y1] format
Returns a [N, M] pairwise matrix, where N = len(boxes1)
and M = len(boxes2)
"""
# degenerate boxes gives inf / nan results
# so do an early check
assert (boxes1[:, 2:] >= boxes1[:, :2]).all()
assert (boxes2[:, 2:] >= boxes2[:, :2]).all()
iou, union = box_iou(boxes1, boxes2)
lt = torch.min(boxes1[:, None, :2], boxes2[:, :2])
rb = torch.max(boxes1[:, None, 2:], boxes2[:, 2:])
wh = (rb - lt).clamp(min=0) # [N,M,2]
area = wh[:, :, 0] * wh[:, :, 1]
return iou - (area - union) / area
def loss_giou_l1(outputs, targets):
"""Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss
targets dicts must contain the key "boxes" containing a tensor of dim [nb_target_boxes, 4]
The target boxes are expected in format (center_x, center_y, w, h), normalized by the image size.
"""
src_boxes = outputs
target_boxes = targets
(num_boxes, n) = src_boxes.shape
loss_l1= F.l1_loss(src_boxes, target_boxes, reduction='none')
losses_l1 = loss_l1.sum() / num_boxes
loss_giou = 1 - torch.diag(generalized_box_iou(box_cxcywh_to_xyxy(src_boxes), box_cxcywh_to_xyxy(target_boxes)))
losses_giou = loss_giou.sum() / num_boxes
losses = losses_l1 + losses_giou
return losses
class MLP(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, num_layers):
super().__init__()
self.num_layers = num_layers
h = [hidden_dim] * (num_layers - 1)
self.layers = nn.ModuleList(nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim]))
def forward(self, x):
for i, layer in enumerate(self.layers):
x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
return x