/
Model.py
executable file
·201 lines (176 loc) · 7.41 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
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
import tensorflow as tf
import numpy as np
import keras.backend as K
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from PIL import Image
from scipy.misc import imsave
from keras.callbacks import TensorBoard
import os
from keras.models import Sequential, Model, load_model, save_model
from keras.layers import Dense, Activation, Dropout, Flatten, Input, Merge, merge
from keras.layers import Conv2D, SeparableConv2D
from keras.layers import MaxPooling2D, UpSampling2D
from keras.layers.merge import Concatenate
from keras import regularizers
from keras.constraints import max_norm
from keras.constraints import min_max_norm
generator_batch = 3000
model_batch = 150
imSize = (128, 128)
theShape = (32, 32, 3)
theShape2 = (32, 32, 1)
dname = 'models2'
os.makedirs('./models2', exist_ok=True)
def autoEncoderGen(path, input_shape=theShape):
if os.path.exists(path):
print('loading: ' + str(sorted(os.listdir('models/'))[-1]))
autoencoder = load_model(os.path.join('models', sorted(os.listdir('models/'))[-1]))
name = str(sorted(os.listdir('models/'))[-1])
print('loaded: ' + name)
else:
print('No previous model found.')
print('Building a new model.')
input_img = Input(shape=input_shape) # adapt this if using `channels_first` image data format
x = UpSampling2D((2, 2))(input_img)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2,2))(x)
x = Conv2D(8, (5, 5), activation='relu', padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = x
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = Conv2D(8, (5, 5), activation='relu', padding='same')(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
decoded = Conv2D(3, (3, 3), activation='relu', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.save('models/autoencoder.h5')
name = 'autoencoderV.h5'
eye_temp = name.replace('.h5', '')
try:
eye = int(eye_temp.replace('autoencoderV', '')) - 10000000
except ValueError:
eye = 0
return autoencoder, name, eye
def autoEncoderGen2(path, input_shape=theShape):
if os.path.exists(path):
print('loading: ' + str(sorted(os.listdir('models2/'))[-1]))
autoencoder = load_model(os.path.join('models2', sorted(os.listdir('models2/'))[-1]))
name = str(sorted(os.listdir('models2/'))[-1])
print('loaded: ' + name)
else:
print('No previous model found.')
print('Building a new model.')
input_img = Input(shape=input_shape) # adapt this if using `channels_first` image data format
residual = UpSampling2D((2, 2))(input_img)
x = Conv2D(42, (2, 2),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=False)(residual)
x = Conv2D(20, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
kernel_regularizer=regularizers.l2(0.01),
use_bias=False)(x)
x = Conv2D(21, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=False)(x)
x = Conv2D(21, (2, 2),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=True)(x)
residual = UpSampling2D((2, 2))(x)
x = Conv2D(42, (2, 2),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=False)(residual)
x = Conv2D(20, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
kernel_regularizer=regularizers.l2(0.01),
use_bias=False)(x)
x = Conv2D(21, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=False)(x)
x = Conv2D(21, (2, 2),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=True)(x)
x = Conv2D(3, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=True)(x)
x = Conv2D(21, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=False)(x)
x = Conv2D(6, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=True)(x)
x = Conv2D(6, (3, 3),
activation='relu',
padding='same',
kernel_initializer='glorot_normal',
use_bias=True)(x)
decoded = Conv2D(3, (3, 3), activation='relu',
padding='same', use_bias=True)(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='mean_absolute_error')
autoencoder.save('models2/autoencoder.h5')
name = 'autoencoderV.h5'
eye_temp = name.replace('.h5', '')
try:
eye = int(eye_temp.replace('autoencoderV', '')) - 10000000 +1
except ValueError:
eye = 0
return autoencoder, name, eye
def autoEncoderGen3(path, input_shape=theShape):
if os.path.exists(path):
print('loading: ' + str(sorted(os.listdir('models2/'))[-1]))
autoencoder = load_model(os.path.join('./models2', sorted(os.listdir('models2/'))[-1]))
name = str(sorted(os.listdir('models2/'))[-1])
print('loaded: ' + name)
else:
print('No previous model have been found.')
print('Building a new model.')
input_img = Input(shape=input_shape) # adapt this if using `channels_first` image data format
x = UpSampling2D((4, 4))(input_img)
print(x.shape)
xr = x[:, :, :, 0]
xg = x[:, :, :, 1]
xb = x[:, :, :, 2]
print(xr.shape)
xr = xr[:, :, :, np.newaxis]
xg = xg[:, :, :, np.newaxis]
xb = xb[:, :, :, np.newaxis]
print(xr.shape)
xr = Conv2D(12, (4, 4), activation='relu', padding='same')(xr)
xg = Conv2D(12, (4, 4), activation='relu', padding='same')(xg)
xb = Conv2D(12, (4, 4), activation='relu', padding='same')(xb)
x = Concatenate((xr, xg, xg))
print('final shape of x: ', x.shape)
decoded = Conv2D(3, (3, 3), activation='relu', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.save('models2/autoencoder.h5')
name = 'autoencoderV.h5'
eye_temp = name.replace('.h5', '')
try:
eye = int(eye_temp.replace('autoencoderV', '')) - 10000000
except ValueError:
eye = 0
return autoencoder, name, eye