Skip to content

Instantly share code, notes, and snippets.

View SubhadityaMukherjee's full-sized avatar

Subhaditya Mukherjee SubhadityaMukherjee

View GitHub Profile

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)

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

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)
@SubhadityaMukherjee
SubhadityaMukherjee / dowres.md
Created January 21, 2020 16:20
downloadres
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 / downloadshow.md
Last active January 21, 2020 16:12
Deep dream implementation
@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))