forked from graphific/DeepDreamVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_dreaming_time.py
executable file
·323 lines (272 loc) · 11.3 KB
/
2_dreaming_time.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
#!/usr/bin/python
__author__ = 'graphific'
import argparse
import os, os.path
import errno
import sys
import time
# imports and basic notebook setup
from cStringIO import StringIO
import numpy as np
import scipy.ndimage as nd
import PIL.Image
from google.protobuf import text_format
import caffe
def showarray(a, fmt='jpeg'):
a = np.uint8(np.clip(a, 0, 255))
f = StringIO()
PIL.Image.fromarray(a).save(f, fmt)
display(Image(data=f.getvalue()))
# a couple of utility functions for converting to and from Caffe's input image layout
def preprocess(net, img):
return np.float32(np.rollaxis(img, 2)[::-1]) - net.transformer.mean['data']
def deprocess(net, img):
return np.dstack((img + net.transformer.mean['data'])[::-1])
#Make dreams
def make_step(net, step_size=1.5, end='inception_4c/output', jitter=32, clip=True):
'''Basic gradient ascent step.'''
src = net.blobs['data'] # input image is stored in Net's 'data' blob
dst = net.blobs[end]
ox, oy = np.random.randint(-jitter, jitter + 1, 2)
src.data[0] = np.roll(np.roll(src.data[0], ox, -1), oy, -2) # apply jitter shift
net.forward(end=end)
dst.diff[:] = dst.data # specify the optimization objective
net.backward(start=end)
g = src.diff[0]
# apply normalized ascent step to the input image
src.data[:] += step_size / np.abs(g).mean() * g
src.data[0] = np.roll(np.roll(src.data[0], -ox, -1), -oy, -2) # unshift image
if clip:
bias = net.transformer.mean['data']
src.data[:] = np.clip(src.data, -bias, 255-bias)
def deepdream(net, base_img, iter_n=10, octave_n=4, octave_scale=1.4, end='inception_4c/output', disp=False, clip=True, **step_params):
# prepare base images for all octaves
octaves = [preprocess(net, base_img)]
for i in xrange(octave_n - 1):
octaves.append(nd.zoom(octaves[-1], (1, 1.0 / octave_scale, 1.0 / octave_scale), order=1))
src = net.blobs['data']
detail = np.zeros_like(octaves[-1]) # allocate image for network-produced details
for octave, octave_base in enumerate(octaves[::-1]):
h, w = octave_base.shape[-2:]
if octave > 0:
# upscale details from the previous octave
h1, w1 = detail.shape[-2:]
detail = nd.zoom(detail, (1, 1.0 * h / h1, 1.0 * w / w1), order=1)
src.reshape(1,3,h,w) # resize the network's input image size
src.data[0] = octave_base+detail
for i in xrange(iter_n):
make_step(net, end=end, clip=clip, **step_params)
# visualization
vis = deprocess(net, src.data[0])
if not clip: # adjust image contrast if clipping is disabled
vis = vis * (255.0 / np.percentile(vis, 99.98))
if disp:
showarray(vis)
print(octave, i, end, vis.shape)
if disp:
clear_output(wait=True)
# extract details produced on the current octave
detail = src.data[0]-octave_base
# returning the resulting image
return deprocess(net, src.data[0])
def resizePicture(image,width):
img = PIL.Image.open(image)
basewidth = width
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
return img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
def morphPicture(filename1,filename2,blend,width):
img1 = PIL.Image.open(filename1)
img2 = PIL.Image.open(filename2)
if width is not 0:
img2 = resizePicture(filename2,width)
return PIL.Image.blend(img1, img2, blend)
def make_sure_path_exists(path):
'''
make sure input and output directory exist, if not create them.
If another error (permission denied) throw an error.
'''
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
layersloop = ['inception_4c/output', 'inception_4d/output',
'inception_4e/output', 'inception_5a/output',
'inception_5b/output', 'inception_5a/output',
'inception_4e/output', 'inception_4d/output',
'inception_4c/output']
def main(input, output, disp, gpu, model_path, model_name, preview, octaves, octave_scale, iterations, jitter, zoom, stepsize, blend, layers):
make_sure_path_exists(input)
make_sure_path_exists(output)
# let max nr of frames
nrframes =len([name for name in os.listdir(input) if os.path.isfile(os.path.join(input, name))])
if nrframes == 0:
print("no frames to process found")
sys.exit(0)
if preview is None: preview = 0
if octaves is None: octaves = 4
if octave_scale is None: octave_scale = 1.5
if iterations is None: iterations = 5
if jitter is None: jitter = 32
if zoom is None: zoom = 1
if stepsize is None: stepsize = 1.5
if blend is None: blend = 0.5
if layers is None: layers = 'customloop' #['inception_4c/output']
#Load DNN
net_fn = model_path + 'deploy.prototxt'
param_fn = model_path + model_name #'bvlc_googlenet.caffemodel'
# Patching model to be able to compute gradients.
# Note that you can also manually add "force_backward: true" line to "deploy.prototxt".
model = caffe.io.caffe_pb2.NetParameter()
text_format.Merge(open(net_fn).read(), model)
model.force_backward = True
open('tmp.prototxt', 'w').write(str(model))
net = caffe.Classifier('tmp.prototxt', param_fn,
mean = np.float32([104.0, 116.0, 122.0]), # ImageNet mean, training set dependent
channel_swap = (2,1,0)) # the reference model has channels in BGR order instead of RGB
# should be picked up by caffe by default, but just in case
# standard caffe:
if gpu != '0':
caffe.set_mode_gpu()
caffe.set_device(int(args.gpu))
print("GPU mode [device id: %s]" % args.gpu)
print("using GPU, but you'd still better make a cup of coffee")
if disp:
from IPython.display import clear_output, Image, display
print("display turned on")
frame = np.float32(PIL.Image.open(input+'/0001.jpg'))
if preview is not 0:
frame = resizePicture(input+'/0001.jpg',preview)
frame_i = 1
now = time.time()
for i in xrange(frame_i,nrframes):
print('Processing frame #{}').format(frame_i)
if layers == 'customloop': #loop over layers as set in layersloop array
endparam = layersloop[frame_i % len(layersloop)]
frame = deepdream(
net, frame, iter_n = iterations, step_size = stepsize, octave_n = octaves, octave_scale = octave_scale,
jitter=jitter, end = endparam)
else: #loop through layers one at a time until this specific layer
endparam = layers[frame_i % len(layers)]
frame = deepdream(
net, frame, iter_n = iterations, step_size = stepsize, octave_n = octaves, octave_scale = octave_scale,
jitter=jitter, end = endparam)
saveframe = output + "/%04d.jpg" % frame_i
later = time.time()
difference = int(later - now)
# Stats (stolen + adapted from Samim: https://github.com/samim23/DeepDreamAnim/blob/master/dreamer.py)
print '***************************************'
print 'Saving Image As: ' + saveframe
print 'Frame ' + str(i) + ' of ' + str(nrframes)
print 'Frame Time: ' + str(difference) + 's'
timeleft = difference * (nrframes - frame_i)
m, s = divmod(timeleft, 60)
h, m = divmod(m, 60)
print 'Estimated Total Time Remaining: ' + str(timeleft) + 's (' + "%d:%02d:%02d" % (h, m, s) + ')'
print '***************************************'
PIL.Image.fromarray(np.uint8(frame)).save(saveframe)
newframe = input + "/%04d.jpg" % frame_i
if blend == 0:
newimg = PIL.Image.open(newframe)
if preview is not 0:
newimg = resizePicture(newframe,preview)
frame = newimg
else:
frame = morphPicture(saveframe,newframe,blend,preview)
frame = np.float32(frame)
now = time.time()
frame_i += 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Dreaming in videos.')
parser.add_argument(
'-i','--input',
help='Input directory where extracted frames are stored',
required=True)
parser.add_argument(
'-o','--output',
help='Output directory where processed frames are to be stored',
required=True)
parser.add_argument(
'-d', '--display',
help='display frames',
action='store_true',
dest='display')
parser.add_argument(
"--gpu",
default='0',
help="Switch for gpu computation."
) #int can chose index of gpu, if theres multiple gpus to chose from
parser.add_argument(
'-t', '--model_path',
dest='model_path',
default='../caffe/models/bvlc_googlenet/',
help='Model directory to use')
parser.add_argument(
'-m', '--model_name',
dest='model_name',
default='bvlc_googlenet.caffemodel',
help='Caffe Model name to use')
parser.add_argument(
'-p','--preview',
type=int,
required=False,
help='Preview image width. Default: 0')
parser.add_argument(
'-oct','--octaves',
type=int,
required=False,
help='Octaves. Default: 4')
parser.add_argument(
'-octs','--octavescale',
type=float,
required=False,
help='Octave Scale. Default: 1.4',)
parser.add_argument(
'-itr','--iterations',
type=int,
required=False,
help='Iterations. Default: 10')
parser.add_argument(
'-j','--jitter',
type=int,
required=False,
help='Jitter. Default: 32')
parser.add_argument(
'-z','--zoom',
type=int,
required=False,
help='Zoom in Amount. Default: 1')
parser.add_argument(
'-s','--stepsize',
type=float,
required=False,
help='Step Size. Default: 1.5')
parser.add_argument(
'-b','--blend',
type=float,
required=False,
help='Blend Amount. Default: 0.5')
parser.add_argument(
'-l','--layers',
nargs="+",
type=str,
required=False,
help='Array of Layers to loop through. Default: [customloop] \
- or choose ie [inception_4c/output] for that single layer')
args = parser.parse_args()
if not args.model_path[-1] == '/':
args.model_path = args.model_path + '/'
if not os.path.exists(args.model_path):
print("Model directory not found")
print("Please set the model_path to a correct caffe model directory")
sys.exit(0)
model = os.path.join(args.model_path, args.model_name)
if not os.path.exists(model):
print("Model not found")
print("Please set the model_name to a correct caffe model")
print("or download one with ./caffe_dir/scripts/download_model_binary.py caffe_dir/models/bvlc_googlenet")
sys.exit(0)
main(
args.input, args.output, args.display, args.gpu, args.model_path, args.model_name,
args.preview, args.octaves, args.octavescale, args.iterations, args.jitter, args.zoom, args.stepsize, args.blend, args.layers)