训练验证和测试用的是同一个计算函数,训练验证时psnr值是正常的,到验证时psnr值就变成负数了,社区里类似的问题说是图像值的范围不对,但是我验证集跟测试集用的是同一个提取代码啊,请问是我哪里出了什么问题吗?
def __getitem__(self, index):
if self.split == 'train':
LR_img = cv2.imread(os.path.join(self.LR_dir, self.img_names[index][:-4] + 'x' + str(self.SR_rate) + '.png')) / 255.
HR_img = cv2.imread(os.path.join(self.HR_dir, self.img_names[index])) / 255.
if self.augment:
# random crop
LR_img, HR_img = random_crop(LR_img, HR_img, self.crop_size, self.SR_rate)
# geometric transformations
if random.random() < 0.5: # hflip
LR_img, HR_img = LR_img[:, ::-1, :], HR_img[:, ::-1, :]
if random.random() < 0.5: # vflip
LR_img, HR_img = LR_img[::-1, :, :], HR_img[::-1, :, :]
if random.random() < 0.5: # rot90
LR_img, HR_img = LR_img.transpose(1, 0, 2), HR_img.transpose(1, 0, 2)
# intensity scale
intensity_scale = random.choice(self.intensity_list)
LR_img *= intensity_scale
HR_img *= intensity_scale
#这里else包括了验证和测试
else:
LR_img = cv2.imread(os.path.join(self.LR_dir, self.img_names[index][:-4] + 'x' + str(self.SR_rate) + '.png')) / 255.
HR_img = cv2.imread(os.path.join(self.HR_dir, self.img_names[index])) / 255.
# Convert
LR_img = np.ascontiguousarray(LR_img.transpose(2, 0, 1)) # HWC => CHW
HR_img = np.ascontiguousarray(HR_img.transpose(2, 0, 1))
return torch.from_numpy(LR_img), torch.from_numpy(HR_img), self.img_names[index]
for LR_img, HR_img, img_name in dataloader:
LR_img, HR_img = LR_img.cuda().float(), HR_img.cuda().float()
if device != 'cpu':
torch.cuda.synchronize()
t0 = time.perf_counter()
HR_pred = model(LR_img)
if device != 'cpu':
torch.cuda.synchronize()
t1 = time.perf_counter()
psnr = cal_psnr2(HR_pred,HR_img).item()
inference_time = t1 - t0
def cal_psnr2(x,y):
mse = torch.mean((x - y)**2,dim=[1,2,3])
if mse < 1.0e-10:
return 100
score = 20 * torch.log10(1.0 / torch.sqrt(mse))
return score