-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlotKITTI.py
132 lines (101 loc) · 3.53 KB
/
PlotKITTI.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
import os
import argparse
from tqdm import tqdm
import time
import datetime
import torch.optim as optim
import torch.nn as nn
import random
import matplotlib.pyplot as plt
import scipy.optimize
import torch.nn.functional as F
import numpy as np
from datasetKITTIEval import KITTIDataset
from models.model_bg import SlotAttentionAutoEncoder
import math
import cv2
from matplotlib.patches import Polygon
import torch
from torch.nn.parameter import Parameter
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
resolution = (368, 1248)
def load_model(model, state_dict):
own_state = model.state_dict()
for name, param in state_dict.items():
if name not in own_state:
continue
if isinstance(param, Parameter):
# backwards compatibility for serialized parameters
param = param.data
if own_state[name].data.shape!=param.shape:
print(name)
continue
own_state[name].copy_(param)
evalname = 'infer/DIOD_KITTI_100'
if not os.path.exists(evalname):
os.mkdir(evalname)
model_path = '/home/users/skara/check_release/checkpoints/DIODKITTI_100.ckpt'
data_path = '/home/data/skara/KITTI_DOM/KITTI_DOM_test'
test_set = KITTIDataset(split = 'test', root = data_path)
model = SlotAttentionAutoEncoder(resolution, 45, 64, 3).to(device)
model = nn.DataParallel(model)
model.load_state_dict(torch.load(model_path)['model_state_dict'])
cmap = plt.get_cmap('rainbow')
tk = 44
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(7)
np.random.shuffle(colors_rain)
np.random.shuffle(colors)
print('model load finished!')
for param in model.module.parameters():
param.requires_grad = False
for k in range(len(test_set)):
print(k)
sample = test_set[k]
image = sample['image'].to(device)
image = image.unsqueeze(0)
image = image.unsqueeze(0)
image = image.repeat(1,5,1,1,1)
recon_combined, masks,_, slots = model(image)
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,92,312))
masks = masks.detach()
masks = masks[0]
image = image[0]
for j in range(1):
image_j = image[j].permute(1,2,0).cpu().numpy()
image_j = image_j * 0.5 + 0.5
masks_j = masks[j]
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)
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 cmax is not None:
polygon = Polygon(
cmax.reshape((-1, 2)),
fill=True, facecolor=colors[seg],
edgecolor='r', linewidth=0.0,
alpha=0.65)
ax.add_patch(polygon)
fig.savefig('{}/scene-{}-frame-{}.png'.format(evalname,k,j))
plt.close(fig)