-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdemo_panoseg.py
106 lines (83 loc) · 3.89 KB
/
demo_panoseg.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
# --------------------------------------------------------
# X-Decoder -- Generalized Decoding for Pixel, Image, and Language
# Copyright (c) 2022 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Xueyan Zou (xueyan@cs.wisc.edu)
# --------------------------------------------------------
import os
import sys
import logging
pth = '/'.join(sys.path[0].split('/')[:-1])
sys.path.insert(0, pth)
from PIL import Image
import numpy as np
np.random.seed(1)
import torch
from torchvision import transforms
from utils.arguments import load_opt_command
from detectron2.data import MetadataCatalog
from detectron2.utils.colormap import random_color
from openseed.BaseModel import BaseModel
from openseed import build_model
from utils.visualizer import Visualizer
logger = logging.getLogger(__name__)
def main(args=None):
'''
Main execution point for PyLearn.
'''
opt, cmdline_args = load_opt_command(args)
if cmdline_args.user_dir:
absolute_user_dir = os.path.abspath(cmdline_args.user_dir)
opt['user_dir'] = absolute_user_dir
# META DATA
pretrained_pth = os.path.join(opt['WEIGHT'])
output_root = './output'
image_pth = 'images/street.jpg'
model = BaseModel(opt, build_model(opt)).from_pretrained(pretrained_pth).eval().cuda()
t = []
t.append(transforms.Resize(512, interpolation=Image.BICUBIC))
transform = transforms.Compose(t)
thing_classes = ['car','person','traffic light', 'truck', 'motorcycle']
stuff_classes = ['building','sky','street','tree','rock','sidewalk']
thing_colors = [random_color(rgb=True, maximum=255).astype(np.int).tolist() for _ in range(len(thing_classes))]
stuff_colors = [random_color(rgb=True, maximum=255).astype(np.int).tolist() for _ in range(len(stuff_classes))]
thing_dataset_id_to_contiguous_id = {x:x for x in range(len(thing_classes))}
stuff_dataset_id_to_contiguous_id = {x+len(thing_classes):x for x in range(len(stuff_classes))}
MetadataCatalog.get("demo").set(
thing_colors=thing_colors,
thing_classes=thing_classes,
thing_dataset_id_to_contiguous_id=thing_dataset_id_to_contiguous_id,
stuff_colors=stuff_colors,
stuff_classes=stuff_classes,
stuff_dataset_id_to_contiguous_id=stuff_dataset_id_to_contiguous_id,
)
model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(thing_classes + stuff_classes, is_eval=False)
metadata = MetadataCatalog.get('demo')
model.model.metadata = metadata
model.model.sem_seg_head.num_classes = len(thing_classes + stuff_classes)
with torch.no_grad():
image_ori = Image.open(image_pth).convert("RGB")
width = image_ori.size[0]
height = image_ori.size[1]
image = transform(image_ori)
image = np.asarray(image)
image_ori = np.asarray(image_ori)
images = torch.from_numpy(image.copy()).permute(2,0,1).cuda()
batch_inputs = [{'image': images, 'height': height, 'width': width}]
outputs = model.forward(batch_inputs)
visual = Visualizer(image_ori, metadata=metadata)
pano_seg = outputs[-1]['panoptic_seg'][0]
pano_seg_info = outputs[-1]['panoptic_seg'][1]
for i in range(len(pano_seg_info)):
if pano_seg_info[i]['category_id'] in metadata.thing_dataset_id_to_contiguous_id.keys():
pano_seg_info[i]['category_id'] = metadata.thing_dataset_id_to_contiguous_id[pano_seg_info[i]['category_id']]
else:
pano_seg_info[i]['isthing'] = False
pano_seg_info[i]['category_id'] = metadata.stuff_dataset_id_to_contiguous_id[pano_seg_info[i]['category_id']]
demo = visual.draw_panoptic_seg(pano_seg.cpu(), pano_seg_info) # rgb Image
if not os.path.exists(output_root):
os.makedirs(output_root)
demo.save(os.path.join(output_root, 'pano.png'))
if __name__ == "__main__":
main()
sys.exit(0)