Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132929 views
License: OTHER
1
"""DeepDream.
2
"""
3
from __future__ import absolute_import
4
from __future__ import division
5
from __future__ import print_function
6
7
import os.path
8
import zipfile
9
10
import numpy as np
11
import PIL.Image
12
13
import tensorflow as tf
14
15
FLAGS = tf.app.flags.FLAGS
16
17
18
tf.app.flags.DEFINE_string('data_dir',
19
'/tmp/inception/',
20
'Directory for storing Inception network.')
21
22
tf.app.flags.DEFINE_string('jpeg_file',
23
'output.jpg',
24
'Where to save the resulting JPEG.')
25
26
27
def get_layer(layer):
28
"""Helper for getting layer output Tensor in model Graph.
29
30
Args:
31
layer: string, layer name
32
33
Returns:
34
Tensor for that layer.
35
"""
36
graph = tf.get_default_graph()
37
return graph.get_tensor_by_name('import/%s:0' % layer)
38
39
40
def maybe_download(data_dir):
41
"""Maybe download pretrained Inception network.
42
43
Args:
44
data_dir: string, path to data
45
"""
46
url = ('https://storage.googleapis.com/download.tensorflow.org/models/'
47
'inception5h.zip')
48
basename = 'inception5h.zip'
49
local_file = tf.contrib.learn.python.learn.datasets.base.maybe_download(
50
basename, data_dir, url)
51
52
# Uncompress the pretrained Inception network.
53
print('Extracting', local_file)
54
zip_ref = zipfile.ZipFile(local_file, 'r')
55
zip_ref.extractall(FLAGS.data_dir)
56
zip_ref.close()
57
58
59
def normalize_image(image):
60
"""Stretch the range and prepare the image for saving as a JPEG.
61
62
Args:
63
image: numpy array
64
65
Returns:
66
numpy array of image in uint8
67
"""
68
# Clip to [0, 1] and then convert to uint8.
69
image = np.clip(image, 0, 1)
70
image = np.uint8(image * 255)
71
return image
72
73
74
def save_jpeg(jpeg_file, image):
75
pil_image = PIL.Image.fromarray(image)
76
pil_image.save(jpeg_file)
77
print('Saved to file: ', jpeg_file)
78
79
80
def main(unused_argv):
81
# Maybe download and uncompress pretrained Inception network.
82
maybe_download(FLAGS.data_dir)
83
84
model_fn = os.path.join(FLAGS.data_dir, 'tensorflow_inception_graph.pb')
85
86
# Load the pretrained Inception model as a GraphDef.
87
with tf.gfile.FastGFile(model_fn, 'rb') as f:
88
graph_def = tf.GraphDef()
89
graph_def.ParseFromString(f.read())
90
91
with tf.Graph().as_default():
92
# Input for the network.
93
input_image = tf.placeholder(np.float32, name='input')
94
pixel_mean = 117.0
95
input_preprocessed = tf.expand_dims(input_image - pixel_mean, 0)
96
tf.import_graph_def(graph_def, {'input': input_preprocessed})
97
98
# Grab a list of the names of Tensor's that are the output of convolutions.
99
graph = tf.get_default_graph()
100
layers = [op.name for op in graph.get_operations()
101
if op.type == 'Conv2D' and 'import/' in op.name]
102
feature_nums = [int(graph.get_tensor_by_name(name+':0').get_shape()[-1])
103
for name in layers]
104
# print('Layers available: %s' % ','.join(layers))
105
print('Number of layers', len(layers))
106
print('Number of features:', sum(feature_nums))
107
108
# Pick an internal layer and node to visualize.
109
# Note that we use outputs before applying the ReLU nonlinearity to
110
# have non-zero gradients for features with negative initial activations.
111
layer = 'mixed4d_3x3_bottleneck_pre_relu'
112
channel = 139
113
layer_channel = get_layer(layer)[:, :, :, channel]
114
print('layer %s, channel %d: %s' % (layer, channel, layer_channel))
115
116
# Define the optimization as the average across all spatial locations.
117
score = tf.reduce_mean(layer_channel)
118
119
# Automatic differentiation with TensorFlow. Magic!
120
input_gradient = tf.gradients(score, input_image)[0]
121
122
# Employ random noise as a image.
123
noise_image = np.random.uniform(size=(224, 224, 3)) + 100.0
124
image = noise_image.copy()
125
126
################################################################
127
# EXERCISE: Implemement the Deep Dream algorithm here!
128
################################################################
129
130
# Save the image.
131
stddev = 0.1
132
image = (image - image.mean()) / max(image.std(), 1e-4) * stddev + 0.5
133
image = normalize_image(image)
134
save_jpeg(FLAGS.jpeg_file, image)
135
136
137
if __name__ == '__main__':
138
tf.app.run()
139