fffiloni commited on
Commit
f1fbc90
1 Parent(s): d561f97

Create open_pano.py

Browse files
Files changed (1) hide show
  1. tasks/open_pano.py +70 -0
tasks/open_pano.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --------------------------------------------------------
2
+ # X-Decoder -- Generalized Decoding for Pixel, Image, and Language
3
+ # Copyright (c) 2022 Microsoft
4
+ # Licensed under The MIT License [see LICENSE for details]
5
+ # Written by Xueyan Zou (xueyan@cs.wisc.edu)
6
+ # --------------------------------------------------------
7
+
8
+ import torch
9
+ import numpy as np
10
+ from PIL import Image
11
+ from torchvision import transforms
12
+ from utils.visualizer import Visualizer
13
+ from detectron2.utils.colormap import random_color
14
+ from detectron2.data import MetadataCatalog
15
+
16
+
17
+ t = []
18
+ t.append(transforms.Resize(512, interpolation=Image.BICUBIC))
19
+ transform = transforms.Compose(t)
20
+ metadata = MetadataCatalog.get('ade20k_panoptic_train')
21
+
22
+ def open_panoseg(model, image, texts, inpainting_text, *args, **kwargs):
23
+ stuff_classes = [x.strip() for x in texts.split(';')[0].replace('stuff:','').split(',')]
24
+ thing_classes = [x.strip() for x in texts.split(';')[1].replace('thing:','').split(',')]
25
+ thing_colors = [random_color(rgb=True, maximum=255).astype(np.int32).tolist() for _ in range(len(thing_classes))]
26
+ stuff_colors = [random_color(rgb=True, maximum=255).astype(np.int32).tolist() for _ in range(len(stuff_classes))]
27
+ thing_dataset_id_to_contiguous_id = {x:x for x in range(len(thing_classes))}
28
+ stuff_dataset_id_to_contiguous_id = {x+len(thing_classes):x for x in range(len(stuff_classes))}
29
+
30
+ MetadataCatalog.get("demo").set(
31
+ thing_colors=thing_colors,
32
+ thing_classes=thing_classes,
33
+ thing_dataset_id_to_contiguous_id=thing_dataset_id_to_contiguous_id,
34
+ stuff_colors=stuff_colors,
35
+ stuff_classes=stuff_classes,
36
+ stuff_dataset_id_to_contiguous_id=stuff_dataset_id_to_contiguous_id,
37
+ )
38
+ model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(thing_classes + stuff_classes + ["background"], is_eval=True)
39
+ metadata = MetadataCatalog.get('demo')
40
+ model.model.metadata = metadata
41
+ model.model.sem_seg_head.num_classes = len(thing_classes + stuff_classes)
42
+
43
+ with torch.no_grad():
44
+ image_ori = transform(image)
45
+ width = image_ori.size[0]
46
+ height = image_ori.size[1]
47
+ image = transform(image_ori)
48
+ image = np.asarray(image)
49
+ images = torch.from_numpy(image.copy()).permute(2,0,1).cuda()
50
+
51
+ batch_inputs = [{'image': images, 'height': height, 'width': width}]
52
+ outputs = model.forward(batch_inputs)
53
+ visual = Visualizer(image_ori, metadata=metadata)
54
+
55
+ pano_seg = outputs[-1]['panoptic_seg'][0]
56
+ pano_seg_info = outputs[-1]['panoptic_seg'][1]
57
+
58
+ for i in range(len(pano_seg_info)):
59
+ if pano_seg_info[i]['category_id'] in metadata.thing_dataset_id_to_contiguous_id.keys():
60
+ pano_seg_info[i]['category_id'] = metadata.thing_dataset_id_to_contiguous_id[pano_seg_info[i]['category_id']]
61
+ else:
62
+ pano_seg_info[i]['isthing'] = False
63
+ pano_seg_info[i]['category_id'] = metadata.stuff_dataset_id_to_contiguous_id[pano_seg_info[i]['category_id']]
64
+
65
+ demo = visual.draw_panoptic_seg(pano_seg.cpu(), pano_seg_info) # rgb Image
66
+ res = demo.get_image()
67
+
68
+ MetadataCatalog.remove('demo')
69
+ torch.cuda.empty_cache()
70
+ return Image.fromarray(res), '', None