Skip to content

Instantly share code, notes, and snippets.

@baku89
Created October 30, 2016 01:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save baku89/73dd11f3b9f325efd884f3469b0d3f32 to your computer and use it in GitHub Desktop.
Save baku89/73dd11f3b9f325efd884f3469b0d3f32 to your computer and use it in GitHub Desktop.
My first video deep-dream
from batcountry import BatCountry
import numpy as np
from PIL import Image
from glob import glob
import os
import random
CAFFE_ROOT = '../caffe'
INPUT_PATH = 'input.jpg'
def gradient_ascent_step(net, step_size=1.5, end="inception_4c/output",
jitter=32, clip=True, objective_fn=None, **objective_params):
# if the objective function is None, initialize it as
# the standard L2 objective
if objective_fn is None:
objective_fn = BatCountry.L2_objective
# input image is stored in Net's 'data' blob
src = net.blobs["data"]
dst = net.blobs[end]
# apply jitter shift
ox, oy = np.random.randint(-jitter, jitter + 1, 2)
print ox, oy
src.data[0] = np.roll(np.roll(src.data[0], ox, -1), oy, -2)
net.forward(end=end)
objective_fn(dst, **objective_params)
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 # origin
#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)
def dream(path):
print '%s/models/bvlc_googlenet' % (CAFFE_ROOT)
bc = BatCountry('%s/models/bvlc_googlenet' % (CAFFE_ROOT))
image = bc.dream(np.float32(Image.open(path)), end='inception_3a/output', step_fn=gradient_ascent_step, step_size=2, octave_n=12)
bc.cleanup()
filename = 'result/perlin_c/out_%s' % os.path.basename(path)
result = Image.fromarray(np.uint8(image))
result.save(filename)
for path in glob('./perlin_c/*.png'):
# print "BK: processing... %s" % path
np.random.seed(1234)
dream(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment