-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
64 lines (52 loc) · 2.13 KB
/
test.py
File metadata and controls
64 lines (52 loc) · 2.13 KB
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
# coding=utf-8
import os
import time
import sys
sys.path.insert(0, '../')
sys.dont_write_bytecode = True
import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
import dataset as dataset
from Net import Net
class Test(object):
def __init__(self, Dataset, Network, path):
## dataset
self.cfg = Dataset.Config(dataset='PMD', datapath=path, snapshot='./PMD-model-best/model-best', mode='test')
self.data = Dataset.Data(self.cfg)
self.loader = DataLoader(self.data, batch_size=1, shuffle=False, num_workers=8)
## network
self.net = Network(self.cfg)
self.net.train(False)
self.net.cuda()
def save(self):
with torch.no_grad():
cost_time = list()
for image, mask, shape, name in self.loader:
image = image.cuda().float()
torch.cuda.synchronize()
start_time = time.perf_counter()
out, out_edge = self.net(image, shape)
torch.cuda.synchronize()
cost_time.append(time.perf_counter() - start_time)
pred = (torch.sigmoid(out[0, 0]) * 255).cpu().numpy()
edge = (torch.sigmoid(out_edge[0, 0]) * 255).cpu().numpy()
save_path = './map-PMD/PMD/' + self.cfg.datapath.split('/')[-1]
save_edge = './map-PMD/Edge/' + self.cfg.datapath.split('/')[-1]
if not os.path.exists(save_path):
os.makedirs(save_path)
if not os.path.exists(save_edge):
os.makedirs(save_edge)
cv2.imwrite(save_path+'/'+name[0]+'.png', np.round(pred))
cv2.imwrite(save_edge + '/' + name[0] + '_edge.png', np.round(edge))
cost_time.pop(0)
print('Mean running time is: ', np.mean(cost_time))
print("FPS is: ", len(self.loader.dataset) / np.sum(cost_time))
if __name__ == '__main__':
for path in ['/home/crh/MirrorDataset/PMD/']:
test = Test(dataset, Net, path)
test.save()