-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_submission_new.py
309 lines (234 loc) · 11.5 KB
/
test_submission_new.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Use this script to test your submission. Do not look at the results, as they are computed with fake annotations and masks.
This is just to see if there's any problem with files, paths, permissions, etc.
If you find a bug, please report it to [email protected]
Usage:
test_submission.py <weekNumber> <teamNumber> [--testDir=<td>]
test_submission.py -h | --help
Options:
--testDir=<td> Directory with the test images & masks [default: /home/dlcv/2020_fake]
"""
import fnmatch
import os
import sys
import pickle
import imageio
from docopt import docopt
# Compute the depth of a list (of lists (of lists ...) ...)
# Empty list not allowed!!
#https://stackoverflow.com/questions/6039103/counting-depth-or-the-deepest-level-a-nested-list-goes-to
list_depth = lambda L: isinstance(L, list) and max(map(list_depth, L))+1
def check_query_file(hypo_name, week, k_val, num_queries, qsn):
if not os.path.isfile(hypo_name):
print ('File {} not found!'.format(hypo_name))
sys.exit()
with open(hypo_name, 'rb') as fd:
hypo = pickle.load(fd)
if type(hypo) is not list:
print ('File {} must contain a list!'.format(hypo_name))
sys.exit()
ld = list_depth(hypo)
if week == 1 and ld != 2:
print ('File {} list must have two levels!'.format(hypo_name))
sys.exit()
if week > 1 and ld != 3:
print ('File {} list must have three levels!'.format(hypo_name))
sys.exit()
elem = hypo[0][0] if week == 1 else hypo[0][0][0]
if type(elem) is not int:
print ('File {} list must contain integers!'.format(hypo_name))
sys.exit()
if (len(hypo) != num_queries):
print ('File {} should contain {} queries.'.format(hypo_name), num_queries)
sys.exit()
for hyp in hypo:
if week == 1:
if len(hyp) != k_val:
asd
else:
if qsn == 1:
if len(hyp) != 1:
print ('In file {}, only one result per image should be given !'.format(hypo_name))
sys.exit()
for hy in hyp:
if week < 4:
if len(hy) != k_val:
print ('In file {}, {} results per query must be given!'.format(hypo_name, k_val))
sys.exit()
else:
if hy[0] == -1 and len(hy) != 1:
print ('In file {}, if there is a -1, no further results must be given in this query'.format(hypo_name))
sys.exit()
if hy[0] != -1 and len(hy) != k_val:
print ('In file {}, {} results per query or [-1] must be given!'.format(hypo_name, k_val))
sys.exit()
def check_text_box_file(tb_name, week, num_queries, qsn):
if not os.path.isfile(tb_name):
print ('File {} not found!'.format(tb_name))
sys.exit()
with open(tb_name, 'rb') as fd:
tb = pickle.load(fd)
if type(tb) is not list:
print ('File {} must contain a list!'.format(tb_name))
sys.exit()
if (len(tb) != num_queries):
print ('File {} contains an incorrect number of queries!'.format(tb_name))
sys.exit()
if list_depth(tb) != 3:
print ('File {} list must have three levels!'.format(tb_name))
sys.exit()
for tt in tb:
if qsn == 1:
if len(tt) != 1:
print ('In file {}, only one result per image should be given !'.format(tb_name))
sys.exit()
for tti in tt:
if len(tti) != 4:
print ('In file {}, text boxes should contain 4 coordinates (int values)!'.format(tb_name))
sys.exit()
def check_frames_file(frames_name, num_queries):
if not os.path.isfile(frames_name):
print ('File {} not found!'.format(frames_name))
sys.exit()
with open(frames_name, 'rb') as fd:
frams = pickle.load(fd)
if type(frams) is not list:
print ('File {} must contain a list!'.format(frames_name))
sys.exit()
if (len(frams) != num_queries):
print ('File {} contains an incorrect number of queries!'.format(frames_name))
sys.exit()
if list_depth(frams) != 3:
print ('File {} list must have three levels!'.format(frames_name))
sys.exit()
for fram in frams:
if qsn == 1:
if len(fram) != 1:
print ('In file {}, only one result per image should be given !'.format(frames_name))
sys.exit()
for fr in fram:
if len(fr) != 2:
print ('In file {}, format must be [alpha,[[px1,py1],[px2,py2],[px3,py3],px4,py4]]'.format(frames_name))
sys.exit()
if not isinstance(fr[0], (float,np.float32)):
print ('In file {}, angle must be given as floating point'.format(frames_name))
sys.exit()
if len(fr[1]) != 4:
print ('In file {}, format must be [alpha,[[px1,py1],[px2,py2],[px3,py3],px4,py4]]'.format(frames_name))
sys.exit()
for pt in fr[1]:
if len(pt) != 2:
print ('In file {}, format must be [alpha,[[px1,py1],[px2,py2],[px3,py3],px4,py4]]'.format(frames_name))
sys.exit()
if __name__ == '__main__':
# read arguments
args = docopt(__doc__)
week = int(args['<weekNumber>'])
team = int(args['<teamNumber>'])
# This folder contains fake masks and text annotations. Do not change this.
test_dir = args['--testDir']
test_qs1 = '{}/week{}/QST1/result.pkl'.format(test_dir,week)
with open(test_qs1, 'rb') as fd:
gt_query = pickle.load(fd)
num_queries = len(gt_query)
k_val = 10
# This folder contains your results: mask images and window list pkl files. Do not change this.
results_dir = '/home/dlcv{:02d}/m1-results/week{}'.format(team, week)
# Test Query set 1:
qs1_dir = '{}/QST1'.format(results_dir)
if not os.path.isdir(qs1_dir):
print ('{} does not exist!'.format(qs1_dir))
sys.exit()
# 1 stands for simple and 2 for complex
qsn = 1 if week < 4 else 2 # Starting at week 4, there is only a query set and is the complex one
# List all folders (corresponding to the different methods) in the results directory
methods = next(os.walk(qs1_dir))[1]
for method in methods:
# Correspondences Hypotesis file
hypo_name = '{}/{}/result.pkl'.format(qs1_dir, method)
check_query_file(hypo_name, week, k_val, num_queries, qsn)
# Text boxes Hypotesis file
if week > 1 and week < 4:
tb_name = '{}/{}/text_boxes.pkl'.format(qs1_dir, method)
check_text_box_file(tb_name, week, num_queries, qsn)
if week > 2 and week < 5: # txt files
result_txt = sorted(fnmatch.filter(os.listdir('{}/{}'.format(qs1_dir, method)), '*.txt'))
result_txt_num = len(result_txt)
if result_txt_num != num_queries:
print ('Method {} : {} result txt files found but there are {} test txt files'.format(method, result_txt_num, num_queries), file = sys.stderr)
sys.exit()
# Frames
if week == 5:
# Frames Hypotesis file
frame_name = '{}/{}/frames.pkl'.format(qs1_dir, method)
check_frames_file(hypo_name, num_queries)
print ('Submission for QST1 {} seems OK'.format(method))
# From week 4, only one test set
if week > 3:
sys.exit()
##################
# Test Query set 2:
##################
test_dir_qs2 = '{}/week{}/QST2'.format(test_dir,week)
with open('{}/result.pkl'.format(test_dir_qs2), 'rb') as fd1:
gt_query = pickle.load(fd1)
num_queries = len(gt_query)
qs2_dir = '{}/QST2'.format(results_dir)
if not os.path.isdir(qs2_dir):
print ('{} does not exist!'.format(qs2_dir))
sys.exit()
qsn = 2
# List all folders (corresponding to the different methods) in the results directory
methods = next(os.walk(qs2_dir))[1]
# Load mask names in the given directory
test_masks = sorted(fnmatch.filter(os.listdir(test_dir_qs2), '*.png'))
for method in methods:
# Correspondences Hypotesis file
hypo_name = '{}/{}/result.pkl'.format(qs2_dir, method)
check_query_file(hypo_name, week, k_val, num_queries, qsn)
# Text boxes Hypotesis file
if week > 1 and week < 4:
tb_name = '{}/{}/text_boxes.pkl'.format(qs2_dir, method)
check_text_box_file(tb_name, week, num_queries, qsn)
if week > 2 and week < 5: # txt files
result_txt = sorted(fnmatch.filter(os.listdir('{}/{}'.format(qs2_dir, method)), '*.txt'))
result_txt_num = len(result_txt)
if result_txt_num != num_queries:
print ('Method {} : {} result txt files found but there are {} test txt files'.format(method, result_txt_num, num_queries), file = sys.stderr)
sys.exit()
# Read masks (if any)
result_masks = sorted(fnmatch.filter(os.listdir('{}/{}'.format(qs2_dir, method)), '*.png'))
result_masks_num = len(result_masks)
if result_masks_num != num_queries:
print ('Method {} : {} result masks found but there are {} test masks'.format(method, result_masks_num, num_queries), file = sys.stderr)
sys.exit()
for ii in range(len(result_masks)):
# Read mask file
candidate_masks_name = '{}/{}/{}'.format(qs2_dir, method, result_masks[ii])
#print ('File: {}'.format(candidate_masks_name), file = sys.stderr)
pixelCandidates = imageio.imread(candidate_masks_name)>0
if len(pixelCandidates.shape) == 3:
pixelCandidates = pixelCandidates[:,:,0]
# Accumulate pixel performance of the current image %%%%%%%%%%%%%%%%%
name, ext = os.path.splitext(test_masks[ii])
gt_mask_name = '{}/{}.png'.format(test_dir_qs2, name)
pixelAnnotation = imageio.imread(gt_mask_name)>0
if len(pixelAnnotation.shape) == 3:
pixelAnnotation = pixelAnnotation[:,:,0]
if pixelAnnotation.shape != pixelCandidates.shape:
print ('Error: hypothesis and GT masks do not match!')
print (pixelAnnotation.shape, pixelCandidates.shape)
sys.exit()
'''
if window_evaluation == 1:
# Read .pkl file
name_r, ext_r = os.path.splitext(result_masks[ii])
pkl_name = '{}/{}/{}.pkl'.format(results_dir, method, name_r)
with open(pkl_name, "rb") as fp: # Unpickling
windowCandidates = pickle.load(fp)
gt_annotations_name = '{}/gt/gt.{}.txt'.format(test_dir_qs2, name)
windowAnnotations = load_annotations(gt_annotations_name)
'''
print ('Submission for QST2 {} seems OK'.format(method))