# 程序代写代做代考 algorithm GPU python Hive deepdream

deepdream

DeepDreaming with TensorFlow¶

Loading and displaying the model graph

Naive feature visualization

Multiscale image generation

Laplacian Pyramid Gradient Normalization

Playing with feature visualzations

DeepDream

This notebook demonstrates a number of Convolutional Neural Network image generation techniques implemented with TensorFlow for fun and science:

visualize individual feature channels and their combinations to explore the space of patterns learned by the neural network (see GoogLeNet and VGG16 galleries)

embed TensorBoard graph visualizations into Jupyter notebooks

produce high-resolution images with tiled computation (example)

use Laplacian Pyramid Gradient Normalization to produce smooth and colorful visuals at low cost

generate DeepDream-like images with TensorFlow (DogSlugs included)

The network under examination is the GoogLeNet architecture, trained to classify images into one of 1000 categories of the ImageNet dataset. It consists of a set of layers that apply a sequence of transformations to the input image. The parameters of these transformations were determined during the training process by a variant of gradient descent algorithm. The internal image representations may seem obscure, but it is possible to visualize and interpret them. In this notebook we are going to present a few tricks that allow to make these visualizations both efficient to generate and even beautiful. Impatient readers can start with exploring the full galleries of images generated by the method described here for GoogLeNet and VGG16 architectures.

In [1]:

# boilerplate code

from __future__ import print_function

import os

from io import BytesIO

import numpy as np

from functools import partial

import PIL.Image

from IPython.display import clear_output, Image, display, HTML

import tensorflow as tf

Loading and displaying the model graph¶

The pretrained network can be downloaded here. Unpack the tensorflow_inception_graph.pb file from the archive and set its path to model_fn variable. Alternatively you can uncomment and run the following cell to download the network:

In [2]:

!wget -nc https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip && unzip -n inception5h.zip

In [3]:

model_fn = ‘tensorflow_inception_graph.pb’

# creating TensorFlow session and loading the model

graph = tf.Graph()

sess = tf.InteractiveSession(graph=graph)

with tf.gfile.FastGFile(model_fn, ‘rb’) as f:

graph_def = tf.GraphDef()

graph_def.ParseFromString(f.read())

t_input = tf.placeholder(np.float32, name=’input’) # define the input tensor

imagenet_mean = 117.0

t_preprocessed = tf.expand_dims(t_input-imagenet_mean, 0)

tf.import_graph_def(graph_def, {‘input’:t_preprocessed})

To take a glimpse into the kinds of patterns that the network learned to recognize, we will try to generate images that maximize the sum of activations of particular channel of a particular convolutional layer of the neural network. The network we explore contains many convolutional layers, each of which outputs tens to hundreds of feature channels, so we have plenty of patterns to explore.

In [4]:

layers = [op.name for op in graph.get_operations() if op.type==’Conv2D’ and ‘import/’ in op.name]

feature_nums = [int(graph.get_tensor_by_name(name+’:0′).get_shape()[-1]) for name in layers]

print(‘Number of layers’, len(layers))

print(‘Total number of feature channels:’, sum(feature_nums))

# Helper functions for TF Graph visualization

def strip_consts(graph_def, max_const_size=32):

“””Strip large constant values from graph_def.”””

strip_def = tf.GraphDef()

for n0 in graph_def.node:

n = strip_def.node.add()

n.MergeFrom(n0)

if n.op == ‘Const’:

tensor = n.attr[‘value’].tensor

size = len(tensor.tensor_content)

if size > max_const_size:

tensor.tensor_content = tf.compat.as_bytes(“

return strip_def

def rename_nodes(graph_def, rename_func):

res_def = tf.GraphDef()

for n0 in graph_def.node:

n = res_def.node.add()

n.MergeFrom(n0)

n.name = rename_func(n.name)

for i, s in enumerate(n.input):

n.input[i] = rename_func(s) if s[0]!=’^’ else ‘^’+rename_func(s[1:])

return res_def

def show_graph(graph_def, max_const_size=32):

“””Visualize TensorFlow graph.”””

if hasattr(graph_def, ‘as_graph_def’):

graph_def = graph_def.as_graph_def()

strip_def = strip_consts(graph_def, max_const_size=max_const_size)

code = “””