Skip to content

Instantly share code, notes, and snippets.

@SubhadityaMukherjee
Last active January 21, 2020 16:12
Show Gist options
  • Save SubhadityaMukherjee/ae347c7ae9744a17309b6c788ecf3655 to your computer and use it in GitHub Desktop.
Save SubhadityaMukherjee/ae347c7ae9744a17309b6c788ecf3655 to your computer and use it in GitHub Desktop.
Deep dream implementation

url = 'https://nicolekessler.files.wordpress.com/2013/04/hellish_demons.jpg?w=1024'

Download

  • We first download the image
  • Resize it for faster computation
def download(url, max_dim=None):
    name = "demons.jpg"
    image_path = tf.keras.utils.get_file(name, origin=url)
    img = PIL.Image.open(image_path)
    if max_dim:
        img.thumbnail((max_dim, max_dim))
    return np.array(img)

Deprocess

  • This is a process called normalization
  • tf.cast is used to convert the tensor into a 8 bit integer value representation
def deprocess(img):
    img = 255 * (img + 1.0) / 2.0
    return tf.cast(img, tf.uint8)

Show

  • just a wrapper to convert the tensor into an array and display
def show(img):
    display.display(PIL.Image.fromarray(np.array(img)))
original_img = download(url, max_dim=500)
show(original_img)
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import tensorflow as tf
import IPython.display as display
import matplotlib as mpl
import numpy as np
import PIL.Image
from tensorflow.keras.preprocessing import image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment