Deep Dream
Google
make_step function
script clarification
Python

Clarification wanted for make_step function of Google's deep dream script

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

The make_step function in DeepDream performs one gradient-ascent update on an input image to amplify features that strongly activate selected network layers. It is not a full training step and does not update model weights. Instead, it updates pixels to increase activations, producing the dream-like visual effect.

Conceptual Role of make_step

DeepDream treats the input image as an optimizable variable. During each step, it computes gradient of an objective with respect to image pixels, then nudges the image in the gradient direction. Repeating this process sharpens patterns detected by the network.

High-level flow:

  • Forward pass to compute chosen layer activations.
  • Compute objective from those activations.
  • Backpropagate gradient to image.
  • Normalize gradient and update pixels.
  • Optionally clip or jitter for stability.

This is gradient ascent on input space, not gradient descent on parameters.

Minimal TensorFlow-style Example

The exact implementation depends on framework version, but the algorithm is stable.

python
1import tensorflow as tf
2
3@tf.function
4def make_step(model, image, step_size=0.01):
5    with tf.GradientTape() as tape:
6        tape.watch(image)
7        activations = model(image)
8        loss = tf.reduce_mean(activations)
9
10    grads = tape.gradient(loss, image)
11    grads /= tf.math.reduce_std(grads) + 1e-8
12    image = image + step_size * grads
13    image = tf.clip_by_value(image, -1.0, 1.0)
14    return image, loss

Here, model is usually a submodel exposing intermediate layers rather than final classification logits.

Why Normalization, Jitter, and Octaves Matter

Raw gradients can be noisy and dominated by high-frequency artifacts. Gradient normalization stabilizes update scale across steps. Random jitter shifts image slightly between steps, reducing tile artifacts and encouraging broader feature amplification.

Octave processing repeats make_step across multiple image scales. Lower scales shape global structure, higher scales add fine texture. This multiscale strategy produces richer dream patterns than single-scale updates.

python
1def run_dream(model, image, steps=50):
2    for _ in range(steps):
3        image, loss = make_step(model, image)
4    return image

In production scripts, add logging for loss values and clamp ranges to avoid exploding pixel values.

Practical Debugging Guidance

If output stays unchanged, confirm gradients are non-zero and that chosen layer actually responds to current input. If output becomes noisy static, reduce step size and increase clipping discipline. Also verify preprocessing and deprocessing paths are inverse-compatible so visualized output is meaningful.

It helps to save intermediate frames every few steps. Visual checkpoints reveal whether changes are smooth and cumulative or unstable.

Layer Selection and Objective Design

DeepDream output quality depends heavily on which layers drive the objective. Lower layers emphasize edges and textures, while higher layers produce complex semantic motifs. Many implementations blend several layers with weighted contributions for balanced visuals.

python
1def deepdream_loss(activations, weights):
2    # activations is a dict of layer name to tensor
3    loss = 0.0
4    for name, tensor in activations.items():
5        loss += weights.get(name, 0.0) * tf.reduce_mean(tensor)
6    return loss

Tune weights gradually and inspect outputs after small step batches. Aggressive weighting can create unstable artifacts quickly.

Octave Workflow in Practice

A common recipe scales image down, runs gradient ascent, then scales back up and repeats. This preserves global structure while adding detail progressively.

python
1def octave_dream(model, image, octaves=(1.0, 1.4, 1.8), steps=20):
2    base = image
3    for scale in octaves:
4        h = int(tf.shape(base)[1] * scale)
5        w = int(tf.shape(base)[2] * scale)
6        resized = tf.image.resize(base, [h, w])
7        for _ in range(steps):
8            resized, _ = make_step(model, resized)
9        base = resized
10    return base

This staged process is why classical DeepDream images contain both coherent large forms and fine-grained local textures.

Common Pitfalls

  • Interpreting make_step as model training instead of input optimization.
  • Using final softmax outputs as objective and getting weak gradients.
  • Skipping gradient normalization and producing unstable updates.
  • Forgetting to clip pixel values after each ascent step.
  • Ignoring preprocessing conventions expected by the base network.

Summary

  • make_step performs one gradient-ascent update on the image pixels.
  • Model weights remain fixed; only input image changes.
  • Gradient normalization and clipping improve stability.
  • Multiscale octave processing yields richer DeepDream images.
  • Debug with intermediate saves and gradient diagnostics for predictable results.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms