Deep Dream

generative
Generating Deep Dreams with Keras.
Authors

fchollet

dfalbel - R translation

Introduction

“Deep dream” is an image-filtering technique which consists of taking an image classification model, and running gradient ascent over an input image to try to maximize the activations of specific layers (and sometimes, specific units in specific layers) for this input. It produces hallucination-like visuals.

It was first introduced by Alexander Mordvintsev from Google in July 2015.

Process:

  • Load the original image.
  • Define a number of processing scales (“octaves”), from smallest to largest.
  • Resize the original image to the smallest scale.
  • For every scale, starting with(the smallest (i$e. current one), { })
    • Run gradient ascent
    • Upscale image to the next scale
    • Reinject the detail that was lost at upscaling time
  • Stop when we are back to the original size. To obtain the detail lost during upscaling, we simply take the original image, shrink it down, upscale it, and compare the result to the (resized) original image.

Setup

library(tensorflow)
library(keras)

base_image_path <- get_file("sky.jpg", "https://i.imgur.com/aGBdQyK.jpg")
Loaded Tensorflow version 2.9.1
result_prefix <- "sky_dream"

# These are the names of the layers
# for which we try to maximize activation,
# as well as their weight in the final loss
# we try to maximize.
# You can tweak these setting to obtain new visual effects.
layer_settings <- list(
  "mixed4" = 1.0,
  "mixed5" = 1.5,
  "mixed6" = 2.0,
  "mixed7" = 2.5
)

# Playing with these hyperparameters will also allow you to achieve new effects

step <- 0.01  # Gradient ascent step size
num_octave <- 3  # Number of scales at which to run gradient ascent
octave_scale <- 1.4  # Size ratio between scales
iterations <- 20  # Number of ascent steps per scale
max_loss <- 15.0

This is our base image:

plot_image <- function(img) {
  img %>%   
    as.raster(max = 255) %>% 
    plot()
}

base_image_path %>% 
  image_load() %>% 
  image_to_array() %>% 
  plot_image()

Let’s set up some image preprocessing/deprocessing utilities:

preprocess_image <- function(image_path) {
  # Util function to open, resize and format pictures
  # into appropriate arrays.
  img <- image_path %>% 
    image_load() %>% 
    image_to_array()
  dim(img) <- c(1, dim(img))
  inception_v3_preprocess_input(img)
}

deprocess_image <- function(x) {
  dim(x) <- dim(x)[-1]
  # Undo inception v3 preprocessing
  x <- x/2.0
  x <- x + 0.5
  x <- x*255.0
  x[] <- raster::clamp(as.numeric(x), 0, 255)
  x
}

Compute the Deep Dream loss

First, build a feature extraction model to retrieve the activations of our target layers given an input image.

# Build an InceptionV3 model loaded with pre-trained ImageNet weights
model <- application_inception_v3(weights = "imagenet", include_top = FALSE)

# Get the symbolic outputs of each "key" layer (we gave them unique names).
outputs_dict <- purrr::imap(layer_settings, function(v, name) {
  layer <- get_layer(model, name)
  layer$output
})

# Set up a model that returns the activation values for every target layer
# (as a dict)

feature_extractor <- keras_model(inputs = model$inputs, outputs = outputs_dict)

The actual loss computation is very simple:

compute_loss <- function(input_image) {
  features <- feature_extractor(input_image)
  # Initialize the loss
  loss <- tf$zeros(shape = shape())
  
  layer_settings %>% 
    purrr::imap(function(coeff, name) {
      activation <- features[[name]]
      scaling <- tf$reduce_prod(tf$cast(tf$shape(activation), "float32"))
      # We avoid border artifacts by only involving non-border pixels in the loss.
      coeff * tf$reduce_sum(tf$square(activation[, 3:-2, 3:-2, ])) / scaling
    }) %>% 
    purrr::reduce(tf$add)
}

Set up the gradient ascent loop for one octave

gradient_ascent_step <- tf_function(function(img, learning_rate) {
  with(tf$GradientTape() %as% tape, {    
    tape$watch(img)
    loss <- compute_loss(img)
  })
  
  # Compute gradients.
  grads <- tape$gradient(loss, img)
  # Normalize gradients.
  grads <- grads/tf$maximum(tf$reduce_mean(tf$abs(grads)), 1e-6)
  img <- img + learning_rate * grads
  list(loss, img)
})


gradient_ascent_loop <- function(img, iterations, learning_rate, max_loss = NULL) {
  for (i in seq_len(iterations)) {
    c(loss, img) %<-% gradient_ascent_step(img, learning_rate)
    if (!is.null(max_loss) && as.logical(loss > max_loss)) {
      break
    }
    cat("... Loss value at step ", i, ": ", as.numeric(loss), "\n")
  }
  img
}

Run the training loop, iterating over different octaves

original_img <- preprocess_image(base_image_path)
original_shape <- dim(original_img)[2:3]

successive_shapes <- list(original_shape)
for (i in seq_len(num_octave - 1)) {
  shape <- as.integer(original_shape / octave_scale^i)
  successive_shapes[[i+1]] <- shape
}
successive_shapes <- rev(successive_shapes)

shrunk_original_img <- tf$image$resize(original_img, successive_shapes[[1]])
img <- tf$identity(original_img)  # Make a copy
for (i in seq_along(successive_shapes)) {
  shape <- successive_shapes[[i]]
  
  cat("Processing octave ", i, "with shape:", shape, "\n")
  
  img <- tf$image$resize(img, shape)
  img <- gradient_ascent_loop(
    img, iterations = iterations, learning_rate = step, max_loss = max_loss
  )
  upscaled_shrunk_original_img <- tf$image$resize(shrunk_original_img, shape)
  same_size_original <- tf$image$resize(original_img, shape)
  lost_detail <- same_size_original - upscaled_shrunk_original_img
  
  img <- img + lost_detail
  shrunk_original_img <- tf$image$resize(original_img, shape)
}
Processing octave  1 with shape: 326 489 
Warning: Negative numbers are interpreted python-style when subsetting tensorflow tensors.
See: ?`[.tensorflow.tensor` for details.
To turn off this warning, set `options(tensorflow.extract.warn_negatives_pythonic = FALSE)`
... Loss value at step  1 :  0.5014444 
... Loss value at step  2 :  0.7033239 
... Loss value at step  3 :  0.9975789 
... Loss value at step  4 :  1.339841 
... Loss value at step  5 :  1.69038 
... Loss value at step  6 :  2.055832 
... Loss value at step  7 :  2.368303 
... Loss value at step  8 :  2.700009 
... Loss value at step  9 :  3.018286 
... Loss value at step  10 :  3.355762 
... Loss value at step  11 :  3.659731 
... Loss value at step  12 :  3.970093 
... Loss value at step  13 :  4.239055 
... Loss value at step  14 :  4.570164 
... Loss value at step  15 :  4.875012 
... Loss value at step  16 :  5.180275 
... Loss value at step  17 :  5.479495 
... Loss value at step  18 :  5.700634 
... Loss value at step  19 :  6.022943 
... Loss value at step  20 :  6.264833 
Processing octave  2 with shape: 457 685 
... Loss value at step  1 :  1.214347 
... Loss value at step  2 :  1.962548 
... Loss value at step  3 :  2.586394 
... Loss value at step  4 :  3.085848 
... Loss value at step  5 :  3.57484 
... Loss value at step  6 :  4.002796 
... Loss value at step  7 :  4.40269 
... Loss value at step  8 :  4.807469 
... Loss value at step  9 :  5.220548 
... Loss value at step  10 :  5.58031 
... Loss value at step  11 :  5.953027 
... Loss value at step  12 :  6.311125 
... Loss value at step  13 :  6.666377 
... Loss value at step  14 :  7.01564 
... Loss value at step  15 :  7.373053 
... Loss value at step  16 :  7.687048 
... Loss value at step  17 :  8.022481 
... Loss value at step  18 :  8.353153 
... Loss value at step  19 :  8.676409 
... Loss value at step  20 :  8.996731 
Processing octave  3 with shape: 640 960 
... Loss value at step  1 :  1.34736 
... Loss value at step  2 :  2.137553 
... Loss value at step  3 :  2.785597 
... Loss value at step  4 :  3.325019 
... Loss value at step  5 :  3.836637 
... Loss value at step  6 :  4.312602 
... Loss value at step  7 :  4.782893 
... Loss value at step  8 :  5.230842 
... Loss value at step  9 :  5.646795 
... Loss value at step  10 :  6.066389 
... Loss value at step  11 :  6.450614 
... Loss value at step  12 :  6.861366 
... Loss value at step  13 :  7.217497 
... Loss value at step  14 :  7.596985 
... Loss value at step  15 :  7.9565 
... Loss value at step  16 :  8.316831 
... Loss value at step  17 :  8.604964 
... Loss value at step  18 :  8.966898 
... Loss value at step  19 :  9.21804 
... Loss value at step  20 :  9.593339 

Display the result.

img %>% 
  as.array() %>% 
  deprocess_image() %>% 
  plot_image()