-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepDreamNewVersion.py
More file actions
134 lines (101 loc) · 3.16 KB
/
Copy pathDeepDreamNewVersion.py
File metadata and controls
134 lines (101 loc) · 3.16 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
# -*- coding: utf-8 -*-
!pip install tensorflow==1.14
from keras.applications import inception_v3
from keras import backend as K
K.set_learning_phase(0)
model=inception_v3.InceptionV3(weights="imagenet",include_top=False)
layer_contributions={'mixed2':0.2,
'mixed3':3. ,
'mixed4':2. ,
'mixed5':1.5}
layer_dict=dict([(layer.name,layer) for layer in model.layers])
loss=K.variable(0.)
loss
for layer_name in layer_contributions:
coeff=layer_contributions[layer_name]
activation=layer_dict[layer_name].output
scaling=K.prod(K.cast(K.shape(activation),'float32'))
loss=loss+coeff*K.sum(K.square(activation[:,2:-2,2:-2,:])) / scaling
K.prod(K.cast(K.shape(activation),'float32'))
layer_dict['mixed3'].output
model.summary()
import keras
dream=model.input
grads=K.gradients(loss,dream)[0]
grads=grads/K.maximum(K.mean(K.abs(grads)),1e-7)
outputs=[loss,grads]
fetch_loss_and_grads=K.function([dream],outputs)
def eval_loss_and_grads(x):
outs=fetch_loss_and_grads([x])
loss_val=outs[0]
grad_val=outs[1]
return loss_val,grad_val
def gradient_ascent(x,iterations,step,max_loss=None):
for i in range(iterations):
loss_value,grad_value=eval_loss_and_grads(x)
if max_loss is not None and loss_value>max_loss :
break
x=x+step*grad_value
return x
import numpy as np
step=0.01
num_octave=3
octave_scale=1.4
iteration=20
max_loss=10
base_image_path="dream.jpeg"
img=preprocess_image(base_image_path)
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:
img=resize_img(img,shape)
img=gradient_ascent(img,
iterations=iteration,
step=step,
max_loss=max_loss)
upscaled_soi=resize_img(shrunk_original_img,shape)
same_size_original=resize_img(original_img,shape)
lost_detail=same_size_original-upscaled_soi
img=img+lost_detail
shrunk_original_img=resize_img(original_img,shape)
save_img(img,fname="stage_shape"+str(shape)+".png")
save_img(img,fname="final.png")
import scipy
import scipy.misc
import imageio
from keras.preprocessing import image
def resize_img(img,size):
img=np.copy(img)
factors=(1,
float(size[0])/img.shape[1],
float(size[1])/img.shape[2],
1)
return scipy.ndimage.zoom(img,factors,order=1)
def save_img(img,fname):
pil_img=deprocess_image(np.copy(img))
imageio.imwrite(fname,pil_img)
def preprocess_image(image_path):
img=image.load_img(image_path)
img=image.img_to_array(img)
img=np.expand_dims(img,axis=0)
img=inception_v3.preprocess_input(img)
return img
def deprocess_image(x):
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=x/2.
x=x+0.5
x=x*255.
x=np.clip(x,0,255).astype('uint8')
return x
!pip install scipy==1.0.0
import imageio