forked from ZeroZero19/HI-GAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_dncnn.py
109 lines (94 loc) · 3.85 KB
/
model_dncnn.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
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
import torch
import torch.nn as nn
import torch.nn.init as init
class DnCNN(nn.Module):
# reference: https://github.com/cszn/DnCNN/tree/master/TrainingCodes/dncnn_pytorch
def __init__(self, depth=17, n_channels=64, image_channels=1,
use_bnorm=True, kernel_size=3):
super(DnCNN, self).__init__()
kernel_size = 3
padding = 1
layers = []
layers.append(nn.Conv2d(image_channels, n_channels,
kernel_size=kernel_size, padding=padding, bias=True))
layers.append(nn.ReLU(inplace=True))
for _ in range(depth-2):
layers.append(nn.Conv2d(n_channels, n_channels,
kernel_size=kernel_size, padding=padding, bias=False))
if use_bnorm:
layers.append(nn.BatchNorm2d(n_channels, eps=0.0001, momentum = 0.95))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(n_channels, image_channels,
kernel_size=kernel_size, padding=padding, bias=False))
self.dncnn = nn.Sequential(*layers)
self._initialize_weights()
print(f'model size: {self._model_size()}')
def forward(self, x):
y = x
out = self.dncnn(x)
# out: residual, y: noisy input
return y - out
def _initialize_weights(self):
print('init weight')
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.orthogonal_(m.weight)
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
def _model_size(self):
n_params, n_conv_layers = 0, 0
for param in self.parameters():
n_params += param.numel()
for module in self.modules():
if 'Conv' in module.__class__.__name__ \
or 'conv' in module.__class__.__name__:
n_conv_layers += 1
return n_params, n_conv_layers
class DnCNN_NRL(nn.Module):
"""Non-residual learning.
"""
def __init__(self, depth=17, n_channels=64, image_channels=1,
use_bnorm=True, kernel_size=3):
super(DnCNN_NRL, self).__init__()
kernel_size = 3
padding = 1
layers = []
layers.append(nn.Conv2d(image_channels, n_channels,
kernel_size=kernel_size, padding=padding, bias=True))
layers.append(nn.ReLU(inplace=True))
for _ in range(depth-2):
layers.append(nn.Conv2d(n_channels, n_channels,
kernel_size=kernel_size, padding=padding, bias=False))
if use_bnorm:
layers.append(nn.BatchNorm2d(n_channels, eps=0.0001, momentum = 0.95))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv2d(n_channels, image_channels,
kernel_size=kernel_size, padding=padding, bias=False))
self.dncnn = nn.Sequential(*layers)
self._initialize_weights()
print(f'model size: {self._model_size()}')
def forward(self, x):
# output denoised directly
return self.dncnn(x)
def _initialize_weights(self):
print('init weight')
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.orthogonal_(m.weight)
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
def _model_size(self):
n_params, n_conv_layers = 0, 0
for param in self.parameters():
n_params += param.numel()
for module in self.modules():
if 'Conv' in module.__class__.__name__ \
or 'conv' in module.__class__.__name__:
n_conv_layers += 1
return n_params, n_conv_layers