This repository was archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathdemo.py
69 lines (57 loc) · 2.43 KB
/
demo.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
import argparse
import matplotlib.pyplot as plt
import chainer
from chainercv.datasets import ade20k_semantic_segmentation_label_colors
from chainercv.datasets import ade20k_semantic_segmentation_label_names
from chainercv.datasets import cityscapes_semantic_segmentation_label_colors
from chainercv.datasets import cityscapes_semantic_segmentation_label_names
from chainercv.datasets import voc_semantic_segmentation_label_colors
from chainercv.datasets import voc_semantic_segmentation_label_names
from chainercv.links import DeepLabV3plusXception65
from chainercv import utils
from chainercv.visualizations import vis_image
from chainercv.visualizations import vis_semantic_segmentation
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--pretrained-model')
parser.add_argument('--min-input-size', type=int, default=None)
parser.add_argument(
'--dataset', choices=('cityscapes', 'ade20k', 'voc'),
default='cityscapes')
parser.add_argument('image')
args = parser.parse_args()
if args.dataset == 'cityscapes':
if args.pretrained_model is None:
args.pretrained_model = 'cityscapes'
label_names = cityscapes_semantic_segmentation_label_names
colors = cityscapes_semantic_segmentation_label_colors
elif args.dataset == 'ade20k':
if args.pretrained_model is None:
args.pretrained_model = 'ade20k'
label_names = ade20k_semantic_segmentation_label_names
colors = ade20k_semantic_segmentation_label_colors
elif args.dataset == 'voc':
if args.pretrained_model is None:
args.pretrained_model = 'voc'
label_names = voc_semantic_segmentation_label_names
colors = voc_semantic_segmentation_label_colors
model = DeepLabV3plusXception65(
pretrained_model=args.pretrained_model,
min_input_size=args.min_input_size)
if args.gpu >= 0:
chainer.cuda.get_device_from_id(args.gpu).use()
model.to_gpu()
img = utils.read_image(args.image, color=True)
labels = model.predict([img])
label = labels[0]
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
vis_image(img, ax=ax1)
ax2 = fig.add_subplot(1, 2, 2)
# Do not overlay the label image on the color image
vis_semantic_segmentation(
None, label, label_names, colors, ax=ax2)
plt.show()
if __name__ == '__main__':
main()