📚 The CoCalc Library - books, templates and other resources
cocalc-examples / stanford-tensorflow-tutorials / 2017 / examples / deepdream / deepdream_exercise.py
132929 viewsLicense: OTHER
"""DeepDream.1"""2from __future__ import absolute_import3from __future__ import division4from __future__ import print_function56import os.path7import zipfile89import numpy as np10import PIL.Image1112import tensorflow as tf1314FLAGS = tf.app.flags.FLAGS151617tf.app.flags.DEFINE_string('data_dir',18'/tmp/inception/',19'Directory for storing Inception network.')2021tf.app.flags.DEFINE_string('jpeg_file',22'output.jpg',23'Where to save the resulting JPEG.')242526def get_layer(layer):27"""Helper for getting layer output Tensor in model Graph.2829Args:30layer: string, layer name3132Returns:33Tensor for that layer.34"""35graph = tf.get_default_graph()36return graph.get_tensor_by_name('import/%s:0' % layer)373839def maybe_download(data_dir):40"""Maybe download pretrained Inception network.4142Args:43data_dir: string, path to data44"""45url = ('https://storage.googleapis.com/download.tensorflow.org/models/'46'inception5h.zip')47basename = 'inception5h.zip'48local_file = tf.contrib.learn.python.learn.datasets.base.maybe_download(49basename, data_dir, url)5051# Uncompress the pretrained Inception network.52print('Extracting', local_file)53zip_ref = zipfile.ZipFile(local_file, 'r')54zip_ref.extractall(FLAGS.data_dir)55zip_ref.close()565758def normalize_image(image):59"""Stretch the range and prepare the image for saving as a JPEG.6061Args:62image: numpy array6364Returns:65numpy array of image in uint866"""67# Clip to [0, 1] and then convert to uint8.68image = np.clip(image, 0, 1)69image = np.uint8(image * 255)70return image717273def save_jpeg(jpeg_file, image):74pil_image = PIL.Image.fromarray(image)75pil_image.save(jpeg_file)76print('Saved to file: ', jpeg_file)777879def main(unused_argv):80# Maybe download and uncompress pretrained Inception network.81maybe_download(FLAGS.data_dir)8283model_fn = os.path.join(FLAGS.data_dir, 'tensorflow_inception_graph.pb')8485# Load the pretrained Inception model as a GraphDef.86with tf.gfile.FastGFile(model_fn, 'rb') as f:87graph_def = tf.GraphDef()88graph_def.ParseFromString(f.read())8990with tf.Graph().as_default():91# Input for the network.92input_image = tf.placeholder(np.float32, name='input')93pixel_mean = 117.094input_preprocessed = tf.expand_dims(input_image - pixel_mean, 0)95tf.import_graph_def(graph_def, {'input': input_preprocessed})9697# Grab a list of the names of Tensor's that are the output of convolutions.98graph = tf.get_default_graph()99layers = [op.name for op in graph.get_operations()100if op.type == 'Conv2D' and 'import/' in op.name]101feature_nums = [int(graph.get_tensor_by_name(name+':0').get_shape()[-1])102for name in layers]103# print('Layers available: %s' % ','.join(layers))104print('Number of layers', len(layers))105print('Number of features:', sum(feature_nums))106107# Pick an internal layer and node to visualize.108# Note that we use outputs before applying the ReLU nonlinearity to109# have non-zero gradients for features with negative initial activations.110layer = 'mixed4d_3x3_bottleneck_pre_relu'111channel = 139112layer_channel = get_layer(layer)[:, :, :, channel]113print('layer %s, channel %d: %s' % (layer, channel, layer_channel))114115# Define the optimization as the average across all spatial locations.116score = tf.reduce_mean(layer_channel)117118# Automatic differentiation with TensorFlow. Magic!119input_gradient = tf.gradients(score, input_image)[0]120121# Employ random noise as a image.122noise_image = np.random.uniform(size=(224, 224, 3)) + 100.0123image = noise_image.copy()124125################################################################126# EXERCISE: Implemement the Deep Dream algorithm here!127################################################################128129# Save the image.130stddev = 0.1131image = (image - image.mean()) / max(image.std(), 1e-4) * stddev + 0.5132image = normalize_image(image)133save_jpeg(FLAGS.jpeg_file, image)134135136if __name__ == '__main__':137tf.app.run()138139