-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
35 lines (32 loc) · 1.2 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
import os
from torch.utils.data import DataLoader
from lib.dataset import Data
import torch.nn.functional as F
import torch
import cv2
import time
from net import Mnet
import numpy as np
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
if __name__ == '__main__':
model_path='./model/final.pth'
out_path = './output'
data = Data(root='/data/to/test',mode='test')
loader = DataLoader(data, batch_size=1,shuffle=False)
net = Mnet().cuda()
print('loading model from %s...' % model_path)
net.load_state_dict(torch.load(model_path))
if not os.path.exists(out_path): os.mkdir(out_path)
time_s = time.time()
img_num = len(loader)
net.eval()
with torch.no_grad():
for rgb, t, _ , (H, W), name in loader:
name = name[0].split('\\')[-1]
print(name)
score, score1, score2,score_g = net(rgb.cuda().float(), t.cuda().float())
score = F.interpolate(score, size=(H, W), mode='bilinear',align_corners=True)
pred = np.squeeze(torch.sigmoid(score).cpu().data.numpy())
cv2.imwrite(os.path.join(out_path, name[:-4] + '.png'), 255 * pred)
time_e = time.time()
print('speed: %f FPS' % (img_num / (time_e - time_s)))