-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfer_csv.py
More file actions
141 lines (127 loc) · 5.02 KB
/
infer_csv.py
File metadata and controls
141 lines (127 loc) · 5.02 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
"""
inference on a image testset and generate CSV file.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from collections import defaultdict
import argparse
import cv2 # NOQA (Must import before importing caffe2 due to bug in cv2)
from glob import glob
import logging
import os
import sys
import time
import numpy as np
from caffe2.python import workspace
import pycocotools.mask as mask_util
from detectron.core.config import assert_and_infer_cfg
from detectron.core.config import cfg
from detectron.core.config import merge_cfg_from_file
from detectron.utils.io import cache_url
from detectron.utils.logging import setup_logging
from detectron.utils.timer import Timer
import detectron.core.test_engine as infer_engine
import detectron.datasets.dummy_datasets as dummy_datasets
import detectron.utils.c2 as c2_utils
c2_utils.import_detectron_ops()
# OpenCL may be enabled by default in OpenCV3; disable it because it's not
# thread safe and causes unwanted GPU memory allocations.
cv2.ocl.setUseOpenCL(False)
def convert_from_cls_format(cls_boxes, cls_segms, cls_keyps):
"""Convert from the class boxes/segms/keyps format generated by the testing
code.
"""
box_list = [b for b in cls_boxes if len(b) > 0]
if len(box_list) > 0:
boxes = np.concatenate(box_list)
else:
boxes = None
if cls_segms is not None:
segms = [s for slist in cls_segms for s in slist]
else:
segms = None
if cls_keyps is not None:
keyps = [k for klist in cls_keyps for k in klist]
else:
keyps = None
classes = []
for j in range(len(cls_boxes)):
classes += [j] * len(cls_boxes[j])
return boxes, segms, keyps, classes
PIC_PATH = sys.argv[1]
CFG_PATH = sys.argv[2] # '/detectron/flags/101.yaml'
MODEL = sys.argv[3]
OUTPUT_CSV_DIR = sys.argv[4]
filenames = glob(PIC_PATH +'*.jpg')
filenames2 = []
labels = []
pix_cnt = []
conf = []
encode_pixel = []
submits = []
label2order = [0, 4, 9, 17, 20, 24, 2, 30, 22, 37, 7, 11, 5, 21, 27, 38, 18, 31, 26, 12, 29,32, 8, 33, 14, 40, 19, 16, 20, 13, 1, 35, 28, 36, 3, 10, 25, 34, 23, 39, 6]
def main():
logger = logging.getLogger(__name__)
merge_cfg_from_file(CFG_PATH)
cfg.NUM_GPUS = 1
assert_and_infer_cfg(cache_urls=False)
model = infer_engine.initialize_model_from_cfg(MODEL)
dummy_coco_dataset = dummy_datasets.get_coco_dataset()
frame_cnt = 0
for filename in filenames:
# filename = filename.split(',')[0]
image_path = filename
frame_cnt = frame_cnt + 1
im = cv2.imread(image_path)
im2 = im
# print(image_path, frame_cnt, (np.array(im)).shape, (np.array(im2)).shape)
timers = defaultdict(Timer)
t = time.time()
with c2_utils.NamedCudaScope(0):
cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(model, im2, None, timers=timers)
# print('!!!!!allbox:-->', cls_boxes)
keypoints=None
if isinstance(cls_boxes, list):
cls_boxes, cls_segms, keypoints, classes = convert_from_cls_format(cls_boxes, cls_segms, keypoints)
# if cls_segms is not None and len(cls_segms) > 0:
# masks = mask_util.decode(cls_segms)
segms = cls_segms
boxes = cls_boxes
#print(boxes)
# submits.append(filename)
if boxes is not None:
scores = boxes[:, -1]
sorted_inds = np.argsort(-scores)
# i = sorted_inds[0]
submit = filename.split('/')[-1]
pic_label = classes[sorted_inds[0]]
for i in sorted_inds:
#print(boxes[i])
bbox = boxes[i, :4]
score = boxes[i, -1]
label = classes[i]
if label != pic_label:
continue
order = label2order[label]
#print(order)
x1, y1, x2, y2 = boxes[i, 0], boxes[i, 1], boxes[i, 2], boxes[i, 3]
w, h = x2-x1, y2-y1
# print(x1,y1,w,h)
submit = submit + '\t(' + str(int(x1)) + ',' + str(int(y1)) + ',' + str(int(w)) + ',' + str(int(h)) + ')\t' + str(order)
submits.append(submit)
else:
submit = filename.split('/')[-1] + '\t(0,0,' + str(im.shape[1]) + ',' + \
str(im.shape[0]) + ')\t1'
submits.append(submit)
# if (frame_cnt>3):
# break
# logger.info('Inference time: {:.3f}s'.format(time.time() - t))
with open(OUTPUT_CSV_DIR, 'w') as f:
f.write('\n'.join(submits))
# f.write('\n'.join(str(submit) for submit in submits))
if __name__ == '__main__':
workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
setup_logging(__name__)
main()