Hey, so you may have heard about Google’s Deep Dream project by now, however if not its basically a convolutional neural network that enhances patterns in images using an algorithmic pareidolia. The result is some really cool and really trippy images. For example, This is the image before 60 iterations of manipulation:
And this is the result:
In this post I want to show you how you can make your own images using Deep Dream. The github page where you can download a bunch of tutorials on Deep Dream can be bound here. I would recommend cloning the repository to a folder on your computer and looking through the different files. They also have some sample images you can use. However, The main repository we will be using can be found here and is hosted by Sentdex who runs pythonprogramming.net. He is actually the inspiration for this post. But anyways lets get started!
'''
This code was originally created by an employee at Google,
then it was edited and hosted by Sentdex.
Copyright (c) 2016 by Magnus Erik Hvass Pedersen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Some info on various layers, so you know what to expect
depending on which layer you choose:
layer 1: wavy
layer 2: lines
layer 3: boxes
layer 4: circles?
layer 6: dogs, bears, cute animals.
layer 7: faces, buildings
layer 8: fish begin to appear, frogs/reptilian eyes.
layer 10: Monkies, lizards, snakes, duck
Choosing various parameters like num_iterations, rescale,
and num_repeats really varies on which layer you are doing.
We could probably come up with some sort of formula. The
deeper the layer is, the more iterations and
repeats you will want.
Layer 3: 20 iterations, 0.5 rescale, and 8 repeats is decent start
Layer 10: 40 iterations and 25 repeats is good. '''
from deepdreamer import model, load_image, recursive_optimize
import numpy as np
import PIL.Image
import random
layer_tensor = model.layer_tensors[4]
# Res: 368 x 326
file_name = "R:\\Programs\\MachineLearning\\generative_ML\\deep_dreaming_start\\the-starry-night\\the-starry-night-800x450.jpg"
img_result = load_image(filename='{}'.format(file_name))
img_result = recursive_optimize(layer_tensor=layer_tensor, image=img_result,
# how clear is the dream vs original image
num_iterations=40, step_size=1.0, rescale_factor=0.5,
# How many "passes" over the data. More passes, the more granular the gradients will be.
num_repeats=8, blend=0.2)
img_result = np.clip(img_result, 0.0, 255.0)
img_result = img_result.astype(np.uint8)
result = PIL.Image.fromarray(img_result, mode='RGB')
save_file = 'dream_image_out_{}.jpg'.format(random.randint(1, 100000))
result.save(save_file)
result.show()
layer_tensor = model.layer_tensors[11]
, which changes the type of images that will start to appear; the num_iterations=100
, which changes the number of iterations over the image; and the file_name
, which will be the path of the file you want to alter. This one used layer 10: Monkies, lizards, snakes, ducks.
This one used layer 8: fish begin to appear, frogs/reptilian eyes.
This one used layer 3: boxes.
file_name
variable to point to a picture on your computer. I recommend picking an image that’s not too large because it will be significantly quicker to process.pip install pillow
and pip install numpy
into the terminal and running each of them. python dream_image.py
, its that simple. It should look like this while its processing:This video was created using Deep Dream by processing an image by one iteration per frame them putting all the images together to make this video. Its super cool, you should watch it.