def get_layers():
    model = inception5h.Inception5h()
    layers = []
    for layer in model.layer_tensors:
        l = {'name': layer.name, 'nbChannels': layer.get_shape().as_list()[3]}
        layers.append(l)
    return layers
def dream(image_filename, layer):
    """
    image_filename is the path to the image to process
    layer is a dictionnary, containing the layer name and the feature channels to maximise.
    It has the following format:
    {
        name: 'conv2d0:0',
        fromChannel: 0,
        toChannel: 10
    }
    """
    global model
    start_loading = time.time()
    model = inception5h.Inception5h()
    session = tf.compat.v1.Session(graph=model.graph)

    image = image_utils.load_image(filename=image_filename)

    start_processing = time.time()

    # The image we're going to be working on should be within 400x600 pixels for performance reasons.
    DESIRED_WIDTH = 400
    DESIRED_HEIGHT = 600
    rescale_factor = min(DESIRED_WIDTH / image.shape[1],
                         DESIRED_HEIGHT / image.shape[0])
    image_downscaled = image_utils.resize_image(image=image,
                                                factor=rescale_factor)
    layer_tensor = model.get_layer_tensor_by_name(
        layer['name'])[:, :, :, layer['fromChannel']:layer['toChannel'] + 1]
    print('Layer tensor: ' + str(layer_tensor))
    img_result = recursive_optimize(layer_tensor=layer_tensor,
                                    image=image_downscaled,
                                    num_iterations=10,
                                    step_size=3.0,
                                    num_repeats=4,
                                    blend=0.2,
                                    session=session)
    upscaled_result = image_utils.resize_image(image=img_result,
                                               size=image.shape)
    image_utils.save_image(upscaled_result, filename='img/deapdream_image.jpg')
    session.close()
    end_processing = time.time()
    print(
        "Loading time: %f sec, processing time: %f sec" %
        (start_processing - start_loading, end_processing - start_processing))
示例#3
0
'''

# https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/14_DeepDream.ipynb

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import random
import math
# Image manipulation.
import PIL.Image
from scipy.ndimage.filters import gaussian_filter
import inception5h

inception5h.maybe_download()
model = inception5h.Inception5h()
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
session = tf.Session(graph=model.graph,
                     config=tf.ConfigProto(gpu_options=gpu_options))


def load_image(filename):
    image = PIL.Image.open(filename)
    return np.float32(image)


def save_image(image, filename):
    # Ensure the pixel-values are between 0 and 255.
    image = np.clip(image, 0.0, 255.0)
    # Convert to bytes.
    image = image.astype(np.uint8)
示例#4
0
def main(image, hyperparameters, mode="image"):
    model = inception5h.Inception5h()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.InteractiveSession(graph=model.graph)

    start = time.time()

    model_layer = hyperparameters["model_layer"]
    subset_start = hyperparameters["subset_start"]
    subset_end = hyperparameters["subset_end"]

    random_flag = hyperparameters["random_flag"]
    load_flag = hyperparameters["load_flag"]
    if load_flag: random_flag = True

    if random_flag:
        if load_flag:
            set = name + "_" + str(model_layer)
            samples = np.load("./gen_im_lib/temp/" + set + ".npy")
        else:
            samples = np.unique(
                np.random.randint(subset_start, subset_end, size=35))
            np.save(
                "./gen_im_lib/temp/" + name + "_" + str(model_layer) + ".npy",
                samples)
    else:
        samples = list(range(subset_start, subset_end))

    if mode == "image":
        layer_tensor = tf.gather(model.layer_tensors[model_layer],
                                 samples,
                                 axis=-1)
        gradient = model.get_gradient(layer_tensor)
        img_result = recursive_optimize(model=model,
                                        session=session,
                                        layer_tensor=layer_tensor,
                                        image=image,
                                        num_iterations=5,
                                        step_size=10.0,
                                        rescale_factor=0.7,
                                        num_repeats=5,
                                        blend=0.2,
                                        gradient=gradient,
                                        tile_size=2000)
        plot_image(img_result)

        save_image(img_result,
                   filename="gen_im_lib/" + name + "_" + str(model_layer) +
                   "_" + str(min(samples)) + "_" + str(max(samples)) + ".jpg")

    elif mode == "gif":
        frames_in = []
        frames_out = []

        extractGifs.extractFrames('im_lib/source.gif', 'im_lib/frames')
        files = glob.glob('im_lib/frames' + "/*.JPG")
        sort_nicely(files)
        for myFile in files:
            image = resize_image(load_image(myFile), factor=0.25)
            frames_in.append(image)

        samples = list(range(subset_start, subset_end))
        layer_tensor = tf.gather(model.layer_tensors[model_layer],
                                 samples,
                                 axis=-1)
        gradient = model.get_gradient(layer_tensor)
        for frame in frames_in:
            img_result = recursive_optimize(model=model,
                                            session=session,
                                            layer_tensor=layer_tensor,
                                            image=frame,
                                            num_iterations=80,
                                            step_size=4.0,
                                            rescale_factor=0.7,
                                            num_repeats=5,
                                            blend=0.2,
                                            gradient=gradient,
                                            tile_size=2000)
            frames_out.append(img_result)

        movie.write_mp4_from_list(frames_out, "source", transition_length=1)
        movie.write_mp4_to_gif("./gen_im_lib/animation/" + name + ".mp4", name)

    elif mode == "image_as_mp4":
        frames = []
        for i in range(0, 40, 2):
            samples = list(range(subset_start + i, subset_end + i))
            layer_tensor = tf.gather(model.layer_tensors[model_layer],
                                     samples,
                                     axis=-1)
            gradient = model.get_gradient(layer_tensor)
            img_result = recursive_optimize(model=model,
                                            session=session,
                                            layer_tensor=layer_tensor,
                                            image=image,
                                            num_iterations=60,
                                            step_size=2,
                                            rescale_factor=0.6,
                                            num_repeats=5,
                                            blend=0.2,
                                            gradient=gradient,
                                            tile_size=2000)
            frames.append(img_result)
            save_image(img_result,
                       filename="gen_im_lib/" + name + "_" + str(model_layer) +
                       "_" + str(min(samples)) + "_" + str(max(samples)) +
                       ".jpg")
        movie.write_mp4_from_list(frames, name, transition_length=5)
        movie.write_mp4_to_gif("./gen_im_lib/animation/" + name + ".mp4", name)

    end = time.time()
    print("It took: " + str(end - start) + " seconds")

    session.close()
示例#5
0
 def __init__(self):
     inception5h.maybe_download()
     self.model = inception5h.Inception5h()
     self.session = tf.InteractiveSession(graph=self.model.graph)
 def __init__(self, layer):
     import tensorflow as tf
     self.model = inception5h.Inception5h()
     self.session = tf.InteractiveSession(graph=self.model.graph)
     self.layer_tensor = self.model.layer_tensors[layer]
def style_transfer(content_image,
                   style_image,
                   content_layer_ids,
                   style_layer_ids,
                   weight_content=1.5,
                   weight_style=10.0,
                   weight_denoise=0.3,
                   num_iterations=120,
                   step_size=10.0):
    """
    Use gradient descent to find an image that minimizes the
    loss-functions of the content-layers and style-layers. This
    should result in a mixed-image that resembles the contours
    of the content-image, and resembles the colours and textures
    of the style-image.

    :param content_image: Numpy 3-dim float-array with the content-image.
    :param style_image: Numpy 3-dim float-array with the style-image.
    :param content_layer_ids: List of integers identifying the content-layers.
    :param style_layer_ids: List of integers identifying the style-layers.
    :param weight_content: Weight of the content-loss-function.
    :param weight_style: Weight of the style-loss-function.
    :param weight_denoise: Weight of the denoising-loss-function.
    :param num_iterations: Number of optimization iterations to perform.
    :param step_size: Step-size for the gradient in each iteration.
    :return:
    """

    # Create an instance of the Inception-model. This is done
    # in each call of this function, because we will add
    # operations to the graph so it can grow very large
    # and run out of RAM if we keep using the same instance.
    model = inception5h.Inception5h()

    # Create a TensorFlow-session.
    session = tf.InteractiveSession(graph=model.graph)

    # Print the names of the content-layers.
    print("Content layers:")
    print(model.get_layer_names(content_layer_ids))
    print()

    # Print the names of the style-layers.
    print("Style layers:")
    print(model.get_layer_names(style_layer_ids))
    print()

    # Create the loss-function for the content-layers and -image.
    loss_content = create_content_loss(session=session,
                                       model=model,
                                       content_image=content_image,
                                       layer_ids=content_layer_ids)

    # Create the loss-function for the style-layers and -image.
    loss_style = create_style_loss(session=session,
                                   model=model,
                                   style_image=style_image,
                                   layer_ids=style_layer_ids)

    # Create the loss-function for the denoising of the mixed-image.
    loss_denoise = create_denoise_loss(model)

    # Create TensorFlow variables for adjusting the values of
    # the loss-functions. This is explained below.
    adj_content = tf.Variable(1e-10, name='adj_content')
    adj_style = tf.Variable(1e-10, name='adj_style')
    adj_denoise = tf.Variable(1e-10, name='adj_denoise')

    # Initialize the adjustment values for the loss-function.
    session.run([
        adj_content.initializer, adj_style.initializer, adj_denoise.initializer
    ])

    # Create TensorFlow operations for updating the adjustment values.
    # These are basically just the reciprocal values of the
    # loss-functions, with a small value 1e-10 added to avoid the
    # possibility of division by zero.

    update_adj_content = adj_content.assign(1.0 / (loss_content + 1e-10))
    update_adj_style = adj_style.assign(1.0 / (loss_style + 1e-10))
    update_adj_denoise = adj_denoise.assign(1.0 / (loss_denoise + 1e-10))

    # This is the weighted loss-function that we will minimize
    # below in order to generate the mixed-image.
    # Because we multiply the loss-values with their reciprocal adjustment values,
    # we can use relative weights for the loss-functions that are easier
    # to select, as they are independent of the exact choice of style- and content-layers.
    loss_combined = weight_content * adj_content * loss_content + \
                    weight_style * adj_style * loss_style + \
                    weight_denoise * adj_denoise * loss_denoise

    # Use TensorFlow to get the mathematical function for the
    # gradient of the combined loss-function with regard to
    # the input image.
    gradient = tf.gradients(loss_combined, model.input)

    # List of tensors that we will run in each optimization iteration.
    run_list = [
        gradient, update_adj_content, update_adj_style, update_adj_denoise
    ]

    # The mixed-image is initialized with random noise.
    # It is the same size as the content-image.
    mixed_image = np.random.rand(*content_image.shape) + 128

    for i in range(num_iterations):
        feed_dict = model.create_feed_dict(image=mixed_image)

        # Use TensorFlow to calculate the value of the
        # gradient, as well as updating the adjustment values.
        grad, adj_content_val, adj_style_val, adj_denoise_val \
            = session.run(run_list, feed_dict=feed_dict)

        # Reduce the dimensionality of the gradient.
        grad = np.squeeze(grad)

        # Scale the step-size according to the gradient-values.
        step_size_scaled = step_size / (np.std(grad) + 1e-8)

        # Update the image by following the gradient.
        mixed_image -= grad * step_size_scaled

        # Ensure the image has valid pixel-values between 0 and 255.
        mixed_image = np.clip(mixed_image, 0.0, 255.0)

        # Print a little progress-indicator.
        print(". ", end="")

        # Display status once every 10 iterations, and the last.
        if (i % 100 == 0) or (i == num_iterations - 1):
            print()
            print("Iteration:", i)

            # Print adjustment weights for loss-functions.
            msg = "Weight Adj. for Content: {0:.2e}, Style: {1:.2e}, Denoise: {2:.2e}"
            print(msg.format(adj_content_val, adj_style_val, adj_denoise_val))

            # Plot the content-, style- and mixed-images.
            plot_images(content_image=content_image,
                        style_image=style_image,
                        mixed_image=mixed_image)

    print()
    print("Final image:")
    plot_image_big(mixed_image)

    # Close the TensorFlow session to release its resources.
    session.close()

    return mixed_image