-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_ytvos.py
150 lines (116 loc) · 5.64 KB
/
submit_ytvos.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
import cv2, json, os
import numpy as np
import scipy.misc as sm
from torchvision import transforms, models
from torch.utils.data import DataLoader
from PIL import Image
import dataloader as dl
from networks import *
from sacred import Experiment
from sacred.observers import FileStorageObserver
ex = Experiment()
# where checkpoints are located
base_path = 'Model/'
# where the sacred experiment will locate
PATH = base_path + 'submission_info/'
ex.observers.append(FileStorageObserver.create(PATH))
device = 'cuda' if torch.cuda.is_available() else 'cpu'
@ex.config
def config():
model_num = None
tr_conf = {
'model_dir': base_path + model_num,
'submission_dir': PATH + 'SUBMISSION_DIR_{}/'.format(model_num),
'scores_dir': PATH + 'SCORES_{}/'.format(model_num),
'rgb_dir': '/ds/videos/YoutubeVOS2018/valid/JPEGImages/',
'ann_dir': '/ds/videos/YoutubeVOS2018/valid/Annotations/',
'meta_dir': '/ds/videos/YoutubeVOS2018/valid/meta.json',
'test_all': False,
}
model = nn.DataParallel(ModelMatch(epsilon=None, backbone=models.resnet50, num_classes=42).to(device))
class Submission:
def __init__(self, conf):
print('good luck with your submission! (updated validation set.)')
self.model_dir = conf['model_dir']
self.submission_dir = conf['submission_dir']
if not os.path.exists(self.submission_dir):
os.mkdir(self.submission_dir)
self.results_dir = conf['scores_dir']
if not os.path.exists(self.results_dir):
os.mkdir(self.results_dir)
self.rgb_dir = conf['rgb_dir']
self.ann_dir = conf['ann_dir']
with open(conf['meta_dir'], 'r') as f:
data = f.read()
self.meta = json.loads(data)
self.test_all = conf['test_all']
self.palette = [0, 0, 0, 128, 0, 0, 0, 128, 0, 128, 128, 0, 0, 0, 128, 128, 0, 128, 0, 128, 128, 128, 128, 128,
64, 0, 0, 191, 0, 0, 64, 128, 0, 191, 128, 0, 64, 0, 128]
self.conf = conf
def save_score_maps(self, test_loader):
c_p = torch.load(self.model_dir)
model.load_state_dict(c_p['model'])
model.eval()
with torch.no_grad():
for sequence in test_loader:
seq_name = sequence['seq_name'][0]
categories = list(sequence.keys())[1:]
if not os.path.exists(self.results_dir + seq_name):
os.makedirs(self.results_dir + seq_name)
save_path = self.results_dir + seq_name + '/'
for cat in categories:
rgb, mask, names = sequence[cat]['image'], sequence[cat]['first_mask'], sequence[cat]['name']
# save score map of gt
temp = os.path.splitext(names[0][0])[0]
np.save(save_path + temp + '_instance_%02d.npy' % int(cat), mask.squeeze().cpu().numpy())
predicted_masks = model(rgb, mask, mode='test')
for ii , decoded_img in enumerate(predicted_masks):
temp = os.path.splitext(names[ii+1][0])[0]
decoded_img = torch.sigmoid(decoded_img)
np.save(save_path + temp + '_instance_%02d.npy' % int(cat), decoded_img.squeeze().cpu().numpy())
def merge_score_maps(self, test_loader):
with torch.no_grad():
for sequence in test_loader:
seq_name = sequence['seq_name'][0]
mask_path = os.path.join(self.submission_dir, seq_name)
if not os.path.exists(mask_path):
os.mkdir(mask_path)
frames = sorted(os.listdir(self.rgb_dir + seq_name))
score_maps = sorted(os.listdir(self.results_dir + seq_name))
for f in frames:
f_score_list = []
f_ids = []
# get the score map and object id for each frame
for sm in score_maps:
if sm.startswith(f[:5]):
sm_path = os.path.join(self.results_dir, seq_name, sm)
# map & id
f_score_list.append(np.load(sm_path))
f_ids.append(int(sm[-6:-4]))
obj_ids_ext = np.array([0] + f_ids, dtype=np.uint8)
bg_score = np.ones((256, 448)) * 0.5
scores = [bg_score] + f_score_list
scores_all = np.stack(scores, axis=0)
pred_idx = scores_all.argmax(axis=0)
label_pred = obj_ids_ext[pred_idx]
res_im = Image.fromarray(label_pred, mode='P')
res_im.putpalette(self.palette)
res_im.save(os.path.join(mask_path, f[:5] + '.png'))
@ex.automain
def main(tr_conf):
im_res = [256, 448]
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
tr = {'image': transforms.Compose([transforms.Resize(im_res),
transforms.ToTensor(),
normalize]),
'gt': transforms.Compose([transforms.Resize(im_res)])}
test_set = dl.YoutubeVOS(mode='test',
json_path=tr_conf['meta_dir'],
im_path=tr_conf['rgb_dir'],
ann_path=tr_conf['ann_dir'],
transform=tr)
test_loader = DataLoader(test_set, batch_size=1, num_workers=0, shuffle=False, pin_memory=True)
sub = Submission(tr_conf)
sub.save_score_maps(test_loader)
sub.merge_score_maps(test_loader)