Skip to content

Instantly share code, notes, and snippets.

View SubhadityaMukherjee's full-sized avatar

Subhaditya Mukherjee SubhadityaMukherjee

View GitHub Profile
@SubhadityaMukherjee
SubhadityaMukherjee / content.py
Last active November 10, 2020 17:40
Neural Style Transfer
def get_content_loss(base_content, target):
return tf.reduce_mean(tf.square(base_content - target))
@SubhadityaMukherjee
SubhadityaMukherjee / downloadshow.md
Last active January 21, 2020 16:12
Deep dream implementation
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
@SubhadityaMukherjee
SubhadityaMukherjee / dowres.md
Created January 21, 2020 16:20
downloadres

Download

  • We first download the image
  • Resize it for faster computation
url = 'https://nicolekessler.files.wordpress.com/2013/04/hellish_demons.jpg?w=1024'
def download(url, max_dim=None):
    name = "demons.jpg"
 image_path = tf.keras.utils.get_file(name, origin=url)

Pre trained

  • We use Inception Net v3 which is a pretrained network that already has some idea of the world.
  • We use imagenet weights which basically allows us to use transfer learning on the network
  • Instead of training from scratch we can just cherry pick layers and use our neural network on it
base_model = tf.keras.applications.InceptionV3(include_top=False,
                                               weights='imagenet')

Model

calc_loss

  • We take the image and the model as inputs
  • expand dims basically adds an extra dimension to our input along the x axis to make it work with inception
  • for every activation in our layers, we calculate the loss and append it to a list
  • reduce_mean() and reduce_sum() are approximately the mean and sum equivalent for tensors instead of just plain arrays
  • Thus the sum is the total loss we get
def calc_loss(img, model):
 img_batch = tf.expand_dims(img, axis=0)

main class simple deepdream

  • the @tf.function allows the function to be precompiled. Since it is compiled, it runs faster
  • tensorspec basically allows us to pre define the shapes of specific arrays as we are pre compiling it

call

  • here we are trying to find the gradients of the image
  • this method is called gradient ascent. This adds the gradients found in every layer to the image and thus increases the activations at that point as well which is what we want
  • GradientTape allows us to keep a sort of history of all the gradients and allows us to use it to calculate loss directly from the history
  • After we get the gradients, we normalize them
  • img = img + gradients * step_size is the main ascent function which maximizes the loss
@SubhadityaMukherjee
SubhadityaMukherjee / mainsimple.md
Created January 21, 2020 16:38
simple deep dream

main class simple deepdream

  • the @tf.function allows the function to be precompiled. Since it is compiled, it runs faster
  • tensorspec basically allows us to pre define the shapes of specific arrays as we are pre compiling it

call

  • here we are trying to find the gradients of the image
  • this method is called gradient ascent. This adds the gradients found in every layer to the image and thus increases the activations at that point as well which is what we want
  • GradientTape allows us to keep a sort of history of all the gradients and allows us to use it to calculate loss directly from the history
  • After we get the gradients, we normalize them
  • img = img + gradients * step_size is the main ascent function which maximizes the loss

run_deep_dream_simple

  • We have to first pre process the image to be able to work with the pre processed network. (Basically converts it into the format inception was trained)
  • We then convert it into a tensor and split the execution into parts
  • We simply use the deepdream function to calculate the losses
  • We use the deprocess function to convert it back into an image from a tensor
  • clear_output is just a function to make sure the notebook doesnt get flooded with outputs and only the latest output is kept
def run_deep_dream_simple(img, steps=100, step_size=0.01):

    img = tf.keras.applications.inception_v3.preprocess_input(img)