-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathe2e_inference_image.py
More file actions
232 lines (188 loc) · 9.26 KB
/
e2e_inference_image.py
File metadata and controls
232 lines (188 loc) · 9.26 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
import argparse
import os
import random
import time
import importlib
from os.path import isfile, join, split
import glob as glob
import cv2
import torch
import torchvision
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim
import numpy as np
import tqdm
import yaml
from skimage.measure import label, regionprops
from lineDetection.dataloader import get_loader
from lineDetection.logger import Logger
from lineDetection.model.network import Net
from lineDetection.utils import reverse_mapping, visulize_mapping, edge_align, get_boundary_point
from mmcv import Config
from mmcv.runner import load_checkpoint
from mmseg.apis import inference_segmentor, init_segmentor, show_result_pyplot
from segmentation.agronav_mobilenetv3 import *
def parse_arguments():
parser = argparse.ArgumentParser(description='Semantic Segmentation and Semantic-Line Detection')
parser.add_argument('--input_dir', default='./inference/input/', help="Input images diretory")
parser.add_argument('--segmentation_model', default='./segmentation/checkpoint/MobileNetV3.pth',
help='Semantic segmentation model file')
parser.add_argument('--segmentation_config', default='./segmentation/agronav_mobilenetv3.py',
help='Semantic segmentation config file')
parser.add_argument('--line_detection_model', default='./lineDetection/checkpoint/model_best.pth',
help='Semantic-line detection model file')
parser.add_argument('--align', default=False, action='store_true', help='Enable alignment')
parser.add_argument(
'--palette',
default='cityscapes',
help='Color palette used for segmentation map')
parser.add_argument('--output_dir', default='./inference/output/', help='Output images directory')
return parser.parse_args()
args = parse_arguments()
# Semantic Segmentation
# importlib.import_module(parser.get_default('segmentation_config'), package='agronav')
# segmentation_cfg = cfg
# segmentation_cfg = Config.fromfile('segmentation/configs/mobilenet_v3/mobilenet_v3_large_1x_coco.py')
segmentation_model = init_segmentor(segmentation_cfg, checkpoint=None, device='cuda:0')
segmentation_cfg.load_from = args.segmentation_model
checkpoint = load_checkpoint(segmentation_model, args.segmentation_model, map_location='cpu')
if 'CLASSES' in checkpoint.get('meta', {}):
segmentation_model.CLASSES = checkpoint['meta']['CLASSES']
else:
segmentation_model.CLASSES = get_classes(args.palette)
# segmentation_checkpoint = load_checkpoint(segmentation_model, args.segmentation_model, map_location='cpu')
# Semantic-Line Detection
line_detection_cfg = yaml.full_load(open('lineDetection/config.yml'))
line_detection_model = Net(numAngle=line_detection_cfg["MODEL"]["NUMANGLE"],
numRho=line_detection_cfg["MODEL"]["NUMRHO"],
backbone=line_detection_cfg["MODEL"]["BACKBONE"])
line_detection_model = line_detection_model.cuda(device=line_detection_cfg["TRAIN"]["GPU_ID"])
line_detection_checkpoint = torch.load(args.line_detection_model)
def main():
if 'state_dict' in line_detection_checkpoint.keys():
line_detection_model.load_state_dict(line_detection_checkpoint['state_dict'])
else:
line_detection_model.load_state_dict(line_detection_checkpoint)
# Data Loader
test_loader = get_loader(line_detection_cfg["DATA"]["TEST_DIR"], line_detection_cfg["DATA"]["TEST_LABEL_FILE"],
batch_size=1, num_thread=line_detection_cfg["DATA"]["WORKERS"], test=True)
# Semantic Segmentation
ntime = 0
for filename in os.listdir(args.input_dir):
if filename.endswith(".jpg") or filename.endswith(".JPG") or filename.endswith(".png"):
image = mmcv.imread(args.input_dir + filename)
# width = 800
# image = cv2.resize(image, (width, int((width / image.shape[1]) * image.shape[0])))
t = time.time()
segmentation_result = inference_segmentor(segmentation_model, image)
ntime += (time.time() - t)
output_file = os.path.join('inference/temp/', os.path.split(filename)[1])
show_result_pyplot(segmentation_model, image, segmentation_result, palette, show=False, opacity=0.5,
out_file=output_file)
continue
else:
continue
print('Semantic segmentation time for total images: %.6f' % ntime)
# Extract and save file names
with open('./inference/inference_filenames.txt', 'w') as f:
for file in glob.glob(os.path.join('inference/temp', '*.jpg')):
filename = os.path.split(file)[1]
filename = os.path.splitext(filename)[0]
f.write('temp/' + filename + "\n")
# Semantic-Line Detection
total_time = run_line_detection(test_loader, line_detection_model, args)
# Visualize centerline
save_centerline(args.output_dir, 'inference/output_centerline/')
def run_line_detection(test_loader, model, args):
# Switch to evaluation mode
model.eval()
with torch.no_grad():
bar = tqdm.tqdm(test_loader)
iter_num = len(test_loader.dataset)
ftime = 0
ntime = 0
for i, data in enumerate(bar):
t = time.time()
images, names, size = data
images = images.cuda(device=line_detection_cfg["TRAIN"]["GPU_ID"])
# width = 800
# images = cv2.resize(images, (width, int((width / images.shape[1]) * images.shape[0])))
key_points = model(images)
key_points = torch.sigmoid(key_points)
ftime += (time.time() - t)
t = time.time()
binary_kmap = key_points.squeeze().cpu().numpy() > line_detection_cfg['MODEL']['THRESHOLD']
kmap_label = label(binary_kmap, connectivity=1)
props = regionprops(kmap_label)
plist = []
for prop in props:
plist.append(prop.centroid)
size = (size[0][0], size[0][1])
b_points = reverse_mapping(plist, numAngle=line_detection_cfg["MODEL"]["NUMANGLE"],
numRho=line_detection_cfg["MODEL"]["NUMRHO"],
size=(400, 400))
scale_w = size[1] / 400
scale_h = size[0] / 400
for i in range(len(b_points)):
y1 = int(np.round(b_points[i][0] * scale_h))
x1 = int(np.round(b_points[i][1] * scale_w))
y2 = int(np.round(b_points[i][2] * scale_h))
x2 = int(np.round(b_points[i][3] * scale_w))
if x1 == x2:
angle = -np.pi / 2
else:
angle = np.arctan((y1 - y2) / (x1 - x2))
(x1, y1), (x2, y2) = get_boundary_point(y1, x1, angle, size[0], size[1])
b_points[i] = (y1, x1, y2, x2)
vis = visulize_mapping(b_points, size[::-1], names[0])
output_path = os.path.join(args.output_dir, names[0])
cv2.imwrite(output_path, vis)
np_data = np.array(b_points)
np.save(os.path.join(args.output_dir, names[0].split('/')[-1].split('.')[0]), np_data)
if line_detection_cfg["MODEL"]["EDGE_ALIGN"] and args.align:
for i in range(len(b_points)):
b_points[i] = edge_align(b_points[i], names[0], size, division=5)
vis = visulize_mapping(b_points, size, names[0])
cv2.imwrite(join(args.output_image + '_align.png'), vis)
np_data = np.array(b_points)
ntime += (time.time() - t)
# print('Forward time for total images: %.6f' % ftime)
print('Line detection time for total images: %.6f' % ntime)
return ftime + ntime
def save_centerline(read_path, output_path):
# for file in glob.glob(os.path.join(read_path, '*')):
for file in glob.glob(read_path + "*.jpg"):
filename = os.path.split(file)[1]
filename = os.path.splitext(filename)[0]
# image = Image.open(file)
img = cv2.imread(file)
height = img.shape[0]
width = img.shape[1]
data = np.load(read_path + filename + '.npy')
line1_p1 = data[0][0:2] # y, x
line1_p2 = data[0][2:4]
line2_p1 = data[1][0:2]
line2_p2 = data[1][2:4]
if line1_p1[1] > line1_p2[1]:
line1_x = line1_p2[1] + 1 / 3 * (line1_p1[1] - line1_p2[1])
elif line1_p1[1] < line1_p2[1]:
line1_x = line1_p2[1] - 1 / 3 * (line1_p2[1] - line1_p1[1])
else:
line1_x = line1_p1[1]
if line2_p1[1] > line2_p2[1]:
line2_x = line2_p2[1] + 1 / 3 * (line2_p1[1] - line2_p2[1])
elif line2_p1[1] < line2_p2[1]:
line2_x = line2_p2[1] - 1 / 3 * (line2_p2[1] - line2_p1[1])
else:
line2_x = line2_p1[1]
centerline_p1 = np.array([2 * height / 3, 0.5 * (line1_x + line2_x)])
centerline_p2 = np.array([height, 0.5 * (line1_p2[1] + line2_p2[1])])
start_point = (int(centerline_p1[1]), int(centerline_p1[0]))
end_point = (int(centerline_p2[1]), int(centerline_p2[0]))
color = (0, 0, 255)
thickness = 10
cv2.line(img, start_point, end_point, color, thickness)
cv2.imwrite(output_path + filename + '_cl.jpg', img)
if __name__ == '__main__':
main()