-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
106 lines (102 loc) · 4.56 KB
/
models.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
import torch.nn as nn
import torch.nn.init as init
import torch
class MeanOnlyBatchNorm_2d(nn.Module):
def __init__(self, num_features, momentum=0.1):
super(MeanOnlyBatchNorm_2d, self).__init__()
self.num_features = num_features
self.momentum = momentum
self.bias = nn.Parameter(torch.zeros(num_features))
self.register_buffer('running_mean', torch.zeros(num_features))
def forward(self, inp):
size = list(inp.size())
beta = self.bias.view(1, self.num_features, 1, 1)
if self.training:
avg = torch.mean(inp, dim=3)
avg = torch.mean(avg, dim=2)
avg = torch.mean(avg, dim=0)
self.running_mean = (1 - self.momentum) * self.running_mean + self.momentum * avg
else:
avg = self.running_mean.repeat(size[0], 1)
output = inp - avg.view(1, self.num_features, 1, 1)
output = output + beta
return output
def extra_repr(self):
return '{num_features}, momentum={momentum} '.format(**self.__dict__)
class MeanOnlyBatchNorm_3d(nn.Module):
def __init__(self, num_features, momentum=0.1):
super(MeanOnlyBatchNorm_3d, self).__init__()
self.num_features = num_features
self.momentum = momentum
self.bias = nn.Parameter(torch.zeros(num_features))
self.register_buffer('running_mean', torch.zeros(num_features))
def forward(self, inp):
size = list(inp.size())
beta = self.bias.view(1, self.num_features, 1, 1, 1)
if self.training:
avg = torch.mean(inp, dim=4)
avg = torch.mean(avg, dim=3)
avg = torch.mean(avg, dim=2)
avg = torch.mean(avg, dim=0)
self.running_mean = (1 - self.momentum) * self.running_mean + self.momentum * avg
else:
avg = self.running_mean.repeat(size[0], 1)
output = inp - avg.view(1, self.num_features, 1, 1, 1)
output = output + beta
return output
def extra_repr(self):
return '{num_features}, momentum={momentum} '.format(**self.__dict__)
class BasicNet2D(nn.Module):
def __init__(self):
layers = []
imchannel = 2
filternum = 128
filtersize = 3
depth = 3
super(BasicNet2D, self).__init__()
layers.append(nn.utils.spectral_norm(nn.Conv2d(imchannel, filternum, filtersize, padding=1, bias=True), n_power_iterations=20))
layers.append(nn.ReLU(inplace=True))
for _ in range(depth):
layers.append(nn.utils.spectral_norm(nn.Conv2d(filternum, filternum, filtersize, padding=1, bias=False), n_power_iterations=20))
layers.append(MeanOnlyBatchNorm_2d(filternum,momentum=0.95))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.utils.spectral_norm(nn.Conv2d(filternum, imchannel, filtersize, padding=1, bias=False), n_power_iterations=20))
self.cnn = nn.Sequential(*layers)
self.init_weights()
def forward(self,x):
y = x
out = self.cnn(x)
return y-out
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.xavier_normal_(m.weight)
if m.bias is not None:
init.constant_(m.bias, 0)
class BasicNet3D(nn.Module):
def __init__(self):
layers = []
imchannel = 2
filternum = 128
filtersize = 3
depth = 3
super(BasicNet3D, self).__init__()
layers.append(nn.utils.spectral_norm(nn.Conv3d(imchannel, filternum, filtersize, padding=1, bias=True), n_power_iterations=20))
layers.append(nn.ReLU(inplace=True))
for _ in range(depth):
layers.append(nn.utils.spectral_norm(nn.Conv3d(filternum, filternum, filtersize, padding=1, bias=False), n_power_iterations=20))
layers.append(MeanOnlyBatchNorm_3d(filternum,momentum=0.95))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.utils.spectral_norm(nn.Conv3d(filternum, imchannel, filtersize, padding=1, bias=False), n_power_iterations=20))
self.cnn = nn.Sequential(*layers)
self.init_weights()
def forward(self,x):
y = x
out = self.cnn(x)
return y-out
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv3d):
init.xavier_normal_(m.weight)
if m.bias is not None:
init.constant_(m.bias, 0)