-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
160 lines (127 loc) · 4.72 KB
/
model.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# By Ashkan Pakzad (ashkanpakzad.github.io) 2022
from torch import nn
import util
def parsemode(mode):
if mode == 'ellipse':
outn = 8
elif mode == 'circle':
outn = 2
else:
raise(ValueError, 'argument mode invalid')
return outn
##==========================CNR==========================#
def getCNRmodel(modelv, mode, device):
outn = parsemode(mode)
if modelv == 0: # default
CNRmodel = CNR(1, outn, nb_features=16)
else:
raise(ValueError, 'argument modelv invalid')
return CNRmodel.to(device)
class CNR(nn.Module):
def __init__(self, in_features, outn, nb_features=16):
super(CNR, self).__init__()
# 1 input image channel, 2 output channels, 32x32 input dimension
self.ConvBlock = nn.Sequential(
nn.Conv2d(in_features, nb_features, kernel_size=3,
stride=1, padding=1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features, nb_features, kernel_size=3,
stride=1, padding=1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features, nb_features, kernel_size=3,
stride=2, padding=1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features, nb_features*2, kernel_size=3,
stride=1, padding=1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features * 2, nb_features * 2,
kernel_size=3, stride=1, padding=1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features * 2, nb_features * 4,
kernel_size=3, stride=2, padding=1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features * 4, nb_features * 4,
kernel_size=3, stride=1, padding=1, bias=True),
nn.ReLU(),
)
self.FCBlock = nn.Sequential(
nn.Flatten(),
nn.Linear(4096, nb_features * 4),
nn.ReLU(),
nn.Linear(nb_features * 4, outn),
)
# todo double check features have been set correctly
def forward(self, x):
x = self.ConvBlock(x)
x = self.FCBlock(x)
return x
def getmodels(modelv, device):
if modelv == 0: # default
refiner = Refiner(1, nb_features=64)
discriminator = Discriminator(input_features=1)
else:
raise(ValueError, 'argument modelv invalid')
return refiner.to(device), discriminator.to(device)
##==========================Refiner/Discriminator==========================#
class Refiner(nn.Module):
def __init__(self, in_features, nb_features=64):
super(Refiner, self).__init__()
self.conv_1 = nn.Sequential(
nn.Conv2d(in_features, nb_features, kernel_size=3,
stride=1, padding=1, bias=True),
nn.ReLU(),
)
self.ResnetBlock1 = nn.Sequential(
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU()
)
self.ResnetBlock2 = nn.Sequential(
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU()
)
self.ResnetBlock3 = nn.Sequential(
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU()
)
self.ResnetBlock4 = nn.Sequential(
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU(),
nn.Conv2d(nb_features, nb_features, 3, 1, 1, bias=True),
nn.ReLU()
)
self.out = nn.Sequential(
nn.Conv2d(nb_features, in_features, 1, stride=1, padding=0),
)
def forward(self, x):
x = self.conv_1(x)
x = self.ResnetBlock1(x)
x = self.ResnetBlock2(x)
x = self.ResnetBlock3(x)
x = self.ResnetBlock4(x)
output = self.out(x)
return output
class Discriminator(nn.Module):
def __init__(self, input_features):
super(Discriminator, self).__init__()
self.convs = nn.Sequential(
nn.Conv2d(input_features, 96, 3, 2, 1, bias=True),
nn.ReLU(),
nn.Conv2d(96, 64, 3, 2, 1, bias=True),
nn.ReLU(),
nn.MaxPool2d(3, 1, 0),
nn.Conv2d(64, 32, 3, 1, 1, bias=True),
nn.ReLU(),
nn.Conv2d(32, 2, 1, 1, 0, bias=True),
nn.ReLU(),
nn.Conv2d(2, 2, 1, 1, 0),
)
def forward(self, x):
convs = self.convs(x)
output = convs.view(convs.size(0), -1, 2)
return output