|
| 1 | +######################################################################## |
| 2 | +# |
| 3 | +# The Inception Model 5h for TensorFlow. |
| 4 | +# |
| 5 | +# This variant of the Inception model is easier to use for DeepDream |
| 6 | +# and other imaging techniques. This is because it allows the input |
| 7 | +# image to be any size, and the optimized images are also prettier. |
| 8 | +# |
| 9 | +# It is unclear which Inception model this implements because the |
| 10 | +# Google developers have (as usual) neglected to document it. |
| 11 | +# It is dubbed the 5h-model because that is the name of the zip-file, |
| 12 | +# but it is apparently simpler than the v.3 model. |
| 13 | +# |
| 14 | +# See the Python Notebook for Tutorial #14 for an example usage. |
| 15 | +# |
| 16 | +# Implemented in Python 3.5 with TensorFlow v0.11.0rc0 |
| 17 | +# |
| 18 | +######################################################################## |
| 19 | +# |
| 20 | +# This file is part of the TensorFlow Tutorials available at: |
| 21 | +# |
| 22 | +# https://github.com/Hvass-Labs/TensorFlow-Tutorials |
| 23 | +# |
| 24 | +# Published under the MIT License. See the file LICENSE for details. |
| 25 | +# |
| 26 | +# Copyright 2016 by Magnus Erik Hvass Pedersen |
| 27 | +# |
| 28 | +######################################################################## |
| 29 | + |
| 30 | +import numpy as np |
| 31 | +import tensorflow as tf |
| 32 | +import download |
| 33 | +import os |
| 34 | + |
| 35 | +######################################################################## |
| 36 | +# Various directories and file-names. |
| 37 | + |
| 38 | +# Internet URL for the tar-file with the Inception model. |
| 39 | +# Note that this might change in the future and will need to be updated. |
| 40 | +data_url = "http://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip" |
| 41 | + |
| 42 | +# Directory to store the downloaded data. |
| 43 | +data_dir = "inception/5h/" |
| 44 | + |
| 45 | +# File containing the TensorFlow graph definition. (Downloaded) |
| 46 | +path_graph_def = "tensorflow_inception_graph.pb" |
| 47 | + |
| 48 | +######################################################################## |
| 49 | + |
| 50 | + |
| 51 | +def maybe_download(): |
| 52 | + """ |
| 53 | + Download the Inception model from the internet if it does not already |
| 54 | + exist in the data_dir. The file is about 50 MB. |
| 55 | + """ |
| 56 | + |
| 57 | + print("Downloading Inception 5h Model ...") |
| 58 | + download.maybe_download_and_extract(url=data_url, download_dir=data_dir) |
| 59 | + |
| 60 | + |
| 61 | +######################################################################## |
| 62 | + |
| 63 | + |
| 64 | +class Inception5h: |
| 65 | + """ |
| 66 | + The Inception model is a Deep Neural Network which has already been |
| 67 | + trained for classifying images into 1000 different categories. |
| 68 | +
|
| 69 | + When you create a new instance of this class, the Inception model |
| 70 | + will be loaded and can be used immediately without training. |
| 71 | + """ |
| 72 | + |
| 73 | + # Name of the tensor for feeding the input image. |
| 74 | + tensor_name_input_image = "input:0" |
| 75 | + |
| 76 | + # Names for some of the commonly used layers in the Inception model. |
| 77 | + layer_names = ['conv2d0', 'conv2d1', 'conv2d2', |
| 78 | + 'mixed3a', 'mixed3b', |
| 79 | + 'mixed4a', 'mixed4b', 'mixed4c', 'mixed4d', 'mixed4e', |
| 80 | + 'mixed5a', 'mixed5b'] |
| 81 | + |
| 82 | + def __init__(self): |
| 83 | + # Now load the Inception model from file. The way TensorFlow |
| 84 | + # does this is confusing and requires several steps. |
| 85 | + |
| 86 | + # Create a new TensorFlow computational graph. |
| 87 | + self.graph = tf.Graph() |
| 88 | + |
| 89 | + # Set the new graph as the default. |
| 90 | + with self.graph.as_default(): |
| 91 | + |
| 92 | + # TensorFlow graphs are saved to disk as so-called Protocol Buffers |
| 93 | + # aka. proto-bufs which is a file-format that works on multiple |
| 94 | + # platforms. In this case it is saved as a binary file. |
| 95 | + |
| 96 | + # Open the graph-def file for binary reading. |
| 97 | + path = os.path.join(data_dir, path_graph_def) |
| 98 | + with tf.gfile.FastGFile(path, 'rb') as file: |
| 99 | + # The graph-def is a saved copy of a TensorFlow graph. |
| 100 | + # First we need to create an empty graph-def. |
| 101 | + graph_def = tf.GraphDef() |
| 102 | + |
| 103 | + # Then we load the proto-buf file into the graph-def. |
| 104 | + graph_def.ParseFromString(file.read()) |
| 105 | + |
| 106 | + # Finally we import the graph-def to the default TensorFlow graph. |
| 107 | + tf.import_graph_def(graph_def, name='') |
| 108 | + |
| 109 | + # Now self.graph holds the Inception model from the proto-buf file. |
| 110 | + |
| 111 | + # Get a reference to the tensor for inputting images to the graph. |
| 112 | + self.input = self.graph.get_tensor_by_name(self.tensor_name_input_image) |
| 113 | + |
| 114 | + # Get references to the tensors for the commonly used layers. |
| 115 | + self.layer_tensors = [self.graph.get_tensor_by_name(name + ":0") for name in self.layer_names] |
| 116 | + |
| 117 | + def create_feed_dict(self, image=None): |
| 118 | + """ |
| 119 | + Create and return a feed-dict with an image. |
| 120 | +
|
| 121 | + :param image: |
| 122 | + The input image is a 3-dim array which is already decoded. |
| 123 | + The pixels MUST be values between 0 and 255 (float or int). |
| 124 | +
|
| 125 | + :return: |
| 126 | + Dict for feeding to the Inception graph in TensorFlow. |
| 127 | + """ |
| 128 | + |
| 129 | + # Expand 3-dim array to 4-dim by prepending an 'empty' dimension. |
| 130 | + # This is because we are only feeding a single image, but the |
| 131 | + # Inception model was built to take multiple images as input. |
| 132 | + image = np.expand_dims(image, axis=0) |
| 133 | + |
| 134 | + # Image is passed in as a 3-dim array of raw pixel-values. |
| 135 | + feed_dict = {self.tensor_name_input_image: image} |
| 136 | + |
| 137 | + return feed_dict |
| 138 | + |
| 139 | + def get_gradient(self, tensor): |
| 140 | + """ |
| 141 | + Get the gradient of the given tensor with respect to |
| 142 | + the input image. This allows us to modify the input |
| 143 | + image so as to maximize the given tensor. |
| 144 | +
|
| 145 | + For use in e.g. DeepDream and Visual Analysis. |
| 146 | +
|
| 147 | + :param tensor: |
| 148 | + The tensor whose value we want to maximize |
| 149 | + by changing the input image. |
| 150 | +
|
| 151 | + :return: |
| 152 | + Gradient for the tensor with regard to the input image. |
| 153 | + """ |
| 154 | + |
| 155 | + # Set the graph as default so we can add operations to it. |
| 156 | + with self.graph.as_default(): |
| 157 | + # Square the tensor-values. |
| 158 | + # You can try and remove this to see the effect. |
| 159 | + tensor = tf.square(tensor) |
| 160 | + |
| 161 | + # Average the tensor so we get a single scalar value. |
| 162 | + tensor_mean = tf.reduce_mean(tensor) |
| 163 | + |
| 164 | + # Use TensorFlow to automatically create a mathematical |
| 165 | + # formula for the gradient using the chain-rule of |
| 166 | + # differentiation. |
| 167 | + gradient = tf.gradients(tensor_mean, self.input)[0] |
| 168 | + |
| 169 | + return gradient |
| 170 | + |
| 171 | +######################################################################## |
0 commit comments