[TensorFlow深度学习深入]实战四·使用DeepDream创造艺术画(机器如何创造艺术)
代码
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
import tensorflow as tf
import numpy as np
import cv2
from imageio import imread, imsave, mimsave
import matplotlib.pyplot as plt
from scipy.ndimage.filters import gaussian_filter
layer_names = ['conv2d0', 'conv2d1', 'conv2d2',
'mixed3a', 'mixed3b', 'mixed4a', 'mixed4b', 'mixed4c', 'mixed4d', 'mixed4e',
'mixed5a', 'mixed5b']
graph = tf.Graph()
with graph.as_default() :
with tf.gfile.FastGFile('/Users/yss/YSSFiles/TFAPP/DeepDream/22 天马星空的DeepDream/inception5h.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
X = graph.get_tensor_by_name('input:0')
layers = [graph.get_tensor_by_name(name + ':0') for name in layer_names]
all_layers_names = [tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
print(all_layers_names)
sess = tf.Session(graph=graph)
def get_gradient(tensor):
with graph.as_default():
return tf.gradients(tf.reduce_mean(tf.square(tensor)), X)[0]
def get_tile_size(num_pixels, tile_size=400):
num_tiles = max(1, int(round(num_pixels / tile_size)))
return int(np.ceil(num_pixels / num_tiles))
def tiled_gradient(gradient, image, tile_size=400):
grad = np.zeros_like(image)
H, W, _ = image.shape
h = get_tile_size(H, tile_size)
h_4 = h // 4
w = get_tile_size(W, tile_size)
w_4 = w // 4
h_start = np.random.randint(-3 * h_4, -h_4)
while h_start < H:
h_end = h_start + h
h_start_lim = max(h_start, 0)
h_end_lim = min(h_end, H)
w_start = np.random.randint(-3 * w_4, -w_4)
while w_start < W:
w_end = w_start + w
w_start_lim = max(w_start, 0)
w_end_lim = min(w_end, W)
g = sess.run(gradient, feed_dict={X: [image[h_start_lim: h_end_lim, w_start_lim: w_end_lim, :]]})[0]
g /= (np.std(g) + 1e-8)
grad[h_start_lim: h_end_lim, w_start_lim: w_end_lim, :] = g
w_start = w_end
h_start = h_end
return grad
def dream(layer_tensor, image, iteration=10, step=3.0, tile_size=400):
img = image.copy()
gradient = get_gradient(layer_tensor)
for i in range(iteration):
grad = tiled_gradient(gradient, img)
sigma = (i * 4.0) / iteration + 0.5
grad = gaussian_filter(grad, 0.5 * sigma) + gaussian_filter(grad, sigma) + gaussian_filter(grad, 2 * sigma)
scaled_step = step / (np.std(grad) + 1e-8)
img += grad * scaled_step
img = np.clip(img, 0, 255)
return img
def recursive_dream(layer_tensor, image, repeat=3, scale=0.7, blend=0.2, iteration=10, step=3.0, tile_size=400):
if repeat > 0:
sigma = 0.5
img_blur = gaussian_filter(image, (sigma, sigma, 0.0))
h0 = img_blur.shape[0]
w0 = img_blur.shape[1]
h1 = int(scale * h0)
w1 = int(scale * w0)
img_downscaled = cv2.resize(img_blur, (w1, h1))
img_dream = recursive_dream(layer_tensor, img_downscaled, repeat - 1, scale, blend, iteration, step, tile_size)
img_upscaled = cv2.resize(img_dream, (w0, h0))
image = blend * image + (1.0 - blend) * img_upscaled
image = np.clip(image, 0, 255)
return dream(layer_tensor, image, iteration, step, tile_size)
image = imread('/Users/yss/YSSFiles/TFAPP/DeepDream/22 天马星空的DeepDream/mountain.jpg')
image = image.astype(np.float32)
for i in range(len(layers)):
print(layer_names[i])
result = recursive_dream(layers[i], image)
imsave('/Users/yss/YSSFiles/TFAPP/DeepDream/22 天马星空的DeepDream/out/%s.jpg' % layer_names[i], result)
结果
参考
- https://blog.csdn.net/zhl493722771/article/details/82806656