forked from yjh0410/yolov2-yolov3_PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_ab_kmeans.py
217 lines (175 loc) · 6.7 KB
/
generate_ab_kmeans.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
from data import BaseTransform, VOC_ROOT, VOCDetection, coco_root, COCODataset
import numpy as np
import random
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='kmeans for anchor box')
parser.add_argument('-d', '--dataset', default='coco',
help='coco, voc.')
parser.add_argument('-na', '--num_anchorbox', default=9, type=int,
help='number of anchor box.')
parser.add_argument('-size', '--input_size', default=416, type=int,
help='input size.')
parser.add_argument('--scale', action='store_true', default=False,
help='divide the sizes of anchor boxes by 32 .')
return parser.parse_args()
args = parse_args()
class Box():
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def iou(box1, box2):
x1, y1, w1, h1 = box1.x, box1.y, box1.w, box1.h
x2, y2, w2, h2 = box2.x, box2.y, box2.w, box2.h
S_1 = w1 * h1
S_2 = w2 * h2
xmin_1, ymin_1 = x1 - w1 / 2, y1 - h1 / 2
xmax_1, ymax_1 = x1 + w1 / 2, y1 + h1 / 2
xmin_2, ymin_2 = x2 - w2 / 2, y2 - h2 / 2
xmax_2, ymax_2 = x2 + w2 / 2, y2 + h2 / 2
I_w = min(xmax_1, xmax_2) - max(xmin_1, xmin_2)
I_h = min(ymax_1, ymax_2) - max(ymin_1, ymin_2)
if I_w < 0 or I_h < 0:
return 0
I = I_w * I_h
IoU = I / (S_1 + S_2 - I)
return IoU
def init_centroids(boxes, n_anchors):
"""
We use kmeans++ to initialize centroids.
"""
centroids = []
boxes_num = len(boxes)
centroid_index = int(np.random.choice(boxes_num, 1)[0])
centroids.append(boxes[centroid_index])
print(centroids[0].w,centroids[0].h)
for centroid_index in range(0, n_anchors-1):
sum_distance = 0
distance_thresh = 0
distance_list = []
cur_sum = 0
for box in boxes:
min_distance = 1
for centroid_i, centroid in enumerate(centroids):
distance = (1 - iou(box, centroid))
if distance < min_distance:
min_distance = distance
sum_distance += min_distance
distance_list.append(min_distance)
distance_thresh = sum_distance * np.random.random()
for i in range(0, boxes_num):
cur_sum += distance_list[i]
if cur_sum > distance_thresh:
centroids.append(boxes[i])
print(boxes[i].w, boxes[i].h)
break
return centroids
def do_kmeans(n_anchors, boxes, centroids):
loss = 0
groups = []
new_centroids = []
# for box in centroids:
# print('box: ', box.x, box.y, box.w, box.h)
# exit()
for i in range(n_anchors):
groups.append([])
new_centroids.append(Box(0, 0, 0, 0))
for box in boxes:
min_distance = 1
group_index = 0
for centroid_index, centroid in enumerate(centroids):
distance = (1 - iou(box, centroid))
if distance < min_distance:
min_distance = distance
group_index = centroid_index
groups[group_index].append(box)
loss += min_distance
new_centroids[group_index].w += box.w
new_centroids[group_index].h += box.h
for i in range(n_anchors):
new_centroids[i].w /= max(len(groups[i]), 1)
new_centroids[i].h /= max(len(groups[i]), 1)
return new_centroids, groups, loss# / len(boxes)
def anchor_box_kmeans(total_gt_boxes, n_anchors, loss_convergence, iters, plus=True):
"""
This function will use k-means to get appropriate anchor boxes for train dataset.
Input:
total_gt_boxes:
n_anchor : int -> the number of anchor boxes.
loss_convergence : float -> threshold of iterating convergence.
iters: int -> the number of iterations for training kmeans.
Output: anchor_boxes : list -> [[w1, h1], [w2, h2], ..., [wn, hn]].
"""
boxes = total_gt_boxes
centroids = []
if plus:
centroids = init_centroids(boxes, n_anchors)
else:
total_indexs = range(len(boxes))
sample_indexs = random.sample(total_indexs, n_anchors)
for i in sample_indexs:
centroids.append(boxes[i])
# iterate k-means
centroids, groups, old_loss = do_kmeans(n_anchors, boxes, centroids)
iterations = 1
while(True):
centroids, groups, loss = do_kmeans(n_anchors, boxes, centroids)
iterations += 1
print("Loss = %f" % loss)
if abs(old_loss - loss) < loss_convergence or iterations > iters:
break
old_loss = loss
for centroid in centroids:
print(centroid.w, centroid.h)
print("k-means result : ")
for centroid in centroids:
if args.scale:
print("w, h: ", round(centroid.w / 32., 2), round(centroid.h / 32., 2),
"area: ", round(centroid.w / 32., 2) * round(centroid.h / 32., 2))
else:
print("w, h: ", round(centroid.w, 2), round(centroid.h, 2),
"area: ", round(centroid.w, 2) * round(centroid.h, 2))
return centroids
if __name__ == "__main__":
n_anchors = args.num_anchorbox
size = args.input_size
dataset = args.dataset
loss_convergence = 1e-6
iters_n = 1000
if args.dataset == 'voc':
dataset = VOCDetection(root=VOC_ROOT, transform=BaseTransform([size, size]))
elif args.dataset == 'coco':
dataset = COCODataset(
data_dir=coco_root,
img_size=size,
transform=BaseTransform([size, size]))
boxes = []
print("The dataset size: ", len(dataset))
print("Loading the dataset ...")
for i in range(len(dataset)):
if i % 5000 == 0:
print('Loading datat [%d / %d]' % (i+1, len(dataset)))
if args.dataset == 'coco':
# For COCO
img, _ = dataset.pull_image(i)
w, h = img.shape[1], img.shape[0]
annotation = dataset.pull_anno(i)
elif args.dataset == 'voc':
# For VOC
img, _ = dataset.pull_image(i)
w, h = img.shape[1], img.shape[0]
_, annotation = dataset.pull_anno(i)
# prepare bbox datas
for box_and_label in annotation:
box = box_and_label[:-1]
xmin, ymin, xmax, ymax = box
bw = (xmax - xmin) / w * size
bh = (ymax - ymin) / h * size
# check bbox
if bw < 1.0 or bh < 1.0:
continue
boxes.append(Box(0, 0, bw, bh))
print("Start k-means !")
centroids = anchor_box_kmeans(boxes, n_anchors, loss_convergence, iters_n, plus=True)