Пример #1
0
    def __init__(self):
        self.model = nst_utils.load_vgg_model(
            "pretrained-model/imagenet-vgg-verydeep-19.mat")
        self.STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2),
                             ('conv3_1', 0.2), ('conv4_1', 0.2),
                             ('conv5_1', 0.2)]

        self.input_shape = (300, 400)  # (hight, width) of VGG16 input layer
        self.content_image = None
        self.style_image = None
Пример #2
0
def NST_model(num_iter=1000):
    content_img = load_img("con_niu.jpg")
    content_img = nu.reshape_and_normalize_image(content_img)
    style_img = load_img("style_cloud.jpg")
    style_img = nu.reshape_and_normalize_image(style_img)
    generated_img = nu.generate_noise_image(content_img)

    print(np.shape(content_img))
    print(np.shape(style_img))
    print(np.shape(generated_img))

    with tf.Session() as sess:
        model = nu.load_vgg_model(
            "pretrained-model/imagenet-vgg-verydeep-19.mat")
        sess.run(model['input'].assign(content_img))
        out = model['conv4_2']

        a_c = sess.run(out)
        a_g = out
        J_content = content_cost(a_c, a_g)

        STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2),
                        ('conv4_1', 0.2), ('conv5_1', 0.2)]
        sess.run(model['input'].assign(style_img))
        J_style = style_cost(model, STYLE_LAYERS, sess)
        J = total_cost(J_content, J_style)

        optimizer = tf.train.AdamOptimizer(2.0)
        train_step = optimizer.minimize(J)

        tf.global_variables_initializer().run()
        sess.run(model['input'].assign(generated_img))

        for i in range(num_iter):
            sess.run(train_step)
            generated_img = sess.run(model['input'])
            if i % 20 == 0:
                Jt, Jc, Js = sess.run([J, J_content, J_style])
                print("Iteration " + str(i) + " :")
                print("total cost = " + str(Jt))
                print("content cost = " + str(Jc))
                print("style cost = " + str(Js))
                print(generated_img.shape)
                nu.save_image("output4/" + str(i) + ".png", generated_img)

        nu.save_image("output4/generated_image.png", generated_img)

    return generated_img
Пример #3
0
def main():
    print("hello world")

    model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat")
    print(model)

    #img = r'images\louvre.jpg'
    # img2 = r"C:\Users\cracr\Desktop\python_projects\deep_learning_small_projects\deepart\images\louvre.jpg"
    # img3 = r"C:/Users/cracr/Desktop/python_projects/deep_learning_small_projects/deepart/images/louvre.jpg"
    #image = Image.open(img)

    # content_image = scipy.misc.imread("images/louvre.jpg")
    # imshow(content_image)
    # if DISPLAY_IMG: plt.show()
    # style_image = scipy.misc.imread("images/monet_800600.jpg")
    # imshow(style_image)
    # if DISPLAY_IMG: plt.show()

    # Reset the graph
    tf.reset_default_graph()

    # Start interactive session
    sess = tf.InteractiveSession()

    # content_image = scipy.misc.imread("images/louvre_small.jpg")
    content_image = scipy.misc.imread("images/" + INPUT_IMG + ".jpg")
    content_image = reshape_and_normalize_image(content_image)

    # style_image = scipy.misc.imread("images/monet.jpg")
    style_image = scipy.misc.imread("images/vangogh.jpg")
    style_image = reshape_and_normalize_image(style_image)

    generated_image = generate_noise_image(content_image)
    imshow(generated_image[0])
    if DISPLAY_IMG: plt.show()

    model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat")

    # Assign the content image to be the input of the VGG model.
    sess.run(model['input'].assign(content_image))

    # Select the output tensor of layer conv4_2
    out = model['conv4_2']

    # Set a_C to be the hidden layer activation from the layer we have selected
    a_C = sess.run(out)

    # Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2']
    # and isn't evaluated yet. Later in the code, we'll assign the image G as the model input, so that
    # when we run the session, this will be the activations drawn from the appropriate layer, with G as input.
    a_G = out

    # Compute the content cost
    J_content = compute_content_cost(a_C, a_G)

    # Assign the input of the model to be the "style" image
    sess.run(model['input'].assign(style_image))

    # Compute the style cost
    J_style = compute_style_cost(model, STYLE_LAYERS, sess)

    J = total_cost(J_content, J_style)

    # define optimizer (1 line)
    optimizer = tf.train.AdamOptimizer(2.0)

    # define train_step (1 line)
    train_step = optimizer.minimize(J)

    model_nn(sess, generated_image, model, train_step, J, J_content, J_style)
Пример #4
0
def generate_noise_image2(content_image, noise_ratio=0.6):
    """
    Generates a noisy image by adding random noise to the content_image
    """
    height = content_image.shape[1]
    width = content_image.shape[2]
    # Generate a random noise_image
    noise_image = np.random.uniform(-20, 20,
                                    (1, height, width, 3)).astype('float32')
    # range_ = np.max(content_image) - np.min(content_image)
    # noise_image = np.random.normal(loc=np.mean(content_image), scale = 0.7*range_, size=(1, height, width, 3)).astype('float32')

    # Set the input_image to be a weighted average of the content_image and a noise_image
    input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio)

    return input_image

    STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2),
                    ('conv4_1', 0.2), ('conv5_1', 0.2)]

    # Reset the graph
    tf.reset_default_graph()

    # Start interactive session
    sess = tf.InteractiveSession()

    content_image = scipy.misc.imread("images/z_me3.jpg")
    content_image = reshape_and_normalize_image(content_image)
    style_image = scipy.misc.imread("images/constable.jpg")
    style_image = reshape_and_normalize_image(style_image)

    generated_image = generate_noise_image2(content_image)
    #print(generated_image.shape)
    imshow(generated_image[0])

    model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat")

    # Assign the content image to be the input of the VGG model.
    sess.run(model['input'].assign(content_image))

    # Select the output tensor of layer conv4_2
    out = model['conv4_2']

    # Set a_C to be the hidden layer activation from the layer we have selected
    a_C = sess.run(out)

    # Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2']
    # and isn't evaluated yet. Later in the code, we'll assign the image G as the model input, so that
    # when we run the session, this will be the activations drawn from the appropriate layer, with G as input.
    a_G = out

    # Compute the content cost
    J_content = compute_content_cost(a_C, a_G)

    # Assign the input of the model to be the "style" image
    sess.run(model['input'].assign(style_image))

    # Compute the style cost
    J_style = compute_style_cost(sess, model, STYLE_LAYERS)

    ### START CODE HERE ### (1 line)
    J = total_cost(J_content, J_style, alpha=10, beta=40)
    ### END CODE HERE ###

    # define optimizer (1 line)
    optimizer = tf.train.AdamOptimizer(1.0)

    # define train_step (1 line)
    train_step = optimizer.minimize(J)

    # Initialize global variables (you need to run the session on the initializer)
    ### START CODE HERE ### (1 line)
    sess.run(tf.global_variables_initializer())
    ### END CODE HERE ###

    # Run the noisy input image (initial generated image) through the model. Use assign().
    ### START CODE HERE ### (1 line)
    sess.run(model['input'].assign(generated_image))
    ### END CODE HERE ###

    for i in range(200):

        # Run the session on the train_step to minimize the total cost
        ### START CODE HERE ### (1 line)
        sess.run(train_step)
        ### END CODE HERE ###

        # Compute the generated image by running the session on the current model['input']
        ### START CODE HERE ### (1 line)
        generated_image = sess.run(model['input'])
        ### END CODE HERE ###

        # Print every 20 iteration.
        if i % 2 == 0:
            Jt, Jc, Js = sess.run([J, J_content, J_style])
            print("Iteration " + str(i) + " :")
            print("total cost = " + str(Jt))
            print("content cost = " + str(Jc))
            print("style cost = " + str(Js))

            # save current generated image in the "/output" directory
            save_image("output/z_m3_3_constable_" + str(i) + ".png",
                       generated_image)

    # save last generated image
    save_image('output/z_me_3_constable_generated_image.jpg', generated_image)

    return generated_image
Пример #5
0
 def initialize(cls):
     """
     This function initialize the model for computing activations
     """
     global model
     model = load_vgg_model(CONFIG.VGG_MODEL)
Пример #6
0
    sess = tf.InteractiveSession()

    #第2步:加载内容图像(卢浮宫博物馆图片),并归一化图像
    content_image = scipy.misc.imread("E:\wuenda\images/louvre_small.jpg")
    content_image = nst_utils.reshape_and_normalize_image(content_image)

    #第3步:加载风格图像(印象派的风格),并归一化图像
    style_image = scipy.misc.imread("E:\wuenda\images/monet.jpg")
    style_image = nst_utils.reshape_and_normalize_image(style_image)

    #第4步:随机初始化生成的图像,通过在内容图像中添加随机噪声来产生噪声图像
    generated_image = nst_utils.generate_noise_image(content_image)
    imshow(generated_image[0])

    #第5步:加载VGG16模型
    model = nst_utils.load_vgg_model(
        "E:\wuenda\pretrained-model/imagenet-vgg-verydeep-19.mat")
    #第6步:构建TensorFlow图:

    ##将内容图像作为VGG模型的输入。
    sess.run(model["input"].assign(content_image))

    ## 获取conv4_2层的输出
    out = model["conv4_2"]

    ## 将a_C设置为“conv4_2”隐藏层的激活值。
    a_C = sess.run(out)

    ## 将a_G设置为来自同一图层的隐藏层激活,这里a_G引用model["conv4_2"],并且还没有计算,
    ## 在后面的代码中,我们将图像G指定为模型输入,这样当我们运行会话时,
    ## 这将是以图像G作为输入,从隐藏层中获取的激活值。
    a_G = out
Пример #7
0
    for layer_name, lm in STYLE_LAYERS:
        out = model[layer_name]
        a_S = sess.run(out)
        a_G = out
        J_style_layer = layer_style_cost(a_S, a_G)
        J_style += lm * J_style_layer
    return J_style


def total_cost(J_content, J_style, alpha=10, beta=100):
    J = alpha * J_content + beta * J_style
    return J


################################################################################
model = load_vgg_model('./model/imagenet-vgg-verydeep-19.mat')
with tf.device('/gpu:0'):
    sess = tf.InteractiveSession()
    style_image = scipy.misc.imread('images/prisma3.jpg')

    style_image = scipy.misc.imresize(style_image, (300, 400, 3))
    #print(style_image.shape)
    style_image = reshape_and_normalize_image(style_image)

    content_image = scipy.misc.imread('images/love.jpeg')
    content_image = scipy.misc.imresize(content_image, (300, 400, 3))
    #plt.imshow(content_image)
    content_image = reshape_and_normalize_image(content_image)

    generated_image = generate_noise_image(content_image)
Пример #8
0
    def load_model(self, model_path):

        self.model = load_vgg_model(model_path)
Пример #9
0
# Reset the graph
tf.reset_default_graph()

# Start interactive session(开始交互式会话)
sess = tf.InteractiveSession()

content_image = scipy.misc.imread("images/louvre_small.jpg")
content_image = reshape_and_normalize_image(content_image)

style_image = scipy.misc.imread("images/monet.jpg")
style_image = reshape_and_normalize_image(style_image)

generated_image = generate_noise_image(content_image)

model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat")

# Assign the content image to be the input of the VGG model.
sess.run(model['input'].assign(content_image))

# Select the output tensor of layer conv4_2
out = model['conv4_2']

# Set a_C to be the hidden layer activation from the layer we have selected
a_C = sess.run(out)

# Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2']
# and isn't evaluated yet. Later in the code, we'll assign the image G as the model input, so that
# when we run the session, this will be the activations drawn from the appropriate layer, with G as input.
a_G = out
Пример #10
0
tf.reset_default_graph()
sess = tf.InteractiveSession()

# Step 2: Load the content image
content_image = scipy.misc.imread(CONFIG.CONTENT_IMAGE)
content_image = reshape_and_normalize_image(content_image)

# Step 3: Load the style image
style_image = scipy.misc.imread(CONFIG.STYLE_IMAGE)
style_image = reshape_and_normalize_image(style_image)

# Step 4: Randomly initialize the image to be generated
generated_image = generate_noise_image(content_image)

# Step 5: Load the VGG16 model
model = load_vgg_model(CONFIG.VGG_MODEL)

# Step 6: Build the tensorflow graph
# Step 6a: Run the content image through VGG16 model and compute content cost
sess.run(model['input'].assign(content_image))
J_content = compute_content_cost(sess, model, CONFIG.CONTENT_LAYER)

# Step 6b: Run the style image through VGG16 model and compute style cost
sess.run(model['input'].assign(style_image))
J_style = compute_style_cost(sess, model, CONFIG.STYLE_LAYERS)

# Step 6c: Compute the total cost
J = total_cost(J_content, J_style, alpha=CONFIG.ALPHA, beta=CONFIG.BETA)

# Step 6d: Define the optimizer and learning rate
optimizer = tf.train.AdamOptimizer(CONFIG.LEARNING_RATE)
Пример #11
0
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 21:20:14 2019

@author: admin
"""

import os
from matplotlib.pyplot import imshow
import nst_utils as util
import tensorflow as tf
import scipy
import numpy as np

os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
model = util.load_vgg_model(util.CONFIG.VGG_MODEL)

STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2),
                ('conv4_1', 0.2), ('conv5_1', 0.2)]

#STYLE_LAYERS = [
#    ('conv1_1', 0.01),
#    ('conv2_1', 0.01),
#    ('conv3_1', 0.01)]

content_image = scipy.misc.imread(util.CONFIG.CONTENT_IMAGE)
content_image = scipy.misc.imresize(content_image,
                                    size=(util.CONFIG.IMAGE_HEIGHT,
                                          util.CONFIG.IMAGE_WIDTH,
                                          util.CONFIG.COLOR_CHANNELS))
content_image = util.reshape_and_normalize_image(content_image)