-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path__init__.py
More file actions
189 lines (163 loc) · 7.14 KB
/
Copy path__init__.py
File metadata and controls
189 lines (163 loc) · 7.14 KB
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
from argparse import ArgumentParser
from common.load_data import load_faces_dataset
from common.util import apply_gaussian_noise, merge_dict
from common.util_keras import reset_tf_session, TQDMProgressCallback
from keras_model.autoencoder.hyperparams import get_constants
from keras_model.autoencoder.model_setup import network_builder, model_builder
from keras_model.autoencoder.util import show_image, show_similar, visualize
from keras_model.util import train
from keras.models import load_model
import matplotlib.pyplot as plt
import numpy as np
import os
from sklearn.neighbors.unsupervised import NearestNeighbors
def denoising_autoencoder(constant_overwrites):
img_shape, attr, x_train, x_test = load_faces_dataset()
constants = merge_dict(get_constants(), constant_overwrites)
constants['img_shape'] = img_shape
constants['code_size'] = 512
autoencoder, encoder, decoder = model_builder(network_builder, constants)
reset_tf_session()
iterations = 25
for i in range(iterations):
print('Epoch %i/%i, Generating corrupted samples...' % (i + 1, iterations))
x_train_noise = apply_gaussian_noise(x_train)
x_test_noise = apply_gaussian_noise(x_test)
# continue to train model with new noise-augmented data
autoencoder.fit(x=x_train_noise, y=x_train, epochs=1,
validation_data=[x_test_noise, x_test],
callbacks=[TQDMProgressCallback()],
verbose=0)
x_test_noise = apply_gaussian_noise(x_test)
denoising_mse = autoencoder.evaluate(x_test_noise, x_test, verbose=0)
print('Denoising MSE:', denoising_mse)
for i in range(5):
img = x_test_noise[i]
visualize(img, encoder, decoder)
def image_retrieval(constant_overwrites):
img_shape, attr, x_train, x_test = load_faces_dataset()
constants = merge_dict(get_constants(), constant_overwrites)
constants['img_shape'] = img_shape
encoder_filename = constants['encoder_filename']
decoder_filename = constants['decoder_filename']
reset_tf_session()
autoencoder, encoder, decoder = model_builder(network_builder, constants)
if os.path.exists(encoder_filename) and not constants['retrain']:
encoder.load_weights(encoder_filename)
else:
data = {
'X_train': x_train,
'X_test': x_test
}
train(autoencoder, data, constants)
encoder.save_weights(encoder_filename)
decoder.save_weights(decoder_filename)
images = x_train
codes = encoder.predict(images)
assert len(codes) == len(images)
nei_clf = NearestNeighbors(metric="euclidean")
nei_clf.fit(codes)
# Cherry-picked examples:
# smiles
show_similar(x_test[247], nei_clf, encoder, images)
# ethnicity
show_similar(x_test[56], nei_clf, encoder, images)
# glasses
show_similar(x_test[63], nei_clf, encoder, images)
def image_morphing(constant_overwrites):
"""
We can take linear combinations of image codes to produce new images with decoder.
:param constant_overwrites:
:return:
"""
img_shape, attr, x_train, x_test = load_faces_dataset()
constants = merge_dict(get_constants(), constant_overwrites)
constants['img_shape'] = img_shape
encoder_filename = constants['encoder_filename']
decoder_filename = constants['decoder_filename']
files = [encoder_filename, decoder_filename]
reset_tf_session()
autoencoder, encoder, decoder = model_builder(network_builder, constants)
if all([os.path.exists(f) for f in files]) and not constants['retrain']:
encoder.load_weights(encoder_filename)
decoder.load_weights(decoder_filename)
else:
data = {
'X_train': x_train,
'X_test': x_test
}
train(autoencoder, data, constants)
encoder.save_weights(encoder_filename)
decoder.save_weights(decoder_filename)
for _ in range(5):
image1, image2 = x_test[np.random.randint(0, len(x_test), size=2)]
code1, code2 = encoder.predict(np.stack([image1, image2]))
plt.figure(figsize=[10, 4])
for i, a in enumerate(np.linspace(0, 1, num=7)):
output_code = code1 * (1 - a) + code2 * a
output_image = decoder.predict(output_code[None])[0]
plt.subplot(1, 7, i + 1)
show_image(output_image)
plt.title('a=%.2f' % a)
plt.show()
def run(constant_overwrites):
img_shape, attr, x_train, x_test = load_faces_dataset()
plt.figure(figsize=[6, 6])
plt.title('Sample images')
for i in range(6):
plt.subplot(2, 3, i + 1)
show_image(x_train[i])
print('X shape:', x_train.shape)
print('attr shape:', attr.shape)
constants = merge_dict(get_constants(), constant_overwrites)
constants['img_shape'] = img_shape
model_filename = constants['model_filename']
encoder_filename = constants['encoder_filename']
decoder_filename = constants['decoder_filename']
# files = [model_filename, encoder_filename, decoder_filename]
reset_tf_session()
autoencoder, encoder, decoder = model_builder(network_builder, constants)
# if all([os.path.exists(f) for f in files]) and not constants['retrain']:
if os.path.exists(model_filename) and not constants['retrain']:
autoencoder = load_model(model_filename.format(constants['n_epochs']))
# encoder.load_weights(encoder_filename)
# decoder.load_weights(decoder_filename)
encoder = autoencoder.layers[1]
decoder = autoencoder.layers[2]
else:
data = {
'X_train': x_train,
'X_test': x_test
}
train(autoencoder, data, constants)
encoder.save_weights(encoder_filename)
decoder.save_weights(decoder_filename)
reconstruction_mse = autoencoder.evaluate(x_test, x_test, verbose=0)
print('Convolutional autoencoder MSE:', reconstruction_mse)
for i in range(5):
img = x_test[i]
visualize(img, encoder, decoder)
print(autoencoder.evaluate(x_test, x_test, verbose=0))
print(reconstruction_mse)
if __name__ == '__main__':
model_type_choices = ['default', 'denoising', 'morphing', 'retrieval']
# read args
parser = ArgumentParser(description='Run Keras Autoencoder')
parser.add_argument('--epochs', dest='n_epochs', type=int, help='number epochs')
parser.add_argument('--model-filename', dest='model_filename', help='model filename')
# noinspection SpellCheckingInspection
parser.add_argument('--ncoder-filename', dest='encoder_filename', help='encoder filename')
parser.add_argument('--decoder-filename', dest='decoder_filename', help='decoder filename')
parser.add_argument('--retrain', dest='retrain', help='retrain flag', action='store_true')
parser.add_argument('--type', dest='model_type', help='model_type', choices=model_type_choices)
parser.set_defaults(retrain=False)
args = parser.parse_args()
model_type = args.model_type
if model_type == 'denoising':
denoising_autoencoder(vars(args))
elif model_type == 'morphing':
image_morphing(vars(args))
elif model_type == 'retrieval':
image_retrieval(vars(args))
else:
run(vars(args))