-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_anchors.py
141 lines (115 loc) · 4.79 KB
/
generate_anchors.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
import random
import argparse
import numpy as np
from detection_dataloader import parse_voc_annotation
import json
def IOU(ann, centroids):
w, h = ann
similarities = []
for centroid in centroids:
c_w, c_h = centroid
if c_w >= w and c_h >= h:
similarity = w*h/(c_w*c_h)
elif c_w >= w and c_h <= h:
similarity = w*c_h/(w*h + (c_w-w)*c_h)
elif c_w <= w and c_h >= h:
similarity = c_w*h/(w*h + c_w*(c_h-h))
else: #means both w,h are bigger than c_w and c_h respectively
similarity = (c_w*c_h)/(w*h)
similarities.append(similarity) # will become (k,) shape
return np.array(similarities)
def avg_IOU(anns, centroids):
n,d = anns.shape
sum = 0.
for i in range(anns.shape[0]):
sum+= max(IOU(anns[i], centroids))
return sum/n
def print_anchors(centroids):
out_string = ''
anchors = centroids.copy()
widths = anchors[:, 0]
sorted_indices = np.argsort(widths)
r = "anchors: ["
for i in sorted_indices:
out_string += str(int(anchors[i,0]*416)) + ',' + str(int(anchors[i,1]*416)) + ', '
print(out_string[:-2])
def run_kmeans(ann_dims, anchor_num):
ann_num = ann_dims.shape[0]
iterations = 0
prev_assignments = np.ones(ann_num)*(-1)
iteration = 0
old_distances = np.zeros((ann_num, anchor_num))
indices = [random.randrange(ann_dims.shape[0]) for i in range(anchor_num)]
centroids = ann_dims[indices]
anchor_dim = ann_dims.shape[1]
while True:
distances = []
iteration += 1
for i in range(ann_num):
d = 1 - IOU(ann_dims[i], centroids)
distances.append(d)
distances = np.array(distances) # distances.shape = (ann_num, anchor_num)
print("iteration {}: dists = {}".format(iteration, np.sum(np.abs(old_distances-distances))))
#assign samples to centroids
assignments = np.argmin(distances,axis=1)
if (assignments == prev_assignments).all() :
return centroids
#calculate new centroids
centroid_sums=np.zeros((anchor_num, anchor_dim), np.float)
for i in range(ann_num):
centroid_sums[assignments[i]]+=ann_dims[i]
for j in range(anchor_num):
centroids[j] = centroid_sums[j]/(np.sum(assignments==j) + 1e-6)
prev_assignments = assignments.copy()
old_distances = distances.copy()
def _main_():
num_anchors = 9
root = 'F:\\Learning\\tensorflow\\detect\\Dataset\\'
'''
train_imgs, train_labels = parse_voc_annotation(
root+'VOC2012\\Annotations\\',
root+'VOC2012\\JPEGImages\\',
'data.pkl',
['person','head','hand','foot','aeroplane','tvmonitor','train','boat','dog','chair',
'bird','bicycle','bottle','sheep','diningtable','horse','motorbike','sofa','cow',
'car','cat','bus','pottedplant']
)
'''
'''
train_imgs, train_labels = parse_voc_annotation(
root+'Fish\\Annotations\\',
root+'Fish\\JPEGImages\\',
'data.pkl',
['heidiao','niyu','lvqimamiantun','hualu','heijun','dalongliuxian','tiaoshiban']
)
'''
train_imgs, train_labels = parse_voc_annotation(
root+'Fish\\Annotations\\',
root+'Fish\\JPEGImages\\',
'data.pkl',
['aeroplane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow','diningtable','dog','horse','motorbike',
'person','pottedplant','sheep','sofa','train','tvmonitor']
)
# run k_mean to find the anchors
annotation_dims = []
for image in train_imgs:
print(image['filename'])
for obj in image['object']:
relative_w = (float(obj['xmax']) - float(obj['xmin']))/image['width']
relatice_h = (float(obj["ymax"]) - float(obj['ymin']))/image['height']
annotation_dims.append(tuple(map(float, (relative_w,relatice_h))))
annotation_dims = np.array(annotation_dims)
centroids = run_kmeans(annotation_dims, num_anchors)
# write anchors to file
print('\naverage IOU for', num_anchors, 'anchors:', '%0.2f' % avg_IOU(annotation_dims, centroids))
print_anchors(centroids)
if __name__ == '__main__':
_main_()
#raccon 117,142, 151,233, 188,341, 245,378, 248,223, 288,302, 349,379, 374,274, 383,387
#voc 24,35, 48,87, 71,192, 120,293, 126,100, 173,186, 218,330, 330,193, 361,362
# 24,34, 46,84, 68,185, 116,286, 122,97, 171,180, 214,327, 326,193, 359,359
#voc 18,27, 28,75, 49,132, 55,43, 65,227, 84,86, 108,162, 109,288, 162,329, 174,103, 190,212, 245,348
# 321,150, 343,256, 372,379
#fish 25,31, 36,52, 42,96, 59,33, 62,64, 65,134, 100,229, 107,82, 111,134, 159,180, 162,308, 223,131,
# 255,198, 272,331, 412,412
#fish 33,36, 53,69, 71,144, 106,85, 137,151, 145,269, 240,173, 257,326, 412,412