-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathsemantic_segmentation.py
136 lines (108 loc) · 4.39 KB
/
semantic_segmentation.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
# Lint as: python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""An example of semantic segmentation.
The following command runs this script and saves a new image showing the
segmented pixels at the location specified by `output`:
```
bash examples/install_requirements.sh semantic_segmentation.py
python3 examples/semantic_segmentation.py \
--model test_data/deeplabv3_mnv2_pascal_quant_edgetpu.tflite \
--input test_data/bird.bmp \
--keep_aspect_ratio \
--output ${HOME}/segmentation_result.jpg
```
"""
import argparse
import numpy as np
from PIL import Image
from pycoral.adapters import common
from pycoral.adapters import segment
from pycoral.utils.edgetpu import make_interpreter
def create_pascal_label_colormap():
"""Creates a label colormap used in PASCAL VOC segmentation benchmark.
Returns:
A Colormap for visualizing segmentation results.
"""
colormap = np.zeros((256, 3), dtype=int)
indices = np.arange(256, dtype=int)
for shift in reversed(range(8)):
for channel in range(3):
colormap[:, channel] |= ((indices >> channel) & 1) << shift
indices >>= 3
return colormap
def label_to_color_image(label):
"""Adds color defined by the dataset colormap to the label.
Args:
label: A 2D array with integer type, storing the segmentation label.
Returns:
result: A 2D array with floating type. The element of the array
is the color indexed by the corresponding element in the input label
to the PASCAL color map.
Raises:
ValueError: If label is not of rank 2 or its value is larger than color
map maximum entry.
"""
if label.ndim != 2:
raise ValueError('Expect 2-D input label')
colormap = create_pascal_label_colormap()
if np.max(label) >= len(colormap):
raise ValueError('label value too large.')
return colormap[label]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--model', required=True,
help='Path of the segmentation model.')
parser.add_argument('--input', required=True,
help='File path of the input image.')
parser.add_argument('--output', default='semantic_segmentation_result.jpg',
help='File path of the output image.')
parser.add_argument(
'--keep_aspect_ratio',
action='store_true',
default=False,
help=(
'keep the image aspect ratio when down-sampling the image by adding '
'black pixel padding (zeros) on bottom or right. '
'By default the image is resized and reshaped without cropping. This '
'option should be the same as what is applied on input images during '
'model training. Otherwise the accuracy may be affected and the '
'bounding box of detection result may be stretched.'))
args = parser.parse_args()
interpreter = make_interpreter(args.model, device=':0')
interpreter.allocate_tensors()
width, height = common.input_size(interpreter)
img = Image.open(args.input)
if args.keep_aspect_ratio:
resized_img, _ = common.set_resized_input(
interpreter, img.size, lambda size: img.resize(size, Image.LANCZOS))
else:
resized_img = img.resize((width, height), Image.LANCZOS)
common.set_input(interpreter, resized_img)
interpreter.invoke()
result = segment.get_output(interpreter)
if len(result.shape) == 3:
result = np.argmax(result, axis=-1)
# If keep_aspect_ratio, we need to remove the padding area.
new_width, new_height = resized_img.size
result = result[:new_height, :new_width]
mask_img = Image.fromarray(label_to_color_image(result).astype(np.uint8))
# Concat resized input image and processed segmentation results.
output_img = Image.new('RGB', (2 * new_width, new_height))
output_img.paste(resized_img, (0, 0))
output_img.paste(mask_img, (width, 0))
output_img.save(args.output)
print('Done. Results saved at', args.output)
if __name__ == '__main__':
main()