Skip to content

Commit

Permalink
Update deep dream example for Keras 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
hertschuh committed Nov 9, 2023
1 parent a7787f6 commit fe285a4
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 101 deletions.
4 changes: 2 additions & 2 deletions examples/generative/deep_dream.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import inception_v3
import keras
from keras.applications import inception_v3

base_image_path = keras.utils.get_file("sky.jpg", "https://i.imgur.com/aGBdQyK.jpg")
result_prefix = "sky_dream"
Expand Down
Binary file modified examples/generative/img/deep_dream/deep_dream_17_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 20 additions & 20 deletions examples/generative/ipynb/deep_dream.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"- Stop when we are back to the original size.\n",
"To obtain the detail lost during upscaling, we simply\n",
"take the original image, shrink it down, upscale it,\n",
"and compare the result to the (resized) original image.\n"
"and compare the result to the (resized) original image."
]
},
{
Expand All @@ -51,7 +51,7 @@
"colab_type": "text"
},
"source": [
"## Setup\n"
"## Setup"
]
},
{
Expand All @@ -64,8 +64,8 @@
"source": [
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras.applications import inception_v3\n",
"import keras\n",
"from keras.applications import inception_v3\n",
"\n",
"base_image_path = keras.utils.get_file(\"sky.jpg\", \"https://i.imgur.com/aGBdQyK.jpg\")\n",
"result_prefix = \"sky_dream\"\n",
Expand All @@ -87,7 +87,7 @@
"num_octave = 3 # Number of scales at which to run gradient ascent\n",
"octave_scale = 1.4 # Size ratio between scales\n",
"iterations = 20 # Number of ascent steps per scale\n",
"max_loss = 15.0\n"
"max_loss = 15.0"
]
},
{
Expand All @@ -96,7 +96,7 @@
"colab_type": "text"
},
"source": [
"This is our base image:\n"
"This is our base image:"
]
},
{
Expand All @@ -109,7 +109,7 @@
"source": [
"from IPython.display import Image, display\n",
"\n",
"display(Image(base_image_path))\n"
"display(Image(base_image_path))"
]
},
{
Expand All @@ -118,7 +118,7 @@
"colab_type": "text"
},
"source": [
"Let's set up some image preprocessing/deprocessing utilities:\n"
"Let's set up some image preprocessing/deprocessing utilities:"
]
},
{
Expand Down Expand Up @@ -150,7 +150,7 @@
" # Convert to uint8 and clip to the valid range [0, 255]\n",
" x = np.clip(x, 0, 255).astype(\"uint8\")\n",
" return x\n",
"\n"
""
]
},
{
Expand All @@ -162,7 +162,7 @@
"## Compute the Deep Dream loss\n",
"\n",
"First, build a feature extraction model to retrieve the activations of our target layers\n",
"given an input image.\n"
"given an input image."
]
},
{
Expand All @@ -186,7 +186,7 @@
"\n",
"# Set up a model that returns the activation values for every target layer\n",
"# (as a dict)\n",
"feature_extractor = keras.Model(inputs=model.inputs, outputs=outputs_dict)\n"
"feature_extractor = keras.Model(inputs=model.inputs, outputs=outputs_dict)"
]
},
{
Expand All @@ -195,7 +195,7 @@
"colab_type": "text"
},
"source": [
"The actual loss computation is very simple:\n"
"The actual loss computation is very simple:"
]
},
{
Expand All @@ -218,7 +218,7 @@
" scaling = tf.reduce_prod(tf.cast(tf.shape(activation), \"float32\"))\n",
" loss += coeff * tf.reduce_sum(tf.square(activation[:, 2:-2, 2:-2, :])) / scaling\n",
" return loss\n",
"\n"
""
]
},
{
Expand All @@ -227,7 +227,7 @@
"colab_type": "text"
},
"source": [
"## Set up the gradient ascent loop for one octave\n"
"## Set up the gradient ascent loop for one octave"
]
},
{
Expand Down Expand Up @@ -259,7 +259,7 @@
" break\n",
" print(\"... Loss value at step %d: %.2f\" % (i, loss))\n",
" return img\n",
"\n"
""
]
},
{
Expand All @@ -268,7 +268,7 @@
"colab_type": "text"
},
"source": [
"## Run the training loop, iterating over different octaves\n"
"## Run the training loop, iterating over different octaves"
]
},
{
Expand All @@ -284,7 +284,7 @@
"\n",
"successive_shapes = [original_shape]\n",
"for i in range(1, num_octave):\n",
" shape = tuple([int(dim / (octave_scale ** i)) for dim in original_shape])\n",
" shape = tuple([int(dim / (octave_scale**i)) for dim in original_shape])\n",
" successive_shapes.append(shape)\n",
"successive_shapes = successive_shapes[::-1]\n",
"shrunk_original_img = tf.image.resize(original_img, successive_shapes[0])\n",
Expand All @@ -303,7 +303,7 @@
" img += lost_detail\n",
" shrunk_original_img = tf.image.resize(original_img, shape)\n",
"\n",
"keras.utils.save_img(result_prefix + \".png\", deprocess_image(img.numpy()))\n"
"keras.utils.save_img(result_prefix + \".png\", deprocess_image(img.numpy()))"
]
},
{
Expand All @@ -314,7 +314,7 @@
"source": [
"Display the result.\n",
"\n",
"You can use the trained model hosted on [Hugging Face Hub](https://huggingface.co/keras-io/deep-dream) ",
"You can use the trained model hosted on [Hugging Face Hub](https://huggingface.co/keras-io/deep-dream)\n",
"and try the demo on [Hugging Face Spaces](https://huggingface.co/spaces/keras-io/deep-dream)."
]
},
Expand All @@ -326,7 +326,7 @@
},
"outputs": [],
"source": [
"display(Image(result_prefix + \".png\"))\n"
"display(Image(result_prefix + \".png\"))"
]
}
],
Expand Down
Loading

0 comments on commit fe285a4

Please sign in to comment.