-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
41 lines (26 loc) · 996 Bytes
/
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
from PIL import Image
import os
import numpy as np
import torch
from model import USLN
from SegDataset import read_file_list
from tqdm import trange
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = USLN()
model.load_state_dict(torch.load(r'logs/.pth'))
model.eval()
model = model.to(device)
test, path_list_images_test= read_file_list( type='test')
for id in trange(len(test)):
image = Image.open(test[id]).convert('RGB')
input = np.transpose(np.array(image, np.float64),(2,0,1))
input=input/255
input = torch.from_numpy(input).type(torch.FloatTensor)
input = input.to(device)
input= input.unsqueeze(0)
output = model(input)
output_np=output.cpu().detach().numpy().copy() # output_np.shape = (4, 2, 160, 160)
output_np=output_np.squeeze()
predictimag=np.transpose(output_np, [1, 2, 0])*255
a=Image.fromarray(predictimag.astype('uint8'))
a.save(os.path.join(r"datasets/pred", path_list_images_test[id]))