Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ave_psnr+= psnr(res, label_v, data_range=255)
ave_ssim+= ski_ssim(res, label_v, data_range=255, multichannel=True)
Image.fromarray(res).save(show_dst+im_name[0].split('.')[0]+'_'+str(vi+1)+'.png')
elif data_name == 'DCPDNData':
ct_num+= 1
label = label.numpy()[0]
label = label.transpose((1,2,0))
hazy = Variable(hazy, requires_grad=False).cuda()
res = cleaner(hazy)
res = res.data.cpu().numpy()[0]
res[res>1] = 1
res[res<0] = 0
res = res.transpose((1,2,0))
ave_psnr+= psnr(res, label, data_range=1)
ave_ssim+= ski_ssim(res, label, data_range=1, multichannel=True)
res*= 255
res = res.astype(np.uint8)
Image.fromarray(res).save(show_dst+im_name[0].split('.')[0]+'.png')
else:
print("Unknown dataset name.")
print('psnr: '+str(ave_psnr/float(ct_num))+'.')
print('ssim: '+str(ave_ssim/float(ct_num))+'.')
print('Test done.')
def get_SSIM(self, X, X_hat):
test_SSIM = measure.compare_ssim(X, X_hat, dynamic_range=X.max() - X.min())
return test_SSIM
psnr = []
ssim = []
file_list = glob.glob('{}/*.png'.format(args.test_dir))
for file in file_list:
# read image
img_clean = np.array(Image.open(file), dtype='float32') / 255.0
img_test = img_clean + np.random.normal(0, args.sigma/255.0, img_clean.shape)
img_test = img_test.astype('float32')
# predict
x_test = img_test.reshape(1, img_test.shape[0], img_test.shape[1], 1)
y_predict = model.predict(x_test)
# calculate numeric metrics
img_out = y_predict.reshape(img_clean.shape)
img_out = np.clip(img_out, 0, 1)
psnr_noise, psnr_denoised = compare_psnr(img_clean, img_test), compare_psnr(img_clean, img_out)
ssim_noise, ssim_denoised = compare_ssim(img_clean, img_test), compare_ssim(img_clean, img_out)
psnr.append(psnr_denoised)
ssim.append(ssim_denoised)
# save images
filename = file.split('/')[-1].split('.')[0] # get the name of image file
name.append(filename)
img_test = Image.fromarray((img_test*255).astype('uint8'))
img_test.save(out_dir+filename+'_sigma'+'{}_psnr{:.2f}.png'.format(args.sigma, psnr_noise))
img_out = Image.fromarray((img_out*255).astype('uint8'))
img_out.save(out_dir+filename+'_psnr{:.2f}.png'.format(psnr_denoised))
psnr_avg = sum(psnr)/len(psnr)
ssim_avg = sum(ssim)/len(ssim)
name.append('Average')
psnr.append(psnr_avg)
ssim.append(ssim_avg)
print('Average PSNR = {0:.2f}, SSIM = {1:.2f}'.format(psnr_avg, ssim_avg))
im2_t = im2_t[..., 0]
else:
im1_t = rgb2ycbcr(im1_t)[:, :, 0:1] / 255.0
im2_t = rgb2ycbcr(im2_t)[:, :, 0:1] / 255.0
if scale > 1:
im1_t = mod_crop(im1_t, scale)
im2_t = mod_crop(im2_t, scale)
# NOTE conventionally, crop scale+6 pixels (EDSR, VDSR etc)
im1_t = crop_boundaries(im1_t, int(scale) + 6)
im2_t = crop_boundaries(im2_t, int(scale) + 6)
psnr_val = compare_psnr(im1_t, im2_t)
ssim_val = compare_ssim(
im1_t,
im2_t,
win_size=11,
gaussian_weights=True,
multichannel=True,
data_range=1.0,
K1=0.01,
K2=0.03,
sigma=1.5)
return psnr_val, ssim_val
rmse_lst = []; psnr_lst = []; ssim_lst = []
for i, (inputs, targets) in enumerate(data_loader):
input_img = inputs.to(device).unsqueeze(0)
target_img = targets.to(device)
output_img = model(input_img)
output_img = output_img.squeeze(0).squeeze(0)
output_img = output_img.data.cpu().numpy()
target_img = target_img.squeeze(0)
target_img = target_img.data.cpu().numpy()
rmse_lst.append(np.sqrt(compare_mse(output_img, target_img)))
psnr_lst.append(compare_psnr(output_img, target_img, data_range=4096))
ssim_lst.append(compare_ssim(output_img, target_img, data_range=4096))
rmse_avg = np.mean(rmse_lst)
psnr_avg = np.mean(psnr_lst)
ssim_avg = np.mean(ssim_lst)
print('RMSE [{:.4f}], PSNR [{:.4f}], SSIM [{:.4f}]'.format(
rmse_avg, psnr_avg, ssim_avg))
return rmse_avg, psnr_avg, ssim_avg
if std < OW.FRAME_VALIDATION_COLOR_STD[self.game.game_type] \
and np.mean(mean) > OW.FRAME_VALIDATION_COLOR_MEAN[self.game.game_type] \
and flag is True:
self.is_valid = True
else:
self.is_valid = False
return
replay_icon = ImageUtils.crop(
self.image, OW.get_replay_icon_pos()[self.game.game_type])
replay_icon_preseason = ImageUtils.crop(
self.image, OW.get_replay_icon_preseason_pos()[self.game.game_type])
max_val = measure.compare_ssim(
replay_icon, self.game.replay_icon_ref, multichannel=True)
max_val_preseason = measure.compare_ssim(
replay_icon_preseason, self.game.replay_icon_ref, multichannel=True)
# TODO: another situation: after replay effect there might be a blue
# rectangle remaining on screen.
max_val = max_val if max_val > max_val_preseason else max_val_preseason
if max_val < OW.FRAME_VALIDATION_REPLAY_PROB[self.game.game_type]:
self.is_valid = True
else:
self.is_valid = False
return
if self.is_valid is True and self.game.team_colors is None:
self.game.set_team_colors(self)
self.game.avatars_ref = self._get_avatars_before_validation()
and np.mean(mean) \
> OW.get_ui_variable("FRAME_VALIDATION_COLOR_MEAN", self.game_type, self.game_version) \
and flag is True:
self.is_valid = True
else:
self.is_valid = False
return
# 3) Test if current frame is during replay. Still, for OWL only.
if self.game_type == OW.GAMETYPE_OWL:
replay_icon = ImageUtils.crop(
self.image, OW.get_replay_icon_pos(self.game_type, self.game_version))
replay_icon_preseason = ImageUtils.crop(
self.image, OW.get_replay_icon_preseason_pos(self.game_type, self.game_version))
max_val = measure.compare_ssim(
replay_icon, self.game.replay_icon_ref, multichannel=True)
max_val_preseason = measure.compare_ssim(
replay_icon_preseason, self.game.replay_icon_ref, multichannel=True)
max_val = max_val if max_val > max_val_preseason else max_val_preseason
if max_val < OW.get_ui_variable("FRAME_VALIDATION_REPLAY_PROB", self.game_type, self.game_version):
self.is_valid = True
else:
self.is_valid = False
self.is_replay = True
return
# 4) If frame is valid and Game info (i.e. team colors, avatars) are not
# set, set them up.
if self.is_valid is True and self.game.team_colors is None:
self.game.set_team_colors(self)
if len(x1.shape) == 3: x1 = x1[0,:,:]
if len(x2.shape) == 3: x2 = x2[0,:,:]
if len(y.shape) == 3: y = y[0,:,:]
# a scaled and shifted version of pred and bilinear
x1 = 2*x1 + 100
x2 = 2*x2 + 100
# normalize/scale images
(y_norm1, x1_norm) = norm_minmse(y, x1)
(y_norm2, x2_norm) = norm_minmse(y, x2)
# calulate psnr and ssim of the normalized/scaled images
psnr1 = compare_psnr(*(y_norm1, x1_norm), data_range = 1.)
psnr2 = compare_psnr(*(y_norm2, x2_norm), data_range = 1.)
ssim1 = compare_ssim(*(y_norm1, x1_norm), data_range = 1.)
ssim2 = compare_ssim(*(y_norm2, x2_norm), data_range = 1.)
return psnr1, ssim1, psnr2, ssim2, y_norm1, x1_norm, y_norm2, x2_norm
def ssim(ims1, ims2, scale):
mean_ssim = 0.0
for im1, im2 in zip(ims1, ims2):
im1 = color.rgb2yuv(im1[scale:-scale, scale:-scale])[..., 0]
im2 = color.rgb2yuv(im2[scale:-scale, scale:-scale])[..., 0]
mean_ssim += measure.compare_ssim(im1, im2) / len(ims1)
return mean_ssim
ax10.set_xticklabels(adver_labs,rotation = 45,ha="right")
diff3 = ssim(gray2, gray3,multichannel=True)
ax4 = fig.add_subplot(4,5,12)
ax4.imshow(gray3)
ax4.set_title('NotSoNormie Cam Grasycale'+label.format(diff3))
diff3 = ssim(color2, color3,multichannel=True)
ax5 = fig.add_subplot(4,5,13)
ax5.imshow(color3)
ax5.set_title('NotSoNormie Cam HeatMap'+label.format(diff3))
diff3 = ssim(result2, result3,multichannel=True)
ax6 = fig.add_subplot(4,5,14)
ax6.imshow(result3)
ax6.set_title('NotSoNormie Cam Result'+label.format(diff3))
diff3 = ssim(gray, gray4,multichannel=True)
ax4 = fig.add_subplot(4,5,17)
ax4.imshow(gray4)
ax4.set_title('Inverse NotSoNormie Cam Grasycale'+label.format(diff3))
diff3 = ssim(color4, color,multichannel=True)
ax5 = fig.add_subplot(4,5,18)
ax5.imshow(color4)
ax5.set_title('Inverse NotSoNormie Cam HeatMap'+label.format(diff3))
diff3 = ssim(result4, result,multichannel=True)
ax6 = fig.add_subplot(4,5,19)
ax6.imshow(result4)
ax6.set_title('Inverse NotSoNormie Cam Result'+label.format(diff3))
fig.set_size_inches(30, 36)
fig.tight_layout()
if isTrained:
train = 'Trained'