jhorwath commited on
Commit
1cd3e6c
1 Parent(s): 8bf08d6

Add model definitions and pretrained weights

Browse files
ModelDefinitions.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.optim as optim
4
+ import torch.nn.functional as F
5
+ from torchsummary import summary
6
+ from torch.utils.data import TensorDataset, DataLoader
7
+
8
+ class recon_encoder(nn.Module):
9
+
10
+ def __init__(self, latent_size, nconv=16, pool=4, drop=0.05):
11
+ super(recon_encoder, self).__init__()
12
+
13
+
14
+ self.encoder = nn.Sequential( # Appears sequential has similar functionality as TF avoiding need for separate model definition and activ
15
+ nn.Conv2d(in_channels=1, out_channels=nconv, kernel_size=3, stride=1, padding=(1,1)),
16
+ nn.Dropout(drop),
17
+ nn.ReLU(),
18
+ nn.Conv2d(nconv, nconv, 3, stride=1, padding=(1,1)),
19
+ nn.Dropout(drop),
20
+ nn.ReLU(),
21
+ nn.MaxPool2d((pool,pool)),
22
+
23
+ nn.Conv2d(nconv, nconv*2, 3, stride=1, padding=(1,1)),
24
+ nn.Dropout(drop),
25
+ nn.ReLU(),
26
+ nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
27
+ nn.Dropout(drop),
28
+ nn.ReLU(),
29
+ nn.MaxPool2d((pool,pool)),
30
+
31
+ nn.Conv2d(nconv*2, nconv*4, 3, stride=1, padding=(1,1)),
32
+ nn.Dropout(drop),
33
+ nn.ReLU(),
34
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
35
+ nn.Dropout(drop),
36
+ nn.ReLU(),
37
+ nn.MaxPool2d((pool,pool)),
38
+
39
+ #nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
40
+ #nn.Dropout(drop),
41
+ #nn.ReLU(),
42
+ #nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
43
+ #nn.Dropout(drop),
44
+ #nn.ReLU(),
45
+ #nn.MaxPool2d((pool,pool)),
46
+ )
47
+
48
+
49
+ self.bottleneck = nn.Sequential(
50
+ # FC layer at bottleneck -- dropout might not make sense here
51
+ nn.Flatten(),
52
+ nn.Linear(1024, latent_size),
53
+ #nn.Dropout(drop),
54
+ nn.ReLU(),
55
+ # nn.Linear(latent_size, 1024),
56
+ # #nn.Dropout(drop),
57
+ # nn.ReLU(),
58
+ # nn.Unflatten(1,(64,4,4))# 0 is batch dimension
59
+ )
60
+
61
+
62
+ self.decoder1 = nn.Sequential(
63
+
64
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
65
+ nn.Dropout(drop),
66
+ nn.ReLU(),
67
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
68
+ nn.Dropout(drop),
69
+ nn.ReLU(),
70
+ nn.Upsample(scale_factor=pool, mode='bilinear'),
71
+
72
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
73
+ nn.Dropout(drop),
74
+ nn.ReLU(),
75
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
76
+ nn.Dropout(drop),
77
+ nn.ReLU(),
78
+ nn.Upsample(scale_factor=pool, mode='bilinear'),
79
+
80
+ nn.Conv2d(nconv*4, nconv*2, 3, stride=1, padding=(1,1)),
81
+ nn.Dropout(drop),
82
+ nn.ReLU(),
83
+ nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
84
+ nn.Dropout(drop),
85
+ nn.ReLU(),
86
+ nn.Upsample(scale_factor=pool, mode='bilinear'),
87
+
88
+ #nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
89
+ #nn.Dropout(drop),
90
+ #nn.ReLU(),
91
+ #nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
92
+ #nn.Dropout(drop),
93
+ #nn.ReLU(),
94
+ #nn.Upsample(scale_factor=pool, mode='bilinear'),
95
+
96
+ nn.Conv2d(nconv*2, 1, 3, stride=1, padding=(1,1)), #Output conv layer has 2 for mu and sigma
97
+ nn.Sigmoid() #Amplitude mode
98
+ )
99
+
100
+
101
+ def forward(self,x):
102
+ with torch.cuda.amp.autocast():
103
+ x1 = self.encoder(x)
104
+ x1 = self.bottleneck(x1)
105
+ #print(x1.shape)
106
+ return x1
107
+
108
+
109
+ #Helper function to calculate size of flattened array from conv layer shapes
110
+ def calc_fc_shape(self):
111
+ x0 = torch.zeros([256,256]).unsqueeze(0)
112
+ x0 = self.encoder(x0)
113
+
114
+ self.conv_bock_output_shape = x0.shape
115
+ #print ("Output of conv block shape is", self.conv_bock_output_shape)
116
+ self.flattened_size = x0.flatten().shape[0]
117
+ #print ("Flattened layer size is", self.flattened_size)
118
+ return self.flattened_size
119
+
120
+ class recon_model(nn.Module):
121
+
122
+ def __init__(self, latent_size, nconv=16, pool=4, drop=0.05):
123
+ super(recon_model, self).__init__()
124
+
125
+
126
+ self.encoder = nn.Sequential( # Appears sequential has similar functionality as TF avoiding need for separate model definition and activ
127
+ nn.Conv2d(in_channels=1, out_channels=nconv, kernel_size=3, stride=1, padding=(1,1)),
128
+ nn.Dropout(drop),
129
+ nn.ReLU(),
130
+ nn.Conv2d(nconv, nconv, 3, stride=1, padding=(1,1)),
131
+ nn.Dropout(drop),
132
+ nn.ReLU(),
133
+ nn.MaxPool2d((pool,pool)),
134
+
135
+ nn.Conv2d(nconv, nconv*2, 3, stride=1, padding=(1,1)),
136
+ nn.Dropout(drop),
137
+ nn.ReLU(),
138
+ nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
139
+ nn.Dropout(drop),
140
+ nn.ReLU(),
141
+ nn.MaxPool2d((pool,pool)),
142
+
143
+ nn.Conv2d(nconv*2, nconv*4, 3, stride=1, padding=(1,1)),
144
+ nn.Dropout(drop),
145
+ nn.ReLU(),
146
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
147
+ nn.Dropout(drop),
148
+ nn.ReLU(),
149
+ nn.MaxPool2d((pool,pool)),
150
+
151
+ #nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
152
+ #nn.Dropout(drop),
153
+ #nn.ReLU(),
154
+ #nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
155
+ #nn.Dropout(drop),
156
+ #nn.ReLU(),
157
+ #nn.MaxPool2d((pool,pool)),
158
+ )
159
+
160
+
161
+ self.bottleneck = nn.Sequential(
162
+ # FC layer at bottleneck -- dropout might not make sense here
163
+ nn.Flatten(),
164
+ nn.Linear(1024, latent_size),
165
+ #nn.Dropout(drop),
166
+ nn.ReLU(),
167
+ nn.Linear(latent_size, 1024),
168
+ #nn.Dropout(drop),
169
+ nn.ReLU(),
170
+ nn.Unflatten(1,(64,4,4))# 0 is batch dimension
171
+ )
172
+
173
+
174
+ self.decoder1 = nn.Sequential(
175
+
176
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
177
+ nn.Dropout(drop),
178
+ nn.ReLU(),
179
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
180
+ nn.Dropout(drop),
181
+ nn.ReLU(),
182
+ nn.Upsample(scale_factor=pool, mode='bilinear'),
183
+
184
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
185
+ nn.Dropout(drop),
186
+ nn.ReLU(),
187
+ nn.Conv2d(nconv*4, nconv*4, 3, stride=1, padding=(1,1)),
188
+ nn.Dropout(drop),
189
+ nn.ReLU(),
190
+ nn.Upsample(scale_factor=pool, mode='bilinear'),
191
+
192
+ nn.Conv2d(nconv*4, nconv*2, 3, stride=1, padding=(1,1)),
193
+ nn.Dropout(drop),
194
+ nn.ReLU(),
195
+ nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
196
+ nn.Dropout(drop),
197
+ nn.ReLU(),
198
+ nn.Upsample(scale_factor=pool, mode='bilinear'),
199
+
200
+ #nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
201
+ #nn.Dropout(drop),
202
+ #nn.ReLU(),
203
+ #nn.Conv2d(nconv*2, nconv*2, 3, stride=1, padding=(1,1)),
204
+ #nn.Dropout(drop),
205
+ #nn.ReLU(),
206
+ #nn.Upsample(scale_factor=pool, mode='bilinear'),
207
+
208
+ nn.Conv2d(nconv*2, 1, 3, stride=1, padding=(1,1)), #Output conv layer has 2 for mu and sigma
209
+ nn.Sigmoid() #Amplitude mode
210
+ )
211
+
212
+
213
+ def forward(self,x):
214
+ with torch.cuda.amp.autocast():
215
+ x1 = self.encoder(x)
216
+ x1 = self.bottleneck(x1)
217
+ #print(x1.shape)
218
+ return self.decoder1(x1)
219
+
220
+
221
+ #Helper function to calculate size of flattened array from conv layer shapes
222
+ def calc_fc_shape(self):
223
+ x0 = torch.zeros([256,256]).unsqueeze(0)
224
+ x0 = self.encoder(x0)
225
+
226
+ self.conv_bock_output_shape = x0.shape
227
+ #print ("Output of conv block shape is", self.conv_bock_output_shape)
228
+ self.flattened_size = x0.flatten().shape[0]
229
+ #print ("Flattened layer size is", self.flattened_size)
230
+ return self.flattened_size
best_model_100x_0064.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7e89e9b45bece44b11c9e00978bb54e5533ea6b146b310a0edad4f2839c98a4b
3
+ size 1538571
best_model_100x_0064_statedict.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af36b48cb6c7818e3e68d443620f4ca61e7c8332ad61e98170111a0feb6c8713
3
+ size 1528395