-
Notifications
You must be signed in to change notification settings - Fork 1
/
StackedAeModel.py
69 lines (60 loc) · 2.73 KB
/
StackedAeModel.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
import keras
from keras.models import Model,Sequential
from keras.layers import Input,Dense, Dropout, Activation, Flatten, Reshape, Conv2D, MaxPooling2D, AveragePooling2D
from keras import regularizers
from keras.losses import mean_squared_error
from keras import losses
import matplotlib.patches as patches
import numpy as np
import dicom
import cv2
import matplotlib.pyplot as plt
def model1(X_train, get_history=False, verbose=0, param_reg=0.001):
autoencoder_0 = Sequential()
encoder_0 = Dense(input_dim=4096, units=100, kernel_regularizer=regularizers.l2(param_reg))
decoder_0 = Dense(input_dim=100, units=4096, kernel_regularizer=regularizers.l2(param_reg))
autoencoder_0.add(encoder_0)
autoencoder_0.add(decoder_0)
autoencoder_0.compile(loss= 'mse',optimizer='adam', metrics=['accuracy'])
h = autoencoder_0.fit(X_train, X_train, epochs=200, verbose=verbose)
temp_0 = Sequential()
temp_0.add(encoder_0)
temp_0.compile(loss= 'mse', optimizer='adam', metrics=['accuracy'])
encoded_X = temp_0.predict(X_train, verbose=0)
if get_history:
return h.history['loss'], encoded_X, encoder_0
else:
return encoded_X, encoder_0
def model2(X_train,encoded_X, encoder_0, get_history=False, verbose=0, param_reg=0.001):
autoencoder_1 = Sequential()
encoder_1 = Dense(input_dim=100, units=100, kernel_regularizer=regularizers.l2(param_reg))
decoder_1 = Dense(input_dim=100, units=100, kernel_regularizer=regularizers.l2(param_reg))
autoencoder_1.add(encoder_1)
autoencoder_1.add(decoder_1)
autoencoder_1.compile(loss= 'mse', optimizer='adam', metrics=['accuracy'])
h = autoencoder_1.fit(encoded_X, encoded_X, epochs=200, verbose=verbose)
temp_0 = Sequential()
temp_0.add(encoder_0)
temp_0.compile(loss= 'mse', optimizer='adam', metrics=['accuracy'])
encoded_X = temp_0.predict(X_train, verbose=0)
if get_history:
return h.history['loss'], encoder_1
else:
return encoder_1
def model3(X_train, Y_train, encoder_0, encoder_1, init='zero',
get_history=False, verbose=0, param_reg=0.001):
model = Sequential()
model.add(encoder_0)
model.add(encoder_1)
model.add(Dense(input_dim=100, units=4096, kernel_initializer=init, kernel_regularizer=regularizers.l2(param_reg)))
model.compile(optimizer = 'adam', loss = "MSE", metrics=['accuracy'])
h = model.fit(X_train, Y_train, epochs=200, verbose=verbose)
if get_history:
return h.history['loss'], model
else:
return model
def SAE(X_train,Y_train,init='zero'):
encoded_X, encoder_0 = model1(X_train)
encoder_1 = model2(X_train,encoded_X,encoder_0)
h, model = model3(X_train, Y_train, encoder_0, encoder_1,init, get_history=True)
return h,model