[TensorFlow deep Learning] Using DeepDream to create art

code

import os
os.environ["KMP_DUPLICATE_LIB_OK"] ="TRUE"

# -*- coding: utf-8 -*-

# to load the library
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']

# load image classification model, use inception5h here

graph = tf.Graph()

with graph.as_default() :
    with tf.gfile.FastGFile('/ Users/yss/YSSFiles/TFAPP/DeepDream / 22 tianma sky 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)

Tensor = gradient tensor = gradient tensor = gradient tensor = gradient tensor

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

# Adjust the input image according to gradient, i.e. DeepDream
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

# Scale the original image, DeepDream multiple scales and overlay

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)

# Read a picture

image = imread('/ Users/yss/YSSFiles/TFAPP/DeepDream / 22 tianma sky DeepDream/mountain. JPG')
image = image.astype(np.float32)

Take the response values of 12 tensor as the optimization target, then process the original image

for i in range(len(layers)):
    print(layer_names[i])
    result = recursive_dream(layers[i], image)
    #plt.figure(figsize=(10, 15))
    #plt.imshow(result / 255.)
    #plt.show()
    imsave('/ Users/yss YSSFiles/TFAPP DeepDream/tianma sky DeepDream/out / % 22 s.j pg' % layer_names[i], result)
Copy the code

The results of



reference

  • Blog.csdn.net/zhl49372277…