inference.py
6.46 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import argparse, os
import torch
from torch.autograd import Variable
import numpy as np
import time, math, glob
import scipy.io as sio
from crop_feature import crop_feature
from PIL import Image
import cv2
from matplotlib import pyplot as plt
from math import log10, sqrt
parser = argparse.ArgumentParser(description="PyTorch VDSR Eval")
parser.add_argument("--cuda", action="store_true", help="use cuda?")
parser.add_argument("--model", default="model/model_epoch_50.pth", type=str, help="model path")
parser.add_argument("--dataset", default="Set5", type=str, help="dataset name, Default: Set5")
parser.add_argument("--gpus", default="0", type=str, help="gpu ids (default: 0)")
parser.add_argument("--featureType", default="p3", type=str)
parser.add_argument("--scaleFactor", default=4, type=int, help="scale factor")
parser.add_argument("--singleImage", type=str, default="N", help="if it is a single image, enter \"y\"")
def PSNR(pred, gt, shave_border=0):
height, width = pred.shape[:2]
pred = pred[shave_border:height - shave_border, shave_border:width - shave_border]
gt = gt[shave_border:height - shave_border, shave_border:width - shave_border]
imdff = pred - gt
rmse = math.sqrt(np.mean(imdff ** 2))
if rmse == 0:
return 100
return 20 * math.log10(255.0 / rmse)
def concatFeatures(features, image_name, bicubic=False):
features_0 = features[:16]
features_1 = features[16:32]
features_2 = features[32:48]
features_3 = features[48:64]
features_4 = features[64:80]
features_5 = features[80:96]
features_6 = features[96:112]
features_7 = features[112:128]
features_8 = features[128:144]
features_9 = features[144:160]
features_10 = features[160:176]
features_11 = features[176:192]
features_12 = features[192:208]
features_13 = features[208:224]
features_14 = features[224:240]
features_15 = features[240:256]
features_new = list()
features_new.extend([
concat_vertical(features_0),
concat_vertical(features_1),
concat_vertical(features_2),
concat_vertical(features_3),
concat_vertical(features_4),
concat_vertical(features_5),
concat_vertical(features_6),
concat_vertical(features_7),
concat_vertical(features_8),
concat_vertical(features_9),
concat_vertical(features_10),
concat_vertical(features_11),
concat_vertical(features_12),
concat_vertical(features_13),
concat_vertical(features_14),
concat_vertical(features_15)
])
final_concat_feature = concat_horizontal(features_new)
if bicubic:
save_path = "features/LR_2/LR/" + opt.featureType + "/" + image_name
if not os.path.exists("features/"):
os.makedirs("features/")
if not os.path.exists("features/LR_2/"):
os.makedirs("features/LR_2/")
if not os.path.exists("features/LR_2/LR/"):
os.makedirs("features/LR_2/LR/")
if not os.path.exists("features/LR_2/LR/" + opt.featureType):
os.makedirs("features/LR_2/LR/" + opt.featureType)
cv2.imwrite(save_path, final_concat_feature)
else:
save_path = "features/LR_2/" + opt.featureType + "/" + image_name
if not os.path.exists("features/"):
os.makedirs("features/")
if not os.path.exists("features/LR_2/"):
os.makedirs("features/LR_2/")
if not os.path.exists("features/LR_2/" + opt.featureType):
os.makedirs("features/LR_2/" + opt.featureType)
cv2.imwrite(save_path, final_concat_feature)
def concat_horizontal(feature):
result = cv2.hconcat([feature[0], feature[1]])
for i in range(2, len(feature)):
result = cv2.hconcat([result, feature[i]])
return result
def concat_vertical(feature):
result = cv2.vconcat([feature[0], feature[1]])
for i in range(2, len(feature)):
result = cv2.vconcat([result, feature[i]])
return result
opt = parser.parse_args()
cuda = opt.cuda
if cuda:
print("=> use gpu id: '{}'".format(opt.gpus))
os.environ["CUDA_VISIBLE_DEVICES"] = opt.gpus
if not torch.cuda.is_available():
raise Exception("No GPU found or Wrong gpu id, please run without --cuda")
model = torch.load(opt.model, map_location=lambda storage, loc: storage)["model"]
scales = [opt.scaleFactor]
if opt.singleImage == "Y" :
image_list = opt.dataset
else:
image_path = os.path.join(opt.dataset, opt.featureType)
image_list = os.listdir(image_path)
print(image_path)
print(image_list)
for scale in scales:
for image in image_list:
avg_psnr_predicted = 0.0
avg_psnr_bicubic = 0.0
avg_elapsed_time = 0.0
count = 0.0
image_name_cropped = crop_feature(os.path.join(image_path, image), opt.featureType, opt.scaleFactor)
features = []
features_bicubic = []
for image_name in image_name_cropped:
count += 1
f_gt = image_name
w, h = image_name.size
f_bi = image_name.resize((w//scale,h//scale), Image.BICUBIC)
f_bi = f_bi.resize((w,h), Image.BICUBIC)
f_gt = np.array(f_gt)
f_bi = np.array(f_bi)
f_gt = f_gt.astype(float)
f_bi = f_bi.astype(float)
features_bicubic.append(f_bi)
psnr_bicubic = PSNR(f_bi, f_gt, shave_border=scale)
avg_psnr_bicubic += psnr_bicubic
f_input = f_bi/255.
f_input = Variable(torch.from_numpy(f_input).float()).view(1, -1, f_input.shape[0], f_input.shape[1])
if cuda:
model = model.cuda()
f_input = f_input.cuda()
else:
model = model.cpu()
start_time = time.time()
SR = model(f_input)
elapsed_time = time.time() - start_time
avg_elapsed_time += elapsed_time
SR = SR.cpu()
f_sr = SR.data[0].numpy().astype(np.float32)
f_sr = f_sr * 255
f_sr[f_sr<0] = 0
f_sr[f_sr>255.] = 255.
f_sr = f_sr[0,:,:]
psnr_predicted = PSNR(f_sr, f_gt, shave_border=scale)
avg_psnr_predicted += psnr_predicted
features.append(f_sr)
concatFeatures(features, image)
concatFeatures(features_bicubic, image, True)
print("Scale=", scale)
print("Dataset=", opt.dataset)
print("Average PSNR_predicted=", avg_psnr_predicted/count)
print("Average PSNR_bicubic=", avg_psnr_bicubic/count)