-
Notifications
You must be signed in to change notification settings - Fork 3
/
coder.py
126 lines (100 loc) · 4.61 KB
/
coder.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
import torch
from abc import ABCMeta, abstractmethod
from config import device
from utils import find_jaccard_overlap, xy_to_cxcy
from anchor import RETINA_Anchor
from utils import cxcy_to_xy
class Coder(metaclass=ABCMeta):
@abstractmethod
def encode(self):
pass
@abstractmethod
def decode(self):
pass
class RETINA_Coder(Coder):
def __init__(self, opts):
super().__init__()
self.data_type = opts.data_type
self.anchor_obj = RETINA_Anchor('retina')
self.num_classes = opts.num_classes
self.anchor_dic = {}
def set_anchors(self, size):
if self.anchor_dic.get(size) is None:
self.anchor_dic[size] = self.anchor_obj.create_anchors(img_size=size)
self.center_anchor = self.anchor_dic[size]
def assign_anchors_to_device(self):
self.center_anchor = self.center_anchor.to(device)
def assign_anchors_to_cpu(self):
self.center_anchor = self.center_anchor.to('cpu')
def encode(self, cxcy):
gcxcy = (cxcy[:, :2] - self.center_anchor[:, :2]) / self.center_anchor[:, 2:]
gwh = torch.log(cxcy[:, 2:] / self.center_anchor[:, 2:])
return torch.cat([gcxcy, gwh], dim=1)
def decode(self, gcxgcy):
cxcy = gcxgcy[:, :2] * self.center_anchor[:, 2:] + self.center_anchor[:, :2]
wh = torch.exp(gcxgcy[:, 2:]) * self.center_anchor[:, 2:]
return torch.cat([cxcy, wh], dim=1)
# IT - IoU Threshold == 0.5
def build_target(self, gt_boxes, gt_labels, IT=None):
batch_size = len(gt_labels)
n_priors = self.center_anchor.size(0)
# ----- 1. make container
gt_locations = torch.zeros((batch_size, n_priors, 4), dtype=torch.float, device=device)
gt_classes = -1 * torch.ones((batch_size, n_priors, self.num_classes), dtype=torch.float, device=device)
anchor_identifier = -1 * torch.ones((batch_size, n_priors), dtype=torch.float32, device=device)
anchor_identifier_1 = -1 * torch.ones((batch_size, n_priors), dtype=torch.float32, device=device)
# if anchor is positive -> 1,
# negative -> 0,
# ignore -> -1
# ----- 2. make corner anchors
corner_anchor = cxcy_to_xy(self.center_anchor)
for i in range(batch_size):
boxes = gt_boxes[i]
labels = gt_labels[i]
# ----- 3. *** normalized_iou_assign ***
iou = find_jaccard_overlap(corner_anchor, boxes)
IoU_max, IoU_argmax = iou.max(dim=1)
IoU_max_per_obj, _ = iou.max(dim=0)
Normed_IoU_max = IoU_max / IoU_max_per_obj[IoU_argmax]
# ----- 4-1. build gt_classes
negative_indices = Normed_IoU_max < 0.7
negative_indices_1 = IoU_max < 0.4
gt_classes[i][negative_indices, :] = 0
anchor_identifier[i][negative_indices] = 0
anchor_identifier_1[i][negative_indices_1] = 0
if IT is not None:
positive_indices = Normed_IoU_max >= 0.7
positive_indices_1 = IoU_max >= 0.5
else:
_, IoU_argmax_per_object = iou.max(dim=0)
positive_indices = torch.zeros_like(IoU_max)
positive_indices[IoU_argmax_per_object] = 1
positive_indices = positive_indices.type(torch.bool)
argmax_labels = labels[IoU_argmax]
gt_classes[i][positive_indices, :] = 0
gt_classes[i][positive_indices, argmax_labels[positive_indices].long()] = 1.
anchor_identifier[i][positive_indices] = Normed_IoU_max[positive_indices]
anchor_identifier_1[i][positive_indices_1] = 1
# ----- 4-2. build gt_locations
argmax_locations = boxes[IoU_argmax]
center_locations = xy_to_cxcy(argmax_locations)
gt_gcxcywh = self.encode(center_locations)
gt_locations[i] = gt_gcxcywh
return gt_classes, gt_locations, anchor_identifier, anchor_identifier_1
def post_processing(self, pred, is_demo=False):
if is_demo:
self.assign_anchors_to_cpu()
pred_cls = pred[0].to('cpu')
pred_loc = pred[1].to('cpu')
else:
pred_cls = pred[0]
pred_loc = pred[1]
n_priors = self.center_anchor.size(0)
assert n_priors == pred_loc.size(1) == pred_cls.size(1)
pred_bboxes = cxcy_to_xy(self.decode(pred_loc.squeeze())).clamp(0, 1)
pred_scores = pred_cls.squeeze(0)
return pred_bboxes, pred_scores
if __name__ == '__main__':
ssd_coder = RETINA_Coder()
ssd_coder.assign_anchors_to_device()
print(ssd_coder.center_anchor)