Missed TensorFlow World? Check out the recap.

# DeepDream

This tutorial contains a minimal implementation of DeepDream, as described in this blog post by Alexander Mordvintsev.

DeepDream is an experiment that visualizes the patterns learned by a neural network. Similar to when a child watches clouds and tries to interpret random shapes, DeepDream over-interprets and enhances the patterns it sees in an image.

It does so by forwarding an image through the network, then calculating the gradient of the image with respect to the activations of a particular layer. The image is then modified to increase these activations, enhancing the patterns seen by the network, and resulting in a dream-like image. This process was dubbed "Inceptionism" (a reference to InceptionNet, and the movie Inception.

Let's demonstrate how you can make a neural network "dream" and enhance the surreal patterns it sees in an image. ``````from __future__ import absolute_import, division, print_function, unicode_literals
``````
``````import tensorflow as tf
``````
``````import numpy as np

import matplotlib as mpl

from IPython.display import clear_output
from matplotlib import pyplot as plt
from tensorflow.keras.preprocessing import image
``````

## Choose an image to dream-ify

For this tutorial, let's use an image of a labrador.

``````url = 'https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg'
``````
``````# Download an image and read it into a NumPy array.
name = url.split('/')[-1]
image_path = tf.keras.utils.get_file(name, origin=url)
return img

# Normalize an image
def deprocess(img):
img = 255*(img + 1.0)/2.0
return tf.cast(img, tf.uint8)

# Display an image
def show(img):
plt.figure(figsize=(12,12))
plt.grid(False)
plt.axis('off')
plt.imshow(img)
plt.show()

# Downsizing the image makes it easier to work with.
original_img = np.array(original_img)

show(original_img)
``````
```Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg
90112/83281 [================================] - 0s 0us/step
``` ## Prepare the feature extraction model

Download and prepare a pre-trained image classification model. You will use InceptionV3 which is similar to the model originally used in DeepDream. Note that any pre-trained model will work, although you will have to adjust the layer names below if you change this.

``````base_model = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet')
``````
```Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5
87916544/87910968 [==============================] - 6s 0us/step
```

The idea in DeepDream is to choose a layer (or layers) and maximize the "loss" in a way that the image increasingly "excites" the layers. The complexity of the features incorporated depends on layers chosen by you, i.e, lower layers produce strokes or simple patterns, while deeper layers give sophisticated features in images, or even whole objects.

The InceptionV3 architecture is quite large (for a graph of the model architecture see TensorFlow's research repo). For DeepDream, the layers of interest are those where the convolutions are concatenated. There are 11 of these layers in InceptionV3, named 'mixed0' though 'mixed10'. Using different layers will result in different dream-like images. Deeper layers respond to higher-level features (such as eyes and faces), while earlier layers respond to simpler features (such as edges, shapes, and textures). Feel free to experiment with the layers selected below, but keep in mind that deeper layers (those with a higher index) will take longer to train on since the gradient computation is deeper.

``````# Maximize the activations of these layers
names = ['mixed3', 'mixed5']
layers = [base_model.get_layer(name).output for name in names]

# Create the feature extraction model
dream_model = tf.keras.Model(inputs=base_model.input, outputs=layers)
``````

## Calculate loss

The loss is the sum of the activations in the chosen layers. The loss is normalizaed at each layer so the contribution from larger layers does not outweigh smaller layers. Normally, loss is a quantity you wish to minimize via gradient descent. In DeepDream, you will maximize this loss via gradient ascent.

``````def calc_loss(img, model):
# Pass forward the image through the model to retrieve the activations.
# Converts the image into a batch of size 1.
img_batch = tf.expand_dims(img, axis=0)
layer_activations = model(img_batch)

losses = []
for act in layer_activations:
loss = tf.math.reduce_mean(act)
losses.append(loss)

return  tf.reduce_sum(losses)
``````

Once you have calculated the loss for the chosen layers, all that is left is to calculate the gradients with respect to the image, and add them to the original image.

Adding the gradients to the image enhances the patterns seen by the network. At each step, you will have created an image that increasingly excites the activations of certain layers in the network.

``````@tf.function
def deepdream(model, img, step_size):
# This needs gradients relative to `img`
# `GradientTape` only watches `tf.Variable`s by default
tape.watch(img)
loss = calc_loss(img, model)

# Calculate the gradient of the loss with respect to the pixels of the input image.

# In gradient ascent, the "loss" is maximized so that the input image increasingly "excites" the layers.
# You can update the image by directly adding the gradients (because they're the same shape!)
img = tf.clip_by_value(img, -1, 1)

return loss, img
``````
``````def run_deep_dream_simple(model, img, steps=100, step_size=0.01):
# Convert from uint8 to the range expected by the model.
img = tf.keras.applications.inception_v3.preprocess_input(img)

for step in range(steps):
loss, img = deepdream(model, img, step_size)

if step % 100 == 0:
clear_output(wait=True)
show(deprocess(img))
print ("Step {}, loss {}".format(step, loss))

result = deprocess(img)
clear_output(wait=True)
show(result)

return result
``````
``````dream_img = run_deep_dream_simple(model=dream_model, img=original_img,
steps=800, step_size=0.001)
`````` ## Taking it up an octave

Pretty good, but there are a few issues with this first attempt:

1. The output is noisy (this could be addressed with a `tf.image.total_variation` loss).
2. The image is low resolution.
3. The patterns appear like they're all happening at the same granularity.

One approach that addresses all these problems is applying gradient ascent at different scales. This will allow patterns generated at smaller scales to be incorporated into patterns at higher scales and filled in with additional detail.

To do this you can perform the previous gradient ascent approach, then increase the size of the image (which is reffered to as an octave), and repeat this process for multiple octaves.

``````OCTAVE_SCALE = 1.3

img = tf.constant(np.array(original_img))
base_shape = tf.cast(tf.shape(img)[:-1], tf.float32)

for n in range(3):
new_shape = tf.cast(base_shape*(OCTAVE_SCALE**n), tf.int32)

img = tf.image.resize(img, new_shape).numpy()

img = run_deep_dream_simple(model=dream_model, img=img, steps=200, step_size=0.001)

clear_output(wait=True)
show(img)
`````` ## Scaling up with tiles

One thing to consider is that as the image increases in size, so will the time and memory necessary to perform the gradient calculation. The above octave implementation will not work on very large images, or many octaves.

To avoid this issue you can split the image into tiles and compute the gradient for each tile.

Applying random shifts to the image before each tiled computation prevents tile seams from appearing.

Start by implementing the random shift:

``````def random_roll(img, maxroll):
# Randomly shift the image to avoid tiled boundaries.
shift = tf.random.uniform(shape=, minval=-maxroll, maxval=maxroll, dtype=tf.int32)
shift_down, shift_right = shift,shift
img_rolled = tf.roll(tf.roll(img, shift_right, axis=1), shift_down, axis=0)
return shift_down, shift_right, img_rolled
``````
``````shift_down, shift_right, img_rolled = random_roll(np.array(original_img), 512)
show(img_rolled)
`````` Here is a tiled equivalent of the `deepdream` function defined earlier:

``````@tf.function
shift_down, shift_right, img_rolled = random_roll(img, tile_size)

# Initialize the image gradients to zero.

for x in tf.range(0, img_rolled.shape, tile_size):
for y in tf.range(0, img_rolled.shape, tile_size):
# Calculate the gradients for this tile.
# This needs gradients relative to `img_rolled`.
# `GradientTape` only watches `tf.Variable`s by default.
tape.watch(img_rolled)

# Extract a tile out of the image.
img_tile = img_rolled[x:x+tile_size, y:y+tile_size]
loss = calc_loss(img_tile, model)

# Update the image gradients for this tile.

# Undo the random shift applied to the image and its gradients.

``````

Putting this together gives a scalable, octave-aware deepdream implementation:

``````def run_deep_dream_with_octaves(model, img, steps_per_octave=100, step_size=0.01,
num_octaves=3, octave_scale=1.3):
img = tf.keras.preprocessing.image.img_to_array(img)
img = tf.keras.applications.inception_v3.preprocess_input(img)

for octave in range(num_octaves):
# Scale the image based on the octave
if octave>0:
new_size = tf.cast(tf.convert_to_tensor(img.shape[:2]), tf.float32)*octave_scale
img = tf.image.resize(img, tf.cast(new_size, tf.int32))

for step in range(steps_per_octave):
img = tf.clip_by_value(img, -1, 1)

if step % 10 == 0:
clear_output(wait=True)
show(deprocess(img))
print ("Octave {}, Step {}".format(octave, step))

clear_output(wait=True)
result = deprocess(img)
show(result)

return result
``````
``````dream_img = run_deep_dream_with_octaves(model=dream_model, img=original_img, step_size=0.01)

clear_output()
show(original_img)
show(dream_img)
``````  Much better! Play around with the number of octaves, octave scale, and activated layers to change how your DeepDream-ed image looks.

Readers might also be interested in TensorFlow Lucid which expands on ideas introduced in this tutorial to visualize and interpret neural networks.