-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpredict.py
47 lines (41 loc) · 1.27 KB
/
predict.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
import cv2
import numpy as np
import torch
import os
from unet import UNet
weight = './weight/weight.pth'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# load net
print('load net')
net = UNet(1, 2).to(device)
if os.path.exists(weight):
checkpoint = torch.load(weight)
net.load_state_dict(checkpoint['net'])
else:
exit(0)
# load img
print('load img')
dir = './data/test/'
filenames = [os.path.join(dir, filename)
for filename in os.listdir(dir)
if filename.endswith('.jpg') or filename.endswith('.png')]
totalN = len(filenames)
for index, filename in enumerate(filenames):
img = cv2.imread(filename, 0)
if img is None:
print('img is None')
continue
h, w = img.shape[0], img.shape[1]
while h % 4 != 0:
h += 1
while w % 4 != 0:
w += 1
img = cv2.resize(img, (w, h))
# img = cv2.blur(img, (3, 3))
input = torch.from_numpy(img[np.newaxis][np.newaxis]).float() / 255
output = net(input.to(device))[0, 0].detach().data.cpu().numpy()
res = np.concatenate((img/255, output), axis=1)
winname = filename + ' %d | %d ' % (index + 1, totalN)
cv2.imshow(winname, res)
cv2.waitKey()
cv2.destroyWindow(winname)