forked from somtobking/DreamStream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_dream.py
218 lines (177 loc) · 8.05 KB
/
deep_dream.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
import random
import numpy as np
import cv2
from functools import partial
import tensorflow as tf
import sys
import urllib
import os
import zipfile
import avmerge
import amplitude
import beat_tracker
output_image_counter = 0
def main(input_filename):
#Step 1 - download google's pre-trained neural network
url = 'https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip'
data_dir = 'data/'
model_name = os.path.split(url)[-1]
if not os.path.exists(data_dir):
os.mkdir(data_dir)
local_zip_file = os.path.join(data_dir, model_name)
if not os.path.exists(local_zip_file):
# Download
model_url = urllib.urlopen(url)
with open(local_zip_file, 'wb') as output:
output.write(model_url.read())
# Extract
with zipfile.ZipFile(local_zip_file, 'r') as zip_ref:
zip_ref.extractall(data_dir)
# start with a gray image with a little noise
img_noise = np.random.uniform(size=(224,224,3)) + 100.0
model_fn = 'tensorflow_inception_graph.pb'
#Step 2 - Creating Tensorflow session and loading the model
graph = tf.Graph()
sess = tf.InteractiveSession(graph=graph)
with tf.gfile.FastGFile(os.path.join(data_dir, model_fn), 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
t_input = tf.placeholder(np.float32, name='input') # define the input tensor
imagenet_mean = 117.0
t_preprocessed = tf.expand_dims(t_input-imagenet_mean, 0)
tf.import_graph_def(graph_def, {'input':t_preprocessed})
layers = [op.name for op in graph.get_operations() if op.type=='Conv2D' and 'import/' in op.name]
feature_nums = [int(graph.get_tensor_by_name(name+':0').get_shape()[-1]) for name in layers]
print('Number of layers', len(layers))
print('Total number of feature channels:', sum(feature_nums))
#####HELPER FUNCTIONS. I didn't go over these in the video for times sake. They are mostly just formatting functions. Scroll
#to the bottom #########################################################################################################
########################################################################################################################
############################################################
# Helper functions for TF Graph visualization
#pylint: disable=unused-variable
def strip_consts(graph_def, max_const_size=32):
"""Strip large constant values from graph_def."""
strip_def = tf.GraphDef()
for n0 in graph_def.node:
n = strip_def.node.add() #pylint: disable=maybe-no-member
n.MergeFrom(n0)
if n.op == 'Const':
tensor = n.attr['value'].tensor
size = len(tensor.tensor_content)
if size > max_const_size:
tensor.tensor_content = "<stripped %d bytes>"%size
return strip_def
def rename_nodes(graph_def, rename_func):
res_def = tf.GraphDef()
for n0 in graph_def.node:
n = res_def.node.add() # pylint: disable=maybe-no-member
n.MergeFrom(n0)
n.name = rename_func(n.name)
for i, s in enumerate(n.input):
n.input[i] = rename_func(s) if s[0]!='^' else '^'+rename_func(s[1:])
return res_def
def visstd(a, s=0.1):
'''Normalize the image range for visualization'''
return (a-a.mean())/max(a.std(), 1e-4)*s + 0.5
def T(layer):
'''Helper for getting layer output tensor'''
return graph.get_tensor_by_name("import/%s:0"%layer)
def tffunc(*argtypes):
'''Helper that transforms TF-graph generating function into a regular one.
See "resize" function below.
'''
placeholders = list(map(tf.placeholder, argtypes))
def wrap(f):
out = f(*placeholders)
def wrapper(*args, **kw):
return out.eval(dict(zip(placeholders, args)), session=kw.get('session'))
return wrapper
return wrap
def resize(img, size):
img = tf.expand_dims(img, 0)
return tf.image.resize_bilinear(img, size)[0,:,:,:]
resize = tffunc(np.float32, np.int32)(resize)
def calc_grad_tiled(img, t_grad, tile_size=512):
'''Compute the value of tensor t_grad over the image in a tiled way.
Random shifts are applied to the image to blur tile boundaries over
multiple iterations.'''
sz = tile_size
h, w = img.shape[:2]
sx, sy = np.random.randint(sz, size=2)
img_shift = np.roll(np.roll(img, sx, 1), sy, 0)
grad = np.zeros_like(img)
for y in range(0, max(h-sz//2, sz),sz):
for x in range(0, max(w-sz//2, sz),sz):
sub = img_shift[y:y+sz,x:x+sz]
g = sess.run(t_grad, {t_input:sub})
grad[y:y+sz,x:x+sz] = g
return np.roll(np.roll(grad, -sx, 1), -sy, 0)
#BACK TO CODE IN THE VIDEO###########################################################################################
########################################################################################################
##############################################################################
def render_deepdream(t_obj, img0=img_noise, octave_n=6,
iter_n=10, step=1.8, octave_scale=1.2):
t_score = tf.reduce_mean(t_obj) # defining the optimization objective
t_grad = tf.gradients(t_score, t_input)[0] # behold the power of automatic differentiation!
# split the image into a number of octaves
img = img0
octaves = []
for _ in range(octave_n - 1):
hw = img.shape[:2]
lo = resize(img, np.int32(np.float32(hw) / octave_scale))
hi = img - resize(lo, hw)
img = lo
octaves.append(hi)
# generate details octave by octave
for octave in range(octave_n):
if octave > 0:
hi = octaves[-octave]
img = resize(img, hi.shape[:2]) + hi
for _ in range(iter_n):
g = calc_grad_tiled(img, t_grad)
img = img + (g * (step / (np.abs(g).mean() + 1e-7)))
#Step 5 return frame
output_frame = img / 255.0
output_frame = np.uint8(np.clip(output_frame, 0, 1)*255)
return output_frame
# Prepare necessary variables and tools for syncing video effects with audio output
audio_file = amplitude.splitAV(input_filename, 'audio.wav')
video_fps, video_tot_frames = avmerge.getFPS(input_filename)
beat_list = avmerge.getBeatFrames(beat_tracker.run_beat_tracker(audio_file), video_fps)
temp_frames = amplitude.getAmplitude(audio_file, video_fps)
frame_amplitudes = amplitude.distort_amplitude(temp_frames, 14)
# open video file
cap = cv2.VideoCapture(input_filename)
writer = None
frame_n = 1
while(cap.isOpened()):
ret, frame = cap.read()
if frame is None:
break
octave_info = octave_details(frame_n-1, beat_list, frame_amplitudes)
# Step 4 - Apply gradient ascent to that layer
output_frame = render_deepdream(tf.square(T('mixed4b')), frame, octave_info)
if writer is None:
frame_size = (output_frame.shape[1], output_frame.shape[0])
writer = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), video_fps, frame_size)
writer.write(output_frame)
print 'frame %i complete. Amplitude = %i' % (frame_n, frame_amplitudes[frame_n - 1])
frame_n += 1
cap.release()
def octave_details(frame, beats, amps):
""" Determine the proper octave for the given frame, adding an extra bump if the frame falls on a beat"""
oct_number = amps[frame]
if frame in beats:
oct_number = oct_number + 2
if oct_number < 0:
oct_number = 0
return oct_number
if __name__ == '__main__':
# if len(sys.argv) < 2:
# input_filename = 'marshmello.mp4'
# else:
# input_filename = sys.argv[1]
#
# main(input_filename)
avmerge.processAV('output.avi', 'audio.wav','marshmello_complete.avi')