-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepdream.py
366 lines (322 loc) · 9.91 KB
/
deepdream.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
from __future__ import print_function
from keras.preprocessing import image
from keras.preprocessing.image import load_img, save_img, img_to_array
from keras import backend as K
import numpy as np
import scipy
import argparse
import os
model_dict = {
1: 'Xception',
2: 'VGG16',
3: 'VGG19',
4: 'ResNet50',
5: 'InceptionV3',
6: 'InceptionResNetV2',
7: 'MobileNet',
8: 'MobileNetV2',
9: 'DenseNet121',
10: 'DenseNet169',
11: 'DenseNet201',
12: 'NASNetMobile',
13: 'NASNetLarge'
}
default_settings = {
'Xception':{
'add_13': 0.33,
'add_14': 0.67,
'add_15': 1.0,
'add_16': 1.33,
'add_17': 1.67,
'add_18': 2.0,
'add_19': 1.67,
'add_20': 1.33,
'add_21': 1.0,
'add_22': 0.67,
'add_23': 0.33,
'add_24': 0.2,
'avg_pool': 0.1
},
'VGG16':{
'block1_pool': 0.67,
'block2_pool': 1.33,
'block3_pool': 2.0,
'block4_pool': 1.5,
'block5_pool': 1.0
},
'VGG19':{
'block1_pool': 0.67,
'block2_pool': 1.33,
'block3_pool': 2.0,
'block4_pool': 1.5,
'block5_pool': 1.0
},
'ResNet50': {
'add_1': 0.25,
'add_2': 0.5,
'add_3': 0.75,
'add_4': 1.0,
'add_5': 1.25,
'add_6': 1.75,
'add_7': 2.0,
'add_8': 1.85,
'add_9': 1.75,
'add_10': 1.5,
'add_11': 1.25,
'add_12': 1.0,
'add_13': 0.75,
'add_14': 0.5,
'add_15': 0.25,
'add_16': 0.1
},
'InceptionV3': {
'mixed0': 0.33,
'mixed1': 0.67,
'mixed2': 1.0,
'mixed3': 1.33,
'mixed4': 1.67,
'mixed5': 2.0,
'mixed6': 1.67,
'mixed7': 1.33,
'mixed8': 1.0,
'mixed9_0': 0.67,
'mixed9': 0.33,
'mixed9_1': 0.2,
'mixed10': 0.1
},
'InceptionResNetV2': {
'mixed_5b': 0.2,
'mixed_6a': 0.5,
'mixed_7a': 2.0,
'avg_pool': 1.5
},
'MobileNet': {
'conv_pw_2_relu': 0.33,
'conv_pw_3_relu': 0.67,
'conv_pw_4_relu': 1.0,
'conv_pw_5_relu': 1.33,
'conv_pw_6_relu': 1.67,
'conv_pw_7_relu': 2.0,
'conv_pw_8_relu': 1.67,
'conv_pw_9_relu': 1.5,
'conv_pw_10_relu': 1.33,
'conv_pw_11_relu': 1.0,
'conv_pw_12_relu': 0.67,
'conv_pw_13_relu': 0.33
},
'MobileNetV2': {
'block_2_add': 0.4,
'block_4_add': 0.8,
'block_5_add': 1.2,
'block_7_add': 1.6,
'block_8_add': 1.8,
'block_9_add': 2.0,
'block_11_add': 1.8,
'block_12_add': 1.6,
'block_14_add': 1.4,
'block_15_add': 0.2
},
'DenseNet121': {
'pool2_pool': 0.2,
'pool3_pool': 0.5,
'pool4_pool': 2.0,
'relu': 1.5
},
'DenseNet169': {
'pool2_pool': 0.2,
'pool3_pool': 0.5,
'pool4_pool': 2.0,
'relu': 1.5
},
'DenseNet201': {
'pool1': 0.2,
'pool2_pool': 0.5,
'pool3_pool': 1.5,
'pool4_pool': 2.0,
'relu': 1.5
},
'NASNetMobile': {
'add_1': 0.2,
'add_2': 0.5,
'add_3': 1.5,
'add_4': 2.0,
'global_average_pooling2d_1': 0.3
},
'NASNetLarge': {
'concatenate_1': 0.2,
'concatenate_2': 0.5,
'concatenate_3': 2.0,
'concatenate_4': 1.5,
'normal_concat_18': 0.2
}
}
def take_layer_no_input(note):
try:
layer_no = input(note)
numbers = []
for i in layer_no.split(','):
numbers.append(int(i.strip()))
return numbers
except:
print('Give numbers in comma seperated format')
take_layer_no_input(note)
def take_layer_proportion_input(note):
try:
proportion = input(note)
numbers = []
for i in proportion.split(','):
numbers.append(float(i.strip()))
return numbers
except:
print('Give numbers in comma seperated format: ')
take_layer_proportion_input(note)
def take_y_n(note):
try:
ans = input(note).lower()
if ans == 'y':
return True
elif ans == 'n':
return False
else:
take_y_n(note)
except:
take_y_n(note)
def select_layer_tweak_setting(model, model_name):
global model_dict
global default_settings
settings = {}
settings['features'] = {}
print('', '#'*120, '\n', "Select the contribution of the layers for which we try to maximize activation, as well as their weight in the final loss.", '\n', "You can tweak these setting to obtain new visual effects.", '\n', '#'*120)
want_default = take_y_n("Do you want to use DEFAULT settings? (Y/N): ")
if want_default and model_name in model_dict.values():
settings['features'] = default_settings[model_name]
else:
for i, layer in enumerate(model.layers):
print(i+1, layer.name)
layer_no = take_layer_no_input('#'*120 + '\n' + "Choose the layer number for which you want to maximize the activation. (Comma Sepearted--> ex: 10, 25, 54, 60)" + '\n' + '#'*120)
numbers = take_layer_proportion_input('#'*120 + '\n' + "Choose the proportion for the layers which you have choosed to maximize the activation. (Comma Sepearted--> ex: 0.2, 0.5, 2.0, 1.5)" + '\n' + '#'*120)
for i, j in zip(layer_no, numbers):
settings['features'][model.layers[i - 1].name] = j
return settings
def preprocess_image(image_path, target_size, preprocess_fun):
img = image.load_img(image_path)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_fun(x)
return x
def deprocess_image(x):
# Util function to convert a tensor into a valid image.
if K.image_data_format() == 'channels_first':
x = x.reshape((3, x.shape[2], x.shape[3]))
x = x.transpose((1, 2, 0))
else:
x = x.reshape((x.shape[1], x.shape[2], 3))
x /= 2.
x += 0.5
x *= 255.
x = np.clip(x, 0, 255).astype('uint8')
return x
def define_loss_fun(model, settings):
dream = model.input
# Get the symbolic outputs of each "key" layer (we gave them unique names).
layer_dict = dict([(layer.name, layer) for layer in model.layers])
# Define the loss.
loss = K.variable(0.)
for layer_name in settings['features']:
# Add the L2 norm of the features of a layer to the loss.
if layer_name not in layer_dict:
raise ValueError('Layer ' + layer_name + ' not found in model.')
coeff = settings['features'][layer_name]
x = layer_dict[layer_name].output
# We avoid border artifacts by only involving non-border pixels in the loss.
scaling = K.prod(K.cast(K.shape(x), 'float32'))
if K.image_data_format() == 'channels_first':
loss += coeff * K.sum(K.square(x[:, :, 2: -2, 2: -2])) / scaling
else:
loss += coeff * K.sum(K.square(x[:, 2: -2, 2: -2, :])) / scaling
# Compute the gradients of the dream wrt the loss.
grads = K.gradients(loss, dream)[0]
# Normalize gradients.
grads /= K.maximum(K.mean(K.abs(grads)), K.epsilon())
# Set up function to retrieve the value
# of the loss and gradients given an input image.
outputs = [loss, grads]
fetch_loss_and_grads = K.function([dream], outputs)
return fetch_loss_and_grads
def eval_loss_and_grads(x, fetch_loss_and_grads):
outs = fetch_loss_and_grads([x])
loss_value = outs[0]
grad_values = outs[1]
return loss_value, grad_values
def resize_img(img, size):
img = np.copy(img)
if K.image_data_format() == 'channels_first':
factors = (1, 1, float(size[0]) / img.shape[2], float(size[1]) / img.shape[3])
else:
factors = (1, float(size[0]) / img.shape[1], float(size[1]) / img.shape[2], 1)
return scipy.ndimage.zoom(img, factors, order=1)
def gradient_ascent(x, fetch_loss_and_grads, iterations, step, max_loss=None):
for i in range(iterations):
loss_value, grad_values = eval_loss_and_grads(x, fetch_loss_and_grads)
if max_loss is not None and loss_value > max_loss:
break
print('..Loss value at', i, ':', loss_value)
x += step * grad_values
return x
def hyperparameters():
hyperparameters_value = take_y_n("Do you want to use default hyperparameters? (Y/N): ")
if hyperparameters_value:
step = 0.01
num_octave = 3
octave_scale = 1.4
iterations = 20
max_loss = 10.
return step, num_octave, octave_scale, iterations, max_loss
else:
step = input("Gradient ascent learning rate (DEFAULT= 0.01): ")
num_octave = input("Number of scales at which to run gradient ascent (DEFAULT= 3): ")
octave_scale = input("Size ratio between scales (DEFAULT= 1.4): ")
iterations = input("Number of ascent steps per scale (DEFAULT= 20): ")
max_loss = input("Maximum loss allowed (DEFAULT= 10.0): ")
return float(step), int(num_octave), float(octave_scale), int(iterations), float(max_loss)
def run_gradient_ascent(image_path, target_size, preprocess_fun, fetch_loss_and_grads, result_prefix):
step, num_octave, octave_scale, iterations, max_loss = hyperparameters()
img = preprocess_image(image_path, target_size, preprocess_fun)
if K.image_data_format() == 'channels_first':
original_shape = img.shape[2:]
else:
original_shape = img.shape[1:3]
successive_shapes = [original_shape]
for i in range(1, num_octave):
shape = tuple([int(dim / (octave_scale ** i)) for dim in original_shape])
successive_shapes.append(shape)
successive_shapes = successive_shapes[::-1]
original_img = np.copy(img)
shrunk_original_img = resize_img(img, successive_shapes[0])
for shape in successive_shapes:
print('Processing image shape', shape)
img = resize_img(img, shape)
img = gradient_ascent(img, fetch_loss_and_grads, iterations=iterations, step=step, max_loss=max_loss)
upscaled_shrunk_original_img = resize_img(shrunk_original_img, shape)
same_size_original = resize_img(original_img, shape)
lost_detail = same_size_original - upscaled_shrunk_original_img
img += lost_detail
shrunk_original_img = resize_img(original_img, shape)
if not os.path.exists(result_prefix):
os.makedirs(result_prefix)
save_img(result_prefix + 'dd_image.png', deprocess_image(np.copy(img)))
def deep_dream(model, model_name, image_path, target_size, preprocess_fun, result_prefix):
settings = select_layer_tweak_setting(model, model_name)
K.set_learning_phase(0)
fetch_loss_and_grads = define_loss_fun(model, settings)
run_gradient_ascent(image_path, target_size, preprocess_fun, fetch_loss_and_grads, result_prefix)
if __name__ == "__main__":
from keras.applications import inception_v3
from keras.applications.inception_v3 import preprocess_input, decode_predictions
model = inception_v3.InceptionV3(weights='imagenet', include_top=True)
model_name = 'InceptionV3'
preprocess_fun = preprocess_input
decode_fun = decode_predictions
target_size = (299, 299)
result_prefix = './vis/deepdream/' + model_name + '/output/'
deep_dream(model, model_name, 'images/cat.jpg', target_size, preprocess_fun, result_prefix)