Session 4: Visualizing Representations

Assignment: Deep Dream and Style Net

Creative Applications of Deep Learning with Google's Tensorflow Parag K. Mital Kadenze, Inc.

Overview

In this homework, we'll first walk through visualizing the gradients of a trained convolutional network. Recall from the last session that we had trained a variational convolutional autoencoder. We also trained a deep convolutional network. In both of these networks, we learned only a few tools for understanding how the model performs. These included measuring the loss of the network and visualizing the W weight matrices and/or convolutional filters of the network.

During the lecture we saw how to visualize the gradients of Inception, Google's state of the art network for object recognition. This resulted in a much more powerful technique for understanding how a network's activations transform or accentuate the representations in the input space. We'll explore this more in Part 1.

We also explored how to use the gradients of a particular layer or neuron within a network with respect to its input for performing "gradient ascent". This resulted in Deep Dream. We'll explore this more in Parts 2-4.

We also saw how the gradients at different layers of a convolutional network could be optimized for another image, resulting in the separation of content and style losses, depending on the chosen layers. This allowed us to synthesize new images that shared another image's content and/or style, even if they came from separate images. We'll explore this more in Part 5.

Finally, you'll packaged all the GIFs you create throughout this notebook and upload them to Kadenze.

Learning Goals

  • Learn how to inspect deep networks by visualizing their gradients
  • Learn how to "deep dream" with different objective functions and regularization techniques
  • Learn how to "stylize" an image using content and style losses from different images

Table of Contents


In [1]:
# First check the Python version
import sys
if sys.version_info < (3,4):
    print('You are running an older version of Python!\n\n',
          'You should consider updating to Python 3.4.0 or',
          'higher as the libraries built for this course',
          'have only been tested in Python 3.4 and higher.\n')
    print('Try installing the Python 3.5 version of anaconda'
          'and then restart `jupyter notebook`:\n',
          'https://www.continuum.io/downloads\n\n')

# Now get necessary libraries
try:
    import os
    import numpy as np
    import matplotlib.pyplot as plt
    from skimage.transform import resize
    from skimage import data
    from scipy.misc import imresize
    from scipy.ndimage.filters import gaussian_filter
    import IPython.display as ipyd
    import tensorflow as tf
    from libs import utils, gif, datasets, dataset_utils, vae, dft, vgg16, nb_utils
except ImportError:
    print("Make sure you have started notebook in the same directory",
          "as the provided zip file which includes the 'libs' folder",
          "and the file 'utils.py' inside of it.  You will NOT be able",
          "to complete this assignment unless you restart jupyter",
          "notebook inside the directory created by extracting",
          "the zip file or cloning the github repo.  If you are still")

# We'll tell matplotlib to inline any drawn figures like so:
%matplotlib inline
plt.style.use('ggplot')

In [2]:
# Bit of formatting because I don't like the default inline code style:
from IPython.core.display import HTML
HTML("""<style> .rendered_html code { 
    padding: 2px 4px;
    color: #c7254e;
    background-color: #f9f2f4;
    border-radius: 4px;
} </style>""")


Out[2]:

Part 1 - Pretrained Networks

In the libs module, you'll see that I've included a few modules for loading some state of the art networks. These include:

  • Inception v3
    • This network has been trained on ImageNet and its finaly output layer is a softmax layer denoting 1 of 1000 possible objects (+ 8 for unknown categories). This network is about only 50MB!
  • Inception v5
    • This network has been trained on ImageNet and its finaly output layer is a softmax layer denoting 1 of 1000 possible objects (+ 8 for unknown categories). This network is about only 50MB! It presents a few extensions to v5 which are not documented anywhere that I've found, as of yet...
  • Visual Group Geometry @ Oxford's 16 layer
    • This network has been trained on ImageNet and its finaly output layer is a softmax layer denoting 1 of 1000 possible objects. This model is nearly half a gigabyte, about 10x larger in size than the inception network. The trade off is that it is very fast.
  • Visual Group Geometry @ Oxford's Face Recognition
    • This network has been trained on the VGG Face Dataset and its final output layer is a softmax layer denoting 1 of 2622 different possible people.
  • Illustration2Vec
    • This network has been trained on illustrations and manga and its final output layer is 4096 features.
  • Illustration2Vec Tag
    • Please do not use this network if you are under the age of 18 (seriously!)
    • This network has been trained on manga and its final output layer is one of 1539 labels.

When we use a pre-trained network, we load a network's definition and its weights which have already been trained. The network's definition includes a set of operations such as convolutions, and adding biases, but all of their values, i.e. the weights, have already been trained.

Graph Definition

In the libs folder, you will see a few new modules for loading the above pre-trained networks. Each module is structured similarly to help you understand how they are loaded and include example code for using them. Each module includes a preprocess function for using before sending the image to the network. And when using deep dream techniques, we'll be using the deprocess function to undo the preprocess function's manipulations.

Let's take a look at loading one of these. Every network except for i2v includes a key 'labels' denoting what labels the network has been trained on. If you are under the age of 18, please do not use the i2v_tag model, as its labels are unsuitable for minors.

Let's load the libaries for the different pre-trained networks:


In [3]:
from libs import vgg16, inception, i2v

Now we can load a pre-trained network's graph and any labels. Explore the different networks in your own time.


In [4]:
# [Choice]
net = inception.get_inception_model(version='v5')
# net = inception.get_inception_model(version='v3')
# net = vgg16.get_vgg_model()
# net = vgg16.get_vgg_face_model()
# net = i2v.get_i2v_model()
# net = i2v.get_i2v_tag_model()

Each network returns a dictionary with the following keys defined. Every network has a key for "labels" except for "i2v", since this is a feature only network, e.g. an unsupervised network, and does not have labels.


In [5]:
print(net.keys())


dict_keys(['deprocess', 'preprocess', 'graph_def', 'labels'])

Preprocess/Deprocessing

Each network has a preprocessing/deprocessing function which we'll use before sending the input to the network. This preprocessing function is slightly different for each network. Recall from the previous sessions what preprocess we had done before sending an image to a network. We would often normalize the input by subtracting the mean and dividing by the standard deviation. We'd also crop/resize the input to a standard size. We'll need to do this for each network except for the Inception network, which is a true convolutional network and does not require us to do this (will be explained in more depth later).

Whenever we preprocess the image, and want to visualize the result of adding back the gradient to the input image (when we use deep dream), we'll need to use the deprocess function stored in the dictionary. Let's explore how these work. We'll confirm this is performing the inverse operation, let's try to preprocess the image, then I'll have you try to deprocess it.


In [6]:
# First, let's get an image:
og = plt.imread('images/bp/brad_pitt_03.jpg')[..., :3]
plt.imshow(og)
print(og.min(), og.max())


0 255

Let's now try preprocessing this image. The function for preprocessing is inside the module we used to load it. For instance, for vgg16, we can find the preprocess function as vgg16.preprocess, or for inception, inception.preprocess, or for i2v, i2v.preprocess. Or, we can just use the key preprocess in our dictionary net, as this is just convenience for us to access the corresponding preprocess function.


In [7]:
# Now call the preprocess function.  This will preprocess our
# image ready for being input to the network, except for changes
# to the dimensions.  I.e., we will still need to convert this
# to a 4-dimensional Tensor once we input it to the network.
# We'll see how that works later.
img = net['preprocess'](og)
print(img.min(), img.max())


-117.0 137.277

Let's undo the preprocessing. Recall that the net dictionary has the key deprocess which is the function we need to use on our processed image, img.


In [8]:
deprocessed = net['deprocess'](img)
plt.imshow(deprocessed)
plt.show()


Tensorboard

I've added a utility module called nb_utils which includes a function show_graph. This will use Tensorboard to draw the computational graph defined by the various Tensorflow functions. I didn't go over this during the lecture because there just wasn't enough time! But explore it in your own time if it interests you, as it is a really unique tool which allows you to monitor your network's training progress via a web interface. It even lets you monitor specific variables or processes within the network, e.g. the reconstruction of an autoencoder, without having to print to the console as we've been doing. We'll just be using it to draw the pretrained network's graphs using the utility function I've given you.

Be sure to interact with the graph and click on the various modules.

For instance, if you've loaded the inception v5 network, locate the "input" to the network. This is where we feed the image, the input placeholder (typically what we've been denoting as X in our own networks). From there, it goes to the "conv2d0" variable scope (i.e. this uses the code: with tf.variable_scope("conv2d0") to create a set of operations with the prefix "conv2d0/". If you expand this scope, you'll see another scope, "pre_relu". This is created using another tf.variable_scope("pre_relu"), so that any new variables will have the prefix "conv2d0/pre_relu". Finally, inside here, you'll see the convolution operation (tf.nn.conv2d) and the 4d weight tensor, "w" (e.g. created using tf.get_variable), used for convolution (and so has the name, "conv2d0/pre_relu/w". Just after the convolution is the addition of the bias, b. And finally after exiting the "pre_relu" scope, you should be able to see the "conv2d0" operation which applies the relu nonlinearity. In summary, that region of the graph can be created in Tensorflow like so:


In [9]:
from tensorflow.python.framework.ops import reset_default_graph
reset_default_graph()

input = tf.placeholder(tf.float32, name = "X")
with tf.variable_scope('conv2d0'):
    with tf.variable_scope('pre_relu'):
        w = tf.get_variable("w", shape = [7, 7, 3, 64], initializer = tf.random_normal_initializer())
        h = tf.nn.conv2d(input, w, strides = [1, 2, 2, 1], padding = "SAME")
        b = tf.get_variable("b", shape = [64])
        h = tf.nn.bias_add(h, b)
    h = tf.nn.relu(h)

In [10]:
nb_utils.show_graph(net['graph_def'])


If you open up the "mixed3a" node above (double click on it), you'll see the first "inception" module. This network encompasses a few advanced concepts that we did not have time to discuss during the lecture, including residual connections, feature concatenation, parallel convolution streams, 1x1 convolutions, and including negative labels in the softmax layer. I'll expand on the 1x1 convolutions here, but please feel free to skip ahead if this isn't of interest to you.

A Note on 1x1 Convolutions

The 1x1 convolutions are setting the ksize parameter of the kernels to 1. This is effectively allowing you to change the number of dimensions. Remember that you need a 4-d tensor as input to a convolution. Let's say its dimensions are $\text{N}\ x\ \text{W}\ x\ \text{H}\ x\ \text{C}_I$, where $\text{C}_I$ represents the number of channels the image has. Let's say it is an RGB image, then $\text{C}_I$ would be 3. Or later in the network, if we have already convolved it, it might be 64 channels instead. Regardless, when you convolve it w/ a $\text{K}_H\ x\ \text{K}_W\ x\ \text{C}_I\ x\ \text{C}_O$ filter, where $\text{K}_H$ is 1 and $\text{K}_W$ is also 1, then the filters size is: $1\ x\ 1\ x\ \text{C}_I$ and this is perfomed for each output channel $\text{C}_O$. What this is doing is filtering the information only in the channels dimension, not the spatial dimensions. The output of this convolution will be a $\text{N}\ x\ \text{W}\ x\ \text{H}\ x\ \text{C}_O$ output tensor. The only thing that changes in the output is the number of output filters.

The 1x1 convolution operation is essentially reducing the amount of information in the channels dimensions before performing a much more expensive operation, e.g. a 3x3 or 5x5 convolution. Effectively, it is a very clever trick for dimensionality reduction used in many state of the art convolutional networks. Another way to look at it is that it is preseving the spatial information, but at each location, there is a fully connected network taking all the information from every input channel, $\text{C}_I$, and reducing it down to $\text{C}_O$ channels (or could easily also be up, but that is not the typical use case for this). So it's not really a convolution, but we can use the convolution operation to perform it at every location in our image.

If you are interested in reading more about this architecture, I highly encourage you to read Network in Network, Christian Szegedy's work on the Inception network, Highway Networks, Residual Networks, and Ladder Networks.

In this course, we'll stick to focusing on the applications of these, while trying to delve as much into the code as possible.

Network Labels

Let's now look at the labels:


In [11]:
net['labels']


Out[11]:
[(0, 'dummy'),
 (1, 'kit fox'),
 (2, 'English setter'),
 (3, 'Siberian husky'),
 (4, 'Australian terrier'),
 (5, 'English springer'),
 (6, 'grey whale'),
 (7, 'lesser panda'),
 (8, 'Egyptian cat'),
 (9, 'ibex'),
 (10, 'Persian cat'),
 (11, 'cougar'),
 (12, 'gazelle'),
 (13, 'porcupine'),
 (14, 'sea lion'),
 (15, 'malamute'),
 (16, 'badger'),
 (17, 'Great Dane'),
 (18, 'Walker hound'),
 (19, 'Welsh springer spaniel'),
 (20, 'whippet'),
 (21, 'Scottish deerhound'),
 (22, 'killer whale'),
 (23, 'mink'),
 (24, 'African elephant'),
 (25, 'Weimaraner'),
 (26, 'soft-coated wheaten terrier'),
 (27, 'Dandie Dinmont'),
 (28, 'red wolf'),
 (29, 'Old English sheepdog'),
 (30, 'jaguar'),
 (31, 'otterhound'),
 (32, 'bloodhound'),
 (33, 'Airedale'),
 (34, 'hyena'),
 (35, 'meerkat'),
 (36, 'giant schnauzer'),
 (37, 'titi'),
 (38, 'three-toed sloth'),
 (39, 'sorrel'),
 (40, 'black-footed ferret'),
 (41, 'dalmatian'),
 (42, 'black-and-tan coonhound'),
 (43, 'papillon'),
 (44, 'skunk'),
 (45, 'Staffordshire bullterrier'),
 (46, 'Mexican hairless'),
 (47, 'Bouvier des Flandres'),
 (48, 'weasel'),
 (49, 'miniature poodle'),
 (50, 'Cardigan'),
 (51, 'malinois'),
 (52, 'bighorn'),
 (53, 'fox squirrel'),
 (54, 'colobus'),
 (55, 'tiger cat'),
 (56, 'Lhasa'),
 (57, 'impala'),
 (58, 'coyote'),
 (59, 'Yorkshire terrier'),
 (60, 'Newfoundland'),
 (61, 'brown bear'),
 (62, 'red fox'),
 (63, 'Norwegian elkhound'),
 (64, 'Rottweiler'),
 (65, 'hartebeest'),
 (66, 'Saluki'),
 (67, 'grey fox'),
 (68, 'schipperke'),
 (69, 'Pekinese'),
 (70, 'Brabancon griffon'),
 (71, 'West Highland white terrier'),
 (72, 'Sealyham terrier'),
 (73, 'guenon'),
 (74, 'mongoose'),
 (75, 'indri'),
 (76, 'tiger'),
 (77, 'Irish wolfhound'),
 (78, 'wild boar'),
 (79, 'EntleBucher'),
 (80, 'zebra'),
 (81, 'ram'),
 (82, 'French bulldog'),
 (83, 'orangutan'),
 (84, 'basenji'),
 (85, 'leopard'),
 (86, 'Bernese mountain dog'),
 (87, 'Maltese dog'),
 (88, 'Norfolk terrier'),
 (89, 'toy terrier'),
 (90, 'vizsla'),
 (91, 'cairn'),
 (92, 'squirrel monkey'),
 (93, 'groenendael'),
 (94, 'clumber'),
 (95, 'Siamese cat'),
 (96, 'chimpanzee'),
 (97, 'komondor'),
 (98, 'Afghan hound'),
 (99, 'Japanese spaniel'),
 (100, 'proboscis monkey'),
 (101, 'guinea pig'),
 (102, 'white wolf'),
 (103, 'ice bear'),
 (104, 'gorilla'),
 (105, 'borzoi'),
 (106, 'toy poodle'),
 (107, 'Kerry blue terrier'),
 (108, 'ox'),
 (109, 'Scotch terrier'),
 (110, 'Tibetan mastiff'),
 (111, 'spider monkey'),
 (112, 'Doberman'),
 (113, 'Boston bull'),
 (114, 'Greater Swiss Mountain dog'),
 (115, 'Appenzeller'),
 (116, 'Shih-Tzu'),
 (117, 'Irish water spaniel'),
 (118, 'Pomeranian'),
 (119, 'Bedlington terrier'),
 (120, 'warthog'),
 (121, 'Arabian camel'),
 (122, 'siamang'),
 (123, 'miniature schnauzer'),
 (124, 'collie'),
 (125, 'golden retriever'),
 (126, 'Irish terrier'),
 (127, 'affenpinscher'),
 (128, 'Border collie'),
 (129, 'hare'),
 (130, 'boxer'),
 (131, 'silky terrier'),
 (132, 'beagle'),
 (133, 'Leonberg'),
 (134, 'German short-haired pointer'),
 (135, 'patas'),
 (136, 'dhole'),
 (137, 'baboon'),
 (138, 'macaque'),
 (139, 'Chesapeake Bay retriever'),
 (140, 'bull mastiff'),
 (141, 'kuvasz'),
 (142, 'capuchin'),
 (143, 'pug'),
 (144, 'curly-coated retriever'),
 (145, 'Norwich terrier'),
 (146, 'flat-coated retriever'),
 (147, 'hog'),
 (148, 'keeshond'),
 (149, 'Eskimo dog'),
 (150, 'Brittany spaniel'),
 (151, 'standard poodle'),
 (152, 'Lakeland terrier'),
 (153, 'snow leopard'),
 (154, 'Gordon setter'),
 (155, 'dingo'),
 (156, 'standard schnauzer'),
 (157, 'hamster'),
 (158, 'Tibetan terrier'),
 (159, 'Arctic fox'),
 (160, 'wire-haired fox terrier'),
 (161, 'basset'),
 (162, 'water buffalo'),
 (163, 'American black bear'),
 (164, 'Angora'),
 (165, 'bison'),
 (166, 'howler monkey'),
 (167, 'hippopotamus'),
 (168, 'chow'),
 (169, 'giant panda'),
 (170, 'American Staffordshire terrier'),
 (171, 'Shetland sheepdog'),
 (172, 'Great Pyrenees'),
 (173, 'Chihuahua'),
 (174, 'tabby'),
 (175, 'marmoset'),
 (176, 'Labrador retriever'),
 (177, 'Saint Bernard'),
 (178, 'armadillo'),
 (179, 'Samoyed'),
 (180, 'bluetick'),
 (181, 'redbone'),
 (182, 'polecat'),
 (183, 'marmot'),
 (184, 'kelpie'),
 (185, 'gibbon'),
 (186, 'llama'),
 (187, 'miniature pinscher'),
 (188, 'wood rabbit'),
 (189, 'Italian greyhound'),
 (190, 'lion'),
 (191, 'cocker spaniel'),
 (192, 'Irish setter'),
 (193, 'dugong'),
 (194, 'Indian elephant'),
 (195, 'beaver'),
 (196, 'Sussex spaniel'),
 (197, 'Pembroke'),
 (198, 'Blenheim spaniel'),
 (199, 'Madagascar cat'),
 (200, 'Rhodesian ridgeback'),
 (201, 'lynx'),
 (202, 'African hunting dog'),
 (203, 'langur'),
 (204, 'Ibizan hound'),
 (205, 'timber wolf'),
 (206, 'cheetah'),
 (207, 'English foxhound'),
 (208, 'briard'),
 (209, 'sloth bear'),
 (210, 'Border terrier'),
 (211, 'German shepherd'),
 (212, 'otter'),
 (213, 'koala'),
 (214, 'tusker'),
 (215, 'echidna'),
 (216, 'wallaby'),
 (217, 'platypus'),
 (218, 'wombat'),
 (219, 'revolver'),
 (220, 'umbrella'),
 (221, 'schooner'),
 (222, 'soccer ball'),
 (223, 'accordion'),
 (224, 'ant'),
 (225, 'starfish'),
 (226, 'chambered nautilus'),
 (227, 'grand piano'),
 (228, 'laptop'),
 (229, 'strawberry'),
 (230, 'airliner'),
 (231, 'warplane'),
 (232, 'airship'),
 (233, 'balloon'),
 (234, 'space shuttle'),
 (235, 'fireboat'),
 (236, 'gondola'),
 (237, 'speedboat'),
 (238, 'lifeboat'),
 (239, 'canoe'),
 (240, 'yawl'),
 (241, 'catamaran'),
 (242, 'trimaran'),
 (243, 'container ship'),
 (244, 'liner'),
 (245, 'pirate'),
 (246, 'aircraft carrier'),
 (247, 'submarine'),
 (248, 'wreck'),
 (249, 'half track'),
 (250, 'tank'),
 (251, 'missile'),
 (252, 'bobsled'),
 (253, 'dogsled'),
 (254, 'bicycle-built-for-two'),
 (255, 'mountain bike'),
 (256, 'freight car'),
 (257, 'passenger car'),
 (258, 'barrow'),
 (259, 'shopping cart'),
 (260, 'motor scooter'),
 (261, 'forklift'),
 (262, 'electric locomotive'),
 (263, 'steam locomotive'),
 (264, 'amphibian'),
 (265, 'ambulance'),
 (266, 'beach wagon'),
 (267, 'cab'),
 (268, 'convertible'),
 (269, 'jeep'),
 (270, 'limousine'),
 (271, 'minivan'),
 (272, 'Model T'),
 (273, 'racer'),
 (274, 'sports car'),
 (275, 'go-kart'),
 (276, 'golfcart'),
 (277, 'moped'),
 (278, 'snowplow'),
 (279, 'fire engine'),
 (280, 'garbage truck'),
 (281, 'pickup'),
 (282, 'tow truck'),
 (283, 'trailer truck'),
 (284, 'moving van'),
 (285, 'police van'),
 (286, 'recreational vehicle'),
 (287, 'streetcar'),
 (288, 'snowmobile'),
 (289, 'tractor'),
 (290, 'mobile home'),
 (291, 'tricycle'),
 (292, 'unicycle'),
 (293, 'horse cart'),
 (294, 'jinrikisha'),
 (295, 'oxcart'),
 (296, 'bassinet'),
 (297, 'cradle'),
 (298, 'crib'),
 (299, 'four-poster'),
 (300, 'bookcase'),
 (301, 'china cabinet'),
 (302, 'medicine chest'),
 (303, 'chiffonier'),
 (304, 'table lamp'),
 (305, 'file'),
 (306, 'park bench'),
 (307, 'barber chair'),
 (308, 'throne'),
 (309, 'folding chair'),
 (310, 'rocking chair'),
 (311, 'studio couch'),
 (312, 'toilet seat'),
 (313, 'desk'),
 (314, 'pool table'),
 (315, 'dining table'),
 (316, 'entertainment center'),
 (317, 'wardrobe'),
 (318, 'Granny Smith'),
 (319, 'orange'),
 (320, 'lemon'),
 (321, 'fig'),
 (322, 'pineapple'),
 (323, 'banana'),
 (324, 'jackfruit'),
 (325, 'custard apple'),
 (326, 'pomegranate'),
 (327, 'acorn'),
 (328, 'hip'),
 (329, 'ear'),
 (330, 'rapeseed'),
 (331, 'corn'),
 (332, 'buckeye'),
 (333, 'organ'),
 (334, 'upright'),
 (335, 'chime'),
 (336, 'drum'),
 (337, 'gong'),
 (338, 'maraca'),
 (339, 'marimba'),
 (340, 'steel drum'),
 (341, 'banjo'),
 (342, 'cello'),
 (343, 'violin'),
 (344, 'harp'),
 (345, 'acoustic guitar'),
 (346, 'electric guitar'),
 (347, 'cornet'),
 (348, 'French horn'),
 (349, 'trombone'),
 (350, 'harmonica'),
 (351, 'ocarina'),
 (352, 'panpipe'),
 (353, 'bassoon'),
 (354, 'oboe'),
 (355, 'sax'),
 (356, 'flute'),
 (357, 'daisy'),
 (358, "yellow lady's slipper"),
 (359, 'cliff'),
 (360, 'valley'),
 (361, 'alp'),
 (362, 'volcano'),
 (363, 'promontory'),
 (364, 'sandbar'),
 (365, 'coral reef'),
 (366, 'lakeside'),
 (367, 'seashore'),
 (368, 'geyser'),
 (369, 'hatchet'),
 (370, 'cleaver'),
 (371, 'letter opener'),
 (372, 'plane'),
 (373, 'power drill'),
 (374, 'lawn mower'),
 (375, 'hammer'),
 (376, 'corkscrew'),
 (377, 'can opener'),
 (378, 'plunger'),
 (379, 'screwdriver'),
 (380, 'shovel'),
 (381, 'plow'),
 (382, 'chain saw'),
 (383, 'cock'),
 (384, 'hen'),
 (385, 'ostrich'),
 (386, 'brambling'),
 (387, 'goldfinch'),
 (388, 'house finch'),
 (389, 'junco'),
 (390, 'indigo bunting'),
 (391, 'robin'),
 (392, 'bulbul'),
 (393, 'jay'),
 (394, 'magpie'),
 (395, 'chickadee'),
 (396, 'water ouzel'),
 (397, 'kite'),
 (398, 'bald eagle'),
 (399, 'vulture'),
 (400, 'great grey owl'),
 (401, 'black grouse'),
 (402, 'ptarmigan'),
 (403, 'ruffed grouse'),
 (404, 'prairie chicken'),
 (405, 'peacock'),
 (406, 'quail'),
 (407, 'partridge'),
 (408, 'African grey'),
 (409, 'macaw'),
 (410, 'sulphur-crested cockatoo'),
 (411, 'lorikeet'),
 (412, 'coucal'),
 (413, 'bee eater'),
 (414, 'hornbill'),
 (415, 'hummingbird'),
 (416, 'jacamar'),
 (417, 'toucan'),
 (418, 'drake'),
 (419, 'red-breasted merganser'),
 (420, 'goose'),
 (421, 'black swan'),
 (422, 'white stork'),
 (423, 'black stork'),
 (424, 'spoonbill'),
 (425, 'flamingo'),
 (426, 'American egret'),
 (427, 'little blue heron'),
 (428, 'bittern'),
 (429, 'crane'),
 (430, 'limpkin'),
 (431, 'American coot'),
 (432, 'bustard'),
 (433, 'ruddy turnstone'),
 (434, 'red-backed sandpiper'),
 (435, 'redshank'),
 (436, 'dowitcher'),
 (437, 'oystercatcher'),
 (438, 'European gallinule'),
 (439, 'pelican'),
 (440, 'king penguin'),
 (441, 'albatross'),
 (442, 'great white shark'),
 (443, 'tiger shark'),
 (444, 'hammerhead'),
 (445, 'electric ray'),
 (446, 'stingray'),
 (447, 'barracouta'),
 (448, 'coho'),
 (449, 'tench'),
 (450, 'goldfish'),
 (451, 'eel'),
 (452, 'rock beauty'),
 (453, 'anemone fish'),
 (454, 'lionfish'),
 (455, 'puffer'),
 (456, 'sturgeon'),
 (457, 'gar'),
 (458, 'loggerhead'),
 (459, 'leatherback turtle'),
 (460, 'mud turtle'),
 (461, 'terrapin'),
 (462, 'box turtle'),
 (463, 'banded gecko'),
 (464, 'common iguana'),
 (465, 'American chameleon'),
 (466, 'whiptail'),
 (467, 'agama'),
 (468, 'frilled lizard'),
 (469, 'alligator lizard'),
 (470, 'Gila monster'),
 (471, 'green lizard'),
 (472, 'African chameleon'),
 (473, 'Komodo dragon'),
 (474, 'triceratops'),
 (475, 'African crocodile'),
 (476, 'American alligator'),
 (477, 'thunder snake'),
 (478, 'ringneck snake'),
 (479, 'hognose snake'),
 (480, 'green snake'),
 (481, 'king snake'),
 (482, 'garter snake'),
 (483, 'water snake'),
 (484, 'vine snake'),
 (485, 'night snake'),
 (486, 'boa constrictor'),
 (487, 'rock python'),
 (488, 'Indian cobra'),
 (489, 'green mamba'),
 (490, 'sea snake'),
 (491, 'horned viper'),
 (492, 'diamondback'),
 (493, 'sidewinder'),
 (494, 'European fire salamander'),
 (495, 'common newt'),
 (496, 'eft'),
 (497, 'spotted salamander'),
 (498, 'axolotl'),
 (499, 'bullfrog'),
 (500, 'tree frog'),
 (501, 'tailed frog'),
 (502, 'whistle'),
 (503, 'wing'),
 (504, 'paintbrush'),
 (505, 'hand blower'),
 (506, 'oxygen mask'),
 (507, 'snorkel'),
 (508, 'loudspeaker'),
 (509, 'microphone'),
 (510, 'screen'),
 (511, 'mouse'),
 (512, 'electric fan'),
 (513, 'oil filter'),
 (514, 'strainer'),
 (515, 'space heater'),
 (516, 'stove'),
 (517, 'guillotine'),
 (518, 'barometer'),
 (519, 'rule'),
 (520, 'odometer'),
 (521, 'scale'),
 (522, 'analog clock'),
 (523, 'digital clock'),
 (524, 'wall clock'),
 (525, 'hourglass'),
 (526, 'sundial'),
 (527, 'parking meter'),
 (528, 'stopwatch'),
 (529, 'digital watch'),
 (530, 'stethoscope'),
 (531, 'syringe'),
 (532, 'magnetic compass'),
 (533, 'binoculars'),
 (534, 'projector'),
 (535, 'sunglasses'),
 (536, 'loupe'),
 (537, 'radio telescope'),
 (538, 'bow'),
 (539, 'cannon [ground]'),
 (540, 'assault rifle'),
 (541, 'rifle'),
 (542, 'projectile'),
 (543, 'computer keyboard'),
 (544, 'typewriter keyboard'),
 (545, 'crane'),
 (546, 'lighter'),
 (547, 'abacus'),
 (548, 'cash machine'),
 (549, 'slide rule'),
 (550, 'desktop computer'),
 (551, 'hand-held computer'),
 (552, 'notebook'),
 (553, 'web site'),
 (554, 'harvester'),
 (555, 'thresher'),
 (556, 'printer'),
 (557, 'slot'),
 (558, 'vending machine'),
 (559, 'sewing machine'),
 (560, 'joystick'),
 (561, 'switch'),
 (562, 'hook'),
 (563, 'car wheel'),
 (564, 'paddlewheel'),
 (565, 'pinwheel'),
 (566, "potter's wheel"),
 (567, 'gas pump'),
 (568, 'carousel'),
 (569, 'swing'),
 (570, 'reel'),
 (571, 'radiator'),
 (572, 'puck'),
 (573, 'hard disc'),
 (574, 'sunglass'),
 (575, 'pick'),
 (576, 'car mirror'),
 (577, 'solar dish'),
 (578, 'remote control'),
 (579, 'disk brake'),
 (580, 'buckle'),
 (581, 'hair slide'),
 (582, 'knot'),
 (583, 'combination lock'),
 (584, 'padlock'),
 (585, 'nail'),
 (586, 'safety pin'),
 (587, 'screw'),
 (588, 'muzzle'),
 (589, 'seat belt'),
 (590, 'ski'),
 (591, 'candle'),
 (592, "jack-o'-lantern"),
 (593, 'spotlight'),
 (594, 'torch'),
 (595, 'neck brace'),
 (596, 'pier'),
 (597, 'tripod'),
 (598, 'maypole'),
 (599, 'mousetrap'),
 (600, 'spider web'),
 (601, 'trilobite'),
 (602, 'harvestman'),
 (603, 'scorpion'),
 (604, 'black and gold garden spider'),
 (605, 'barn spider'),
 (606, 'garden spider'),
 (607, 'black widow'),
 (608, 'tarantula'),
 (609, 'wolf spider'),
 (610, 'tick'),
 (611, 'centipede'),
 (612, 'isopod'),
 (613, 'Dungeness crab'),
 (614, 'rock crab'),
 (615, 'fiddler crab'),
 (616, 'king crab'),
 (617, 'American lobster'),
 (618, 'spiny lobster'),
 (619, 'crayfish'),
 (620, 'hermit crab'),
 (621, 'tiger beetle'),
 (622, 'ladybug'),
 (623, 'ground beetle'),
 (624, 'long-horned beetle'),
 (625, 'leaf beetle'),
 (626, 'dung beetle'),
 (627, 'rhinoceros beetle'),
 (628, 'weevil'),
 (629, 'fly'),
 (630, 'bee'),
 (631, 'grasshopper'),
 (632, 'cricket'),
 (633, 'walking stick'),
 (634, 'cockroach'),
 (635, 'mantis'),
 (636, 'cicada'),
 (637, 'leafhopper'),
 (638, 'lacewing'),
 (639, 'dragonfly'),
 (640, 'damselfly'),
 (641, 'admiral'),
 (642, 'ringlet'),
 (643, 'monarch'),
 (644, 'cabbage butterfly'),
 (645, 'sulphur butterfly'),
 (646, 'lycaenid'),
 (647, 'jellyfish'),
 (648, 'sea anemone'),
 (649, 'brain coral'),
 (650, 'flatworm'),
 (651, 'nematode'),
 (652, 'conch'),
 (653, 'snail'),
 (654, 'slug'),
 (655, 'sea slug'),
 (656, 'chiton'),
 (657, 'sea urchin'),
 (658, 'sea cucumber'),
 (659, 'iron'),
 (660, 'espresso maker'),
 (661, 'microwave'),
 (662, 'Dutch oven'),
 (663, 'rotisserie'),
 (664, 'toaster'),
 (665, 'waffle iron'),
 (666, 'vacuum'),
 (667, 'dishwasher'),
 (668, 'refrigerator'),
 (669, 'washer'),
 (670, 'Crock Pot'),
 (671, 'frying pan'),
 (672, 'wok'),
 (673, 'caldron'),
 (674, 'coffeepot'),
 (675, 'teapot'),
 (676, 'spatula'),
 (677, 'altar'),
 (678, 'triumphal arch'),
 (679, 'patio'),
 (680, 'steel arch bridge'),
 (681, 'suspension bridge'),
 (682, 'viaduct'),
 (683, 'barn'),
 (684, 'greenhouse'),
 (685, 'palace'),
 (686, 'monastery'),
 (687, 'library'),
 (688, 'apiary'),
 (689, 'boathouse'),
 (690, 'church'),
 (691, 'mosque'),
 (692, 'stupa'),
 (693, 'planetarium'),
 (694, 'restaurant'),
 (695, 'cinema'),
 (696, 'home theater'),
 (697, 'lumbermill'),
 (698, 'coil'),
 (699, 'obelisk'),
 (700, 'totem pole'),
 (701, 'castle'),
 (702, 'prison'),
 (703, 'grocery store'),
 (704, 'bakery'),
 (705, 'barbershop'),
 (706, 'bookshop'),
 (707, 'butcher shop'),
 (708, 'confectionery'),
 (709, 'shoe shop'),
 (710, 'tobacco shop'),
 (711, 'toyshop'),
 (712, 'fountain'),
 (713, 'cliff dwelling'),
 (714, 'yurt'),
 (715, 'dock'),
 (716, 'brass'),
 (717, 'megalith'),
 (718, 'bannister'),
 (719, 'breakwater'),
 (720, 'dam'),
 (721, 'chainlink fence'),
 (722, 'picket fence'),
 (723, 'worm fence'),
 (724, 'stone wall'),
 (725, 'grille'),
 (726, 'sliding door'),
 (727, 'turnstile'),
 (728, 'mountain tent'),
 (729, 'scoreboard'),
 (730, 'honeycomb'),
 (731, 'plate rack'),
 (732, 'pedestal'),
 (733, 'beacon'),
 (734, 'mashed potato'),
 (735, 'bell pepper'),
 (736, 'head cabbage'),
 (737, 'broccoli'),
 (738, 'cauliflower'),
 (739, 'zucchini'),
 (740, 'spaghetti squash'),
 (741, 'acorn squash'),
 (742, 'butternut squash'),
 (743, 'cucumber'),
 (744, 'artichoke'),
 (745, 'cardoon'),
 (746, 'mushroom'),
 (747, 'shower curtain'),
 (748, 'jean'),
 (749, 'carton'),
 (750, 'handkerchief'),
 (751, 'sandal'),
 (752, 'ashcan'),
 (753, 'safe'),
 (754, 'plate'),
 (755, 'necklace'),
 (756, 'croquet ball'),
 (757, 'fur coat'),
 (758, 'thimble'),
 (759, 'pajama'),
 (760, 'running shoe'),
 (761, 'cocktail shaker'),
 (762, 'chest'),
 (763, 'manhole cover'),
 (764, 'modem'),
 (765, 'tub'),
 (766, 'tray'),
 (767, 'balance beam'),
 (768, 'bagel'),
 (769, 'prayer rug'),
 (770, 'kimono'),
 (771, 'hot pot'),
 (772, 'whiskey jug'),
 (773, 'knee pad'),
 (774, 'book jacket'),
 (775, 'spindle'),
 (776, 'ski mask'),
 (777, 'beer bottle'),
 (778, 'crash helmet'),
 (779, 'bottlecap'),
 (780, 'tile roof'),
 (781, 'mask'),
 (782, 'maillot'),
 (783, 'Petri dish'),
 (784, 'football helmet'),
 (785, 'bathing cap'),
 (786, 'teddy bear'),
 (787, 'holster'),
 (788, 'pop bottle'),
 (789, 'photocopier'),
 (790, 'vestment'),
 (791, 'crossword puzzle'),
 (792, 'golf ball'),
 (793, 'trifle'),
 (794, 'suit'),
 (795, 'water tower'),
 (796, 'feather boa'),
 (797, 'cloak'),
 (798, 'red wine'),
 (799, 'drumstick'),
 (800, 'shield'),
 (801, 'Christmas stocking'),
 (802, 'hoopskirt'),
 (803, 'menu'),
 (804, 'stage'),
 (805, 'bonnet'),
 (806, 'meat loaf'),
 (807, 'baseball'),
 (808, 'face powder'),
 (809, 'scabbard'),
 (810, 'sunscreen'),
 (811, 'beer glass'),
 (812, 'hen-of-the-woods'),
 (813, 'guacamole'),
 (814, 'lampshade'),
 (815, 'wool'),
 (816, 'hay'),
 (817, 'bow tie'),
 (818, 'mailbag'),
 (819, 'water jug'),
 (820, 'bucket'),
 (821, 'dishrag'),
 (822, 'soup bowl'),
 (823, 'eggnog'),
 (824, 'mortar'),
 (825, 'trench coat'),
 (826, 'paddle'),
 (827, 'chain'),
 (828, 'swab'),
 (829, 'mixing bowl'),
 (830, 'potpie'),
 (831, 'wine bottle'),
 (832, 'shoji'),
 (833, 'bulletproof vest'),
 (834, 'drilling platform'),
 (835, 'binder'),
 (836, 'cardigan'),
 (837, 'sweatshirt'),
 (838, 'pot'),
 (839, 'birdhouse'),
 (840, 'hamper'),
 (841, 'ping-pong ball'),
 (842, 'pencil box'),
 (843, 'pay-phone'),
 (844, 'consomme'),
 (845, 'apron'),
 (846, 'punching bag'),
 (847, 'backpack'),
 (848, 'groom'),
 (849, 'bearskin'),
 (850, 'pencil sharpener'),
 (851, 'broom'),
 (852, 'mosquito net'),
 (853, 'abaya'),
 (854, 'mortarboard'),
 (855, 'poncho'),
 (856, 'crutch'),
 (857, 'Polaroid camera'),
 (858, 'space bar'),
 (859, 'cup'),
 (860, 'racket'),
 (861, 'traffic light'),
 (862, 'quill'),
 (863, 'radio'),
 (864, 'dough'),
 (865, 'cuirass'),
 (866, 'military uniform'),
 (867, 'lipstick'),
 (868, 'shower cap'),
 (869, 'monitor'),
 (870, 'oscilloscope'),
 (871, 'mitten'),
 (872, 'brassiere'),
 (873, 'French loaf'),
 (874, 'vase'),
 (875, 'milk can'),
 (876, 'rugby ball'),
 (877, 'paper towel'),
 (878, 'earthstar'),
 (879, 'envelope'),
 (880, 'miniskirt'),
 (881, 'cowboy hat'),
 (882, 'trolleybus'),
 (883, 'perfume'),
 (884, 'bathtub'),
 (885, 'hotdog'),
 (886, 'coral fungus'),
 (887, 'bullet train'),
 (888, 'pillow'),
 (889, 'toilet tissue'),
 (890, 'cassette'),
 (891, "carpenter's kit"),
 (892, 'ladle'),
 (893, 'stinkhorn'),
 (894, 'lotion'),
 (895, 'hair spray'),
 (896, 'academic gown'),
 (897, 'dome'),
 (898, 'crate'),
 (899, 'wig'),
 (900, 'burrito'),
 (901, 'pill bottle'),
 (902, 'chain mail'),
 (903, 'theater curtain'),
 (904, 'window shade'),
 (905, 'barrel'),
 (906, 'washbasin'),
 (907, 'ballpoint'),
 (908, 'basketball'),
 (909, 'bath towel'),
 (910, 'cowboy boot'),
 (911, 'gown'),
 (912, 'window screen'),
 (913, 'agaric'),
 (914, 'cellular telephone'),
 (915, 'nipple'),
 (916, 'barbell'),
 (917, 'mailbox'),
 (918, 'lab coat'),
 (919, 'fire screen'),
 (920, 'minibus'),
 (921, 'packet'),
 (922, 'maze'),
 (923, 'pole'),
 (924, 'horizontal bar'),
 (925, 'sombrero'),
 (926, 'pickelhaube'),
 (927, 'rain barrel'),
 (928, 'wallet'),
 (929, 'cassette player'),
 (930, 'comic book'),
 (931, 'piggy bank'),
 (932, 'street sign'),
 (933, 'bell cote'),
 (934, 'fountain pen'),
 (935, 'Windsor tie'),
 (936, 'volleyball'),
 (937, 'overskirt'),
 (938, 'sarong'),
 (939, 'purse'),
 (940, 'bolo tie'),
 (941, 'bib'),
 (942, 'parachute'),
 (943, 'sleeping bag'),
 (944, 'television'),
 (945, 'swimming trunks'),
 (946, 'measuring cup'),
 (947, 'espresso'),
 (948, 'pizza'),
 (949, 'breastplate'),
 (950, 'shopping basket'),
 (951, 'wooden spoon'),
 (952, 'saltshaker'),
 (953, 'chocolate sauce'),
 (954, 'ballplayer'),
 (955, 'goblet'),
 (956, 'gyromitra'),
 (957, 'stretcher'),
 (958, 'water bottle'),
 (959, 'dial telephone'),
 (960, 'soap dispenser'),
 (961, 'jersey'),
 (962, 'school bus'),
 (963, 'jigsaw puzzle'),
 (964, 'plastic bag'),
 (965, 'reflex camera'),
 (966, 'diaper'),
 (967, 'Band Aid'),
 (968, 'ice lolly'),
 (969, 'velvet'),
 (970, 'tennis ball'),
 (971, 'gasmask'),
 (972, 'doormat'),
 (973, 'Loafer'),
 (974, 'ice cream'),
 (975, 'pretzel'),
 (976, 'quilt'),
 (977, 'maillot'),
 (978, 'tape player'),
 (979, 'clog'),
 (980, 'iPod'),
 (981, 'bolete'),
 (982, 'scuba diver'),
 (983, 'pitcher'),
 (984, 'matchstick'),
 (985, 'bikini'),
 (986, 'sock'),
 (987, 'CD player'),
 (988, 'lens cap'),
 (989, 'thatch'),
 (990, 'vault'),
 (991, 'beaker'),
 (992, 'bubble'),
 (993, 'cheeseburger'),
 (994, 'parallel bars'),
 (995, 'flagpole'),
 (996, 'coffee mug'),
 (997, 'rubber eraser'),
 (998, 'stole'),
 (999, 'carbonara'),
 ...]

In [13]:
label_i = 657
print(net['labels'][label_i])


(657, 'sea urchin')

Using Context Managers

Up until now, we've mostly used a single tf.Session within a notebook and didn't give it much thought. Now that we're using some bigger models, we're going to have to be more careful. Using a big model and being careless with our session can result in a lot of unexpected behavior, program crashes, and out of memory errors. The VGG network and the I2V networks are quite large. So we'll need to start being more careful with our sessions using context managers.

Let's see how this works w/ VGG:


In [14]:
# Load the VGG network. 
net = vgg16.get_vgg_model()

assert(net['labels'][0] == (0, 'n01440764 tench, Tinca tinca'))

In [15]:
# Let's explicity use the CPU, since we don't gain anything using the GPU
# when doing Deep Dream (it's only a single image, benefits come w/ many images).
device = '/cpu:0'

# We'll now explicitly create a graph
g = tf.Graph()

# And here is a context manager.  We use the python "with" notation to create a context
# and create a session that only exists within this indent,  as soon as we leave it,
# the session is automatically closed!  We also tel the session which graph to use.
# We can pass a second context after the comma,
# which we'll use to be explicit about using the CPU instead of a GPU.
with tf.Session(graph=g) as sess, g.device(device):
    
    # Now load the graph_def, which defines operations and their values into `g`
    tf.import_graph_def(net['graph_def'], name='net')

In [16]:
# Now we can get all the operations that belong to the graph `g`:
names = [op.name for op in g.get_operations()]
print(names)


['net/images', 'net/mul/y', 'net/mul', 'net/split/split_dim', 'net/split', 'net/sub/y', 'net/sub', 'net/sub_1/y', 'net/sub_1', 'net/sub_2/y', 'net/sub_2', 'net/concat/concat_dim', 'net/concat', 'net/conv1_1/filter', 'net/conv1_1/Conv2D', 'net/conv1_1/biases', 'net/conv1_1/BiasAdd', 'net/conv1_1/conv1_1', 'net/conv1_2/filter', 'net/conv1_2/Conv2D', 'net/conv1_2/biases', 'net/conv1_2/BiasAdd', 'net/conv1_2/conv1_2', 'net/pool1', 'net/conv2_1/filter', 'net/conv2_1/Conv2D', 'net/conv2_1/biases', 'net/conv2_1/BiasAdd', 'net/conv2_1/conv2_1', 'net/conv2_2/filter', 'net/conv2_2/Conv2D', 'net/conv2_2/biases', 'net/conv2_2/BiasAdd', 'net/conv2_2/conv2_2', 'net/pool2', 'net/conv3_1/filter', 'net/conv3_1/Conv2D', 'net/conv3_1/biases', 'net/conv3_1/BiasAdd', 'net/conv3_1/conv3_1', 'net/conv3_2/filter', 'net/conv3_2/Conv2D', 'net/conv3_2/biases', 'net/conv3_2/BiasAdd', 'net/conv3_2/conv3_2', 'net/conv3_3/filter', 'net/conv3_3/Conv2D', 'net/conv3_3/biases', 'net/conv3_3/BiasAdd', 'net/conv3_3/conv3_3', 'net/pool3', 'net/conv4_1/filter', 'net/conv4_1/Conv2D', 'net/conv4_1/biases', 'net/conv4_1/BiasAdd', 'net/conv4_1/conv4_1', 'net/conv4_2/filter', 'net/conv4_2/Conv2D', 'net/conv4_2/biases', 'net/conv4_2/BiasAdd', 'net/conv4_2/conv4_2', 'net/conv4_3/filter', 'net/conv4_3/Conv2D', 'net/conv4_3/biases', 'net/conv4_3/BiasAdd', 'net/conv4_3/conv4_3', 'net/pool4', 'net/conv5_1/filter', 'net/conv5_1/Conv2D', 'net/conv5_1/biases', 'net/conv5_1/BiasAdd', 'net/conv5_1/conv5_1', 'net/conv5_2/filter', 'net/conv5_2/Conv2D', 'net/conv5_2/biases', 'net/conv5_2/BiasAdd', 'net/conv5_2/conv5_2', 'net/conv5_3/filter', 'net/conv5_3/Conv2D', 'net/conv5_3/biases', 'net/conv5_3/BiasAdd', 'net/conv5_3/conv5_3', 'net/pool5', 'net/Reshape/shape', 'net/Reshape', 'net/Const', 'net/Const_1', 'net/MatMul', 'net/fc6', 'net/fc6_relu', 'net/dropout/Shape', 'net/dropout/random_uniform/min', 'net/dropout/random_uniform/range', 'net/dropout/random_uniform/RandomUniform', 'net/dropout/random_uniform/mul', 'net/dropout/random_uniform', 'net/dropout/add/x', 'net/dropout/add', 'net/dropout/Floor', 'net/dropout/mul/y', 'net/dropout/mul', 'net/dropout/mul_1', 'net/Reshape_1/shape', 'net/Reshape_1', 'net/Const_2', 'net/Const_3', 'net/MatMul_1', 'net/fc7', 'net/fc7_relu', 'net/dropout_1/Shape', 'net/dropout_1/random_uniform/min', 'net/dropout_1/random_uniform/range', 'net/dropout_1/random_uniform/RandomUniform', 'net/dropout_1/random_uniform/mul', 'net/dropout_1/random_uniform', 'net/dropout_1/add/x', 'net/dropout_1/add', 'net/dropout_1/Floor', 'net/dropout_1/mul/y', 'net/dropout_1/mul', 'net/dropout_1/mul_1', 'net/Reshape_2/shape', 'net/Reshape_2', 'net/Const_4', 'net/Const_5', 'net/MatMul_2', 'net/fc8', 'net/prob', 'net/init']

Part 2 - Visualizing Gradients

Now that we know how to load a network and extract layers from it, let's grab only the pooling layers:


In [17]:
# First find all the pooling layers in the network.  You can
# use list comprehension to iterate over all the "names" we just
# created, finding whichever ones have the name "pool" in them.
# Then be sure to append a ":0" to the names
features = [name + ":0" for name in names if "pool" in name]

# Let's print them
print(features)

# This is what we want to have at the end.  You could just copy this list
# if you are stuck!
assert(features == ['net/pool1:0', 'net/pool2:0', 'net/pool3:0', 'net/pool4:0', 'net/pool5:0'])


['net/pool1:0', 'net/pool2:0', 'net/pool3:0', 'net/pool4:0', 'net/pool5:0']

Let's also grab the input layer:


In [18]:
# Use the function 'get_tensor_by_name' and the 'names' array to help you
# get the first tensor in the network.  Remember you have to add ":0" to the
# name to get the output of an operation which is the tensor.
x = g.get_tensor_by_name("net/images:0")

assert(x.name == 'net/images:0')

We'll now try to find the gradient activation that maximizes a layer with respect to the input layer x.


In [19]:
def plot_gradient(img, x, feature, g, device='/cpu:0'):
    """Let's visualize the network's gradient activation
    when backpropagated to the original input image.  This
    is effectively telling us which pixels contribute to the
    predicted layer, class, or given neuron with the layer"""
    
    # We'll be explicit about the graph and the device by using a context manager:
    with tf.Session(graph=g) as sess, g.device(device):
        saliency = tf.gradients(tf.reduce_mean(feature), x)
        this_res = sess.run(saliency[0], feed_dict={x: img})
        grad = this_res[0] / np.max(np.abs(this_res))
        return grad

Let's try this w/ an image now. We're going to use the plot_gradient function to help us. This is going to take our input image, run it through the network up to a layer, find the gradient of the mean of that layer's activation with respect to the input image, then backprop that gradient back to the input layer. We'll then visualize the gradient by normalizing it's values using the utils.normalize function.


In [20]:
og = plt.imread('images/bp/brad_pitt_03.jpg')[..., :3]
img = net['preprocess'](og)[np.newaxis]

fig, axs = plt.subplots(1, len(features), figsize=(20, 10))

for i in range(len(features)):
    axs[i].set_title(features[i])
    grad = plot_gradient(img, x, g.get_tensor_by_name(features[i]), g)
    axs[i].imshow(utils.normalize(grad))


Part 3 - Basic Deep Dream

In the lecture we saw how Deep Dreaming takes the backpropagated gradient activations and simply adds it to the image, running the same process again and again in a loop. We also saw many tricks one can add to this idea, such as infinitely zooming into the image by cropping and scaling, adding jitter by randomly moving the image around, or adding constraints on the total activations.

Have a look here for inspiration:

https://research.googleblog.com/2015/06/inceptionism-going-deeper-into-neural.html

https://photos.google.com/share/AF1QipPX0SCl7OzWilt9LnuQliattX4OUCj_8EP65_cTVnBmS1jnYgsGQAieQUc1VQWdgQ?key=aVBxWjhwSzg2RjJWLWRuVFBBZEN1d205bUdEMnhB

https://mtyka.github.io/deepdream/2016/02/05/bilateral-class-vis.html

Let's stick the necessary bits in a function and try exploring how deep dream amplifies the representations of the chosen layers:


In [21]:
def dream(img, gradient, step, net, x, n_iterations=50, plot_step=10):
    # Copy the input image as we'll add the gradient to it in a loop
    img_copy = img.copy()

    fig, axs = plt.subplots(1, n_iterations // plot_step, figsize=(20, 10))

    with tf.Session(graph=g) as sess, g.device(device):
        for it_i in range(n_iterations):

            # This will calculate the gradient of the layer we chose with respect to the input image.
            this_res = sess.run(gradient[0], feed_dict={x: img_copy})[0]

            # [Choice] normalize it by the maximum activation / standard deviation / range
            this_res /= (np.max(np.abs(this_res) + 1e-8))
            # this_res /= (np.std(this_res) + 1e-8)
            # this_res = utils.normalize(this_res)
            
            # Experiment with all of the above options.  They will drastically
            # effect the resulting dream, and really depend on the network
            # you use, and the way the network handles normalization of the
            # input image, and the step size you choose!  Lots to explore!

            # Then add the gradient back to the input image
            # Think about what this gradient represents?
            # It says what direction we should move our input
            # in order to meet our objective stored in "gradient"
            img_copy += this_res * step

            # Plot the image
            if (it_i + 1) % plot_step == 0:
                m = net['deprocess'](img_copy[0])
                axs[it_i // plot_step].imshow(m)

In [22]:
# We'll run it for 3 iterations
n_iterations = 25

# Think of this as our learning rate.  This is how much of
# the gradient we'll add back to the input image
step = 0.5

# Every 1 iterations, we'll plot the current deep dream
plot_step = 5

Let's now try running Deep Dream for every feature, each of our 5 pooling layers. We'll need to get the layer corresponding to our feature. Then find the gradient of this layer's mean activation with respect to our input, x. Then pass these to our dream function. This can take awhile (about 10 minutes using the CPU on my Macbook Pro).


In [23]:
for feature_i in range(len(features)):
    with tf.Session(graph=g) as sess, g.device(device):
        # Get a feature layer
        layer = g.get_tensor_by_name(features[feature_i])

        # Find the gradient of this layer's mean activation
        # with respect to the input image
        gradient = tf.gradients(tf.reduce_mean(layer), x)
        
        # Dream w/ our image
        dream(img, gradient, step, net, x, n_iterations=n_iterations, plot_step=plot_step)


Instead of using an image, we can use an image of noise and see how it "hallucinates" the representations that the layer most responds to:


In [24]:
noise = net['preprocess'](
    np.random.rand(256, 256, 3) * 0.1 + 0.45)[np.newaxis]

We'll do the same thing as before, now w/ our noise image:


In [25]:
# [Choice] number of iteration
n_iterations = 100
# [Choice] learning rate
step = 1.0
# [Choice] plot per x iteration
plot_step = 10

In [26]:
for feature_i in range(len(features)):
    with tf.Session(graph=g) as sess, g.device(device):
        # Get a feature layer
        layer = g.get_tensor_by_name(features[feature_i])

        # Find the gradient of this layer's mean activation
        # with respect to the input image
        gradient = tf.gradients(tf.reduce_mean(layer), x)
        
        # Dream w/ the noise image.  Complete this!
        dream(img, gradient, step, net, x, n_iterations, plot_step)


Part 4 - Deep Dream Extensions

As we saw in the lecture, we can also use the final softmax layer of a network to use during deep dream. This allows us to be explicit about the object we want hallucinated in an image.

Using the Softmax Layer

Let's get another image to play with, preprocess it, and then make it 4-dimensional.


In [27]:
# Load your own image here
og = plt.imread("images/cloud/cloud_2.jpg")[..., :3]
plt.imshow(og)

# Preprocess the image and make sure it is 4-dimensional by adding a new axis to the 0th dimension:
img = net['preprocess'](og)[np.newaxis]

assert(img.ndim == 4)



In [28]:
# Let's get the softmax layer
print(names[-2])
layer = g.get_tensor_by_name(names[-2] + ":0")

# And find its shape
with tf.Session(graph=g) as sess, g.device(device):
    layer_shape = tf.shape(layer).eval(feed_dict={x:img})

# We can find out how many neurons it has by feeding it an image and
# calculating the shape.  The number of output channels is the last dimension.
n_els = layer_shape[-1]


net/prob

In [29]:
# Let's pick a label.  First let's print out every label and then find one we like:
print(net['labels'])


[(0, 'n01440764 tench, Tinca tinca'), (1, 'n01443537 goldfish, Carassius auratus'), (2, 'n01484850 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias'), (3, 'n01491361 tiger shark, Galeocerdo cuvieri'), (4, 'n01494475 hammerhead, hammerhead shark'), (5, 'n01496331 electric ray, crampfish, numbfish, torpedo'), (6, 'n01498041 stingray'), (7, 'n01514668 cock'), (8, 'n01514859 hen'), (9, 'n01518878 ostrich, Struthio camelus'), (10, 'n01530575 brambling, Fringilla montifringilla'), (11, 'n01531178 goldfinch, Carduelis carduelis'), (12, 'n01532829 house finch, linnet, Carpodacus mexicanus'), (13, 'n01534433 junco, snowbird'), (14, 'n01537544 indigo bunting, indigo finch, indigo bird, Passerina cyanea'), (15, 'n01558993 robin, American robin, Turdus migratorius'), (16, 'n01560419 bulbul'), (17, 'n01580077 jay'), (18, 'n01582220 magpie'), (19, 'n01592084 chickadee'), (20, 'n01601694 water ouzel, dipper'), (21, 'n01608432 kite'), (22, 'n01614925 bald eagle, American eagle, Haliaeetus leucocephalus'), (23, 'n01616318 vulture'), (24, 'n01622779 great grey owl, great gray owl, Strix nebulosa'), (25, 'n01629819 European fire salamander, Salamandra salamandra'), (26, 'n01630670 common newt, Triturus vulgaris'), (27, 'n01631663 eft'), (28, 'n01632458 spotted salamander, Ambystoma maculatum'), (29, 'n01632777 axolotl, mud puppy, Ambystoma mexicanum'), (30, 'n01641577 bullfrog, Rana catesbeiana'), (31, 'n01644373 tree frog, tree-frog'), (32, 'n01644900 tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui'), (33, 'n01664065 loggerhead, loggerhead turtle, Caretta caretta'), (34, 'n01665541 leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea'), (35, 'n01667114 mud turtle'), (36, 'n01667778 terrapin'), (37, 'n01669191 box turtle, box tortoise'), (38, 'n01675722 banded gecko'), (39, 'n01677366 common iguana, iguana, Iguana iguana'), (40, 'n01682714 American chameleon, anole, Anolis carolinensis'), (41, 'n01685808 whiptail, whiptail lizard'), (42, 'n01687978 agama'), (43, 'n01688243 frilled lizard, Chlamydosaurus kingi'), (44, 'n01689811 alligator lizard'), (45, 'n01692333 Gila monster, Heloderma suspectum'), (46, 'n01693334 green lizard, Lacerta viridis'), (47, 'n01694178 African chameleon, Chamaeleo chamaeleon'), (48, 'n01695060 Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis'), (49, 'n01697457 African crocodile, Nile crocodile, Crocodylus niloticus'), (50, 'n01698640 American alligator, Alligator mississipiensis'), (51, 'n01704323 triceratops'), (52, 'n01728572 thunder snake, worm snake, Carphophis amoenus'), (53, 'n01728920 ringneck snake, ring-necked snake, ring snake'), (54, 'n01729322 hognose snake, puff adder, sand viper'), (55, 'n01729977 green snake, grass snake'), (56, 'n01734418 king snake, kingsnake'), (57, 'n01735189 garter snake, grass snake'), (58, 'n01737021 water snake'), (59, 'n01739381 vine snake'), (60, 'n01740131 night snake, Hypsiglena torquata'), (61, 'n01742172 boa constrictor, Constrictor constrictor'), (62, 'n01744401 rock python, rock snake, Python sebae'), (63, 'n01748264 Indian cobra, Naja naja'), (64, 'n01749939 green mamba'), (65, 'n01751748 sea snake'), (66, 'n01753488 horned viper, cerastes, sand viper, horned asp, Cerastes cornutus'), (67, 'n01755581 diamondback, diamondback rattlesnake, Crotalus adamanteus'), (68, 'n01756291 sidewinder, horned rattlesnake, Crotalus cerastes'), (69, 'n01768244 trilobite'), (70, 'n01770081 harvestman, daddy longlegs, Phalangium opilio'), (71, 'n01770393 scorpion'), (72, 'n01773157 black and gold garden spider, Argiope aurantia'), (73, 'n01773549 barn spider, Araneus cavaticus'), (74, 'n01773797 garden spider, Aranea diademata'), (75, 'n01774384 black widow, Latrodectus mactans'), (76, 'n01774750 tarantula'), (77, 'n01775062 wolf spider, hunting spider'), (78, 'n01776313 tick'), (79, 'n01784675 centipede'), (80, 'n01795545 black grouse'), (81, 'n01796340 ptarmigan'), (82, 'n01797886 ruffed grouse, partridge, Bonasa umbellus'), (83, 'n01798484 prairie chicken, prairie grouse, prairie fowl'), (84, 'n01806143 peacock'), (85, 'n01806567 quail'), (86, 'n01807496 partridge'), (87, 'n01817953 African grey, African gray, Psittacus erithacus'), (88, 'n01818515 macaw'), (89, 'n01819313 sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita'), (90, 'n01820546 lorikeet'), (91, 'n01824575 coucal'), (92, 'n01828970 bee eater'), (93, 'n01829413 hornbill'), (94, 'n01833805 hummingbird'), (95, 'n01843065 jacamar'), (96, 'n01843383 toucan'), (97, 'n01847000 drake'), (98, 'n01855032 red-breasted merganser, Mergus serrator'), (99, 'n01855672 goose'), (100, 'n01860187 black swan, Cygnus atratus'), (101, 'n01871265 tusker'), (102, 'n01872401 echidna, spiny anteater, anteater'), (103, 'n01873310 platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus'), (104, 'n01877812 wallaby, brush kangaroo'), (105, 'n01882714 koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus'), (106, 'n01883070 wombat'), (107, 'n01910747 jellyfish'), (108, 'n01914609 sea anemone, anemone'), (109, 'n01917289 brain coral'), (110, 'n01924916 flatworm, platyhelminth'), (111, 'n01930112 nematode, nematode worm, roundworm'), (112, 'n01943899 conch'), (113, 'n01944390 snail'), (114, 'n01945685 slug'), (115, 'n01950731 sea slug, nudibranch'), (116, 'n01955084 chiton, coat-of-mail shell, sea cradle, polyplacophore'), (117, 'n01968897 chambered nautilus, pearly nautilus, nautilus'), (118, 'n01978287 Dungeness crab, Cancer magister'), (119, 'n01978455 rock crab, Cancer irroratus'), (120, 'n01980166 fiddler crab'), (121, 'n01981276 king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica'), (122, 'n01983481 American lobster, Northern lobster, Maine lobster, Homarus americanus'), (123, 'n01984695 spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish'), (124, 'n01985128 crayfish, crawfish, crawdad, crawdaddy'), (125, 'n01986214 hermit crab'), (126, 'n01990800 isopod'), (127, 'n02002556 white stork, Ciconia ciconia'), (128, 'n02002724 black stork, Ciconia nigra'), (129, 'n02006656 spoonbill'), (130, 'n02007558 flamingo'), (131, 'n02009229 little blue heron, Egretta caerulea'), (132, 'n02009912 American egret, great white heron, Egretta albus'), (133, 'n02011460 bittern'), (134, 'n02012849 crane'), (135, 'n02013706 limpkin, Aramus pictus'), (136, 'n02017213 European gallinule, Porphyrio porphyrio'), (137, 'n02018207 American coot, marsh hen, mud hen, water hen, Fulica americana'), (138, 'n02018795 bustard'), (139, 'n02025239 ruddy turnstone, Arenaria interpres'), (140, 'n02027492 red-backed sandpiper, dunlin, Erolia alpina'), (141, 'n02028035 redshank, Tringa totanus'), (142, 'n02033041 dowitcher'), (143, 'n02037110 oystercatcher, oyster catcher'), (144, 'n02051845 pelican'), (145, 'n02056570 king penguin, Aptenodytes patagonica'), (146, 'n02058221 albatross, mollymawk'), (147, 'n02066245 grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus'), (148, 'n02071294 killer whale, killer, orca, grampus, sea wolf, Orcinus orca'), (149, 'n02074367 dugong, Dugong dugon'), (150, 'n02077923 sea lion'), (151, 'n02085620 Chihuahua'), (152, 'n02085782 Japanese spaniel'), (153, 'n02085936 Maltese dog, Maltese terrier, Maltese'), (154, 'n02086079 Pekinese, Pekingese, Peke'), (155, 'n02086240 Shih-Tzu'), (156, 'n02086646 Blenheim spaniel'), (157, 'n02086910 papillon'), (158, 'n02087046 toy terrier'), (159, 'n02087394 Rhodesian ridgeback'), (160, 'n02088094 Afghan hound, Afghan'), (161, 'n02088238 basset, basset hound'), (162, 'n02088364 beagle'), (163, 'n02088466 bloodhound, sleuthhound'), (164, 'n02088632 bluetick'), (165, 'n02089078 black-and-tan coonhound'), (166, 'n02089867 Walker hound, Walker foxhound'), (167, 'n02089973 English foxhound'), (168, 'n02090379 redbone'), (169, 'n02090622 borzoi, Russian wolfhound'), (170, 'n02090721 Irish wolfhound'), (171, 'n02091032 Italian greyhound'), (172, 'n02091134 whippet'), (173, 'n02091244 Ibizan hound, Ibizan Podenco'), (174, 'n02091467 Norwegian elkhound, elkhound'), (175, 'n02091635 otterhound, otter hound'), (176, 'n02091831 Saluki, gazelle hound'), (177, 'n02092002 Scottish deerhound, deerhound'), (178, 'n02092339 Weimaraner'), (179, 'n02093256 Staffordshire bullterrier, Staffordshire bull terrier'), (180, 'n02093428 American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier'), (181, 'n02093647 Bedlington terrier'), (182, 'n02093754 Border terrier'), (183, 'n02093859 Kerry blue terrier'), (184, 'n02093991 Irish terrier'), (185, 'n02094114 Norfolk terrier'), (186, 'n02094258 Norwich terrier'), (187, 'n02094433 Yorkshire terrier'), (188, 'n02095314 wire-haired fox terrier'), (189, 'n02095570 Lakeland terrier'), (190, 'n02095889 Sealyham terrier, Sealyham'), (191, 'n02096051 Airedale, Airedale terrier'), (192, 'n02096177 cairn, cairn terrier'), (193, 'n02096294 Australian terrier'), (194, 'n02096437 Dandie Dinmont, Dandie Dinmont terrier'), (195, 'n02096585 Boston bull, Boston terrier'), (196, 'n02097047 miniature schnauzer'), (197, 'n02097130 giant schnauzer'), (198, 'n02097209 standard schnauzer'), (199, 'n02097298 Scotch terrier, Scottish terrier, Scottie'), (200, 'n02097474 Tibetan terrier, chrysanthemum dog'), (201, 'n02097658 silky terrier, Sydney silky'), (202, 'n02098105 soft-coated wheaten terrier'), (203, 'n02098286 West Highland white terrier'), (204, 'n02098413 Lhasa, Lhasa apso'), (205, 'n02099267 flat-coated retriever'), (206, 'n02099429 curly-coated retriever'), (207, 'n02099601 golden retriever'), (208, 'n02099712 Labrador retriever'), (209, 'n02099849 Chesapeake Bay retriever'), (210, 'n02100236 German short-haired pointer'), (211, 'n02100583 vizsla, Hungarian pointer'), (212, 'n02100735 English setter'), (213, 'n02100877 Irish setter, red setter'), (214, 'n02101006 Gordon setter'), (215, 'n02101388 Brittany spaniel'), (216, 'n02101556 clumber, clumber spaniel'), (217, 'n02102040 English springer, English springer spaniel'), (218, 'n02102177 Welsh springer spaniel'), (219, 'n02102318 cocker spaniel, English cocker spaniel, cocker'), (220, 'n02102480 Sussex spaniel'), (221, 'n02102973 Irish water spaniel'), (222, 'n02104029 kuvasz'), (223, 'n02104365 schipperke'), (224, 'n02105056 groenendael'), (225, 'n02105162 malinois'), (226, 'n02105251 briard'), (227, 'n02105412 kelpie'), (228, 'n02105505 komondor'), (229, 'n02105641 Old English sheepdog, bobtail'), (230, 'n02105855 Shetland sheepdog, Shetland sheep dog, Shetland'), (231, 'n02106030 collie'), (232, 'n02106166 Border collie'), (233, 'n02106382 Bouvier des Flandres, Bouviers des Flandres'), (234, 'n02106550 Rottweiler'), (235, 'n02106662 German shepherd, German shepherd dog, German police dog, alsatian'), (236, 'n02107142 Doberman, Doberman pinscher'), (237, 'n02107312 miniature pinscher'), (238, 'n02107574 Greater Swiss Mountain dog'), (239, 'n02107683 Bernese mountain dog'), (240, 'n02107908 Appenzeller'), (241, 'n02108000 EntleBucher'), (242, 'n02108089 boxer'), (243, 'n02108422 bull mastiff'), (244, 'n02108551 Tibetan mastiff'), (245, 'n02108915 French bulldog'), (246, 'n02109047 Great Dane'), (247, 'n02109525 Saint Bernard, St Bernard'), (248, 'n02109961 Eskimo dog, husky'), (249, 'n02110063 malamute, malemute, Alaskan malamute'), (250, 'n02110185 Siberian husky'), (251, 'n02110341 dalmatian, coach dog, carriage dog'), (252, 'n02110627 affenpinscher, monkey pinscher, monkey dog'), (253, 'n02110806 basenji'), (254, 'n02110958 pug, pug-dog'), (255, 'n02111129 Leonberg'), (256, 'n02111277 Newfoundland, Newfoundland dog'), (257, 'n02111500 Great Pyrenees'), (258, 'n02111889 Samoyed, Samoyede'), (259, 'n02112018 Pomeranian'), (260, 'n02112137 chow, chow chow'), (261, 'n02112350 keeshond'), (262, 'n02112706 Brabancon griffon'), (263, 'n02113023 Pembroke, Pembroke Welsh corgi'), (264, 'n02113186 Cardigan, Cardigan Welsh corgi'), (265, 'n02113624 toy poodle'), (266, 'n02113712 miniature poodle'), (267, 'n02113799 standard poodle'), (268, 'n02113978 Mexican hairless'), (269, 'n02114367 timber wolf, grey wolf, gray wolf, Canis lupus'), (270, 'n02114548 white wolf, Arctic wolf, Canis lupus tundrarum'), (271, 'n02114712 red wolf, maned wolf, Canis rufus, Canis niger'), (272, 'n02114855 coyote, prairie wolf, brush wolf, Canis latrans'), (273, 'n02115641 dingo, warrigal, warragal, Canis dingo'), (274, 'n02115913 dhole, Cuon alpinus'), (275, 'n02116738 African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus'), (276, 'n02117135 hyena, hyaena'), (277, 'n02119022 red fox, Vulpes vulpes'), (278, 'n02119789 kit fox, Vulpes macrotis'), (279, 'n02120079 Arctic fox, white fox, Alopex lagopus'), (280, 'n02120505 grey fox, gray fox, Urocyon cinereoargenteus'), (281, 'n02123045 tabby, tabby cat'), (282, 'n02123159 tiger cat'), (283, 'n02123394 Persian cat'), (284, 'n02123597 Siamese cat, Siamese'), (285, 'n02124075 Egyptian cat'), (286, 'n02125311 cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'), (287, 'n02127052 lynx, catamount'), (288, 'n02128385 leopard, Panthera pardus'), (289, 'n02128757 snow leopard, ounce, Panthera uncia'), (290, 'n02128925 jaguar, panther, Panthera onca, Felis onca'), (291, 'n02129165 lion, king of beasts, Panthera leo'), (292, 'n02129604 tiger, Panthera tigris'), (293, 'n02130308 cheetah, chetah, Acinonyx jubatus'), (294, 'n02132136 brown bear, bruin, Ursus arctos'), (295, 'n02133161 American black bear, black bear, Ursus americanus, Euarctos americanus'), (296, 'n02134084 ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus'), (297, 'n02134418 sloth bear, Melursus ursinus, Ursus ursinus'), (298, 'n02137549 mongoose'), (299, 'n02138441 meerkat, mierkat'), (300, 'n02165105 tiger beetle'), (301, 'n02165456 ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle'), (302, 'n02167151 ground beetle, carabid beetle'), (303, 'n02168699 long-horned beetle, longicorn, longicorn beetle'), (304, 'n02169497 leaf beetle, chrysomelid'), (305, 'n02172182 dung beetle'), (306, 'n02174001 rhinoceros beetle'), (307, 'n02177972 weevil'), (308, 'n02190166 fly'), (309, 'n02206856 bee'), (310, 'n02219486 ant, emmet, pismire'), (311, 'n02226429 grasshopper, hopper'), (312, 'n02229544 cricket'), (313, 'n02231487 walking stick, walkingstick, stick insect'), (314, 'n02233338 cockroach, roach'), (315, 'n02236044 mantis, mantid'), (316, 'n02256656 cicada, cicala'), (317, 'n02259212 leafhopper'), (318, 'n02264363 lacewing, lacewing fly'), (319, "n02268443 dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk"), (320, 'n02268853 damselfly'), (321, 'n02276258 admiral'), (322, 'n02277742 ringlet, ringlet butterfly'), (323, 'n02279972 monarch, monarch butterfly, milkweed butterfly, Danaus plexippus'), (324, 'n02280649 cabbage butterfly'), (325, 'n02281406 sulphur butterfly, sulfur butterfly'), (326, 'n02281787 lycaenid, lycaenid butterfly'), (327, 'n02317335 starfish, sea star'), (328, 'n02319095 sea urchin'), (329, 'n02321529 sea cucumber, holothurian'), (330, 'n02325366 wood rabbit, cottontail, cottontail rabbit'), (331, 'n02326432 hare'), (332, 'n02328150 Angora, Angora rabbit'), (333, 'n02342885 hamster'), (334, 'n02346627 porcupine, hedgehog'), (335, 'n02356798 fox squirrel, eastern fox squirrel, Sciurus niger'), (336, 'n02361337 marmot'), (337, 'n02363005 beaver'), (338, 'n02364673 guinea pig, Cavia cobaya'), (339, 'n02389026 sorrel'), (340, 'n02391049 zebra'), (341, 'n02395406 hog, pig, grunter, squealer, Sus scrofa'), (342, 'n02396427 wild boar, boar, Sus scrofa'), (343, 'n02397096 warthog'), (344, 'n02398521 hippopotamus, hippo, river horse, Hippopotamus amphibius'), (345, 'n02403003 ox'), (346, 'n02408429 water buffalo, water ox, Asiatic buffalo, Bubalus bubalis'), (347, 'n02410509 bison'), (348, 'n02412080 ram, tup'), (349, 'n02415577 bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis'), (350, 'n02417914 ibex, Capra ibex'), (351, 'n02422106 hartebeest'), (352, 'n02422699 impala, Aepyceros melampus'), (353, 'n02423022 gazelle'), (354, 'n02437312 Arabian camel, dromedary, Camelus dromedarius'), (355, 'n02437616 llama'), (356, 'n02441942 weasel'), (357, 'n02442845 mink'), (358, 'n02443114 polecat, fitch, foulmart, foumart, Mustela putorius'), (359, 'n02443484 black-footed ferret, ferret, Mustela nigripes'), (360, 'n02444819 otter'), (361, 'n02445715 skunk, polecat, wood pussy'), (362, 'n02447366 badger'), (363, 'n02454379 armadillo'), (364, 'n02457408 three-toed sloth, ai, Bradypus tridactylus'), (365, 'n02480495 orangutan, orang, orangutang, Pongo pygmaeus'), (366, 'n02480855 gorilla, Gorilla gorilla'), (367, 'n02481823 chimpanzee, chimp, Pan troglodytes'), (368, 'n02483362 gibbon, Hylobates lar'), (369, 'n02483708 siamang, Hylobates syndactylus, Symphalangus syndactylus'), (370, 'n02484975 guenon, guenon monkey'), (371, 'n02486261 patas, hussar monkey, Erythrocebus patas'), (372, 'n02486410 baboon'), (373, 'n02487347 macaque'), (374, 'n02488291 langur'), (375, 'n02488702 colobus, colobus monkey'), (376, 'n02489166 proboscis monkey, Nasalis larvatus'), (377, 'n02490219 marmoset'), (378, 'n02492035 capuchin, ringtail, Cebus capucinus'), (379, 'n02492660 howler monkey, howler'), (380, 'n02493509 titi, titi monkey'), (381, 'n02493793 spider monkey, Ateles geoffroyi'), (382, 'n02494079 squirrel monkey, Saimiri sciureus'), (383, 'n02497673 Madagascar cat, ring-tailed lemur, Lemur catta'), (384, 'n02500267 indri, indris, Indri indri, Indri brevicaudatus'), (385, 'n02504013 Indian elephant, Elephas maximus'), (386, 'n02504458 African elephant, Loxodonta africana'), (387, 'n02509815 lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens'), (388, 'n02510455 giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca'), (389, 'n02514041 barracouta, snoek'), (390, 'n02526121 eel'), (391, 'n02536864 coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch'), (392, 'n02606052 rock beauty, Holocanthus tricolor'), (393, 'n02607072 anemone fish'), (394, 'n02640242 sturgeon'), (395, 'n02641379 gar, garfish, garpike, billfish, Lepisosteus osseus'), (396, 'n02643566 lionfish'), (397, 'n02655020 puffer, pufferfish, blowfish, globefish'), (398, 'n02666196 abacus'), (399, 'n02667093 abaya'), (400, "n02669723 academic gown, academic robe, judge's robe"), (401, 'n02672831 accordion, piano accordion, squeeze box'), (402, 'n02676566 acoustic guitar'), (403, 'n02687172 aircraft carrier, carrier, flattop, attack aircraft carrier'), (404, 'n02690373 airliner'), (405, 'n02692877 airship, dirigible'), (406, 'n02699494 altar'), (407, 'n02701002 ambulance'), (408, 'n02704792 amphibian, amphibious vehicle'), (409, 'n02708093 analog clock'), (410, 'n02727426 apiary, bee house'), (411, 'n02730930 apron'), (412, 'n02747177 ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin'), (413, 'n02749479 assault rifle, assault gun'), (414, 'n02769748 backpack, back pack, knapsack, packsack, rucksack, haversack'), (415, 'n02776631 bakery, bakeshop, bakehouse'), (416, 'n02777292 balance beam, beam'), (417, 'n02782093 balloon'), (418, 'n02783161 ballpoint, ballpoint pen, ballpen, Biro'), (419, 'n02786058 Band Aid'), (420, 'n02787622 banjo'), (421, 'n02788148 bannister, banister, balustrade, balusters, handrail'), (422, 'n02790996 barbell'), (423, 'n02791124 barber chair'), (424, 'n02791270 barbershop'), (425, 'n02793495 barn'), (426, 'n02794156 barometer'), (427, 'n02795169 barrel, cask'), (428, 'n02797295 barrow, garden cart, lawn cart, wheelbarrow'), (429, 'n02799071 baseball'), (430, 'n02802426 basketball'), (431, 'n02804414 bassinet'), (432, 'n02804610 bassoon'), (433, 'n02807133 bathing cap, swimming cap'), (434, 'n02808304 bath towel'), (435, 'n02808440 bathtub, bathing tub, bath, tub'), (436, 'n02814533 beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon'), (437, 'n02814860 beacon, lighthouse, beacon light, pharos'), (438, 'n02815834 beaker'), (439, 'n02817516 bearskin, busby, shako'), (440, 'n02823428 beer bottle'), (441, 'n02823750 beer glass'), (442, 'n02825657 bell cote, bell cot'), (443, 'n02834397 bib'), (444, 'n02835271 bicycle-built-for-two, tandem bicycle, tandem'), (445, 'n02837789 bikini, two-piece'), (446, 'n02840245 binder, ring-binder'), (447, 'n02841315 binoculars, field glasses, opera glasses'), (448, 'n02843684 birdhouse'), (449, 'n02859443 boathouse'), (450, 'n02860847 bobsled, bobsleigh, bob'), (451, 'n02865351 bolo tie, bolo, bola tie, bola'), (452, 'n02869837 bonnet, poke bonnet'), (453, 'n02870880 bookcase'), (454, 'n02871525 bookshop, bookstore, bookstall'), (455, 'n02877765 bottlecap'), (456, 'n02879718 bow'), (457, 'n02883205 bow tie, bow-tie, bowtie'), (458, 'n02892201 brass, memorial tablet, plaque'), (459, 'n02892767 brassiere, bra, bandeau'), (460, 'n02894605 breakwater, groin, groyne, mole, bulwark, seawall, jetty'), (461, 'n02895154 breastplate, aegis, egis'), (462, 'n02906734 broom'), (463, 'n02909870 bucket, pail'), (464, 'n02910353 buckle'), (465, 'n02916936 bulletproof vest'), (466, 'n02917067 bullet train, bullet'), (467, 'n02927161 butcher shop, meat market'), (468, 'n02930766 cab, hack, taxi, taxicab'), (469, 'n02939185 caldron, cauldron'), (470, 'n02948072 candle, taper, wax light'), (471, 'n02950826 cannon'), (472, 'n02951358 canoe'), (473, 'n02951585 can opener, tin opener'), (474, 'n02963159 cardigan'), (475, 'n02965783 car mirror'), (476, 'n02966193 carousel, carrousel, merry-go-round, roundabout, whirligig'), (477, "n02966687 carpenter's kit, tool kit"), (478, 'n02971356 carton'), (479, 'n02974003 car wheel'), (480, 'n02977058 cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM'), (481, 'n02978881 cassette'), (482, 'n02979186 cassette player'), (483, 'n02980441 castle'), (484, 'n02981792 catamaran'), (485, 'n02988304 CD player'), (486, 'n02992211 cello, violoncello'), (487, 'n02992529 cellular telephone, cellular phone, cellphone, cell, mobile phone'), (488, 'n02999410 chain'), (489, 'n03000134 chainlink fence'), (490, 'n03000247 chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour'), (491, 'n03000684 chain saw, chainsaw'), (492, 'n03014705 chest'), (493, 'n03016953 chiffonier, commode'), (494, 'n03017168 chime, bell, gong'), (495, 'n03018349 china cabinet, china closet'), (496, 'n03026506 Christmas stocking'), (497, 'n03028079 church, church building'), (498, 'n03032252 cinema, movie theater, movie theatre, movie house, picture palace'), (499, 'n03041632 cleaver, meat cleaver, chopper'), (500, 'n03042490 cliff dwelling'), (501, 'n03045698 cloak'), (502, 'n03047690 clog, geta, patten, sabot'), (503, 'n03062245 cocktail shaker'), (504, 'n03063599 coffee mug'), (505, 'n03063689 coffeepot'), (506, 'n03065424 coil, spiral, volute, whorl, helix'), (507, 'n03075370 combination lock'), (508, 'n03085013 computer keyboard, keypad'), (509, 'n03089624 confectionery, confectionary, candy store'), (510, 'n03095699 container ship, containership, container vessel'), (511, 'n03100240 convertible'), (512, 'n03109150 corkscrew, bottle screw'), (513, 'n03110669 cornet, horn, trumpet, trump'), (514, 'n03124043 cowboy boot'), (515, 'n03124170 cowboy hat, ten-gallon hat'), (516, 'n03125729 cradle'), (517, 'n03126707 crane'), (518, 'n03127747 crash helmet'), (519, 'n03127925 crate'), (520, 'n03131574 crib, cot'), (521, 'n03133878 Crock Pot'), (522, 'n03134739 croquet ball'), (523, 'n03141823 crutch'), (524, 'n03146219 cuirass'), (525, 'n03160309 dam, dike, dyke'), (526, 'n03179701 desk'), (527, 'n03180011 desktop computer'), (528, 'n03187595 dial telephone, dial phone'), (529, 'n03188531 diaper, nappy, napkin'), (530, 'n03196217 digital clock'), (531, 'n03197337 digital watch'), (532, 'n03201208 dining table, board'), (533, 'n03207743 dishrag, dishcloth'), (534, 'n03207941 dishwasher, dish washer, dishwashing machine'), (535, 'n03208938 disk brake, disc brake'), (536, 'n03216828 dock, dockage, docking facility'), (537, 'n03218198 dogsled, dog sled, dog sleigh'), (538, 'n03220513 dome'), (539, 'n03223299 doormat, welcome mat'), (540, 'n03240683 drilling platform, offshore rig'), (541, 'n03249569 drum, membranophone, tympan'), (542, 'n03250847 drumstick'), (543, 'n03255030 dumbbell'), (544, 'n03259280 Dutch oven'), (545, 'n03271574 electric fan, blower'), (546, 'n03272010 electric guitar'), (547, 'n03272562 electric locomotive'), (548, 'n03290653 entertainment center'), (549, 'n03291819 envelope'), (550, 'n03297495 espresso maker'), (551, 'n03314780 face powder'), (552, 'n03325584 feather boa, boa'), (553, 'n03337140 file, file cabinet, filing cabinet'), (554, 'n03344393 fireboat'), (555, 'n03345487 fire engine, fire truck'), (556, 'n03347037 fire screen, fireguard'), (557, 'n03355925 flagpole, flagstaff'), (558, 'n03372029 flute, transverse flute'), (559, 'n03376595 folding chair'), (560, 'n03379051 football helmet'), (561, 'n03384352 forklift'), (562, 'n03388043 fountain'), (563, 'n03388183 fountain pen'), (564, 'n03388549 four-poster'), (565, 'n03393912 freight car'), (566, 'n03394916 French horn, horn'), (567, 'n03400231 frying pan, frypan, skillet'), (568, 'n03404251 fur coat'), (569, 'n03417042 garbage truck, dustcart'), (570, 'n03424325 gasmask, respirator, gas helmet'), (571, 'n03425413 gas pump, gasoline pump, petrol pump, island dispenser'), (572, 'n03443371 goblet'), (573, 'n03444034 go-kart'), (574, 'n03445777 golf ball'), (575, 'n03445924 golfcart, golf cart'), (576, 'n03447447 gondola'), (577, 'n03447721 gong, tam-tam'), (578, 'n03450230 gown'), (579, 'n03452741 grand piano, grand'), (580, 'n03457902 greenhouse, nursery, glasshouse'), (581, 'n03459775 grille, radiator grille'), (582, 'n03461385 grocery store, grocery, food market, market'), (583, 'n03467068 guillotine'), (584, 'n03476684 hair slide'), (585, 'n03476991 hair spray'), (586, 'n03478589 half track'), (587, 'n03481172 hammer'), (588, 'n03482405 hamper'), (589, 'n03483316 hand blower, blow dryer, blow drier, hair dryer, hair drier'), (590, 'n03485407 hand-held computer, hand-held microcomputer'), (591, 'n03485794 handkerchief, hankie, hanky, hankey'), (592, 'n03492542 hard disc, hard disk, fixed disk'), (593, 'n03494278 harmonica, mouth organ, harp, mouth harp'), (594, 'n03495258 harp'), (595, 'n03496892 harvester, reaper'), (596, 'n03498962 hatchet'), (597, 'n03527444 holster'), (598, 'n03529860 home theater, home theatre'), (599, 'n03530642 honeycomb'), (600, 'n03532672 hook, claw'), (601, 'n03534580 hoopskirt, crinoline'), (602, 'n03535780 horizontal bar, high bar'), (603, 'n03538406 horse cart, horse-cart'), (604, 'n03544143 hourglass'), (605, 'n03584254 iPod'), (606, 'n03584829 iron, smoothing iron'), (607, "n03590841 jack-o'-lantern"), (608, 'n03594734 jean, blue jean, denim'), (609, 'n03594945 jeep, landrover'), (610, 'n03595614 jersey, T-shirt, tee shirt'), (611, 'n03598930 jigsaw puzzle'), (612, 'n03599486 jinrikisha, ricksha, rickshaw'), (613, 'n03602883 joystick'), (614, 'n03617480 kimono'), (615, 'n03623198 knee pad'), (616, 'n03627232 knot'), (617, 'n03630383 lab coat, laboratory coat'), (618, 'n03633091 ladle'), (619, 'n03637318 lampshade, lamp shade'), (620, 'n03642806 laptop, laptop computer'), (621, 'n03649909 lawn mower, mower'), (622, 'n03657121 lens cap, lens cover'), (623, 'n03658185 letter opener, paper knife, paperknife'), (624, 'n03661043 library'), (625, 'n03662601 lifeboat'), (626, 'n03666591 lighter, light, igniter, ignitor'), (627, 'n03670208 limousine, limo'), (628, 'n03673027 liner, ocean liner'), (629, 'n03676483 lipstick, lip rouge'), (630, 'n03680355 Loafer'), (631, 'n03690938 lotion'), (632, 'n03691459 loudspeaker, speaker, speaker unit, loudspeaker system, speaker system'), (633, "n03692522 loupe, jeweler's loupe"), (634, 'n03697007 lumbermill, sawmill'), (635, 'n03706229 magnetic compass'), (636, 'n03709823 mailbag, postbag'), (637, 'n03710193 mailbox, letter box'), (638, 'n03710637 maillot'), (639, 'n03710721 maillot, tank suit'), (640, 'n03717622 manhole cover'), (641, 'n03720891 maraca'), (642, 'n03721384 marimba, xylophone'), (643, 'n03724870 mask'), (644, 'n03729826 matchstick'), (645, 'n03733131 maypole'), (646, 'n03733281 maze, labyrinth'), (647, 'n03733805 measuring cup'), (648, 'n03742115 medicine chest, medicine cabinet'), (649, 'n03743016 megalith, megalithic structure'), (650, 'n03759954 microphone, mike'), (651, 'n03761084 microwave, microwave oven'), (652, 'n03763968 military uniform'), (653, 'n03764736 milk can'), (654, 'n03769881 minibus'), (655, 'n03770439 miniskirt, mini'), (656, 'n03770679 minivan'), (657, 'n03773504 missile'), (658, 'n03775071 mitten'), (659, 'n03775546 mixing bowl'), (660, 'n03776460 mobile home, manufactured home'), (661, 'n03777568 Model T'), (662, 'n03777754 modem'), (663, 'n03781244 monastery'), (664, 'n03782006 monitor'), (665, 'n03785016 moped'), (666, 'n03786901 mortar'), (667, 'n03787032 mortarboard'), (668, 'n03788195 mosque'), (669, 'n03788365 mosquito net'), (670, 'n03791053 motor scooter, scooter'), (671, 'n03792782 mountain bike, all-terrain bike, off-roader'), (672, 'n03792972 mountain tent'), (673, 'n03793489 mouse, computer mouse'), (674, 'n03794056 mousetrap'), (675, 'n03796401 moving van'), (676, 'n03803284 muzzle'), (677, 'n03804744 nail'), (678, 'n03814639 neck brace'), (679, 'n03814906 necklace'), (680, 'n03825788 nipple'), (681, 'n03832673 notebook, notebook computer'), (682, 'n03837869 obelisk'), (683, 'n03838899 oboe, hautboy, hautbois'), (684, 'n03840681 ocarina, sweet potato'), (685, 'n03841143 odometer, hodometer, mileometer, milometer'), (686, 'n03843555 oil filter'), (687, 'n03854065 organ, pipe organ'), (688, 'n03857828 oscilloscope, scope, cathode-ray oscilloscope, CRO'), (689, 'n03866082 overskirt'), (690, 'n03868242 oxcart'), (691, 'n03868863 oxygen mask'), (692, 'n03871628 packet'), (693, 'n03873416 paddle, boat paddle'), (694, 'n03874293 paddlewheel, paddle wheel'), (695, 'n03874599 padlock'), (696, 'n03876231 paintbrush'), (697, "n03877472 pajama, pyjama, pj's, jammies"), (698, 'n03877845 palace'), (699, 'n03884397 panpipe, pandean pipe, syrinx'), (700, 'n03887697 paper towel'), (701, 'n03888257 parachute, chute'), (702, 'n03888605 parallel bars, bars'), (703, 'n03891251 park bench'), (704, 'n03891332 parking meter'), (705, 'n03895866 passenger car, coach, carriage'), (706, 'n03899768 patio, terrace'), (707, 'n03902125 pay-phone, pay-station'), (708, 'n03903868 pedestal, plinth, footstall'), (709, 'n03908618 pencil box, pencil case'), (710, 'n03908714 pencil sharpener'), (711, 'n03916031 perfume, essence'), (712, 'n03920288 Petri dish'), (713, 'n03924679 photocopier'), (714, 'n03929660 pick, plectrum, plectron'), (715, 'n03929855 pickelhaube'), (716, 'n03930313 picket fence, paling'), (717, 'n03930630 pickup, pickup truck'), (718, 'n03933933 pier'), (719, 'n03935335 piggy bank, penny bank'), (720, 'n03937543 pill bottle'), (721, 'n03938244 pillow'), (722, 'n03942813 ping-pong ball'), (723, 'n03944341 pinwheel'), (724, 'n03947888 pirate, pirate ship'), (725, 'n03950228 pitcher, ewer'), (726, "n03954731 plane, carpenter's plane, woodworking plane"), (727, 'n03956157 planetarium'), (728, 'n03958227 plastic bag'), (729, 'n03961711 plate rack'), (730, 'n03967562 plow, plough'), (731, "n03970156 plunger, plumber's helper"), (732, 'n03976467 Polaroid camera, Polaroid Land camera'), (733, 'n03976657 pole'), (734, 'n03977966 police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria'), (735, 'n03980874 poncho'), (736, 'n03982430 pool table, billiard table, snooker table'), (737, 'n03983396 pop bottle, soda bottle'), (738, 'n03991062 pot, flowerpot'), (739, "n03992509 potter's wheel"), (740, 'n03995372 power drill'), (741, 'n03998194 prayer rug, prayer mat'), (742, 'n04004767 printer'), (743, 'n04005630 prison, prison house'), (744, 'n04008634 projectile, missile'), (745, 'n04009552 projector'), (746, 'n04019541 puck, hockey puck'), (747, 'n04023962 punching bag, punch bag, punching ball, punchball'), (748, 'n04026417 purse'), (749, 'n04033901 quill, quill pen'), (750, 'n04033995 quilt, comforter, comfort, puff'), (751, 'n04037443 racer, race car, racing car'), (752, 'n04039381 racket, racquet'), (753, 'n04040759 radiator'), (754, 'n04041544 radio, wireless'), (755, 'n04044716 radio telescope, radio reflector'), (756, 'n04049303 rain barrel'), (757, 'n04065272 recreational vehicle, RV, R.V.'), (758, 'n04067472 reel'), (759, 'n04069434 reflex camera'), (760, 'n04070727 refrigerator, icebox'), (761, 'n04074963 remote control, remote'), (762, 'n04081281 restaurant, eating house, eating place, eatery'), (763, 'n04086273 revolver, six-gun, six-shooter'), (764, 'n04090263 rifle'), (765, 'n04099969 rocking chair, rocker'), (766, 'n04111531 rotisserie'), (767, 'n04116512 rubber eraser, rubber, pencil eraser'), (768, 'n04118538 rugby ball'), (769, 'n04118776 rule, ruler'), (770, 'n04120489 running shoe'), (771, 'n04125021 safe'), (772, 'n04127249 safety pin'), (773, 'n04131690 saltshaker, salt shaker'), (774, 'n04133789 sandal'), (775, 'n04136333 sarong'), (776, 'n04141076 sax, saxophone'), (777, 'n04141327 scabbard'), (778, 'n04141975 scale, weighing machine'), (779, 'n04146614 school bus'), (780, 'n04147183 schooner'), (781, 'n04149813 scoreboard'), (782, 'n04152593 screen, CRT screen'), (783, 'n04153751 screw'), (784, 'n04154565 screwdriver'), (785, 'n04162706 seat belt, seatbelt'), (786, 'n04179913 sewing machine'), (787, 'n04192698 shield, buckler'), (788, 'n04200800 shoe shop, shoe-shop, shoe store'), (789, 'n04201297 shoji'), (790, 'n04204238 shopping basket'), (791, 'n04204347 shopping cart'), (792, 'n04208210 shovel'), (793, 'n04209133 shower cap'), (794, 'n04209239 shower curtain'), (795, 'n04228054 ski'), (796, 'n04229816 ski mask'), (797, 'n04235860 sleeping bag'), (798, 'n04238763 slide rule, slipstick'), (799, 'n04239074 sliding door'), (800, 'n04243546 slot, one-armed bandit'), (801, 'n04251144 snorkel'), (802, 'n04252077 snowmobile'), (803, 'n04252225 snowplow, snowplough'), (804, 'n04254120 soap dispenser'), (805, 'n04254680 soccer ball'), (806, 'n04254777 sock'), (807, 'n04258138 solar dish, solar collector, solar furnace'), (808, 'n04259630 sombrero'), (809, 'n04263257 soup bowl'), (810, 'n04264628 space bar'), (811, 'n04265275 space heater'), (812, 'n04266014 space shuttle'), (813, 'n04270147 spatula'), (814, 'n04273569 speedboat'), (815, "n04275548 spider web, spider's web"), (816, 'n04277352 spindle'), (817, 'n04285008 sports car, sport car'), (818, 'n04286575 spotlight, spot'), (819, 'n04296562 stage'), (820, 'n04310018 steam locomotive'), (821, 'n04311004 steel arch bridge'), (822, 'n04311174 steel drum'), (823, 'n04317175 stethoscope'), (824, 'n04325704 stole'), (825, 'n04326547 stone wall'), (826, 'n04328186 stopwatch, stop watch'), (827, 'n04330267 stove'), (828, 'n04332243 strainer'), (829, 'n04335435 streetcar, tram, tramcar, trolley, trolley car'), (830, 'n04336792 stretcher'), (831, 'n04344873 studio couch, day bed'), (832, 'n04346328 stupa, tope'), (833, 'n04347754 submarine, pigboat, sub, U-boat'), (834, 'n04350905 suit, suit of clothes'), (835, 'n04355338 sundial'), (836, 'n04355933 sunglass'), (837, 'n04356056 sunglasses, dark glasses, shades'), (838, 'n04357314 sunscreen, sunblock, sun blocker'), (839, 'n04366367 suspension bridge'), (840, 'n04367480 swab, swob, mop'), (841, 'n04370456 sweatshirt'), (842, 'n04371430 swimming trunks, bathing trunks'), (843, 'n04371774 swing'), (844, 'n04372370 switch, electric switch, electrical switch'), (845, 'n04376876 syringe'), (846, 'n04380533 table lamp'), (847, 'n04389033 tank, army tank, armored combat vehicle, armoured combat vehicle'), (848, 'n04392985 tape player'), (849, 'n04398044 teapot'), (850, 'n04399382 teddy, teddy bear'), (851, 'n04404412 television, television system'), (852, 'n04409515 tennis ball'), (853, 'n04417672 thatch, thatched roof'), (854, 'n04418357 theater curtain, theatre curtain'), (855, 'n04423845 thimble'), (856, 'n04428191 thresher, thrasher, threshing machine'), (857, 'n04429376 throne'), (858, 'n04435653 tile roof'), (859, 'n04442312 toaster'), (860, 'n04443257 tobacco shop, tobacconist shop, tobacconist'), (861, 'n04447861 toilet seat'), (862, 'n04456115 torch'), (863, 'n04458633 totem pole'), (864, 'n04461696 tow truck, tow car, wrecker'), (865, 'n04462240 toyshop'), (866, 'n04465501 tractor'), (867, 'n04467665 trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi'), (868, 'n04476259 tray'), (869, 'n04479046 trench coat'), (870, 'n04482393 tricycle, trike, velocipede'), (871, 'n04483307 trimaran'), (872, 'n04485082 tripod'), (873, 'n04486054 triumphal arch'), (874, 'n04487081 trolleybus, trolley coach, trackless trolley'), (875, 'n04487394 trombone'), (876, 'n04493381 tub, vat'), (877, 'n04501370 turnstile'), (878, 'n04505470 typewriter keyboard'), (879, 'n04507155 umbrella'), (880, 'n04509417 unicycle, monocycle'), (881, 'n04515003 upright, upright piano'), (882, 'n04517823 vacuum, vacuum cleaner'), (883, 'n04522168 vase'), (884, 'n04523525 vault'), (885, 'n04525038 velvet'), (886, 'n04525305 vending machine'), (887, 'n04532106 vestment'), (888, 'n04532670 viaduct'), (889, 'n04536866 violin, fiddle'), (890, 'n04540053 volleyball'), (891, 'n04542943 waffle iron'), (892, 'n04548280 wall clock'), (893, 'n04548362 wallet, billfold, notecase, pocketbook'), (894, 'n04550184 wardrobe, closet, press'), (895, 'n04552348 warplane, military plane'), (896, 'n04553703 washbasin, handbasin, washbowl, lavabo, wash-hand basin'), (897, 'n04554684 washer, automatic washer, washing machine'), (898, 'n04557648 water bottle'), (899, 'n04560804 water jug'), (900, 'n04562935 water tower'), (901, 'n04579145 whiskey jug'), (902, 'n04579432 whistle'), (903, 'n04584207 wig'), (904, 'n04589890 window screen'), (905, 'n04590129 window shade'), (906, 'n04591157 Windsor tie'), (907, 'n04591713 wine bottle'), (908, 'n04592741 wing'), (909, 'n04596742 wok'), (910, 'n04597913 wooden spoon'), (911, 'n04599235 wool, woolen, woollen'), (912, 'n04604644 worm fence, snake fence, snake-rail fence, Virginia fence'), (913, 'n04606251 wreck'), (914, 'n04612504 yawl'), (915, 'n04613696 yurt'), (916, 'n06359193 web site, website, internet site, site'), (917, 'n06596364 comic book'), (918, 'n06785654 crossword puzzle, crossword'), (919, 'n06794110 street sign'), (920, 'n06874185 traffic light, traffic signal, stoplight'), (921, 'n07248320 book jacket, dust cover, dust jacket, dust wrapper'), (922, 'n07565083 menu'), (923, 'n07579787 plate'), (924, 'n07583066 guacamole'), (925, 'n07584110 consomme'), (926, 'n07590611 hot pot, hotpot'), (927, 'n07613480 trifle'), (928, 'n07614500 ice cream, icecream'), (929, 'n07615774 ice lolly, lolly, lollipop, popsicle'), (930, 'n07684084 French loaf'), (931, 'n07693725 bagel, beigel'), (932, 'n07695742 pretzel'), (933, 'n07697313 cheeseburger'), (934, 'n07697537 hotdog, hot dog, red hot'), (935, 'n07711569 mashed potato'), (936, 'n07714571 head cabbage'), (937, 'n07714990 broccoli'), (938, 'n07715103 cauliflower'), (939, 'n07716358 zucchini, courgette'), (940, 'n07716906 spaghetti squash'), (941, 'n07717410 acorn squash'), (942, 'n07717556 butternut squash'), (943, 'n07718472 cucumber, cuke'), (944, 'n07718747 artichoke, globe artichoke'), (945, 'n07720875 bell pepper'), (946, 'n07730033 cardoon'), (947, 'n07734744 mushroom'), (948, 'n07742313 Granny Smith'), (949, 'n07745940 strawberry'), (950, 'n07747607 orange'), (951, 'n07749582 lemon'), (952, 'n07753113 fig'), (953, 'n07753275 pineapple, ananas'), (954, 'n07753592 banana'), (955, 'n07754684 jackfruit, jak, jack'), (956, 'n07760859 custard apple'), (957, 'n07768694 pomegranate'), (958, 'n07802026 hay'), (959, 'n07831146 carbonara'), (960, 'n07836838 chocolate sauce, chocolate syrup'), (961, 'n07860988 dough'), (962, 'n07871810 meat loaf, meatloaf'), (963, 'n07873807 pizza, pizza pie'), (964, 'n07875152 potpie'), (965, 'n07880968 burrito'), (966, 'n07892512 red wine'), (967, 'n07920052 espresso'), (968, 'n07930864 cup'), (969, 'n07932039 eggnog'), (970, 'n09193705 alp'), (971, 'n09229709 bubble'), (972, 'n09246464 cliff, drop, drop-off'), (973, 'n09256479 coral reef'), (974, 'n09288635 geyser'), (975, 'n09332890 lakeside, lakeshore'), (976, 'n09399592 promontory, headland, head, foreland'), (977, 'n09421951 sandbar, sand bar'), (978, 'n09428293 seashore, coast, seacoast, sea-coast'), (979, 'n09468604 valley, vale'), (980, 'n09472597 volcano'), (981, 'n09835506 ballplayer, baseball player'), (982, 'n10148035 groom, bridegroom'), (983, 'n10565667 scuba diver'), (984, 'n11879895 rapeseed'), (985, 'n11939491 daisy'), (986, "n12057211 yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum"), (987, 'n12144580 corn'), (988, 'n12267677 acorn'), (989, 'n12620546 hip, rose hip, rosehip'), (990, 'n12768682 buckeye, horse chestnut, conker'), (991, 'n12985857 coral fungus'), (992, 'n12998815 agaric'), (993, 'n13037406 gyromitra'), (994, 'n13040303 stinkhorn, carrion fungus'), (995, 'n13044778 earthstar'), (996, 'n13052670 hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa'), (997, 'n13054560 bolete'), (998, 'n13133613 ear, spike, capitulum'), (999, 'n15075141 toilet tissue, toilet paper, bathroom tissue')]

In [86]:
# Pick a neuron.  Or pick a random one.  This should be 0-n_els
neuron_i = np.random.randint(0, n_els)

print(net['labels'][neuron_i])
assert(neuron_i >= 0 and neuron_i < n_els)


(16, 'n01560419 bulbul')

In [87]:
# And we'll create an activation of this layer which is very close to 0
layer_vec = np.ones(layer_shape) / 100.0

# Except for the randomly chosen neuron which will be very close to 1
layer_vec[..., neuron_i] = 0.99

print(layer_vec.shape)
print(layer_vec)


(1, 1000)
[[ 0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.99  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01  0.01
   0.01  0.01  0.01  0.01]]

Let's decide on some parameters of our deep dream. We'll need to decide how many iterations to run for. And we'll plot the result every few iterations, also saving it so that we can produce a GIF. And at every iteration, we need to decide how much to ascend our gradient.


In [88]:
# [Choice] number of iteration
n_iterations = 51
# [Choice] plot per x iteration
plot_step = 5

# [Choice] learning rate
# different meaning to each network
step = 0.5

Now let's dream. We're going to define a context manager to create a session and use our existing graph, and make sure we use the CPU device, as there is no gain in using GPU, and we have much more CPU memory than GPU memory.


In [89]:
imgs = []
with tf.Session(graph=g) as sess, g.device(device):
    gradient = tf.gradients(tf.reduce_max(layer), x)

    # Copy the input image as we'll add the gradient to it in a loop
    img_copy = img.copy()

    with tf.Session(graph=g) as sess, g.device(device):
        for it_i in range(n_iterations):

            # This will calculate the gradient of the layer we chose with respect to the input image.
            this_res = sess.run(gradient[0], feed_dict={
                    x: img_copy, layer: layer_vec})[0]
            
            # Let's normalize it by the maximum activation
            this_res /= (np.max(np.abs(this_res) + 1e-8))
            
            # Or alternatively, we can normalize by standard deviation
            # this_res /= (np.std(this_res) + 1e-8)

            # Then add the gradient back to the input image
            # Think about what this gradient represents?
            # It says what direction we should move our input
            # in order to meet our objective stored in "gradient"
            img_copy += this_res * step

            # Plot the image
            if (it_i + 1) % plot_step == 0:
                m = net['deprocess'](img_copy[0])

                plt.figure(figsize=(5, 5))
                plt.grid('off')
                plt.imshow(m)
                plt.show()
                
                imgs.append(m)



In [90]:
# Save the gif
gif.build_gif(imgs, saveto='softmax.gif')


Out[90]:
<matplotlib.animation.ArtistAnimation at 0x7f5aff4b2278>

In [91]:
ipyd.Image(url='softmax.gif?i={}'.format(
        np.random.rand()), height=300, width=300)


Out[91]:

Fractal

During the lecture we also saw a simple trick for creating an infinite fractal: crop the image and then resize it. This can produce some lovely aesthetics and really show some strong object hallucinations if left long enough and with the right parameters for step size/normalization/regularization. Feel free to experiment with the code below, adding your own regularizations as shown in the lecture to produce different results!


In [96]:
# [Choice]
n_iterations = 101
plot_step = 10
step = 0.2
crop = 1
imgs = []

n_imgs, height, width, *ch = img.shape

with tf.Session(graph=g) as sess, g.device(device):
    # Explore changing the gradient here from max to mean
    # or even try using different concepts we learned about
    # when creating style net, such as using a total variational
    # loss on `x`.
    gradient = tf.gradients(tf.reduce_max(layer), x)

    # Copy the input image as we'll add the gradient to it in a loop
    img_copy = img.copy()

    with tf.Session(graph=g) as sess, g.device(device):
        for it_i in range(n_iterations):

            # This will calculate the gradient of the layer
            # we chose with respect to the input image.
            this_res = sess.run(gradient[0], 
                                feed_dict = {x: img_copy, layer: layer_vec})[0]
            
            # This is just one way we could normalize the
            # gradient.  It helps to look at the range of your image's
            # values, e.g. if it is 0 - 1, or -115 to +115,
            # and then consider the best way to normalize the gradient.
            # For some networks, it might not even be necessary
            # to perform this normalization, especially if you
            # leave the dream to run for enough iterations.
            this_res = this_res / (np.max(np.abs(this_res)) + 1e-10)
            # this_res = this_res / (np.std(this_res) + 1e-10)
            
            # Then add the gradient back to the input image
            # Think about what this gradient represents?
            # It says what direction we should move our input
            # in order to meet our objective stored in "gradient"
            img_copy += this_res * step
            
            # Optionally, we could apply any number of regularization
            # techniques... Try exploring different ways of regularizing
            # gradient. ascent process.  If you are adventurous, you can
            # also explore changing the gradient above using a
            # total variational loss, as we used in the style net
            # implementation during the lecture.  I leave that to you
            # as an exercise!

            # Crop a 1 pixel border from height and width
            img_copy = img_copy[:, crop:-crop, crop:-crop, :]

            # Resize (Note: in the lecture, we used scipy's resize which
            # could not resize images outside of 0-1 range, and so we had
            # to store the image ranges.  This is a much simpler resize
            # method that allows us to `preserve_range`.)
            img_copy = resize(img_copy[0],
                              (height, width),
                              order = 3,
                              clip = False,
                              preserve_range = True)[np.newaxis].astype(np.float32)

            # Plot the image
            if (it_i + 1) % plot_step == 0:
                m = net['deprocess'](img_copy[0])

                plt.grid('off')
                plt.imshow(m)
                plt.show()
                
                imgs.append(m)

# Create a GIF
gif.build_gif(imgs, saveto='fractal.gif')


Out[96]:
<matplotlib.animation.ArtistAnimation at 0x7f5aff28ff60>

In [98]:
ipyd.Image(url='fractal.gif?', height=300, width=300)


Out[98]:

Guided Hallucinations

Instead of following the gradient of an arbitrary mean or max of a particular layer's activation, or a particular object that we want to synthesize, we can also try to guide our image to look like another image. One way to try this is to take one image, the guide, and find the features at a particular layer or layers. Then, we take our synthesis image and find the gradient which makes it's own layers activations look like the guide image.


In [126]:
# [Choice] images
guide_og = plt.imread("images/fc/fight_club_12.jpg")[..., :3]
dream_og = plt.imread("images/fc/fight_club_11.jpg")[..., :3]

assert(guide_og.ndim == 3 and guide_og.shape[-1] == 3)
assert(dream_og.ndim == 3 and dream_og.shape[-1] == 3)

Preprocess both images:


In [127]:
guide_img = net['preprocess'](guide_og)[np.newaxis]
dream_img = net['preprocess'](dream_og)[np.newaxis]

fig, axs = plt.subplots(1, 2, figsize=(7, 4))
axs[0].imshow(guide_og)
axs[1].imshow(dream_og)


Out[127]:
<matplotlib.image.AxesImage at 0x7f5afe047470>

Like w/ Style Net, we are going to measure how similar the features in the guide image are to the dream images. In order to do that, we'll calculate the dot product. Experiment with other measures such as l1 or l2 loss to see how this impacts the resulting Dream!


In [140]:
# [Choice] weight of guide_img influence
feature_loss_weight = 0.1

x = g.get_tensor_by_name(names[0] + ":0")

with tf.Session(graph=g) as sess, g.device(device):
    feature_loss = tf.Variable(0.0)
    
    # Explore different layers/subsets of layers.  This is just an example.
    for feature_i in features[3:5]:

        # Get the activation of the feature
        layer = g.get_tensor_by_name(feature_i)
        
        # Do the same for our guide image
        guide_layer = sess.run(layer, feed_dict={x: guide_img})
        
        # Now we need to measure how similar they are!
        # We'll use the dot product, which requires us to first reshape both
        # features to a 2D vector.  But you should experiment with other ways
        # of measuring similarity such as l1 or l2 loss.
        
        # Reshape each layer to 2D vector
        layer = tf.reshape(layer, [-1, 1])
        guide_layer = guide_layer.reshape(-1, 1)
        
        # Now calculate their dot product
        correlation = tf.matmul(guide_layer.T, layer)
        
        # And weight the loss by a factor so we can control its influence
        feature_loss += feature_loss_weight * correlation

We'll now use another measure that we saw when developing Style Net during the lecture. This measure the pixel to pixel difference of neighboring pixels. What we're doing when we try to optimize a gradient that makes the mean differences small is saying, we want the difference to be low. This allows us to smooth our image in the same way that we did using the Gaussian to blur the image.


In [141]:
# [Choice] weight of total variational loss
tv_loss_weight = 0.5
# [Choice] power to push smaller differences closer to 0 and larger differences higher
power = 1.3

n_img, height, width, ch = dream_img.shape

with tf.Session(graph = g) as sess, g.device(device):
    # Penalize variations in neighboring pixels, enforcing smoothness
    dx = tf.square(x[:, :height - 1, :width - 1, :] - x[:, :height - 1, 1:, :])
    dy = tf.square(x[:, :height - 1, :width - 1, :] - x[:, 1:, :width - 1, :])
    
    # calculate their difference raised to a power
    tv_loss = tv_loss_weight * tf.reduce_mean(tf.pow(dx + dy, power))

Now we train just like before, except we'll need to combine our two loss terms, feature_loss and tv_loss by simply adding them! The one thing we have to keep in mind is that we want to minimize the tv_loss while maximizing the feature_loss. That means we'll need to use the negative tv_loss and the positive feature_loss. As an experiment, try just optimizing the tv_loss and removing the feature_loss from the tf.gradients call. What happens?


In [144]:
# [Choice]
n_iterations = 103
plot_step = 3
step = 0.3

imgs = []

with tf.Session(graph=g) as sess, g.device(device):
    # Experiment with just optimizing the tv_loss or negative tv_loss to understand what it is doing!
    gradient = tf.gradients(-tv_loss + feature_loss, x)

    # Copy the input image as we'll add the gradient to it in a loop
    img_copy = dream_img.copy()

    with tf.Session(graph=g) as sess, g.device(device):
        sess.run(tf.initialize_all_variables())
        
        for it_i in range(n_iterations):

            # This will calculate the gradient of the layer we chose with respect to the input image.
            this_res = sess.run(gradient[0], feed_dict={x: img_copy})[0]
            
            # [Choice] Let's normalize it by the maximum activation or standard deviation
            this_res /= (np.max(np.abs(this_res) + 1e-8))
            # this_res /= (np.std(this_res) + 1e-8)

            # Then add the gradient back to the input image
            # Think about what this gradient represents?
            # It says what direction we should move our input
            # in order to meet our objective stored in "gradient"
            img_copy += this_res * step

            # Plot the image
            if it_i == 0 or (it_i + 1) % plot_step == 0:
                m = net['deprocess'](img_copy[0])

                plt.figure(figsize=(5, 5))
                plt.grid('off')
                plt.imshow(m)
                plt.show()
                
                imgs.append(m)

gif.build_gif(imgs, saveto='guided.gif')


Out[144]:
<matplotlib.animation.ArtistAnimation at 0x7f5afcaa4518>

In [145]:
ipyd.Image(url='guided.gif?', height=300, width=300)


Out[145]:

Further Explorations

In the libs module, I've included a deepdream module which has two functions for performing Deep Dream and the Guided Deep Dream. Feel free to explore these to create your own deep dreams.

Part 5 - Style Net

We'll now work on creating our own style net implementation. We've seen all the steps for how to do this during the lecture, and you can always refer to the Lecture Transcript if you need to. I want to you to explore using different networks and different layers in creating your content and style losses. This is completely unexplored territory so it can be frustrating to find things that work. Think of this as your empty canvas! If you are really stuck, you will find a stylenet implementation under the libs module that you can use instead.

Have a look here for inspiration:

https://mtyka.github.io/code/2015/10/02/experiments-with-style-transfer.html

http://kylemcdonald.net/stylestudies/

Network

Let's reset the graph and load up a network. I'll include code here for loading up any of our pretrained networks so you can explore each of them!


In [121]:
#sess.close()
tf.reset_default_graph()

# [Choice] Type of network
net = vgg16.get_vgg_model()
# net = vgg16.get_vgg_face_model()
# net = inception.get_inception_model(version='v5')
# net = inception.get_inception_model(version='v3')
# net = i2v.get_i2v_model()
# net = i2v.get_i2v_tag_model()

In [85]:
nb_utils.show_graph(net['graph_def'])



In [122]:
# Let's explicity use the CPU, since we don't gain anything using the GPU
# when doing Deep Dream (it's only a single image, benefits come w/ many images).
device = '/cpu:0'

# We'll now explicitly create a graph
g = tf.Graph()

Let's now import the graph definition into our newly created Graph using a context manager and specifying that we want to use the CPU.


In [123]:
# And here is a context manager.  We use the python "with" notation to create a context
# and create a session that only exists within this indent,  as soon as we leave it,
# the session is automatically closed!  We also tell the session which graph to use.
# We can pass a second context after the comma,
# which we'll use to be explicit about using the CPU instead of a GPU.
with tf.Session(graph = g) as sess, g.device(device):
    
    # Now load the graph_def, which defines operations and their values into `g`
    tf.import_graph_def(net['graph_def'], name='net')

Let's then grab the names of every operation in our network:


In [124]:
names = [op.name for op in g.get_operations()]
print(names)


['net/images', 'net/mul/y', 'net/mul', 'net/split/split_dim', 'net/split', 'net/sub/y', 'net/sub', 'net/sub_1/y', 'net/sub_1', 'net/sub_2/y', 'net/sub_2', 'net/concat/concat_dim', 'net/concat', 'net/conv1_1/filter', 'net/conv1_1/Conv2D', 'net/conv1_1/biases', 'net/conv1_1/BiasAdd', 'net/conv1_1/conv1_1', 'net/conv1_2/filter', 'net/conv1_2/Conv2D', 'net/conv1_2/biases', 'net/conv1_2/BiasAdd', 'net/conv1_2/conv1_2', 'net/pool1', 'net/conv2_1/filter', 'net/conv2_1/Conv2D', 'net/conv2_1/biases', 'net/conv2_1/BiasAdd', 'net/conv2_1/conv2_1', 'net/conv2_2/filter', 'net/conv2_2/Conv2D', 'net/conv2_2/biases', 'net/conv2_2/BiasAdd', 'net/conv2_2/conv2_2', 'net/pool2', 'net/conv3_1/filter', 'net/conv3_1/Conv2D', 'net/conv3_1/biases', 'net/conv3_1/BiasAdd', 'net/conv3_1/conv3_1', 'net/conv3_2/filter', 'net/conv3_2/Conv2D', 'net/conv3_2/biases', 'net/conv3_2/BiasAdd', 'net/conv3_2/conv3_2', 'net/conv3_3/filter', 'net/conv3_3/Conv2D', 'net/conv3_3/biases', 'net/conv3_3/BiasAdd', 'net/conv3_3/conv3_3', 'net/pool3', 'net/conv4_1/filter', 'net/conv4_1/Conv2D', 'net/conv4_1/biases', 'net/conv4_1/BiasAdd', 'net/conv4_1/conv4_1', 'net/conv4_2/filter', 'net/conv4_2/Conv2D', 'net/conv4_2/biases', 'net/conv4_2/BiasAdd', 'net/conv4_2/conv4_2', 'net/conv4_3/filter', 'net/conv4_3/Conv2D', 'net/conv4_3/biases', 'net/conv4_3/BiasAdd', 'net/conv4_3/conv4_3', 'net/pool4', 'net/conv5_1/filter', 'net/conv5_1/Conv2D', 'net/conv5_1/biases', 'net/conv5_1/BiasAdd', 'net/conv5_1/conv5_1', 'net/conv5_2/filter', 'net/conv5_2/Conv2D', 'net/conv5_2/biases', 'net/conv5_2/BiasAdd', 'net/conv5_2/conv5_2', 'net/conv5_3/filter', 'net/conv5_3/Conv2D', 'net/conv5_3/biases', 'net/conv5_3/BiasAdd', 'net/conv5_3/conv5_3', 'net/pool5', 'net/Reshape/shape', 'net/Reshape', 'net/Const', 'net/Const_1', 'net/MatMul', 'net/fc6', 'net/fc6_relu', 'net/dropout/Shape', 'net/dropout/random_uniform/min', 'net/dropout/random_uniform/range', 'net/dropout/random_uniform/RandomUniform', 'net/dropout/random_uniform/mul', 'net/dropout/random_uniform', 'net/dropout/add/x', 'net/dropout/add', 'net/dropout/Floor', 'net/dropout/mul/y', 'net/dropout/mul', 'net/dropout/mul_1', 'net/Reshape_1/shape', 'net/Reshape_1', 'net/Const_2', 'net/Const_3', 'net/MatMul_1', 'net/fc7', 'net/fc7_relu', 'net/dropout_1/Shape', 'net/dropout_1/random_uniform/min', 'net/dropout_1/random_uniform/range', 'net/dropout_1/random_uniform/RandomUniform', 'net/dropout_1/random_uniform/mul', 'net/dropout_1/random_uniform', 'net/dropout_1/add/x', 'net/dropout_1/add', 'net/dropout_1/Floor', 'net/dropout_1/mul/y', 'net/dropout_1/mul', 'net/dropout_1/mul_1', 'net/Reshape_2/shape', 'net/Reshape_2', 'net/Const_4', 'net/Const_5', 'net/MatMul_2', 'net/fc8', 'net/prob', 'net/init']

Now we need an image for our content image and another one for our style image.


In [125]:
content_og = plt.imread('images/fc/fight_club_17.jpg')[..., :3]
style_og = plt.imread('images/style/style_01.jpg')[..., :3]

fig, axs = plt.subplots(1, 2)
axs[0].imshow(content_og)
axs[0].set_title('Content Image')
axs[0].grid('off')
axs[1].imshow(style_og)
axs[1].set_title('Style Image')
axs[1].grid('off')

# We'll save these with a specific name to include in your submission
plt.imsave(arr=content_og, fname='content.png')
plt.imsave(arr=style_og, fname='style.png')



In [126]:
content_img = net['preprocess'](content_og)[np.newaxis]
print(content_img.shape)
style_img = net['preprocess'](style_og)[np.newaxis]
print(style_img.shape)


(1, 224, 224, 3)
(1, 224, 224, 3)

Let's see what the network classifies these images as just for fun:


In [127]:
# Grab the tensor defining the input to the network
x = g.get_tensor_by_name(names[0] + ":0")

# And grab the tensor defining the softmax layer of the network
softmax = g.get_tensor_by_name(names[-2] + ":0")

for img in [content_img, style_img]:
    with tf.Session(graph=g) as sess, g.device('/cpu:0'):
        # Remember from the lecture that we have to set the dropout
        # "keep probability" to 1.0.
        res = softmax.eval(feed_dict={x: img,
                    'net/dropout_1/random_uniform:0': [[1.0]],
                    'net/dropout/random_uniform:0': [[1.0]]})[0]
        print([(res[idx], net['labels'][idx])
               for idx in res.argsort()[-5:][::-1]])


[(0.97554421, (999, 'n15075141 toilet tissue, toilet paper, bathroom tissue')), (0.02375754, (700, 'n03887697 paper towel')), (0.00064870232, (643, 'n03724870 mask')), (3.8189886e-05, (861, 'n04447861 toilet seat')), (9.7551756e-06, (796, 'n04229816 ski mask'))]
[(1.0, (643, 'n03724870 mask')), (1.1716333e-09, (641, 'n03720891 maraca')), (1.0510269e-09, (796, 'n04229816 ski mask')), (8.2470719e-10, (711, 'n03916031 perfume, essence')), (7.5598017e-10, (300, 'n02165105 tiger beetle'))]

Content Features

We're going to need to find the layer or layers we want to use to help us define our "content loss". Recall from the lecture when we used VGG, we used the 4th convolutional layer.


In [128]:
print(names)


['net/images', 'net/mul/y', 'net/mul', 'net/split/split_dim', 'net/split', 'net/sub/y', 'net/sub', 'net/sub_1/y', 'net/sub_1', 'net/sub_2/y', 'net/sub_2', 'net/concat/concat_dim', 'net/concat', 'net/conv1_1/filter', 'net/conv1_1/Conv2D', 'net/conv1_1/biases', 'net/conv1_1/BiasAdd', 'net/conv1_1/conv1_1', 'net/conv1_2/filter', 'net/conv1_2/Conv2D', 'net/conv1_2/biases', 'net/conv1_2/BiasAdd', 'net/conv1_2/conv1_2', 'net/pool1', 'net/conv2_1/filter', 'net/conv2_1/Conv2D', 'net/conv2_1/biases', 'net/conv2_1/BiasAdd', 'net/conv2_1/conv2_1', 'net/conv2_2/filter', 'net/conv2_2/Conv2D', 'net/conv2_2/biases', 'net/conv2_2/BiasAdd', 'net/conv2_2/conv2_2', 'net/pool2', 'net/conv3_1/filter', 'net/conv3_1/Conv2D', 'net/conv3_1/biases', 'net/conv3_1/BiasAdd', 'net/conv3_1/conv3_1', 'net/conv3_2/filter', 'net/conv3_2/Conv2D', 'net/conv3_2/biases', 'net/conv3_2/BiasAdd', 'net/conv3_2/conv3_2', 'net/conv3_3/filter', 'net/conv3_3/Conv2D', 'net/conv3_3/biases', 'net/conv3_3/BiasAdd', 'net/conv3_3/conv3_3', 'net/pool3', 'net/conv4_1/filter', 'net/conv4_1/Conv2D', 'net/conv4_1/biases', 'net/conv4_1/BiasAdd', 'net/conv4_1/conv4_1', 'net/conv4_2/filter', 'net/conv4_2/Conv2D', 'net/conv4_2/biases', 'net/conv4_2/BiasAdd', 'net/conv4_2/conv4_2', 'net/conv4_3/filter', 'net/conv4_3/Conv2D', 'net/conv4_3/biases', 'net/conv4_3/BiasAdd', 'net/conv4_3/conv4_3', 'net/pool4', 'net/conv5_1/filter', 'net/conv5_1/Conv2D', 'net/conv5_1/biases', 'net/conv5_1/BiasAdd', 'net/conv5_1/conv5_1', 'net/conv5_2/filter', 'net/conv5_2/Conv2D', 'net/conv5_2/biases', 'net/conv5_2/BiasAdd', 'net/conv5_2/conv5_2', 'net/conv5_3/filter', 'net/conv5_3/Conv2D', 'net/conv5_3/biases', 'net/conv5_3/BiasAdd', 'net/conv5_3/conv5_3', 'net/pool5', 'net/Reshape/shape', 'net/Reshape', 'net/Const', 'net/Const_1', 'net/MatMul', 'net/fc6', 'net/fc6_relu', 'net/dropout/Shape', 'net/dropout/random_uniform/min', 'net/dropout/random_uniform/range', 'net/dropout/random_uniform/RandomUniform', 'net/dropout/random_uniform/mul', 'net/dropout/random_uniform', 'net/dropout/add/x', 'net/dropout/add', 'net/dropout/Floor', 'net/dropout/mul/y', 'net/dropout/mul', 'net/dropout/mul_1', 'net/Reshape_1/shape', 'net/Reshape_1', 'net/Const_2', 'net/Const_3', 'net/MatMul_1', 'net/fc7', 'net/fc7_relu', 'net/dropout_1/Shape', 'net/dropout_1/random_uniform/min', 'net/dropout_1/random_uniform/range', 'net/dropout_1/random_uniform/RandomUniform', 'net/dropout_1/random_uniform/mul', 'net/dropout_1/random_uniform', 'net/dropout_1/add/x', 'net/dropout_1/add', 'net/dropout_1/Floor', 'net/dropout_1/mul/y', 'net/dropout_1/mul', 'net/dropout_1/mul_1', 'net/Reshape_2/shape', 'net/Reshape_2', 'net/Const_4', 'net/Const_5', 'net/MatMul_2', 'net/fc8', 'net/prob', 'net/init']

Pick a layer for using for the content features. If you aren't using VGG remember to get rid of the dropout stuff!


In [129]:
# [Choice] layers
# Might need to change this if you use another network!
content_layer = 'net/conv3_2/conv3_2:0'

with tf.Session(graph=g) as sess, g.device('/cpu:0'):
    content_features = g.get_tensor_by_name(content_layer).eval(
            session=sess,
            feed_dict={x: content_img,
                'net/dropout_1/random_uniform:0': [[1.0]],
                'net/dropout/random_uniform:0': [[1.0]]})

Style Features

Let's do the same thing now for the style features. We'll use more than 1 layer though so we'll append all the features in a list. If you aren't using VGG remember to get rid of the dropout stuff!


In [130]:
# [Choice] layers
# Might need to change this if you use another network!
style_layers = ['net/conv1_1/conv1_1:0',
                'net/conv2_1/conv2_1:0',
                'net/conv3_1/conv3_1:0',
                'net/conv4_1/conv4_1:0',
                'net/conv5_1/conv5_1:0']
style_activations = []

with tf.Session(graph=g) as sess, g.device('/cpu:0'):
    for style_i in style_layers:
        style_activation_i = g.get_tensor_by_name(style_i).eval(
            feed_dict={x: style_img,
                    'net/dropout_1/random_uniform:0': [[1.0]],
                    'net/dropout/random_uniform:0': [[1.0]]})
        style_activations.append(style_activation_i)

Now we find the gram matrix which we'll use to optimize our features.


In [131]:
style_features = []
for style_activation_i in style_activations:
    s_i = np.reshape(style_activation_i, [-1, style_activation_i.shape[-1]])
    gram_matrix = np.matmul(s_i.T, s_i) / s_i.size
    style_features.append(gram_matrix.astype(np.float32))

Remapping the Input

We're almost done building our network. We just have to change the input to the network to become "trainable". Instead of a placeholder, we'll have a tf.Variable, which allows it to be trained. We could set this to the content image, another image entirely, or an image of noise. Experiment with all three options!


In [132]:
tf.reset_default_graph()
g = tf.Graph()

# Get the network again
net = vgg16.get_vgg_model()

# Load up a session which we'll use to import the graph into.
with tf.Session(graph=g) as sess, g.device('/cpu:0'):
    # [Choice] content image / another image / image of noise
    #net_input = tf.Variable(content_img / 255.0)
    net_input = tf.get_variable(
       name = 'input',
       shape = content_img.shape,
       dtype = tf.float32,
       initializer = tf.random_normal_initializer(
            mean = np.mean(content_img), 
            stddev = np.std(content_img)))
    
    # Now we load the network again, but this time replacing our placeholder
    # with the trainable tf.Variable
    tf.import_graph_def(
        net['graph_def'],
        name = 'net',
        input_map = {'images:0': net_input})

Content Loss

In the lecture we saw that we'll simply find the l2 loss between our content layer features.


In [133]:
with tf.Session(graph=g) as sess, g.device('/cpu:0'):
    content_loss = tf.nn.l2_loss((g.get_tensor_by_name(content_layer) -
                                 content_features) /
                                 content_features.size)

Style Loss

Instead of straight l2 loss on the raw feature activations, we're going to calculate the gram matrix and find the loss between these. Intuitively, this is finding what is common across all convolution filters, and trying to enforce the commonality between the synthesis and style image's gram matrix.


In [134]:
with tf.Session(graph=g) as sess, g.device('/cpu:0'):
    style_loss = np.float32(0.0)
    for style_layer_i, style_gram_i in zip(style_layers, style_features):
        layer_i = g.get_tensor_by_name(style_layer_i)
        layer_shape = layer_i.get_shape().as_list()
        layer_size = layer_shape[1] * layer_shape[2] * layer_shape[3]
        layer_flat = tf.reshape(layer_i, [-1, layer_shape[3]])
        gram_matrix = tf.matmul(tf.transpose(layer_flat), layer_flat) / layer_size
        style_loss = tf.add(style_loss, tf.nn.l2_loss((gram_matrix - style_gram_i) / np.float32(style_gram_i.size)))

Total Variation Loss

And just like w/ guided hallucinations, we'll try to enforce some smoothness using a total variation loss.


In [135]:
def total_variation_loss(x):
    h, w = x.get_shape().as_list()[1], x.get_shape().as_list()[1]
    dx = tf.square(x[:, :h-1, :w-1, :] - x[:, :h-1, 1:, :])
    dy = tf.square(x[:, :h-1, :w-1, :] - x[:, 1:, :w-1, :])
    return tf.reduce_sum(tf.pow(dx + dy, 1.25))

with tf.Session(graph=g) as sess, g.device('/cpu:0'):
    tv_loss = total_variation_loss(net_input)

Training

We're almost ready to train! Let's just combine our three loss measures and stick it in an optimizer.


In [136]:
with tf.Session(graph = g) as sess, g.device('/cpu:0'):
    # [Choice] Weights
    loss = 15.0 * content_loss + 1.0 * style_loss + 0.001 * tv_loss
    optimizer = tf.train.AdamOptimizer(0.05).minimize(loss)

And now iterate! Feel free to play with the number of iterations or how often you save an image. If you use a different network to VGG, then you will not need to feed in the dropout parameters like I've done here.


In [137]:
imgs = []
# [Choice] number of iteration
n_iterations = 250

with tf.Session(graph=g) as sess, g.device('/cpu:0'):
    sess.run(tf.initialize_all_variables())

    # map input to noise
    og_img = net_input.eval()
    
    for it_i in range(n_iterations):
        _, this_loss, synth = sess.run([optimizer, loss, net_input], feed_dict={
                    'net/dropout_1/random_uniform:0': np.ones(
                        g.get_tensor_by_name(
                            'net/dropout_1/random_uniform:0'
                        ).get_shape().as_list()),
                    'net/dropout/random_uniform:0': np.ones(
                        g.get_tensor_by_name(
                            'net/dropout/random_uniform:0'
                        ).get_shape().as_list())
                })
        print("%d: %f, (%f - %f)" %
            (it_i, this_loss, np.min(synth), np.max(synth)))
        if it_i % 5 == 0:
            m = vgg16.deprocess(synth[0])
            imgs.append(m)
            plt.imshow(m)
            plt.show()
    gif.build_gif(imgs, saveto='stylenet.gif')


0: 156.792542, (-1.316509 - 2.491663)
1: 111.043716, (-1.268750 - 2.442991)
2: 91.571808, (-1.224744 - 2.396919)
3: 78.609451, (-1.184996 - 2.354313)
4: 67.184441, (-1.149001 - 2.315151)
5: 56.723221, (-1.115653 - 2.278761)
6: 47.847267, (-1.083349 - 2.243556)
7: 41.272861, (-1.050375 - 2.207840)
8: 36.931427, (-1.015527 - 2.170556)
9: 33.696323, (-0.978438 - 2.131452)
10: 30.496399, (-0.939557 - 2.090867)
11: 27.237314, (-0.899747 - 2.049501)
12: 24.405750, (-0.860115 - 2.008330)
13: 22.358839, (-0.821926 - 1.968459)
14: 21.040550, (-0.786391 - 1.930877)
15: 20.137854, (-0.754296 - 1.896253)
16: 19.354292, (-0.725717 - 1.864756)
17: 18.560230, (-0.700556 - 1.836162)
18: 17.796368, (-0.678009 - 1.809858)
19: 17.170284, (-0.657019 - 1.785055)
20: 16.726482, (-0.636585 - 1.760950)
21: 16.378490, (-0.616198 - 1.736950)
22: 15.979147, (-0.595569 - 1.712910)
23: 15.464283, (-0.574916 - 1.689074)
24: 14.899635, (-0.554825 - 1.665906)
25: 14.393991, (-0.536068 - 1.644098)
26: 13.989502, (-0.519294 - 1.624197)
27: 13.648487, (-0.528674 - 1.606624)
28: 13.312626, (-0.552758 - 1.591300)
29: 12.962496, (-0.579134 - 1.600772)
30: 12.626600, (-0.605485 - 1.614232)
31: 12.343929, (-0.629035 - 1.626332)
32: 12.118055, (-0.647085 - 1.636140)
33: 11.907416, (-0.658410 - 1.643098)
34: 11.673027, (-0.663539 - 1.647441)
35: 11.420002, (-0.664455 - 1.649686)
36: 11.183064, (-0.663510 - 1.650640)
37: 10.982433, (-0.662823 - 1.651129)
38: 10.807712, (-0.663680 - 1.651599)
39: 10.637205, (-0.666605 - 1.653998)
40: 10.464108, (-0.670962 - 1.666021)
41: 10.301688, (-0.675003 - 1.678260)
42: 10.162621, (-0.677182 - 1.689512)
43: 10.042105, (-0.676386 - 1.698906)
44: 9.922893, (-0.672696 - 1.706151)
45: 9.797144, (-0.666864 - 1.711598)
46: 9.673410, (-0.660326 - 1.716236)
47: 9.562863, (-0.654727 - 1.721031)
48: 9.464588, (-0.651231 - 1.726589)
49: 9.369534, (-0.650294 - 1.733142)
50: 9.273851, (-0.651373 - 1.740260)
51: 9.182551, (-0.653196 - 1.746674)
52: 9.100834, (-0.654281 - 1.751514)
53: 9.026162, (-0.653812 - 1.754400)
54: 8.951574, (-0.651441 - 1.755301)
55: 8.874990, (-0.647686 - 1.754916)
56: 8.801119, (-0.643494 - 1.753777)
57: 8.733486, (-0.639778 - 1.752573)
58: 8.670052, (-0.641396 - 1.751631)
59: 8.607296, (-0.650545 - 1.750913)
60: 8.545332, (-0.659100 - 1.750004)
61: 8.487274, (-0.666029 - 1.748438)
62: 8.433795, (-0.670545 - 1.745749)
63: 8.381868, (-0.672594 - 1.741585)
64: 8.329849, (-0.673060 - 1.736468)
65: 8.278658, (-0.673201 - 1.730840)
66: 8.230321, (-0.674132 - 1.724929)
67: 8.184649, (-0.676246 - 1.719109)
68: 8.139877, (-0.679645 - 1.713451)
69: 8.096252, (-0.683931 - 1.707689)
70: 8.054802, (-0.688066 - 1.701481)
71: 8.015404, (-0.691167 - 1.694508)
72: 7.976768, (-0.692785 - 1.686432)
73: 7.938099, (-0.693250 - 1.677580)
74: 7.899985, (-0.693044 - 1.668711)
75: 7.863548, (-0.692731 - 1.660229)
76: 7.828439, (-0.692757 - 1.652323)
77: 7.794102, (-0.692840 - 1.644526)
78: 7.760780, (-0.692610 - 1.640730)
79: 7.728799, (-0.691692 - 1.652158)
80: 7.697851, (-0.689965 - 1.659983)
81: 7.667561, (-0.687512 - 1.664168)
82: 7.637729, (-0.684687 - 1.665435)
83: 7.608860, (-0.681825 - 1.664781)
84: 7.580891, (-0.679390 - 1.666523)
85: 7.553667, (-0.677467 - 1.669840)
86: 7.526994, (-0.675805 - 1.672229)
87: 7.501035, (-0.674187 - 1.673707)
88: 7.475830, (-0.672330 - 1.674295)
89: 7.451232, (-0.670323 - 1.674141)
90: 7.427064, (-0.668158 - 1.673590)
91: 7.403388, (-0.666011 - 1.672909)
92: 7.380423, (-0.664012 - 1.672610)
93: 7.358083, (-0.662420 - 1.672764)
94: 7.336112, (-0.661372 - 1.673207)
95: 7.314591, (-0.660689 - 1.673537)
96: 7.293578, (-0.660007 - 1.673653)
97: 7.273074, (-0.659025 - 1.673340)
98: 7.253045, (-0.657647 - 1.672737)
99: 7.233479, (-0.656141 - 1.671724)
100: 7.214337, (-0.654565 - 1.670660)
101: 7.195662, (-0.652798 - 1.669562)
102: 7.177430, (-0.650678 - 1.668551)
103: 7.159562, (-0.648219 - 1.667549)
104: 7.142110, (-0.645484 - 1.666517)
105: 7.124971, (-0.642540 - 1.664938)
106: 7.108055, (-0.639678 - 1.663080)
107: 7.091482, (-0.636886 - 1.660932)
108: 7.075196, (-0.634306 - 1.658624)
109: 7.059259, (-0.631875 - 1.656372)
110: 7.043536, (-0.629597 - 1.654573)
111: 7.028075, (-0.627405 - 1.652954)
112: 7.012921, (-0.625311 - 1.651113)
113: 6.998045, (-0.623344 - 1.649131)
114: 6.983451, (-0.621514 - 1.647275)
115: 6.969064, (-0.619797 - 1.645879)
116: 6.954841, (-0.618082 - 1.645121)
117: 6.940748, (-0.616164 - 1.645144)
118: 6.926835, (-0.613858 - 1.645509)
119: 6.913157, (-0.611204 - 1.645809)
120: 6.899754, (-0.611197 - 1.646169)
121: 6.886560, (-0.610983 - 1.646492)
122: 6.873607, (-0.610666 - 1.646698)
123: 6.860900, (-0.610285 - 1.647136)
124: 6.848434, (-0.609999 - 1.647733)
125: 6.836102, (-0.609643 - 1.648233)
126: 6.823932, (-0.609267 - 1.648553)
127: 6.811934, (-0.608692 - 1.648395)
128: 6.800075, (-0.608002 - 1.647831)
129: 6.788353, (-0.607152 - 1.647100)
130: 6.776702, (-0.606246 - 1.646194)
131: 6.765126, (-0.605239 - 1.645051)
132: 6.753679, (-0.603973 - 1.643564)
133: 6.742386, (-0.602431 - 1.641880)
134: 6.731241, (-0.600798 - 1.639364)
135: 6.720266, (-0.599252 - 1.636472)
136: 6.709387, (-0.597935 - 1.633306)
137: 6.698559, (-0.597197 - 1.629921)
138: 6.687909, (-0.597719 - 1.626364)
139: 6.677361, (-0.599560 - 1.623283)
140: 6.666950, (-0.601354 - 1.620241)
141: 6.656682, (-0.603131 - 1.616893)
142: 6.646504, (-0.604906 - 1.613362)
143: 6.636460, (-0.606635 - 1.611695)
144: 6.626556, (-0.608275 - 1.610541)
145: 6.616782, (-0.609982 - 1.614440)
146: 6.607125, (-0.611821 - 1.623313)
147: 6.597635, (-0.613705 - 1.632134)
148: 6.588287, (-0.615806 - 1.640859)
149: 6.579076, (-0.617928 - 1.649436)
150: 6.569971, (-0.620177 - 1.657814)
151: 6.560927, (-0.622404 - 1.665837)
152: 6.551948, (-0.624572 - 1.673526)
153: 6.543040, (-0.626726 - 1.680753)
154: 6.534198, (-0.628746 - 1.687298)
155: 6.525476, (-0.630730 - 1.693341)
156: 6.516806, (-0.632214 - 1.698859)
157: 6.508181, (-0.633110 - 1.703894)
158: 6.499647, (-0.633696 - 1.708708)
159: 6.491196, (-0.634077 - 1.713384)
160: 6.482807, (-0.634215 - 1.717997)
161: 6.474515, (-0.633760 - 1.722454)
162: 6.466354, (-0.632626 - 1.726673)
163: 6.458239, (-0.631264 - 1.730638)
164: 6.450220, (-0.629690 - 1.734460)
165: 6.442389, (-0.628175 - 1.738065)
166: 6.434639, (-0.627035 - 1.741471)
167: 6.426914, (-0.626095 - 1.744683)
168: 6.419251, (-0.625394 - 1.747701)
169: 6.411613, (-0.625248 - 1.750527)
170: 6.404049, (-0.625484 - 1.753175)
171: 6.396626, (-0.626167 - 1.755615)
172: 6.389297, (-0.628915 - 1.757752)
173: 6.382043, (-0.636115 - 1.759567)
174: 6.374825, (-0.642627 - 1.760965)
175: 6.367714, (-0.648347 - 1.761867)
176: 6.360682, (-0.653356 - 1.762321)
177: 6.353734, (-0.657377 - 1.762497)
178: 6.346945, (-0.660473 - 1.762375)
179: 6.340212, (-0.662569 - 1.761913)
180: 6.333534, (-0.663642 - 1.761142)
181: 6.326936, (-0.663900 - 1.760044)
182: 6.320397, (-0.663574 - 1.758660)
183: 6.313898, (-0.662826 - 1.757032)
184: 6.307429, (-0.664234 - 1.755251)
185: 6.300983, (-0.666169 - 1.753366)
186: 6.294559, (-0.667665 - 1.751478)
187: 6.288185, (-0.668300 - 1.749680)
188: 6.281840, (-0.668149 - 1.747938)
189: 6.275545, (-0.667737 - 1.746239)
190: 6.269301, (-0.666945 - 1.744727)
191: 6.263068, (-0.665917 - 1.743430)
192: 6.256886, (-0.666607 - 1.742270)
193: 6.250738, (-0.666699 - 1.741154)
194: 6.244601, (-0.666300 - 1.740191)
195: 6.238572, (-0.665736 - 1.739426)
196: 6.232606, (-0.665052 - 1.738635)
197: 6.226615, (-0.664425 - 1.737838)
198: 6.220607, (-0.663942 - 1.737042)
199: 6.214659, (-0.663304 - 1.736510)
200: 6.208743, (-0.662149 - 1.736147)
201: 6.202900, (-0.660550 - 1.735872)
202: 6.197118, (-0.658341 - 1.735522)
203: 6.191386, (-0.655541 - 1.735127)
204: 6.185691, (-0.652627 - 1.734717)
205: 6.179978, (-0.649747 - 1.734339)
206: 6.174309, (-0.647086 - 1.733726)
207: 6.168667, (-0.644729 - 1.733218)
208: 6.163065, (-0.642734 - 1.732811)
209: 6.157502, (-0.641586 - 1.732465)
210: 6.151979, (-0.641288 - 1.732288)
211: 6.146487, (-0.641380 - 1.732291)
212: 6.141041, (-0.641868 - 1.732536)
213: 6.135654, (-0.642285 - 1.733189)
214: 6.130290, (-0.642529 - 1.734140)
215: 6.124989, (-0.642454 - 1.735347)
216: 6.119745, (-0.641939 - 1.736541)
217: 6.114574, (-0.641055 - 1.737730)
218: 6.109398, (-0.639755 - 1.738873)
219: 6.104250, (-0.638276 - 1.740005)
220: 6.099126, (-0.637001 - 1.741119)
221: 6.094021, (-0.637196 - 1.741971)
222: 6.088992, (-0.639491 - 1.742567)
223: 6.083959, (-0.641991 - 1.742875)
224: 6.078922, (-0.643750 - 1.742896)
225: 6.073916, (-0.645055 - 1.742614)
226: 6.068918, (-0.645982 - 1.742153)
227: 6.063921, (-0.646650 - 1.741700)
228: 6.058934, (-0.647203 - 1.741346)
229: 6.053982, (-0.648709 - 1.741119)
230: 6.049063, (-0.650723 - 1.741004)
231: 6.044188, (-0.653008 - 1.740786)
232: 6.039390, (-0.654957 - 1.740261)
233: 6.034640, (-0.656720 - 1.739637)
234: 6.029911, (-0.658478 - 1.738934)
235: 6.025257, (-0.659881 - 1.738136)
236: 6.020615, (-0.660230 - 1.737230)
237: 6.016012, (-0.659286 - 1.736276)
238: 6.011496, (-0.657806 - 1.735233)
239: 6.007030, (-0.656218 - 1.734150)
240: 6.002611, (-0.654393 - 1.732880)
241: 5.998187, (-0.652642 - 1.731483)
242: 5.993806, (-0.650895 - 1.729960)
243: 5.989461, (-0.649248 - 1.728369)
244: 5.985138, (-0.648034 - 1.726846)
245: 5.980844, (-0.647530 - 1.725384)
246: 5.976606, (-0.647488 - 1.723980)
247: 5.972373, (-0.646860 - 1.722591)
248: 5.968177, (-0.645691 - 1.720939)
249: 5.964003, (-0.644297 - 1.719098)

In [138]:
ipyd.Image(url='stylenet.gif', height=300, width=300)


Out[138]:

Assignment Submission

After you've completed the notebook, create a zip file of the current directory using the code below. This code will make sure you have included this completed ipython notebook and the following files named exactly as:

    session-4/
      session-4.ipynb
      softmax.gif
      fractal.gif
      guided.gif
      content.png
      style.png
      stylenet.gif

You'll then submit this zip file for your third assignment on Kadenze for "Assignment 4: Deep Dream and Style Net"! Remember to complete the rest of the assignment, gallery commenting on your peers work, to receive full credit! If you have any questions, remember to reach out on the forums and connect with your peers or with me.

To get assessed, you'll need to be a premium student! This will allow you to build an online portfolio of all of your work and receive grades. If you aren't already enrolled as a student, register now at http://www.kadenze.com/ and join the #CADL community to see what your peers are doing! https://www.kadenze.com/courses/creative-applications-of-deep-learning-with-tensorflow/info

Also, if you share any of the GIFs on Facebook/Twitter/Instagram/etc..., be sure to use the #CADL hashtag so that other students can find your work!


In [139]:
utils.build_submission('session-4.zip',
                       ('softmax.gif',
                        'fractal.gif',
                        'guided.gif',
                        'content.png',
                        'style.png',
                        'stylenet.gif',
                        'session-4.ipynb'))


Your assignment zip file has been created!
Now submit the file:
/notebooks/session-4/session-4.zip
to Kadenze for grading!