Skip to content

Commit

Permalink
Update colab for arbitrary image stylization.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 273130120
  • Loading branch information
TensorFlow Hub Authors authored and andresusanopinto committed Oct 7, 2019
1 parent defef88 commit fe1965a
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions examples/colab/tf2_arbitrary_image_stylization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"import functools\n",
"import os\n",
"\n",
"from matplotlib import gridspec\n",
"import matplotlib.pylab as plt\n",
"import numpy as np\n",
"import tensorflow as tf\n",
Expand All @@ -131,21 +132,39 @@
"source": [
"# @title Define image loading and visualization functions { display-mode: \"form\" }\n",
"\n",
"def crop_center(image):\n",
" \"\"\"Returns a cropped square image.\"\"\"\n",
" shape = image.shape\n",
" new_shape = min(shape[1], shape[2])\n",
" offset_y = max(shape[1] - shape[2], 0) // 2\n",
" offset_x = max(shape[2] - shape[1], 0) // 2\n",
" image = tf.image.crop_to_bounding_box(\n",
" image, offset_y, offset_x, new_shape, new_shape)\n",
" return image\n",
"\n",
"@functools.lru_cache(maxsize=None)\n",
"def load_image(image_url, image_size=(256, 256), preserve_aspect_ratio=True):\n",
" \"\"\"Loads and preprocesses images.\"\"\"\n",
" # Cache image file locally.\n",
" image_path = tf.keras.utils.get_file(os.path.basename(image_url)[-128:], image_url)\n",
" # Load and convert to float32 numpy array, add batch dimension, and normalize to range [0, 1].\n",
" img = plt.imread(image_path).astype(np.float32)[np.newaxis, ...] / 255.\n",
" img = plt.imread(image_path).astype(np.float32)[np.newaxis, ...]\n",
" if img.max() > 1.0:\n",
" img = img / 255.\n",
" if len(img.shape) == 3:\n",
" img = tf.stack([img, img, img], axis=-1)\n",
" img = crop_center(img)\n",
" img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)\n",
" return img\n",
"\n",
"def show_n(images, titles=('',), w=8):\n",
"def show_n(images, titles=('',)):\n",
" n = len(images)\n",
" plt.figure(figsize=(w * n, w))\n",
" image_sizes = [image.shape[1] for image in images]\n",
" w = (image_sizes[0] * 6) // 320\n",
" plt.figure(figsize=(w * n, w))\n",
" gs = gridspec.GridSpec(1, n, width_ratios=image_sizes)\n",
" for i in range(n):\n",
" plt.subplot(1, n, 1 + i)\n",
" plt.subplot(gs[i])\n",
" plt.imshow(images[i][0], aspect='equal')\n",
" plt.axis('off')\n",
" plt.title(titles[i] if len(titles) > i else '')\n",
Expand Down Expand Up @@ -187,7 +206,7 @@
"\n",
"content_image = load_image(content_image_url, content_img_size)\n",
"style_image = load_image(style_image_url, style_img_size)\n",
"\n",
"style_image = tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME')\n",
"show_n([content_image, style_image], ['Content image', 'Style image'])"
],
"execution_count": 0,
Expand Down Expand Up @@ -313,17 +332,23 @@
" kandinsky_composition_7='https://upload.wikimedia.org/wikipedia/commons/b/b4/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg',\n",
" hubble_pillars_of_creation='https://upload.wikimedia.org/wikipedia/commons/6/68/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg',\n",
" van_gogh_starry_night='https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg',\n",
" turner_shipwreck='https://upload.wikimedia.org/wikipedia/commons/2/2e/Shipwreck_turner.jpg',\n",
" turner_nantes='https://upload.wikimedia.org/wikipedia/commons/b/b7/JMW_Turner_-_Nantes_from_the_Ile_Feydeau.jpg',\n",
" turner_rome='https://upload.wikimedia.org/wikipedia/commons/9/99/Joseph_Mallord_William_Turner_%28British_-_Modern_Rome-Campo_Vaccino_-_Google_Art_Project.jpg',\n",
" munch_scream='https://upload.wikimedia.org/wikipedia/commons/c/c5/Edvard_Munch%2C_1893%2C_The_Scream%2C_oil%2C_tempera_and_pastel_on_cardboard%2C_91_x_73_cm%2C_National_Gallery_of_Norway.jpg',\n",
" picasso_demoiselles_avignon='https://upload.wikimedia.org/wikipedia/en/4/4c/Les_Demoiselles_d%27Avignon.jpg',\n",
" picasso_violin='https://upload.wikimedia.org/wikipedia/en/3/3c/Pablo_Picasso%2C_1911-12%2C_Violon_%28Violin%29%2C_oil_on_canvas%2C_Kr%C3%B6ller-M%C3%BCller_Museum%2C_Otterlo%2C_Netherlands.jpg',\n",
" picasso_bottle_of_rum='https://upload.wikimedia.org/wikipedia/en/7/7f/Pablo_Picasso%2C_1911%2C_Still_Life_with_a_Bottle_of_Rum%2C_oil_on_canvas%2C_61.3_x_50.5_cm%2C_Metropolitan_Museum_of_Art%2C_New_York.jpg'\n",
" picasso_bottle_of_rum='https://upload.wikimedia.org/wikipedia/en/7/7f/Pablo_Picasso%2C_1911%2C_Still_Life_with_a_Bottle_of_Rum%2C_oil_on_canvas%2C_61.3_x_50.5_cm%2C_Metropolitan_Museum_of_Art%2C_New_York.jpg',\n",
" fire='https://upload.wikimedia.org/wikipedia/commons/3/36/Large_bonfire.jpg',\n",
" derkovits_woman_head='https://upload.wikimedia.org/wikipedia/commons/0/0d/Derkovits_Gyula_Woman_head_1922.jpg',\n",
" amadeo_style_life='https://upload.wikimedia.org/wikipedia/commons/8/8e/Untitled_%28Still_life%29_%281913%29_-_Amadeo_Souza-Cardoso_%281887-1918%29_%2817385824283%29.jpg',\n",
" derkovtis_talig='https://upload.wikimedia.org/wikipedia/commons/3/37/Derkovits_Gyula_Talig%C3%A1s_1920.jpg',\n",
" amadeo_cardoso='https://upload.wikimedia.org/wikipedia/commons/7/7d/Amadeo_de_Souza-Cardoso%2C_1915_-_Landscape_with_black_figure.jpg'\n",
")\n",
"\n",
"content_images = {k: load_image(v, (800, 800)) for k, v in content_urls.items()}\n",
"style_images = {k: load_image(v, (256, 256)) for k, v in style_urls.items()}"
"content_image_size = 384\n",
"style_image_size = 256\n",
"content_images = {k: load_image(v, (content_image_size, content_image_size)) for k, v in content_urls.items()}\n",
"style_images = {k: load_image(v, (style_image_size, style_image_size)) for k, v in style_urls.items()}\n",
"style_images = {k: tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME') for k, style_image in style_images.items()}\n"
],
"execution_count": 0,
"outputs": []
Expand All @@ -339,13 +364,13 @@
"#@title Specify the main content image and the style you want to use. { display-mode: \"form\" }\n",
"\n",
"content_name = 'sea_turtle' # @param ['sea_turtle', 'tuebingen', 'grace_hopper']\n",
"style_name = 'munch_scream' # @param ['kanagawa_great_wave', 'kandinsky_composition_7', 'hubble_pillars_of_creation', 'van_gogh_starry_night', 'turner_shipwreck', 'turner_nantes', 'turner_rome', 'munch_scream', 'picasso_demoiselles_avignon', 'picasso_violin', 'picasso_bottle_of_rum']\n",
"style_name = 'munch_scream' # @param ['kanagawa_great_wave', 'kandinsky_composition_7', 'hubble_pillars_of_creation', 'van_gogh_starry_night', 'turner_nantes', 'munch_scream', 'picasso_demoiselles_avignon', 'picasso_violin', 'picasso_bottle_of_rum', 'fire', 'derkovits_woman_head', 'amadeo_style_life', 'derkovtis_talig', 'amadeo_cardoso']\n",
"\n",
"stylized_image = hub_module(tf.constant(content_images[content_name]),\n",
" tf.constant(style_images[style_name]))[0]\n",
"\n",
"show_n([content_images[content_name], style_images[style_name], stylized_image],\n",
" titles=['Original content image', 'Style image', 'Stylized image'], w=9)"
" titles=['Original content image', 'Style image', 'Stylized image'])"
],
"execution_count": 0,
"outputs": []
Expand Down Expand Up @@ -374,7 +399,7 @@
"\n",
"content_image_url = '' # @param {type:\"string\"}\n",
"style_image_url = '' # @param {type:\"string\"}\n",
"output_image_size = 384 # @param {type:\"integer\"}\n",
"output_image_size = 384 # @param {type:\"integer\"}\n",
"\n",
"# The content image size can be arbitrary.\n",
"content_img_size = (output_image_size, output_image_size)\n",
Expand All @@ -385,12 +410,13 @@
"\n",
"content_image = load_image(content_image_url, content_img_size)\n",
"style_image = load_image(style_image_url, style_img_size)\n",
"style_image = tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME')\n",
"\n",
"show_n([content_image, style_image], ['Content image', 'Style image'])\n",
"\n",
"outputs = hub_module(tf.constant(content_image), tf.constant(style_image))\n",
"\n",
"show_n([outputs[0]], titles=['Stylized image'], w=13)"
"show_n([outputs[0]], titles=['Stylized image'])"
],
"execution_count": 0,
"outputs": []
Expand Down

0 comments on commit fe1965a

Please sign in to comment.