forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deeplabv3.py
228 lines (186 loc) · 6.84 KB
/
deeplabv3.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
import time
import sys
import cv2
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import gridspec
import ailia
from deeplab_utils import *
# import original modules
sys.path.append('../../util')
from utils import get_base_parser, update_parser, get_savepath # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from image_utils import load_image # noqa: E402
import webcamera_utils # noqa: E402
# logger
from logging import getLogger # noqa: E402
logger = getLogger(__name__)
# ======================
# PARAMETERS
# ======================
IMAGE_PATH = 'couple.jpg'
SAVE_IMAGE_PATH = 'output.png'
LABEL_NAMES = np.asarray([
'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv'
])
CLASS_NUM = 21
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
assert CLASS_NUM == len(LABEL_NAMES), 'The number of labels is incorrect.'
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
('DeepLab is a state-of-art deep learning model '
'for semantic image segmentation.'),
IMAGE_PATH,
SAVE_IMAGE_PATH,
)
parser.add_argument(
'-n', '--normal',
action='store_true',
help='By default, the optimized model is used, but with this option, ' +
'you can switch to the normal (not optimized) model'
)
args = update_parser(parser)
# ======================
# MODEL PARAMETERS
# ======================
if args.normal:
MODEL_PATH = 'deeplabv3.opt.onnx.prototxt'
WEIGHT_PATH = 'deeplabv3.opt.onnx'
else:
MODEL_PATH = 'deeplabv3.onnx.prototxt'
WEIGHT_PATH = 'deeplabv3.onnx'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/deeplabv3/'
# ======================
# Main functions
# ======================
def segment_from_image():
# net initialize
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id)
ailia_input_w = net.get_input_shape()[3]
ailia_input_h = net.get_input_shape()[2]
input_shape = [ailia_input_h, ailia_input_w]
# input image loop
for image_path in args.input:
# prepare input data
logger.info(image_path)
img = load_image(
image_path,
input_shape,
normalize_type='127.5',
gen_input_ailia=True,
)
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
total_time = 0
for i in range(args.benchmark_count):
start = int(round(time.time() * 1000))
preds_ailia = net.predict(img)[0]
end = int(round(time.time() * 1000))
logger.info(f'ailia processing time {end - start} ms')
if i != 0:
total_time = total_time + (end - start)
logger.info(f'\taverage time {total_time / (args.benchmark_count-1)} ms')
else:
preds_ailia = net.predict(img)[0]
# postprocessing
seg_map = np.argmax(preds_ailia.transpose(1, 2, 0), axis=2)
seg_image = label_to_color_image(seg_map).astype(np.uint8)
# save just segmented image (simple)
# seg_image = cv2.cvtColor(seg_image, cv2.COLOR_RGB2BGR)
# cv2.imwrite('seg_test.png', seg_image)
# save org_img, segmentation-map, segmentation-overlay
org_img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
org_img = cv2.resize(org_img, (seg_image.shape[1], seg_image.shape[0]))
plt.figure(figsize=(15, 5))
grid_spec = gridspec.GridSpec(1, 4, width_ratios=[6, 6, 6, 1])
plt.subplot(grid_spec[0])
plt.imshow(org_img)
plt.axis('off')
plt.title('input image')
plt.subplot(grid_spec[1])
plt.imshow(seg_image)
plt.axis('off')
plt.title('segmentation map')
plt.subplot(grid_spec[2])
plt.imshow(org_img)
plt.imshow(seg_image, alpha=0.7)
plt.axis('off')
plt.title('segmentation overlay')
unique_labels = np.unique(seg_map)
ax = plt.subplot(grid_spec[3])
plt.imshow(
FULL_COLOR_MAP[unique_labels].astype(np.uint8),
interpolation='nearest',
)
ax.yaxis.tick_right()
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
plt.xticks([], [])
ax.tick_params(width=0.0)
plt.grid('off')
savepath = get_savepath(args.savepath, image_path)
logger.info(f'saved at : {savepath}')
plt.savefig(savepath)
logger.info('Script finished successfully.')
def segment_from_video():
# net initialize
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id)
ailia_input_w = net.get_input_shape()[3]
ailia_input_h = net.get_input_shape()[2]
capture = webcamera_utils.get_capture(args.video)
# create video writer if savepath is specified as video format
if args.savepath != SAVE_IMAGE_PATH:
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
save_h, save_w = webcamera_utils.calc_adjust_fsize(
f_h, f_w, ailia_input_h, ailia_input_w
)
writer = webcamera_utils.get_writer(args.savepath, save_h, save_w)
else:
writer = None
while(True):
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
input_image, input_data = webcamera_utils.preprocess_frame(
frame, ailia_input_h, ailia_input_w, normalize_type='127.5'
)
# inference
input_blobs = net.get_input_blob_list()
net.set_input_blob_data(input_data, input_blobs[0])
net.update()
preds_ailia = np.array(net.get_results())[0, 0] # TODO why?
# postprocessing
seg_map = np.argmax(preds_ailia.transpose(1, 2, 0), axis=2)
seg_image = label_to_color_image(seg_map).astype(np.uint8)
# showing the segmented image (simple)
seg_image = cv2.cvtColor(seg_image, cv2.COLOR_RGB2BGR)
seg_image = cv2.resize(
seg_image, (input_image.shape[1], input_image.shape[0])
)
cv2.imshow('frame', seg_image)
# save results
if writer is not None:
writer.write(seg_image)
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
if args.video is not None:
# video mode
segment_from_video()
else:
# image mode
segment_from_image()
if __name__ == '__main__':
main()