-
Notifications
You must be signed in to change notification settings - Fork 132
/
demo_refseg.py
executable file
·90 lines (69 loc) · 3.03 KB
/
demo_refseg.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
# --------------------------------------------------------
# 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 ([email protected])
# --------------------------------------------------------
import os
import sys
import json
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(27)
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 detectron2.data.datasets.builtin_meta import COCO_CATEGORIES
from xdecoder.BaseModel import BaseModel
from xdecoder import build_model
from utils.visualizer import Visualizer
from utils.distributed import init_distributed
# logging.basicConfig(level = logging.INFO)
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
opt = init_distributed(opt)
# META DATA
pretrained_pth = os.path.join(opt['WEIGHT'])
output_root = './output'
image_pth = 'images/fruit.jpg'
text = [['The larger watermelon.'], ['The front white flower.'], ['White tea pot.'], ['Flower bunch.'], ['white vase.'], ['The left peach.'], ['The brown knife.']]
model = BaseModel(opt, build_model(opt)).from_pretrained(pretrained_pth).eval().cuda()
model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(["background", "background"], is_eval=False)
t = []
t.append(transforms.Resize(512, interpolation=Image.BICUBIC))
transform = transforms.Compose(t)
metadata = MetadataCatalog.get('ade20k_panoptic_train')
model.model.metadata = metadata
with torch.no_grad():
image_ori = Image.open(image_pth)
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, 'groundings': {'texts': text}}]
outputs = model.model.evaluate_grounding(batch_inputs, None)
visual = Visualizer(image_ori, metadata=metadata)
grd_mask = (outputs[0]['grounding_mask'] > 0).float().cpu().numpy()
for idx, mask in enumerate(grd_mask):
demo = visual.draw_binary_mask(mask, color=random_color(rgb=True, maximum=1).astype(np.int).tolist(), text=text[idx], alpha=0.3)
output_folder = os.path.join(os.path.join(output_root))
if not os.path.exists(output_folder):
os.makedirs(output_folder)
demo.save(os.path.join(output_folder, 'refseg.png'))
if __name__ == "__main__":
main()
sys.exit(0)