-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpretrain_CAE.py
409 lines (330 loc) · 14.4 KB
/
pretrain_CAE.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.activation import LeakyReLU
from utils import initialize_weights_he
# The MNIST datasets are hosted on yann.lecun.com that has moved under CloudFlare protection
# Run this script to enable the datasets download
# Reference: https://github.com/pytorch/vision/issues/1938
from six.moves import urllib
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
from Networks import ResNetBlock
import torch
import numpy as np
from torchvision import datasets
import torchvision.transforms as transforms
import cv2
from zu_resnet import ResNetEncoder
# define the NN architecture
class ConvAutoencoder_NAV2(nn.Module):
def __init__(self, imgChannels=1, zDim=512,featureDim=12*10*10, fix_params=False):
super(ConvAutoencoder_NAV2, self).__init__()
self.featureDim = featureDim
## encoder layers ##
# https://stackoverflow.com/questions/39691902/ordering-of-batch-normalization-and-dropout
self.encode = nn.Sequential(
nn.Conv2d(imgChannels, 32, 5, padding=2) ,
nn.BatchNorm2d(32),
nn.ReLU(),
ResNetBlock(32,64,3),
ResNetBlock(64,128,3),
ResNetBlock(128,256,3),
ResNetBlock(256,128,3), # 64x5x5 = 3200 feature vector
).apply(initialize_weights_he)
## decoder layers ##
## a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.decode = nn.Sequential(
nn.ConvTranspose2d(128, 256, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(256, 128, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(64, imgChannels, 2, stride=2),
).apply(initialize_weights_he)
def fix_params(self):
for param in self.encode.parameters():
param.requires_grad = False
for param in self.decode.parameters():
param.requires_grad = False
def encode_(self, x):
return self.encode(x)
def forward(self, x):
x = self.encode(x)
# print(x.shape)
# x = x.reshape(64,5,5)
x = self.decode(x)
x = torch.sigmoid(x)
return x
# define the NN architecture
class ConvAutoencoder_NAV3(nn.Module):
def __init__(self, imgChannels=1, zDim=512,featureDim=12*10*10, fix_params=False):
super(ConvAutoencoder_NAV3, self).__init__()
self.featureDim = featureDim
## encoder layers ##
# https://stackoverflow.com/questions/39691902/ordering-of-batch-normalization-and-dropout
self.encode = ResNetEncoder(12,blocks_sizes=[64,128,256,384],deepths=[2,2,2,2])
## decoder layers ##
## a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.decode = nn.Sequential(
nn.ConvTranspose2d(384, 512, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(512, 256, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(256, 128, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(64, imgChannels, 2, stride=2)
).apply(initialize_weights_he)
def fix_params(self):
for param in self.encode.parameters():
param.requires_grad = False
for param in self.decode.parameters():
param.requires_grad = False
def encode_(self, x):
return self.encode(x)
def forward(self, x):
x = self.encode(x)
# print(x.shape)
# x = x.reshape(64,5,5)
x = self.decode(x)
x = torch.sigmoid(x)
return x
# define the NN architecture
class ConvAutoencoder_NAV4(nn.Module):
def __init__(self, imgChannels=1, zDim=512,featureDim=12*10*10, fix_params=False):
super(ConvAutoencoder_NAV4, self).__init__()
self.featureDim = featureDim
## encoder layers ##
# https://stackoverflow.com/questions/39691902/ordering-of-batch-normalization-and-dropout
self.encode = nn.Sequential(
ResNetBlock(imgChannels,64,3),
ResNetBlock(64,128,3),
ResNetBlock(128,256,3),
ResNetBlock(256,128,3), # 64x5x5 = 3200 feature vector
).apply(initialize_weights_he)
## decoder layers ##
## a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.decode = nn.Sequential(
nn.ConvTranspose2d(128, 256, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(256, 128, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(64, imgChannels, 2, stride=2),
).apply(initialize_weights_he)
def fix_params(self):
for param in self.encode.parameters():
param.requires_grad = False
for param in self.decode.parameters():
param.requires_grad = False
def encode_(self, x):
return self.encode(x)
def forward(self, x):
x = self.encode(x)
# print(x.shape)
# x = x.reshape(64,5,5)
x = self.decode(x)
x = torch.sigmoid(x)
return x
# define the NN architecture
class ConvAutoencoder_NAV6(nn.Module):
def __init__(self, imgChannels=1, zDim=1024,featureDim=64*5*5, fix_params=False):
super(ConvAutoencoder_NAV6, self).__init__()
self.featureDim = featureDim
## encoder layers ##
# https://stackoverflow.com/questions/39691902/ordering-of-batch-normalization-and-dropout
self.encode = nn.Sequential(
ResNetBlock(imgChannels,64,3),
ResNetBlock(64,128,3),
ResNetBlock(128,256,3),
ResNetBlock(256,64,3), # 64x5x5 = 3200 feature vector,
nn.Flatten(),
nn.Linear(featureDim,zDim)
).apply(initialize_weights_he)
self. FC_1 = nn.Linear(zDim,featureDim)
## decoder layers ##
## a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.decode = nn.Sequential(
nn.ConvTranspose2d(64, 128, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(128, 256, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(256, 128, 2, stride=2),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, 2, stride=2),
).apply(initialize_weights_he)
def fix_params(self):
for param in self.encode.parameters():
param.requires_grad = False
for param in self.decode.parameters():
param.requires_grad = False
def encode_(self, x):
return self.encode(x)
def forward(self, x):
x = self.encode(x)
x = x.view(-1, self.fedim)
x = self.decode(x)
x = torch.sigmoid(x)
return x
if __name__ == '__main__':
GPU = True
device_idx = 0
if GPU:
device = torch.device("cuda:" + str(device_idx) if torch.cuda.is_available() else "cpu")
else:
device = torch.device("cpu")
# convert data to torch.FloatTensor
transform = transforms.ToTensor()
channels = 3
n_s_f = 4
inputshape = (80,80,channels)
cv2_resz = (80,80)
imshape = (channels,*cv2_resz)
show_shape = (*cv2_resz,channels)
model = ConvAutoencoder_NAV4(imgChannels=channels*n_s_f)
# model.load_state_dict(torch.load("/home/developer/Training_results/Qricculum_Learning/big_and_small/final/Models/1/VAE_20"))
model.load_state_dict(torch.load("/home/developer/Training_results/Qricculum_Learning/big_and_small/hoffentlich/VAE_80803_615"))
model.eval()
model.to(device)
train_images = []
test_images = []
moving_database = np.load("/home/developer/Training_results/Qricculum_Learning/big_and_small/hoffentlich/VAE_dtb_12_8080_final_hoffentlich.npy")
# moving_database = np.load("/home/developer/VAE_dtb_12_128128_final.npy")
# moving_database = np.load("/home/developer/Training_results/Qricculum_Learning/big_and_small/3/VAE_dtb_3_8080.npy")
print(moving_database.shape)
print(moving_database[0])
stacked_images = []
train_data = (moving_database[0:45000]/ 2**8).astype(np.float32)
test_data = (moving_database[45000:] / 2**8).astype(np.float32)
print(train_data.shape)
print(test_data.shape)
# Create training and test dataloaders
num_workers = 10
# how many samples per batch to load
batch_size = 32
# prepare data loaders
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=num_workers,shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=num_workers,shuffle=True)
import matplotlib.pyplot as plt
infostring = "net: \n" + str(model) + " \n \n \n"
print(infostring)
filename = "/home/developer/Training_results/VA/"+"Infofile.txt"
text_file = open(filename, "w")
n = text_file.write(infostring)
text_file.close()
learning_rate = 0.01
# specify loss function
criterion = nn.MSELoss()
# specify loss function
# torch.optim.Adam
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# from torch.optim.lr_scheduler import ExponentialLR
from torch.optim.lr_scheduler import MultiStepLR
# scheduler1 = ExponentialLR(optimizer, gamma=0.90)
scheduler2 = MultiStepLR(optimizer, milestones=[30,50,70,90], gamma=0.25)
# number of epochs to train the model
n_epochs = 100
# for epoch in range(1, n_epochs+1):
# # monitor training loss
# train_loss = 0.0
# test_loss = 0.0
# ##################
# # train the model #
# ##################
# for data in train_loader:
# # _ stands in for labels, here
# # no need to flatten images
# images = data
# images = images.to(device)
# # clear the gradients of all optimized variables
# optimizer.zero_grad()
# # forward pass: compute predicted outputs by passing inputs to the model
# outputs = model(images).to(device)
# # output_decoder = decoder(images)
# # print(output_decoder)
# # print(output_decoder.shape)
# # calculate the loss
# loss = criterion(outputs, images)
# # backward pass: compute gradient of the loss with respect to model parameters
# loss.backward()
# # perform a single optimization step (parameter update)
# optimizer.step()
# # update running training loss
# train_loss += loss.item()*images.size(0)
# # print avg training statistics
# train_loss = train_loss/len(train_loader)
# print('Epoch: {} \tTraining Loss: {:.6f}'.format(
# epoch,
# train_loss
# ))
# for test_i_data in test_loader:
# # _ stands in for labels, here
# # no need to flatten images
# test_images = test_i_data
# test_images = test_images.to(device)
# # clear the gradients of all optimized variables
# with torch.no_grad():
# # forward pass: compute predicted outputs by passing inputs to the model
# outputs = model(test_images).to(device)
# loss = criterion(outputs, test_images)
# test_loss += loss.item()*test_images.size(0)
# print('Epoch: {} \tTesting Loss: {:.6f}'.format(
# epoch,
# test_loss
# ))
# torch.save(model.state_dict(), "/home/developer/Training_results/VA/VAE_RESNET18"+str(epoch))
# # scheduler1.step()
# scheduler2.step()
# obtain one batch of test images
dataiter = iter(test_loader)
while True:
show_images = dataiter.next()
show_images = show_images.to(device)
# get sample outputs
output = model(show_images)
# prep images for display
show_images = show_images.detach().cpu().numpy()
# output is resized into a batch of iages
output = output.view(batch_size,n_s_f*channels,*cv2_resz)
# use detach when it's an output that requires_grad
output = output.detach().cpu().numpy()
print(output.shape)
print(show_images.shape)
# torch.save(model.state_dict(), "/home/developer/Training_results/VAE")
# plot the first ten input images and then reconstructed images
fig, axes = plt.subplots(nrows=2, ncols=4, sharex=True, sharey=True, figsize=(20,20))
axes[0][0].imshow(show_images[0][0:3].reshape(show_shape))
axes[0][0].get_xaxis().set_visible(False)
axes[0][0].get_yaxis().set_visible(False)
axes[0][1].imshow(show_images[0][3:6].reshape(show_shape))
axes[0][1].get_xaxis().set_visible(False)
axes[0][1].get_yaxis().set_visible(False)
axes[0][2].imshow(show_images[0][6:9].reshape(show_shape))
axes[0][2].get_xaxis().set_visible(False)
axes[0][2].get_yaxis().set_visible(False)
axes[0][3].imshow(show_images[0][9:12].reshape(show_shape))
axes[0][3].get_xaxis().set_visible(False)
axes[0][3].get_yaxis().set_visible(False)
axes[1][0].imshow(output[0][0:3].reshape(show_shape))
axes[1][0].get_xaxis().set_visible(False)
axes[1][0].get_yaxis().set_visible(False)
axes[1][1].imshow(output[0][3:6].reshape(show_shape))
axes[1][1].get_xaxis().set_visible(False)
axes[1][1].get_yaxis().set_visible(False)
axes[1][2].imshow(output[0][6:9].reshape(show_shape))
axes[1][2].get_xaxis().set_visible(False)
axes[1][2].get_yaxis().set_visible(False)
axes[1][3].imshow(output[0][9:12].reshape(show_shape))
axes[1][3].get_xaxis().set_visible(False)
axes[1][3].get_yaxis().set_visible(False)
# input images on top row, reconstructions on bottom
# for show_images, row in zip([show_images, output], axes):
# for img, ax in zip(show_images, row):
# ax.imshow(img[0:3].reshape(show_shape))
# ax.get_xaxis().set_visible(False)
# ax.get_yaxis().set_visible(False)
plt.show()