-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
141 lines (112 loc) · 4.26 KB
/
plot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import torch.nn as nn
import random
import matplotlib.pyplot as plt
import torch.nn.functional as F
import numpy as np
from matplotlib.patches import Polygon
import torch
import math
import cv2
from dataset.datasetPDEval import PDDataset
from models.model import SlotAttentionAutoEncoder
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
resolution = (480, 968)
# notice that you may try to change the number of frames in datasetPDEval to speed up plot
data_path = '/data/pd_v2/'
model_path = './pre-trained/pd-est.ckpt'
evalname = 'plot'
test_set = PDDataset(split = 'test', root = data_path)
model = SlotAttentionAutoEncoder(resolution, 45, 128, 3, 128).to(device)
model = nn.DataParallel(model)
model.load_state_dict(torch.load(model_path)['model_state_dict'])
print('model load finished!')
for param in model.module.parameters():
param.requires_grad = False
tk = 10
for k in range(len(test_set)):
sample = test_set[k]
image = sample['image'].to(device)
image = image.unsqueeze(0)
recon_combined, masks, slots, _,_, latents = model(image)
n_frames = image.shape[1]
recon_combined = recon_combined.view(1,n_frames,3,480,968)
index_mask = masks.argmax(dim = 2)
index_mask = F.one_hot(index_mask,num_classes = 45)
index_mask = index_mask.permute(0,1,4,2,3)
masks = masks * index_mask
image = F.interpolate(image, (3,120,242))
recon_combined = recon_combined.detach()
masks = masks.detach()
recon_combined, masks = recon_combined[0], masks[0]
image = image[0]
latents = latents.detach().cpu().numpy()
cmap = plt.get_cmap('rainbow')
colors = [cmap(i) for i in np.linspace(0, 1, tk)]
colors_rain = [cmap(i) for i in np.linspace(0, 1, 128)]
colors_rain = np.array(colors_rain)
np.random.seed(6)
np.random.shuffle(colors_rain)
np.random.shuffle(colors)
idx = None
for j in range(n_frames):
image_j = image[j].permute(1,2,0).cpu().numpy()
image_j = image_j * 0.5 + 0.5
recon_combined_j = recon_combined[j].permute(1,2,0).cpu().numpy()
recon_combined_j = recon_combined_j * 0.5 + 0.5
latents_j = latents[j]
masks_j = masks[j]
if idx is None:
scores = masks_j.sum(dim = 1).sum(dim = 1) / ((masks_j>0).float().sum(dim = 1).sum(dim = 1) + 0.000001)
_, idx = torch.topk(scores, tk)
masks_j = masks_j[idx,:,:]
masks_j = masks_j.cpu().numpy()
fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.axis('off')
fig.add_axes(ax)
recon_combined_j = recon_combined_j[:,:,-1::-1]
ax.imshow(recon_combined_j, alpha = 1)
fig.savefig('./{}/recon-{}-frame-{}.png'.format(evalname,k,j))
plt.close(fig)
fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.axis('off')
fig.add_axes(ax)
image_j = image_j[:,:,-1::-1]
ax.imshow(image_j, alpha = 1)
for seg in range(tk):
msk = masks_j[seg]
threshold = 0
e = (msk > threshold).astype('uint8')
contour, hier = cv2.findContours(
e.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
cmax = None
for c in contour:
if cmax is None:
cmax = c
if len(c) > len(cmax):
cmax = c
if (msk > 0).sum() > 10000:
continue
if cmax is None:
# print(j, seg)
continue
else:
polygon = Polygon(
cmax.reshape((-1, 2)),
fill=True, facecolor=colors[seg],
edgecolor='r', linewidth=0.0,
alpha=0.5)
ax.add_patch(polygon)
fig.savefig('./{}/slot-{}-frame-{}.png'.format(evalname,k,j))
plt.close(fig)
fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.axis('off')
fig.add_axes(ax)
tmp = latents_j.reshape((120*242,))
tmp = colors_rain[tmp]
tmp = tmp.reshape((120,242,4))
ax.imshow(tmp, alpha = 1)
fig.savefig('./{}/token-{}-frame-{}.png'.format(evalname,k,j))
plt.close(fig)