-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_test.py
81 lines (65 loc) · 2.59 KB
/
color_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
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
import glob
import os
import time
import torch
import cv2
import numpy as np
import argparse
from resblock import Model
from torch import nn
from torch.autograd import Variable
parser = argparse.ArgumentParser()
parser.add_argument("--input", type=str, default='/home/lyh/Source/CGI2023/inputs/')
parser.add_argument("--output", type=str, default='/home/lyh/Source/CGI2023/results/')
args = parser.parse_args()
def get_image_list(root):
ppath = os.path.join(root, '*.png')
jpath = os.path.join(root, '*.jpg')
return list(glob.glob(ppath) + glob.glob(jpath))
test_path = get_image_list(args.input)
len = len(test_path)
save_path_folder = args.output
os.makedirs(save_path_folder, exist_ok=True)
if __name__ == '__main__':
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
torch.cuda.manual_seed_all(123)
model = Model().to(device=device, dtype=torch.float32)
number_parameters = sum(map(lambda x: x.numel(), model.parameters()))
# print(number_parameters)
model = nn.DataParallel(model, device_ids=[0])
# plan-1
checkpoint = torch.load('/home/lyh/Source/CGI2023/weights/epoch_30.pth', map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
with torch.no_grad():
model.eval()
sum = 0.0
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
for img_path in test_path:
name = img_path.split('/')[-1]
img = cv2.imread(img_path).astype(np.float32)
img = cv2.normalize(img, None, 0, 1, cv2.NORM_MINMAX).transpose(2,0,1)
img_tensor = torch.from_numpy(img).to(device)
img_tensor = Variable(torch.unsqueeze(img_tensor, dim=0).float(), requires_grad=False)
start.record()
pred_image, pred_line = model(img_tensor)
end.record()
torch.cuda.synchronize()
sum += start.elapsed_time(end)
pred_image = Variable(torch.squeeze(pred_image, dim=0).float(), requires_grad=False)
pred_image = pred_image.cpu()
result = pred_image.numpy()
result = result.transpose(1,2,0)
result = result * 255.0
save_path = save_path_folder + name
cv2.imwrite(save_path, result)
avgtime = sum / len
avgtime /= 1000.0
print("*----Color {}---*".format(avgtime))
print(" | ")
print(" | ")
print(" | ")
print(" | ")
print('\n')
print(" Done! ")