-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
227 lines (186 loc) · 6.08 KB
/
inference.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
import os
import glob
import json
import argparse
from pathlib import Path
# 0. to tfrecord
# 1. inference
# 2. convert task2GT of pred
# 3. convert OBB2HBB of GT
# 4. DOTA2COCO.py for both
# 5. use pycocotools from IQF
PROTOBUF_PY="/usr/local/bin/python"
def get_file_size(filename):
return float(Path(filename).stat().st_size) / 1024 / 1024
def get_avg_file_size(glob_crit):
size_sum = 0
fnlst = glob.glob(glob_crit)
for fn in fnlst:
size = get_file_size(fn)
size_sum+=size
return size_sum/len(fnlst)
def main(
trainds, outputpath,
MODEL="rfcn_resnet101_coco_11_06_2017",
CROPSZ=1024,
CUDA_VISIBLE_DEVICES='2',
STEPS = [0,1,2,3,4,5]
):
###############################
# 0. to tfrecord
###############################
original_ds = (trainds.split('#')[0] if '#' in trainds else trainds)
data_dir = trainds
#data_dir = f'/Nas/DOTA1_0/split_ss_dota1_0_glasgow_{CROPSZ}/val'
indexfile = os.path.join(trainds, f'val.txt')
output_name = os.path.join(trainds, f'tf_records/dota_val.record')
# make val.txt file
txt='\n'.join(glob.glob(os.path.join(data_dir,f'images','*')))
with open(f'{data_dir}/val.txt','w') as f:
f.write(txt)
cmd = [
f"cd object_detection &&",
f"{PROTOBUF_PY}",
f"./create_dota_tf_record.py",
f"--data_dir {data_dir}",
f"--indexfile {indexfile}",
f"--output_name {output_name}",
f"--label_map_path data/dota_label_map.pbtxt",
]
if 0 in STEPS:
print('**************************************\n\t\t\tSTEP 0\n**************************************')
print(cmd)
os.system( ' '.join(cmd) )
###############################
# 1. inference
###############################
cmd = [
f"cd object_detection &&",
f"{PROTOBUF_PY}",
f"./getresultfromtfrecord.py",
f"--model {MODEL}", # useless now
f"--trainds {trainds}",
f"--outputpath {outputpath}",
f"--cu {CUDA_VISIBLE_DEVICES}",
]
if 1 in STEPS:
print('**************************************\n\t\t\tSTEP 1\n**************************************')
print(cmd)
os.system( ' '.join(cmd) )
###############################
# 2. convert task2HBB of pred
###############################
cmd = [
"cd DOTA_devkit &&",
f"{PROTOBUF_PY}",
"./task2gt.py",
f"--outputpath {outputpath}"
]
if 2 in STEPS:
print('**************************************\n\t\t\tSTEP 2\n**************************************')
print(cmd)
os.system( ' '.join(cmd) )
# ###############################
# # 3. convert OBB2HBB of GT
# ###############################
OBBDIR = os.path.join(original_ds,'labelTxt')
HBBDIR = os.path.join(original_ds,'labelTxtHBB')
cmd = [
"cd DOTA_devkit &&",
f"{PROTOBUF_PY}",
"./results_obb2hbb.py",
f"--obbdir {OBBDIR}",
f"--hbbdir {HBBDIR}"
]
if 3 in STEPS:
print('**************************************\n\t\t\tSTEP 3\n**************************************')
print(cmd)
os.system( ' '.join(cmd) )
# ###############################
# # 4. DOTA2COCO.py for both
# ###############################
cmd = [
"cd DOTA_devkit",
"&&",
f"{PROTOBUF_PY}",
"./DOTA2COCO.py",
f"--is_gt true",
f"--img_id_reference_coco_fn {original_ds}", # reference file
f"--outputpath {original_ds}" # out
"&&",
f"{PROTOBUF_PY}",
"./DOTA2COCO.py",
f"--is_gt false",
f"--img_id_reference_coco_fn {original_ds}", # reference file
f"--outputpath {outputpath}" # out
]
if 4 in STEPS:
print('**************************************\n\t\t\tSTEP 4\n**************************************')
print(cmd)
os.system( ' '.join(cmd) )
# ###############################
# # 5. use pycocotools from IQF
# ###############################
if 5 in STEPS:
print('**************************************\n\t\t\tSTEP 5\n**************************************')
print(cmd)
from iquaflow.metrics import BBDetectionMetrics
bbmet = BBDetectionMetrics()
results = bbmet.apply(
predictions=os.path.join(outputpath,'output.json'),
gt_path=os.path.join(trainds,'coco.json')
)
# metric format (list)
results = { k:[results[k]] for k in results }
# more metrics
results['format'] = (
glob.glob(os.path.join(outputpath,'images','*'))[0].split('.')[-1]
if not os.path.isdir(os.path.join(outputpath,'images_compressed'))
else glob.glob(os.path.join(outputpath,'images_compressed','*'))[0].split('.')[-1]
)
results['Mb'] = [ (
get_avg_file_size(os.path.join(outputpath,'images','*'))
if not os.path.isdir(os.path.join(outputpath,'images_compressed'))
else get_avg_file_size(os.path.join(outputpath,'images_compressed','*'))
) ]
# add hyperparams
results['MODEL'] = MODEL
results['CROPSZ'] = CROPSZ
# results['quality'] = 101
# results['scaleperc'] = 101
# results['bits'] = 9
with open(os.path.join(outputpath,'results.json'), 'w') as outfile:
json.dump(results, outfile)
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--trainds', type=str, default='/data/DOTA1_0/split_ss_dota1_0_glasgow_1024/val', help='input dataset path')
parser.add_argument('--outputpath', type=str, default='/iqf/outputpath', help='input dataset path')
#parser.add_argument('--cropsz', type=int, default=1024, help='crop size')
parser.add_argument('--steps', type=str, default='0,1,2,3,4,5', help='CUDA_VISIBLE_DEVICES')
parser.add_argument('--cu', type=str, default='0,1', help='CUDA_VISIBLE_DEVICES')
parser.add_argument('--model',
type=str,
default='/data/DOTA1_0/split_ss_dota1_0_glasgow_1024/train/chkpt/dota_rfcn_output_2000000_136610/frozen_inference_graph.pb',
help='model full subfolder name'
)
opt = parser.parse_args()
# import sys; print(f'{sys.argv}'); raise 'sdfsdf'
CUDA_VISIBLE_DEVICES = opt.cu
os.environ['CUDA_VISIBLE_DEVICES'] = opt.cu
trainds = opt.trainds
outputpath = opt.outputpath
STEPS = [int(s) for s in opt.steps.split(',')]
MODEL = opt.model
CROPSZ = os.path.basename(os.path.dirname(trainds)).split('_')[-1]
if '#' in opt.trainds:
# copy labelTxt from original
original_ds = os.path.join( opt.trainds.split('#')[0], 'labelTxt' )
os.system(f'ln -sf {original_ds} {opt.trainds}')
main(
trainds,
outputpath,
MODEL,
CROPSZ,
CUDA_VISIBLE_DEVICES,
STEPS
)