项目源码

image-20220711212748824

降噪部分的修改与运行

导入所需库

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
from __future__ import print_function
import matplotlib.pyplot as plt
%matplotlib inline

import os
#os.environ['CUDA_VISIBLE_DEVICES'] = '3'

import numpy as np
from models import *

import torch
import torch.optim

# from skimage.measure import compare_psnr
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
from utils.denoising_utils import *

torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark =True
dtype = torch.cuda.FloatTensor

imsize =-1
PLOT = True
sigma = 25
sigma_ = sigma/255.

在上述代码中,第十四行

1
from skimage.measure import compare_psnr

已经跟不上版本,不可用,所以我替换成下面的语句

1
from skimage.metrics import peak_signal_noise_ratio as compare_psnr

设置参数

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
INPUT = 'noise' # 'meshgrid'
pad = 'reflection'
OPT_OVER = 'net' # 'net,input'

reg_noise_std = 1./30. # set to 1./20. for sigma=50
LR = 0.01

OPTIMIZER='adam' # 'LBFGS'
show_every = 100
exp_weight=0.99

if fname == 'data/denoising/snail.jpg':
num_iter = 2400
input_depth = 3
figsize = 5

net = skip(
input_depth, 3,
num_channels_down = [8, 16, 32, 64, 128],
num_channels_up = [8, 16, 32, 64, 128],
num_channels_skip = [0, 0, 0, 4, 4],
upsample_mode='bilinear',
need_sigmoid=True, need_bias=True, pad=pad, act_fun='LeakyReLU')

net = net.type(dtype)

elif fname == 'data/denoising/F16_GT.png':
num_iter = 3000
input_depth = 32
figsize = 4


net = get_net(input_depth, 'skip', pad,
skip_n33d=128,
skip_n33u=128,
skip_n11=4,
num_scales=5,
upsample_mode='bilinear').type(dtype)

else:
assert False

net_input = get_noise(input_depth, INPUT, (img_pil.size[1], img_pil.size[0])).type(dtype).detach()

# Compute number of parameters
s = sum([np.prod(list(p.size())) for p in net.parameters()]);
print ('Number of params: %d' % s)

# Loss
mse = torch.nn.MSELoss().type(dtype)

img_noisy_torch = np_to_torch(img_noisy_np).type(dtype)

该部分会对测试图片进行噪声污染

但是,该部分仅针对“飞机”和“蜗牛”图片,普适性仍待考究

迭代优化

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
net_input_saved = net_input.detach().clone()
noise = net_input.detach().clone()
out_avg = None
last_net = None
psrn_noisy_last = 0

i = 0
def closure():

global i, out_avg, psrn_noisy_last, last_net, net_input

if reg_noise_std > 0:
net_input = net_input_saved + (noise.normal_() * reg_noise_std)

out = net(net_input)

# Smoothing
if out_avg is None:
out_avg = out.detach()
else:
out_avg = out_avg * exp_weight + out.detach() * (1 - exp_weight)

total_loss = mse(out, img_noisy_torch)
total_loss.backward()


psrn_noisy = compare_psnr(img_noisy_np, out.detach().cpu().numpy()[0])
psrn_gt = compare_psnr(img_np, out.detach().cpu().numpy()[0])
psrn_gt_sm = compare_psnr(img_np, out_avg.detach().cpu().numpy()[0])

# Note that we do not have GT for the "snail" example
# So 'PSRN_gt', 'PSNR_gt_sm' make no sense
print ('Iteration %05d Loss %f PSNR_noisy: %f PSRN_gt: %f PSNR_gt_sm: %f' % (i, total_loss.item(), psrn_noisy, psrn_gt, psrn_gt_sm), '\r', end='')
if PLOT and i % show_every == 0:
out_np = torch_to_np(out)
plot_image_grid([np.clip(out_np, 0, 1),
np.clip(torch_to_np(out_avg), 0, 1)], factor=figsize, nrow=1)



# Backtracking
if i % show_every:
if psrn_noisy - psrn_noisy_last < -5:
print('Falling back to previous checkpoint.')

for new_param, net_param in zip(last_net, net.parameters()):
net_param.data.copy_(new_param.cuda())

return total_loss*0
else:
last_net = [x.detach().cpu() for x in net.parameters()]
psrn_noisy_last = psrn_noisy

i += 1

return total_loss

p = get_params(OPT_OVER, net, net_input)
optimize(OPTIMIZER, p, closure, LR, num_iter)

PS:因笔记本性能原因,仅迭代到2400次

1
Iteration 00000    Loss 0.078438   PSNR_noisy: 11.054710   PSRN_gt: 11.548670 PSNR_gt_sm: 11.548670
image-20220711220044207
1
Iteration 00100    Loss 0.018721   PSNR_noisy: 17.276807   PSRN_gt: 20.124856 PSNR_gt_sm: 16.768199
image-20220711220133231
1
Iteration 02400    Loss 0.009465   PSNR_noisy: 20.238596   PSRN_gt: 30.566463 PSNR_gt_sm: 32.614663
image-20220711220227017

待完成

  • 由于设置参数部分的非普适性,想要测试更多的图片,仍需要时间调试
  • 该项目中还有一些修复图像等模块,仍需要时间研究