-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
226 lines (185 loc) · 8.61 KB
/
test.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import torch
import torch.utils.data as data
import os
import argparse
from utils.utility import *
from config import cfg
from models.model import build_dtfvos
from datasets import multibatch_collate_fn
from datasets.davis import DAVIS_Test
from datasets.youtubevos import YouTube_Test
def parse_args():
parser = argparse.ArgumentParser('Testing Mask Segmentation')
parser.add_argument('--checkpoint', default='', type=str, help='checkpoint to test the network')
parser.add_argument('--results', default='results', type=str, help='result directory')
parser.add_argument('--gpu', default='0', type=str, help='set gpu id to test the network')
return parser.parse_args()
def main(args):
# Use CUDA
use_gpu = torch.cuda.is_available() and int(args.gpu) >= 0
device = 'cuda:{}'.format(args.gpu) if use_gpu else 'cpu'
# Data
print('==> Preparing dataset')
data_name = cfg.DATA.TEST.DATASET_NAME
if data_name == 'DAVIS17':
testset = DAVIS_Test(os.path.join(cfg.DATA.DAVIS_ROOT, 'DAVIS-test-dev'), img_set='2017/test-dev.txt')
elif data_name == 'YTBVOS':
testset = YouTube_Test(cfg.DATA.YTBVOS_ROOT, (480, 854))
else:
raise NameError
testloader = data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=cfg.TRAIN.NUM_WORKER, collate_fn=multibatch_collate_fn)
# Model
print("==> creating model")
net = build_dtfvos(cfg)
print('==> Total params: %.2fM' % (sum(p.numel() for p in net.parameters())/1000000.0))
# set eval to freeze batchnorm update
net.eval()
net.to(device)
# set testing parameters
for p in net.parameters():
p.requires_grad = False
# Weights
if args.checkpoint:
# Load checkpoint.
logger.info('==> Loading checkpoint {}'.format(args.checkpoint))
assert os.path.isfile(args.checkpoint), 'Error: no checkpoint directory found!'
checkpoint = torch.load(args.checkpoint, map_location=device)
state = checkpoint['state_dict']
net.load_param(state)
# Test
print('==> Runing model on dataset, totally {:d} videos'.format(len(testloader)))
if data_name == 'DAVIS17':
test_DAVIS(
testloader,
model=net,
device=device)
elif data_name == 'YTBVOS':
test_YouTube(
testloader,
model=net,
device=device)
else:
raise NotImplementedError()
print('==> Results are saved at: {}'.format(args.results))
def test_DAVIS(testloader, model, device):
with torch.no_grad():
for batch_idx, data in enumerate(testloader):
frames, masks, objs, infos = data
frames = frames.to(device)
masks = masks.to(device)
frames = frames[0]
masks = masks[0]
n_obj = objs[0]
info = infos[0]
original_size = info['original_size']
T, _, _, _ = frames.shape
print('==>Runing video {}, objects {:d}'.format(info['name'], n_obj-1))
# process reference pairs
first_pairs = {
'frame': frames[0:1], # [1 x 3 x H x W]
'obj_mask': masks[0:1, 1:n_obj] # [1 x no x H x W]
}
first_feats_dict = model.extract_ref_feats(first_pairs)
# segment frames
vid_writer = VideoWriter(args.results, info['name'])
vid_writer.write(f'{0:05d}.png', masks[0:1], original_size)
for i in range(1, T):
if i > 1:
previous_feats_dict = model.extract_ref_feats(previous_pairs)
ref_feats_dict = model.concat_features([first_feats_dict, previous_feats_dict])
# if i == 5:
# first_feats_dict = model.concat_features([first_feats_dict, previous_feats_dict])
else:
ref_feats_dict = first_feats_dict
seg_feats_dict, median_layers = model.extract_seg_feats(frames[i:i+1])
seq_dict = model.concat_features([ref_feats_dict, seg_feats_dict], expand=True)
hs, enc_mem = model.forward_transformer(seq_dict)
logits = model.segment(hs, enc_mem, seq_dict, median_layers, masks[0:1]) # [1, M, H, W]
out = torch.softmax(logits, dim=1)
vid_writer.write(f'{i:05d}.png', out, original_size)
previous_pairs = {
'frame': frames[i:i+1],
'obj_mask': out[:, 1:n_obj]
}
def test_YouTube(testloader, model, device):
with torch.no_grad():
for batch_idx, data in enumerate(testloader):
frames, masks, objs, infos = data
frames = frames.to(device)
masks = masks.to(device)
frames = frames[0]
masks = masks[0]
obj_n = objs[0]
info = infos[0]
T, _, _, _ = frames.shape
print('==>Runing video {}, objects {:d}'.format(info['name'], obj_n-1))
obj_n = obj_n.item()
obj_st = info['obj_st']
obj_vis = info['obj_vis']
vid_name = info['name']
original_size = info['original_size']
basename_list = info['basename_list']
basename_to_save = info['basename_to_save']
# Compose the first mask
pred_mask = torch.zeros_like(masks).unsqueeze(0).float()
for i in range(1, obj_n):
if obj_st[i] == 0:
pred_mask[0, i] = masks[i]
pred_mask[0, 0] = 1 - pred_mask.sum(dim=1)
# process reference pairs
first_pairs = {
'frame': frames[0:1], # [1 x 3 x H x W]
'obj_mask': pred_mask[0:1, 1:obj_n] # [1 x no x H x W]
}
previous_pairs = None
vid_writer = VideoWriter(args.results, vid_name)
vid_writer.write(basename_list[0]+'.png', pred_mask, original_size)
for t in range(1, T):
if previous_pairs is not None:
previous_feats_dict = model.extract_ref_feats(previous_pairs)
ref_feats_dict = model.concat_features([first_feats_dict, previous_feats_dict])
else:
first_feats_dict = model.extract_ref_feats(first_pairs)
ref_feats_dict = first_feats_dict
seg_feats_dict, median_layers = model.extract_seg_feats(frames[t:t+1])
seq_dict = model.concat_features([ref_feats_dict, seg_feats_dict], expand=True)
hs, enc_mem = model.forward_transformer(seq_dict)
score = model.segment(hs, enc_mem, seq_dict, median_layers, masks.unsqueeze(0)) # [1, M, H, W]
reset_list = list()
for i in range(1, obj_n):
# If this object is invisible.
if obj_vis[t, i] == 0:
score[0, i] = -1000
# If this object appears, reset the score map
if obj_st[i] == t:
reset_list.append(i)
score[0, i] = -1000
score[0, i][masks[i]] = 1000
for j in range(obj_n):
if j != i:
score[0, j][masks[i]] = -1000
pred_mask = torch.softmax(score, dim=1)
if basename_list[t] in basename_to_save:
vid_writer.write(basename_list[t]+'.png', pred_mask, original_size)
if len(reset_list) > 0:
first_pairs = {
'frame': frames[t:t+1], # [1 x 3 x H x W]
'obj_mask': pred_mask[0:1, 1:obj_n] # [1 x no x H x W]
}
previous_pairs = None
else:
previous_pairs = {
'frame': frames[t:t+1],
'obj_mask': pred_mask[0:1, 1:obj_n]
}
if __name__ == '__main__':
args = parse_args()
data_name = cfg.DATA.TEST.DATASET_NAME
if data_name == 'DAVIS17':
data_name = 'DAVIS17-test-dev'
print('==> Test dataset: {}'.format(data_name))
args.results = os.path.join(args.results, data_name)
print('==> Save directory: {}'.format(args.results))
if not os.path.exists(args.results):
os.makedirs(args.results)
main(args)