-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclustering.py
More file actions
413 lines (326 loc) · 11 KB
/
Copy pathclustering.py
File metadata and controls
413 lines (326 loc) · 11 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import cytools
from os.path import join as pjoin
from os.path import exists as pexists
from os.path import split as psplit
import json
import random
import csv
import time
from random import shuffle
import tqdm
import cv2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Enum like dict wih indices for each classname
classes = {
"person": 1,
"bicycle": 2,
"car": 3,
"motorcycle": 4,
"airplane": 5,
"bus": 6,
"train": 7,
"truck": 8,
"boat": 9,
"trafficlight": 10,
"firehydrant": 11,
"stopsign": 13,
"parkingmeter": 14,
"bench": 15,
"bird": 16,
"cat": 17,
"dog": 18,
"horse": 19,
"sheep": 20,
"cow": 21,
"elephant": 22,
"bear": 23,
"zebra": 24,
"giraffe": 25,
"backpack": 27,
"umbrella": 28,
"handbag": 31,
"tie": 32,
"suitcase": 33,
"frisbee": 34,
"skis": 35,
"snowboard": 36,
"sportsball": 37,
"kite": 38,
"baseballbat": 39,
"baseballglove": 40,
"skateboard": 41,
"surfboard": 42,
"tennisracket": 43,
"bottle": 44,
"wineglass": 46,
"cup": 47,
"fork": 48,
"knife": 49,
"spoon": 50,
"bowl": 51,
"banana": 52,
"apple": 53,
"sandwich": 54,
"orange": 55,
"broccoli": 56,
"carrot": 57,
"hotdog": 58,
"pizza": 59,
"donut": 60,
"cake": 61,
"chair": 62,
"couch": 63,
"pottedplant": 64,
"bed": 65,
"diningtable": 67,
"toilet": 70,
"tv": 72,
"laptop": 73,
"mouse": 74,
"remote": 75,
"keyboard": 76,
"cellphone": 77,
"microwave": 78,
"oven": 79,
"toaster": 80,
"sink": 81,
"refrigerator": 82,
"book": 84,
"clock": 85,
"vase": 86,
"scissors": 87,
"teddybear": 88,
"hairdrier": 89,
"toothbrush": 90,
}
def print_results(results, model, image):
fig, ax = plt.subplots(figsize=(12, 12))
fig = ax.imshow(image, aspect='equal')
plt.axis('off')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
for result in results:
bbox = result[:4]
score = result[4:-1].max()
# Get label
label = list(classes.keys())[list(classes.values()).index(
int(np.array(result[4:-1]).argmax()))]
print("Teacher: ", model)
print("Score: ", score)
print("Category: ", label)
print("Number of teacher inferences: ", result[-1])
xmin = bbox[0]
ymin = bbox[1]
xmax = bbox[2]
ymax = bbox[3]
opacity = 1
if model == "cluster":
opacity = (1-(1/result[-1]))**2
if float("{0:.1f}".format(score)) < 0:
score_color = 0
else:
score_color = (
1-(float("{0:.1f}".format(score)))**3, .6, .0, opacity)
ax.set_title(model)
ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
fill=False, edgecolor=score_color, linewidth=4.0))
ax.text(xmin+1, ymin-3, '{:s}'.format("{}_{}".format(label, score)), bbox=dict(
facecolor=score_color, ec='black', lw=2, alpha=0.5), fontsize=15, color='white', weight='bold')
import pdb; pdb.set_trace()
plt.show()
plt.close()
def merge_scores(bboxA, bboxB, method="mean"):
if method == "mean":
return (bboxA+bboxB)/2
def merge_bboxes(bboxA, bboxB, method="mean"):
if method == "mean":
return (bboxA+bboxB)/2
# bboxes -> [x1, y1, x2, y2, score1, score2, ..., scoreN, category]
def combine(bboxesA, bboxesB, thr=.5):
ious = cytools.bbox_overlaps(bboxesA[:, :4], bboxesB[:, :4])
# [
# [0.4, 0.0, 0.05, 0.1, 0.9],
# [0.9, 0.23, 0.0, 0.1, 0.1],
# [0.6, 0.1, 0.2, 0.1, 0.1],
# ]
max_ious = ious.max(1) # [0.9, 0.9, 0.6]
oks = max_ious > thr
# [True, True, False]
rels = ious.argmax(1)
# [4, 0, 0]
if oks.size:
bboxesC = np.zeros(bboxesA[oks, :].shape)
bboxesC[:, :-1] = (bboxesA[oks, :-1] + bboxesB[rels[oks], :-1]) / 2
bboxesC[:, -1] = (bboxesA[oks, -1] + bboxesB[rels[oks], -1])
no_oks = oks == False
# [False, False, True]
bboxesC = np.vstack((bboxesC, bboxesA[no_oks, :]))
max_iousB = ious.max(0) # [0.9, 0.23, 0.2, 0.1, 0.9]
oksB = max_iousB > thr
# [True, False, False, False, True]
no_oksB = oksB == False
# [False, True, True, True, False]
bboxesC = np.vstack((bboxesC, bboxesB[no_oksB, :]))
# [x1, y1, x2, y2, score1, score2, ..., scoreN, category]
test = [x[4:-1].argmax() for x in bboxesC]
return bboxesC
def parse_teacher_results(inference):
# Parse category name to make data more consistent removing underscores and spaces
category = inference["category_id"].replace("_", "").replace(" ", "")
# Transform score to a len(classes) 0 valued list
# with the score value in the category position of the list
# [0, 0, 0 ,0 , 0.8, ... 0]
# 91 for 90 posible classes index and 1 for the number of teachers inferences
score_and_n_of_teachers = [0]*(91+1)
# Minimum one techer have inference
score_and_n_of_teachers[-1] = 1.0
category_position = classes[category]
score_and_n_of_teachers[category_position] = inference["score"]
# Return new data structure
return inference["bbox"] + score_and_n_of_teachers
# Convert bbox from: xmin, ymin, width, height -- to --> xmin, ymin, xmax, ymax
def convert_bbox(inference):
# We cannot mutate original iterator
x1 = inference["bbox"][0]
x2 = inference["bbox"][2] + inference["bbox"][0]
y1 = inference["bbox"][1]
y2 = inference["bbox"][3] + inference["bbox"][1]
return {
"category_id": inference["category_id"],
"bbox": [x1, y1, x2, y2],
"score": inference["score"]
}
def save_result(input_path, output_path, annotations, file):
# Paths
images_path = "{}_images.csv".format(output_path)
ann_paths = "{}_ann.csv".format(output_path)
# Get image HxW
img = cv2.imread(input_path)
dimensions = img.shape
# Get an ID
a_id = int(str(time.time()).replace(".", ""))
ref, _ = psplit(file)
with open(images_path, 'a') as f:
wr = csv.writer(f)
wr.writerow((dimensions[1], dimensions[0], "{}.jpg".format(
ref), "http://{}.jpg".format(ref), ref))
with open(ann_paths, 'a') as f:
wr = csv.writer(f)
# Add annotation
for annotation in annotations:
wr.writerow((0, ref, annotation[:4].tolist(), int(
annotation[4:-1].argmax()), a_id, int(annotation[4:-1].max())))
def cluster():
# Enable debugging features
debug = True
# Make sure the experiment does not repeat
random.seed(int(time.time()))
# random.seed(0)
# Paths
data_dir = "/home/toni/datasets/openimages"
filenames_paths = [x.strip() for x in tqdm.tqdm(open(
"/home/toni/datasets/sorted_4_oi_names.txt"), desc="Reading filenames")]
shuffle(filenames_paths)
inferences_jsons = [x.strip()[:-4] + "/results.json" for x in tqdm.tqdm(
filenames_paths, desc="Creating reasults paths")]
inferences_path = "/home/toni/datasets/results"
results_path = "/home/toni/datasets/openimages/annotations"
teachers = [
"CenterNet-104_480000",
"ATSS",
"GCNET"
]
# Clustering results
cluster_result = []
counter = 0
# Create csv columnssudo apt install nfs-common
for count in tqdm.tqdm(range(0, len(inferences_jsons)), ncols=80, desc="Clustering..."):
if counter == 100000:
break
file = inferences_jsons[count]
# Check if all teachers have inferences
# of the file, otherwise skip loop
all_teachers_have_inferred = True
for teacher in teachers:
# Get results path for each teacher
teacher_inferences = pjoin(inferences_path, teacher)
if not pexists(pjoin(teacher_inferences, file)):
all_teachers_have_inferred = False
break
if not all_teachers_have_inferred:
if debug:
print("Skipping {}...".format(count))
continue
# Get image
if debug:
print(file)
image_file = pjoin(data_dir, filenames_paths[count])
image = cv2.imread(image_file)[:, :, ::-1]
# In case all teachers have inferences
# of the file
# Make sure result is
# ignored if not all json
# files load correct
jsonLoadFailed = False
# Start clustering
for index, teacher in enumerate(teachers):
if teacher != "GCNET":
continue
# Get results path for each teacher
teacher_inferences = pjoin(inferences_path, teacher)
# Get results path for each FILE
file_inferences = pjoin(teacher_inferences, file)
# Read teacher file inferences
with open(file_inferences) as f_i:
try:
inferences = json.load(f_i)
except:
print("failed to load: ", file_inferences)
jsonLoadFailed = True
break
inferences = np.array(inferences)
# We need to parse Centernet bbox, so we must check
# if teacher is centernet
if teacher == "CenterNet-104_480000":
inferences = list(map(convert_bbox, inferences))
# Filter inferences by score
if teacher == "GCNET":
inferences = np.array(list(filter(lambda x: x["score"] > 0.6, inferences)))
if len(inferences) == 0:
continue
# Transform results data structure from dict to list
# in order to speed up clustering between inferences
inferences = np.array(list(map(parse_teacher_results, inferences)))
if debug:
print_results(inferences, teacher, image)
# Do not cluster until there are at least two teachers
#if index == 0:
# cluster_result = inferences
# continue
cluster_result = inferences
#cluster_result = combine(cluster_result, inferences)
# If some json have failed to load skip clustering
if jsonLoadFailed:
continue
# Print clustering result
if debug:
print_results(cluster_result, "cluster", image)
# Split between train test and validation
save_as_type = "train"
r = random.random()
save_as_type = "train"
counter+=1
#save_result(pjoin(
# data_dir, filenames_paths[count]), "{}/instances_{}2017".format(results_path, save_as_type), cluster_result, file)
yield count
if __name__ == "__main__":
# uncomment to test only one image
# next(cluster())
# return
NUMBER_OF_EXAMPLES = 40000000
for i, x in enumerate(cluster()):
if i == NUMBER_OF_EXAMPLES:
break
continue