Skip to content

Commit 224a52a

Browse files
authored
Merge pull request #183 from wenh06/master
modify the evaluate function in train.py along with the modification of the model inference output
2 parents fa60ecf + 2a38df2 commit 224a52a

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

tool/utils_iou.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,17 @@ def bboxes_iou(bboxes_a, bboxes_b, fmt='voc', iou_type='iou'):
171171
if iou_type.lower() == 'diou':
172172
return diou
173173

174+
""" the legacy custom cosine similarity:
175+
174176
# bb_a of shape `(N,2)`, bb_b of shape `(K,2)`
175177
v = torch.einsum('nm,km->nk', bb_a, bb_b)
176178
v = _true_divide(v, (torch.norm(bb_a, p='fro', dim=1)[:,np.newaxis] * torch.norm(bb_b, p='fro', dim=1)))
177179
# avoid nan for torch.acos near \pm 1
178180
# https://github.com/pytorch/pytorch/issues/8069
179181
eps = 1e-7
180182
v = torch.clamp(v, -1+eps, 1-eps)
183+
"""
184+
v = F.cosine_similarity(bb_a[:,np.newaxis,:], bb_b, dim=-1)
181185
v = (_true_divide(2*torch.acos(v), np.pi)).pow(2)
182186
with torch.no_grad():
183187
alpha = (_true_divide(v, 1-iou+v)) * ((iou>=0.5).type(iou.type()))

tool/utils_iou_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ def bboxes_iou_test(bboxes_a, bboxes_b, fmt='voc', iou_type='iou'):
138138
# if iou_type.lower() == 'diou':
139139
# return diou
140140

141+
""" the legacy custom cosine similarity:
142+
141143
# bb_a of shape `(N,2)`, bb_b of shape `(K,2)`
142144
v = torch.einsum('nm,km->nk', bb_a, bb_b)
143145
v = _true_divide(v, (torch.norm(bb_a, p='fro', dim=1)[:,np.newaxis] * torch.norm(bb_b, p='fro', dim=1)))
146+
# avoid nan for torch.acos near \pm 1
147+
# https://github.com/pytorch/pytorch/issues/8069
144148
eps = 1e-7
145149
v = torch.clamp(v, -1+eps, 1-eps)
150+
"""
151+
v = F.cosine_similarity(bb_a[:,np.newaxis,:], bb_b, dim=-1)
146152
v = (_true_divide(2*torch.acos(v), np.pi)).pow(2)
147153
alpha = (_true_divide(v, 1-iou+v))*((iou>=0.5).type(iou.type()))
148154

train.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,18 +487,21 @@ def evaluate(model, data_loader, cfg, device, logger=None, **kwargs):
487487
# outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs]
488488
model_time = time.time() - model_time
489489

490-
outputs = outputs.cpu().detach().numpy()
490+
# outputs = outputs.cpu().detach().numpy()
491491
res = {}
492-
for img, target, output in zip(images, targets, outputs):
492+
# for img, target, output in zip(images, targets, outputs):
493+
for img, target, (boxes, confs) in zip(images, targets, outputs):
493494
img_height, img_width = img.shape[:2]
494-
boxes = output[...,:4].copy() # output boxes in yolo format
495+
# boxes = output[...,:4].copy() # output boxes in yolo format
496+
boxes = boxes.squeeze(2).cpu().detach().numpy()
495497
boxes[...,:2] = boxes[...,:2] - boxes[...,2:]/2 # to coco format
496498
boxes[...,0] = boxes[...,0]*img_width
497499
boxes[...,1] = boxes[...,1]*img_height
498500
boxes[...,2] = boxes[...,2]*img_width
499501
boxes[...,3] = boxes[...,3]*img_height
500502
boxes = torch.as_tensor(boxes, dtype=torch.float32)
501-
confs = output[...,4:].copy()
503+
# confs = output[...,4:].copy()
504+
confs = confs.cpu().detach().numpy()
502505
labels = np.argmax(confs, axis=1).flatten()
503506
labels = torch.as_tensor(labels, dtype=torch.int64)
504507
scores = np.max(confs, axis=1).flatten()

0 commit comments

Comments
 (0)