-
Notifications
You must be signed in to change notification settings - Fork 78
/
inference.py
309 lines (283 loc) · 9.85 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
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
import numpy as np
import cv2
import pandas.io.common
import torch
import glob as glob
import os
import time
import argparse
import yaml
import matplotlib.pyplot as plt
import pandas
from models.create_fasterrcnn_model import create_model
from utils.annotations import (
inference_annotations, convert_detections
)
from utils.general import set_infer_dir
from utils.transforms import infer_transforms, resize
from utils.logging import LogJSON
def collect_all_images(dir_test):
"""
Function to return a list of image paths.
:param dir_test: Directory containing images or single image path.
Returns:
test_images: List containing all image paths.
"""
test_images = []
if os.path.isdir(dir_test):
image_file_types = ['*.jpg', '*.jpeg', '*.png', '*.ppm']
for file_type in image_file_types:
test_images.extend(glob.glob(f"{dir_test}/{file_type}"))
else:
test_images.append(dir_test)
return test_images
def parse_opt():
# Construct the argument parser.
parser = argparse.ArgumentParser()
parser.add_argument(
'-i', '--input',
help='folder path to input input image (one image or a folder path)',
)
parser.add_argument(
'-o', '--output',
default=None,
help='folder path to output data',
)
parser.add_argument(
'--data',
default=None,
help='(optional) path to the data config file'
)
parser.add_argument(
'-m', '--model',
default=None,
help='name of the model'
)
parser.add_argument(
'-w', '--weights',
default=None,
help='path to trained checkpoint weights if providing custom YAML file'
)
parser.add_argument(
'-th', '--threshold',
default=0.3,
type=float,
help='detection threshold'
)
parser.add_argument(
'-si', '--show',
action='store_true',
help='visualize output only if this argument is passed'
)
parser.add_argument(
'-mpl', '--mpl-show',
dest='mpl_show',
action='store_true',
help='visualize using matplotlib, helpful in notebooks'
)
parser.add_argument(
'-d', '--device',
default=torch.device('cuda:0' if torch.cuda.is_available() else 'cpu'),
help='computation/training device, default is GPU if GPU present'
)
parser.add_argument(
'-ims', '--imgsz',
default=None,
type=int,
help='resize image to, by default use the original frame/image size'
)
parser.add_argument(
'-nlb', '--no-labels',
dest='no_labels',
action='store_true',
help='do not show labels during on top of bounding boxes'
)
parser.add_argument(
'--square-img',
dest='square_img',
action='store_true',
help='whether to use square image resize, else use aspect ratio resize'
)
parser.add_argument(
'--classes',
nargs='+',
type=int,
default=None,
help='filter classes by visualization, --classes 1 2 3'
)
parser.add_argument(
'--track',
action='store_true'
)
parser.add_argument(
'--log-json',
dest='log_json',
action='store_true',
help='store a json log file in COCO format in the output directory'
)
parser.add_argument(
'-t', '--table',
dest='table',
action='store_true',
help='outputs a csv file with a table summarizing the predicted boxes'
)
args = vars(parser.parse_args())
return args
def main(args):
# For same annotation colors each time.
np.random.seed(42)
# Load the data configurations.
data_configs = None
if args['data'] is not None:
with open(args['data']) as file:
data_configs = yaml.safe_load(file)
NUM_CLASSES = data_configs['NC']
CLASSES = data_configs['CLASSES']
DEVICE = args['device']
if args['output'] is not None:
OUT_DIR = args['output']
if not os.path.exists(OUT_DIR):
os.makedirs(OUT_DIR)
else:
OUT_DIR=set_infer_dir()
# Load the pretrained model
if args['weights'] is None:
# If the config file is still None,
# then load the default one for COCO.
if data_configs is None:
with open(os.path.join('data_configs', 'test_image_config.yaml')) as file:
data_configs = yaml.safe_load(file)
NUM_CLASSES = data_configs['NC']
CLASSES = data_configs['CLASSES']
try:
build_model = create_model[args['model']]
model, coco_model = build_model(num_classes=NUM_CLASSES, coco_model=True)
except:
build_model = create_model['fasterrcnn_resnet50_fpn_v2']
model, coco_model = build_model(num_classes=NUM_CLASSES, coco_model=True)
# Load weights if path provided.
if args['weights'] is not None:
checkpoint = torch.load(args['weights'], map_location=DEVICE)
# If config file is not given, load from model dictionary.
if data_configs is None:
data_configs = True
NUM_CLASSES = checkpoint['data']['NC']
CLASSES = checkpoint['data']['CLASSES']
try:
print('Building from model name arguments...')
build_model = create_model[str(args['model'])]
except:
build_model = create_model[checkpoint['model_name']]
model = build_model(num_classes=NUM_CLASSES, coco_model=False)
model.load_state_dict(checkpoint['model_state_dict'])
model.to(DEVICE).eval()
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
if args['input'] == None:
DIR_TEST = data_configs['image_path']
test_images = collect_all_images(DIR_TEST)
else:
DIR_TEST = args['input']
test_images = collect_all_images(DIR_TEST)
print(f"Test instances: {len(test_images)}")
# Define the detection threshold any detection having
# score below this will be discarded.
detection_threshold = args['threshold']
# Define dictionary to collect boxes detected in each file
pred_boxes = {}
box_id = 1
if args['log_json']:
log_json = LogJSON(os.path.join(OUT_DIR, 'log.json'))
# To count the total number of frames iterated through.
frame_count = 0
# To keep adding the frames' FPS.
total_fps = 0
for i in range(len(test_images)):
# Get the image file name for saving output later on.
image_name = test_images[i].split(os.path.sep)[-1].split('.')[0]
orig_image = cv2.imread(test_images[i])
frame_height, frame_width, _ = orig_image.shape
if args['imgsz'] != None:
RESIZE_TO = args['imgsz']
else:
RESIZE_TO = frame_width
# orig_image = image.copy()
image_resized = resize(
orig_image, RESIZE_TO, square=args['square_img']
)
image = image_resized.copy()
# BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = infer_transforms(image)
# Add batch dimension.
image = torch.unsqueeze(image, 0)
start_time = time.time()
with torch.no_grad():
outputs = model(image.to(DEVICE))
end_time = time.time()
# Get the current fps.
fps = 1 / (end_time - start_time)
# Add `fps` to `total_fps`.
total_fps += fps
# Increment frame count.
frame_count += 1
# Load all detection to CPU for further operations.
outputs = [{k: v.to('cpu') for k, v in t.items()} for t in outputs]
# Carry further only if there are detected boxes.
if len(outputs[0]['boxes']) != 0:
draw_boxes, pred_classes, scores, labels = convert_detections(
outputs, detection_threshold, CLASSES, args
)
orig_image = inference_annotations(
draw_boxes,
pred_classes,
scores,
CLASSES,
COLORS,
orig_image,
image_resized,
args
)
if args['show']:
cv2.imshow('Prediction', orig_image)
cv2.waitKey(1)
if args['mpl_show']:
plt.imshow(orig_image[:, :, ::-1])
plt.axis('off')
plt.show()
if args['table']:
for box, label in zip(draw_boxes, pred_classes):
xmin, ymin, xmax, ymax = box
width = xmax - xmin
height = ymax - ymin
pred_boxes[box_id] = {
"image": image_name,
"label": str(label),
"xmin": xmin,
"xmax": xmax,
"ymin": ymin,
"ymax": ymax,
"width": width,
"height": height,
"area": width * height
}
box_id = box_id + 1
df = pandas.DataFrame.from_dict(pred_boxes, orient='index')
df = df.fillna(0)
df.to_csv(f"{OUT_DIR}/boxes.csv", index=False)
if args['log_json']:
log_json.update(orig_image, image_name, draw_boxes, labels, CLASSES)
cv2.imwrite(f"{OUT_DIR}/{image_name}.jpg", orig_image)
print(f"Image {i+1} done...")
print('-'*50)
print('TEST PREDICTIONS COMPLETE')
cv2.destroyAllWindows()
# Save JSON log file.
if args['log_json']:
log_json.save(os.path.join(OUT_DIR, 'log.json'))
# Calculate and print the average FPS.
avg_fps = total_fps / frame_count
print(f"Average FPS: {avg_fps:.3f}")
print('Path to output files: '+OUT_DIR)
if __name__ == '__main__':
args = parse_opt()
main(args)