-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviz.py
124 lines (103 loc) · 3.74 KB
/
viz.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
import numpy as np
import torch
from torch import nn
from model2 import PatchModel
from params import SlotAttentionParams
import os
from data_voc import VOCSegmentation
from data2 import DinoValTransforms, TrainTransforms
from matplotlib import pyplot as plt
from torchvision import transforms
create_pascal_label_colormap=np.array([
[255, 0, 0],
[0, 128, 0],
[0, 0, 128],
[128, 255, 0],
[0, 0, 255],
[128, 0, 255],
[0, 128, 255],
[255, 0, 255],
[255, 0, 31],
[0, 184, 255],
[0, 214, 255],
[255, 0, 112],
[92, 255, 0],
[0, 224, 255],
[112, 224, 255],
[70, 184, 160],
[71, 255, 0],
[255, 0, 163],
[255, 204, 0],
[255, 0, 143],
[0, 255, 235],
[0, 255, 204]])
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
img=torch.randn(4,3,320,320).to(device)
dino=torch.randn(4,3,320,320).to(device)
params = SlotAttentionParams()
val = VOCSegmentation(
root=params.data_root,
# segments_dir=params.data_root+'/VOCdevkit/VOC2007/SegmentationClass',
year='2012',
image_set='val',
transform=TrainTransforms().transforms,
dino_transform=DinoValTransforms().transforms,
evo=True
)
val_dataloader = torch.utils.data.DataLoader(
val, batch_size=1, shuffle=True,
num_workers=params.num_workers)
class Model2(nn.Module):
def __init__(self):
super().__init__()
self.model = PatchModel(num_slots=params.num_slots,
num_iterations=3,
empty_cache=params.empty_cache,
slot_size=384,
masking=params.masking_ratio)
def forward(self, x, y):
return self.model(x, y)
model_best = Model2()
device = torch.device(device)
val = 42886497
addr = f'/home/rishavp/projects/def-mpederso/rishavp/object_unsup/lightning_logs/version_{val}/checkpoints/'+os.listdir(
f'/home/rishavp/projects/def-mpederso/rishavp/object_unsup/lightning_logs/version_{val}/checkpoints/')[-1]
ckpt_key = torch.load(addr)
model_best.load_state_dict(ckpt_key['state_dict'])
model_best.eval()
model_best.to(device)
def visualize_seg_mask(image: np.ndarray, mask: np.ndarray, filename):
"visualize the masks"
# print(image.shape)
color_seg = np.zeros((mask.shape[0], mask.shape[1], 3), dtype=np.uint8)
palette = np.array(create_pascal_label_colormap)
for label, color in enumerate(palette):
color_seg[mask == label, :] = color
color_seg = color_seg[..., ::-1] # convert to BGR
tmg = image*0.98 + color_seg * 0.02 # plot the image with the segmentation map
plt.axis("off")
plt.imshow(tmg)
plt.savefig(filename,bbox_inches='tight')
invTrans = transforms.Compose([transforms.Normalize(
mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],
std=[1/0.229, 1/0.224, 1/0.255]
) # ,
# transforms.Resize((320,320))
])
#If you want a custom image feel free to pass it accordingly
img, dino,label=next(iter(val_dataloader))
img,dino,label=img.to(device),dino.to(device),label.to(device)
invTrans = transforms.Compose([transforms.Normalize(
mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],
std=[1/0.229, 1/0.224, 1/0.255]
) # ,
# transforms.Resize((320,320))
])
reconstruction, mask, mask_as_image, target, slots = model_best(img, dino)
mskb = torch.argmax(mask_as_image[0], dim=0).detach()
img = invTrans(img)
image = img[0].permute(1, 2, 0).cpu().numpy()
plt.axis("off")
plt.imshow(image)
plt.savefig('/home/rishavp/projects/def-mpederso/rishavp/raw_img.png',bbox_inches='tight')
visualize_seg_mask(image,mskb.cpu().numpy(),'/home/rishavp/projects/def-mpederso/rishavp/mask.png')