From 7068c160cb15d716e1589a506f62113b1c66a10f Mon Sep 17 00:00:00 2001 From: Yvan Sraka Date: Tue, 22 Sep 2015 17:45:58 +0200 Subject: [PATCH 1/4] Remove useless trailing whitespaces --- 2_dreaming_time.py | 11 ++++------- LICENSE | 1 - README.md | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/2_dreaming_time.py b/2_dreaming_time.py index eb6f40e..a75da8e 100644 --- a/2_dreaming_time.py +++ b/2_dreaming_time.py @@ -311,7 +311,7 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave now = time.time() totaltime = 0 - + if blend == 'loop': blend_forward = True blend_at = 0.4 @@ -347,7 +347,7 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave print 'Saving Image As: ' + saveframe print 'Frame ' + str(i) + ' of ' + str(nrframes-1) print 'Frame Time: ' + str(difference) + 's' - timeleft = avgtime * ((nrframes-1) - frame_i) + timeleft = avgtime * ((nrframes-1) - 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) + ')' @@ -362,7 +362,7 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave newimg = resizePicture(newframe,preview) frame = newimg else: - + if blend == 'random': blendval=randint(5,10)/10. elif blend == 'loop': @@ -498,12 +498,9 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave 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) - + if args.extract is 1: extractVideo(args.input, args.output) else: main(args.input, args.output, args.image_type, 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, args.guide_image, args.start_frame, args.end_frame, args.verbose) - - - diff --git a/LICENSE b/LICENSE index 8cdb845..23cb790 100644 --- a/LICENSE +++ b/LICENSE @@ -337,4 +337,3 @@ proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. - diff --git a/README.md b/README.md index 5045964..fdfbbde 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ or `./1_movie2frames.sh avconv [original_video] [frames_directory] [png / jpg]` -or +or `./1_movie2frames.sh mplayer [original_video] [frames_directory] [png / jpg]` From 77676f688b14da629217e285f23d7420c8218054 Mon Sep 17 00:00:00 2001 From: Yvan Sraka Date: Tue, 22 Sep 2015 17:46:36 +0200 Subject: [PATCH 2/4] Convert tabs to spaces --- 2_dreaming_time.py | 56 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/2_dreaming_time.py b/2_dreaming_time.py index a75da8e..55c8cf7 100644 --- a/2_dreaming_time.py +++ b/2_dreaming_time.py @@ -137,7 +137,7 @@ def deepdream(net, base_img, image_type, iter_n=10, octave_n=4, octave_scale=1.4 showarrayHQ(vis) elif image_type == "jpg": showarray(vis) - print(octave, i, end, vis.shape) + print(octave, i, end, vis.shape) clear_output(wait=True) elif verbose == 2: print(octave, i, end, vis.shape) @@ -209,7 +209,7 @@ def deepdream_guided(net, base_img, image_type, iter_n=10, octave_n=4, octave_sc showarrayHQ(vis) elif image_type == "jpg": showarray(vis) - print octave, i, end, vis.shape + print octave, i, end, vis.shape clear_output(wait=True) elif verbose == 2: print octave, i, end, vis.shape @@ -220,18 +220,18 @@ def deepdream_guided(net, base_img, image_type, iter_n=10, octave_n=4, octave_sc return deprocess(net, src.data[0]) def resizePicture(image,width): - img = PIL.Image.open(image) + 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) + 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) + 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): ''' @@ -271,11 +271,11 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave if verbose is None: verbose = 1 if layers is None: layers = 'customloop' #['inception_4c/output'] if start_frame is None: - frame_i = 1 + frame_i = 1 else: frame_i = int(start_frame) if not end_frame is None: - nrframes = int(end_frame)+1 + nrframes = int(end_frame)+1 else: nrframes = nrframes+1 @@ -364,7 +364,7 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave else: if blend == 'random': - blendval=randint(5,10)/10. + blendval=randint(5,10)/10. elif blend == 'loop': if blend_at > 1 - blend_step: blend_forward = False elif blend_at <= 0.5: blend_forward = True @@ -462,24 +462,24 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave required=False, help="verbosity [0-3]") parser.add_argument( - '-gi', '--guide_image', - required=False, - help="path to guide image") + '-gi', '--guide_image', + required=False, + help="path to guide image") parser.add_argument( - '-sf', '--start_frame', + '-sf', '--start_frame', type=int, - required=False, - help="starting frame nr") + required=False, + help="starting frame nr") parser.add_argument( - '-ef', '--end_frame', + '-ef', '--end_frame', type=int, - required=False, - help="end frame nr") + required=False, + help="end frame nr") parser.add_argument( - '-e', '--extract', - type=int, - required=False, - help="Extract frames from video") + '-e', '--extract', + type=int, + required=False, + help="Extract frames from video") args = parser.parse_args() @@ -503,4 +503,4 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave extractVideo(args.input, args.output) else: - main(args.input, args.output, args.image_type, 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, args.guide_image, args.start_frame, args.end_frame, args.verbose) + main(args.input, args.output, args.image_type, 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, args.guide_image, args.start_frame, args.end_frame, args.verbose) From 2c3017535caedaaecd9b98a5a1f86c699091d9b5 Mon Sep 17 00:00:00 2001 From: Yvan Sraka Date: Tue, 22 Sep 2015 18:04:53 +0200 Subject: [PATCH 3/4] PEP8 Python script --- 2_dreaming_time.py | 408 ++++++++++++++++++++------------------------- 1 file changed, 180 insertions(+), 228 deletions(-) diff --git a/2_dreaming_time.py b/2_dreaming_time.py index 55c8cf7..c67bcf3 100644 --- a/2_dreaming_time.py +++ b/2_dreaming_time.py @@ -2,7 +2,8 @@ __author__ = 'graphific' import argparse -import os, os.path +import os +import os.path import errno import sys import time @@ -17,9 +18,10 @@ import caffe + def extractVideo(inputdir, outputdir): - print subprocess.Popen('ffmpeg -i ' + inputdir + ' -f image2 ' + outputdir + '/%08d.png', shell=True, - stdout=subprocess.PIPE).stdout.read() + print(subprocess.Popen('ffmpeg -i ' + inputdir + ' -f image2 ' + outputdir + '/%08d.png', shell=True, stdout=subprocess.PIPE).stdout.read()) + def showarray(a, fmt='jpeg'): a = np.uint8(np.clip(a, 0, 255)) @@ -27,60 +29,68 @@ def showarray(a, fmt='jpeg'): PIL.Image.fromarray(a).save(f, fmt) display(Image(data=f.getvalue())) + def showarrayHQ(a, fmt='png'): 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): - #print np.float32(img).shape + #print(np.float32(img).shape) 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]) + def objective_L2(dst): dst.diff[:] = dst.data -#objective for guided dreaming -def objective_guide(dst,guide_features): + +# objective for guided dreaming +def objective_guide(dst, guide_features): x = dst.data[0].copy() y = guide_features ch = x.shape[0] - x = x.reshape(ch,-1) - y = y.reshape(ch,-1) - A = x.T.dot(y) # compute the matrix of dot-products with guide features - dst.diff[0].reshape(ch,-1)[:] = y[:,A.argmax(1)] # select ones that match best - -#from https://github.com/jrosebr1/bat-country/blob/master/batcountry/batcountry.py -def prepare_guide(net, image, end="inception_4c/output", maxW=224, maxH=224): - # grab dimensions of input image - (w, h) = image.size - - # GoogLeNet was trained on images with maximum width and heights - # of 224 pixels -- if either dimension is larger than 224 pixels, - # then we'll need to do some resizing - if h > maxH or w > maxW: - # resize based on width - if w > h: - r = maxW / float(w) - - # resize based on height - else: - r = maxH / float(h) + x = x.reshape(ch, -1) + y = y.reshape(ch, -1) + A = x.T.dot(y) # compute the matrix of dot-products with guide features + dst.diff[0].reshape(ch, -1)[:] = y[:, A.argmax(1)] # select ones that match best + + +# from https://github.com/jrosebr1/bat-country/blob/master/batcountry/batcountry.py +def prepare_guide(net, image, end='inception_4c/output', maxW=224, maxH=224): + # grab dimensions of input image + (w, h) = image.size + + # GoogLeNet was trained on images with maximum width and heights + # of 224 pixels -- if either dimension is larger than 224 pixels, + # then we'll need to do some resizing + if h > maxH or w > maxW: + # resize based on width + if w > h: + r = maxW / float(w) + + # resize based on height + else: + r = maxH / float(h) - # resize the image - (nW, nH) = (int(r * w), int(r * h)) - image = np.float32(image.resize((nW, nH), PIL.Image.BILINEAR)) + # resize the image + (nW, nH) = (int(r * w), int(r * h)) + image = np.float32(image.resize((nW, nH), PIL.Image.BILINEAR)) - (src, dst) = (net.blobs["data"], net.blobs[end]) - src.reshape(1, 3, nH, nW) - src.data[0] = preprocess(net, image) - net.forward(end=end) - guide_features = dst.data[0].copy() + (src, dst) = (net.blobs['data'], net.blobs[end]) + src.reshape(1, 3, nH, nW) + src.data[0] = preprocess(net, image) + net.forward(end=end) + guide_features = dst.data[0].copy() + + return guide_features - return guide_features # ------- # Make dreams @@ -88,11 +98,11 @@ def prepare_guide(net, image, end="inception_4c/output", maxW=224, maxH=224): 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 + 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 + 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 @@ -101,13 +111,14 @@ def make_step(net, step_size=1.5, end='inception_4c/output', jitter=32, clip=Tru # 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 + 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) + src.data[:] = np.clip(src.data, -bias, 255 - bias) + -def deepdream(net, base_img, image_type, iter_n=10, octave_n=4, octave_scale=1.4, end='inception_4c/output', verbose = 1, clip=True, **step_params): +def deepdream(net, base_img, image_type, iter_n=10, octave_n=4, octave_scale=1.4, end='inception_4c/output', verbose=1, clip=True, **step_params): # prepare base images for all octaves octaves = [preprocess(net, base_img)] @@ -115,7 +126,7 @@ def deepdream(net, base_img, image_type, iter_n=10, octave_n=4, octave_scale=1.4 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 + 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: @@ -123,19 +134,19 @@ def deepdream(net, base_img, image_type, iter_n=10, octave_n=4, octave_scale=1.4 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 + 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 + if not clip: # adjust image contrast if clipping is disabled vis = vis * (255.0 / np.percentile(vis, 99.98)) if verbose == 3: - if image_type == "png": + if image_type == 'png': showarrayHQ(vis) - elif image_type == "jpg": + elif image_type == 'jpg': showarray(vis) print(octave, i, end, vis.shape) clear_output(wait=True) @@ -143,38 +154,39 @@ def deepdream(net, base_img, image_type, iter_n=10, octave_n=4, octave_scale=1.4 print(octave, i, end, vis.shape) # extract details produced on the current octave - detail = src.data[0]-octave_base + detail = src.data[0] - octave_base # returning the resulting image return deprocess(net, src.data[0]) + # -------------- # Guided Dreaming # -------------- -def make_step_guided(net, step_size=1.5, end='inception_4c/output', - jitter=32, clip=True, objective_fn=objective_guide, **objective_params): +def make_step_guided(net, step_size=1.5, end='inception_4c/output', jitter=32, clip=True, objective_fn=objective_guide, **objective_params): '''Basic gradient ascent step.''' #if objective_fn is None: # objective_fn = objective_L2 - src = net.blobs['data'] # input image is stored in Net's 'data' blob + 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 + 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) objective_fn(dst, **objective_params) # 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[:] += step_size / np.abs(g).mean() * g - src.data[0] = np.roll(np.roll(src.data[0], -ox, -1), -oy, -2) # unshift image + 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) + src.data[:] = np.clip(src.data, -bias, 255 - bias) + def deepdream_guided(net, base_img, image_type, iter_n=10, octave_n=4, octave_scale=1.4, end='inception_4c/output', clip=True, verbose=1, objective_fn=objective_guide, **step_params): @@ -183,56 +195,59 @@ def deepdream_guided(net, base_img, image_type, iter_n=10, octave_n=4, octave_sc # 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)) + 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 + 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) + 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 + 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_guided(net, end=end, clip=clip, objective_fn=objective_fn, **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 not clip: # adjust image contrast if clipping is disabled + vis = vis * (255.0 / np.percentile(vis, 99.98)) if verbose == 3: - if image_type == "png": + if image_type == 'png': showarrayHQ(vis) - elif image_type == "jpg": + elif image_type == 'jpg': showarray(vis) - print octave, i, end, vis.shape + print(octave, i, end, vis.shape) clear_output(wait=True) elif verbose == 2: - print octave, i, end, vis.shape + print(octave, i, end, vis.shape) # extract details produced on the current octave - detail = src.data[0]-octave_base + detail = src.data[0] - octave_base # returning the resulting image return deprocess(net, src.data[0]) -def resizePicture(image,width): + +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) + 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): +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) + 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. @@ -250,46 +265,56 @@ def make_sure_path_exists(path): 'inception_4e/output', 'inception_4d/output', 'inception_4c/output'] + def main(input, output, image_type, gpu, model_path, model_name, preview, octaves, octave_scale, iterations, jitter, zoom, stepsize, blend, layers, guide_image, start_frame, end_frame, verbose): 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))]) + # 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") + 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 #can be nr (constant), random, or loop - if verbose is None: verbose = 1 - if layers is None: layers = 'customloop' #['inception_4c/output'] + 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 # can be nr (constant), random, or loop + if verbose is None: + verbose = 1 + if layers is None: + layers = 'customloop' # ['inception_4c/output'] if start_frame is None: frame_i = 1 else: frame_i = int(start_frame) if not end_frame is None: - nrframes = int(end_frame)+1 + nrframes = int(end_frame) + 1 else: - nrframes = nrframes+1 + nrframes = nrframes + 1 - #Load DNN - net_fn = model_path + 'deploy.prototxt' - param_fn = model_path + model_name #'bvlc_googlenet.caffemodel' + # Load DNN + net_fn = model_path + 'deploy.prototxt' + param_fn = model_path + model_name # 'bvlc_googlenet.caffemodel' if gpu is None: - print("SHITTTTTTTTTTTTTT You're running CPU man =D") + print('CPU mode') else: 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") + print('GPU mode [device id: %s]' % args.gpu) # Patching model to be able to compute gradients. # Note that you can also manually add "force_backward: true" line to "deploy.prototxt". @@ -299,13 +324,13 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave 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 + 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 if verbose == 3: from IPython.display import clear_output, Image, display - print("display turned on") - frame = np.float32(PIL.Image.open(input + '/%08d.%s' % (frame_i, image_type) )) + print('display turned on') + frame = np.float32(PIL.Image.open(input + '/%08d.%s' % (frame_i, image_type))) if preview is not 0: frame = np.float32(resizePicture(input + '/%08d.%s' % (frame_i, image_type), preview)) @@ -320,59 +345,64 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave for i in xrange(frame_i, nrframes): print('Processing frame #{}').format(frame_i) - #Choosing Layer - if layers == 'customloop': #loop over layers as set in layersloop array + # Choosing Layer + if layers == 'customloop': # loop over layers as set in layersloop array endparam = layersloop[frame_i % len(layersloop)] - else: #loop through layers one at a time until this specific layer + else: # loop through layers one at a time until this specific layer endparam = layers[frame_i % len(layers)] - #Choosing between normal dreaming, and guided dreaming + # Choosing between normal dreaming, and guided dreaming if guide_image is None: - frame = deepdream(net, frame, image_type=image_type, verbose=verbose, iter_n = iterations, step_size = stepsize, octave_n = octaves, octave_scale = octave_scale, jitter=jitter, end = endparam) + frame = deepdream(net, frame, image_type=image_type, verbose=verbose, iter_n=iterations, step_size=stepsize, octave_n=octaves, octave_scale=octave_scale, jitter=jitter, end=endparam) else: guide = np.float32(PIL.Image.open(guide_image)) print('Setting up Guide with selected image') - guide_features = prepare_guide(net,PIL.Image.open(guide_image), end=endparam) + guide_features = prepare_guide(net, PIL.Image.open(guide_image), end=endparam) - frame = deepdream_guided(net, frame, image_type=image_type, verbose=verbose, iter_n = iterations, step_size = stepsize, octave_n = octaves, octave_scale = octave_scale, jitter=jitter, end = endparam, objective_fn=objective_guide, guide_features=guide_features,) + frame = deepdream_guided(net, frame, image_type=image_type, verbose=verbose, iter_n=iterations, step_size=stepsize, octave_n=octaves, octave_scale=octave_scale, jitter=jitter, end=endparam, objective_fn=objective_guide, guide_features=guide_features,) - saveframe = output + "/%08d.%s" % (frame_i, image_type) + saveframe = output + '/%08d.%s' % (frame_i, image_type) later = time.time() difference = int(later - now) totaltime += difference avgtime = (totaltime / i) # 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-1) - print 'Frame Time: ' + str(difference) + 's' - timeleft = avgtime * ((nrframes-1) - frame_i) + print('***************************************') + print('Saving Image As: ' + saveframe) + print('Frame ' + str(i) + ' of ' + str(nrframes - 1)) + print('Frame Time: ' + str(difference) + 's') + timeleft = avgtime * ((nrframes - 1) - 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 '***************************************' + 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 + "/%08d.%s" % (frame_i,image_type) + newframe = input + '/%08d.%s' % (frame_i, image_type) if blend == 0: newimg = PIL.Image.open(newframe) if preview is not 0: - newimg = resizePicture(newframe,preview) + newimg = resizePicture(newframe, preview) frame = newimg else: if blend == 'random': - blendval=randint(5,10)/10. + blendval = randint(5, 10) / 10. elif blend == 'loop': - if blend_at > 1 - blend_step: blend_forward = False - elif blend_at <= 0.5: blend_forward = True - if blend_forward: blend_at += blend_step - else: blend_at -= blend_step + if blend_at > 1 - blend_step: + blend_forward = False + elif blend_at <= 0.5: + blend_forward = True + if blend_forward: + blend_at += blend_step + else: + blend_at -= blend_step blendval = blend_at - else: blendval = float(blend) - frame = morphPicture(saveframe,newframe,blendval,preview) + else: + blendval = float(blend) + frame = morphPicture(saveframe, newframe, blendval, preview) frame = np.float32(frame) @@ -380,106 +410,28 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave frame_i += 1 -if __name__ == "__main__": +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( - '-it','--image_type', - help='Specify whether jpg or png ', - required=True) - parser.add_argument( - "--gpu", - default= None, - help="Switch for gpu computation." - ) #int can chose index of gpu, if there are multiple gpu's 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=str, - required=False, - help='Blend Amount. Default: "0.5" (constant), or "loop" (0.5-1.0), or "random"') - 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') - parser.add_argument( - '-v', '--verbose', - type=int, - required=False, - help="verbosity [0-3]") - parser.add_argument( - '-gi', '--guide_image', - required=False, - help="path to guide image") - parser.add_argument( - '-sf', '--start_frame', - type=int, - required=False, - help="starting frame nr") - parser.add_argument( - '-ef', '--end_frame', - type=int, - required=False, - help="end frame nr") - parser.add_argument( - '-e', '--extract', - type=int, - required=False, - help="Extract frames from video") + 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('-it', '--image_type', help='Specify whether jpg or png ', required=True) + parser.add_argument('--gpu', default=None, help='Switch for gpu computation.') # int can chose index of gpu, if there are multiple gpu's 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=str, required=False, help='Blend Amount. Default: "0.5" (constant), or "loop" (0.5-1.0), or "random"') + 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') + parser.add_argument('-v', '--verbose', type=int, required=False, help='verbosity [0-3]') + parser.add_argument('-gi', '--guide_image', required=False, help='path to guide image') + parser.add_argument('-sf', '--start_frame', type=int, required=False, help='starting frame nr') + parser.add_argument('-ef', '--end_frame', type=int, required=False, help='end frame nr') + parser.add_argument('-e', '--extract', type=int, required=False, help='Extract frames from video') args = parser.parse_args() @@ -487,16 +439,16 @@ def main(input, output, image_type, gpu, model_path, model_name, preview, octave 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") + 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") + 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) if args.extract is 1: From 7daecf3a099a68a207f54a5b2e383740254f798f Mon Sep 17 00:00:00 2001 From: Yvan Sraka Date: Sun, 25 Oct 2015 11:51:33 +0100 Subject: [PATCH 4/4] Fix 2nd step --- 2_dreaming_time.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 2_dreaming_time.py diff --git a/2_dreaming_time.py b/2_dreaming_time.py old mode 100644 new mode 100755