Ejemplo n.º 1
0
 def load_init(self, fname):
     if fname is None:
         self.init_image = nst_utils.generate_noise_image(
             self.content_image)
     else:
         self.init_image, self.init_flip = self.read_check_image(fname)
     print("shape of initial image: ", self.init_image.shape)
Ejemplo n.º 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
Ejemplo n.º 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)
Ejemplo n.º 4
0
    #重设图
    tf.reset_default_graph()

    #第1步:创建交互会话
    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)
Ejemplo n.º 5
0
    def Generate(cls,
                 content,
                 style,
                 alpha=10,
                 beta=40,
                 no_iter=100,
                 display=False):
        """
        call signature : Generate(content,style,alpha=10,beta=40,iter=100)
        input --  content : content image,
                  style   : style image,
                  alpha   : content cost multiplier,
                  beta    : style cost multiplier,
                  iter    : number of iteration
        return --- a dictionary
                  total_cost : array of total cost at eact 10th iteration,
                  content_cost : array of content cost at each 10th iteration,
                  style_cost : array of style cost at each 10th iteration,
                  image : np array of generated image of shape(1,h,w,c)
        side effect ---
                  save the generated image in output dir

        """

        J_content = cls.calculateTotalContentCost(content, cls.content)
        J_style = cls.calculateTotalStyleCost(style, cls.style)

        #compute Total Cost
        J = alpha * J_content + beta * J_style

        #Initialize noisy Generated Image
        image = generate_noise_image(content)

        #Set Optimizer
        optimizer = tf.train.AdamOptimizer(cls.learning_rate)
        train_step = optimizer.minimize(J)

        #initialize plot
        if (display):
            fig, ax1, ax2 = cls._createFig()
        J_show = []
        J_C_show = []
        J_S_show = []

        #Start the Session
        with tf.Session() as sess:
            #initialize Variables
            sess.run(tf.global_variables_initializer())
            sess.run(model['input'].assign(image))

            #run optimization
            for i in range(no_iter):
                sess.run(train_step)
                generated_image = sess.run(model['input'])

                #print infomation
                if i % 10 == 0:
                    temp_1, temp_2, temp_3 = sess.run([J, J_content, J_style])
                    J_show.append(temp_1)
                    J_C_show.append(temp_2)
                    J_S_show.append(temp_3)
                    print('iter : {}, J : {}'.format(i, temp_1))
                    if (display):
                        cls._updateFig(fig, ax1, ax2, generated_image,
                                       (J_show, J_C_show, J_S_show), no_iter)

        mat = {
            'total_cost': J_show,
            'content_cost': J_C_show,
            'style_cost': J_S_show,
            'image': generated_image
        }

        save_image(CONFIG.OUTPUT_DIR + cls.name + '.jpg', generated_image)
        scipy.io.savemat(CONFIG.OUTPUT_DIR + cls.name + '.mat', mat)
        return mat
Ejemplo n.º 6
0
################################################################################
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)

    sess.run(model['input'].assign(content_image))
    out = model['conv4_2']
    a_C = sess.run(out)
    a_G = out
    J_content = content_cost(a_C, a_G)

    sess.run(model['input'].assign(style_image))
    J_style = style_cost(model, STYLE_LAYERS)

    J = total_cost(J_content, J_style, 1, 2e2)

    optimizer = tf.train.AdamOptimizer(learning_rate=1.2)
    train_step = optimizer.minimize(J)
Ejemplo n.º 7
0
    def generate_noisy_img(content_img):

        generated_noisy_img = generate_noise_image(content_img)
        return generated_noisy_img
Ejemplo n.º 8
0
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)
content_image = np.float32(content_image)

style_image = scipy.misc.imread(util.CONFIG.STYLE_IMAGE)
style_image = scipy.misc.imresize(style_image,
                                  size=(util.CONFIG.IMAGE_HEIGHT,
                                        util.CONFIG.IMAGE_WIDTH,
                                        util.CONFIG.COLOR_CHANNELS))
style_image = util.reshape_and_normalize_image(style_image)
style_image = np.float32(style_image)

generated_image = util.generate_noise_image(content_image)
imshow(generated_image[0])
imshow(content_image[0])
imshow(style_image[0])

# Assign the content image to be the input of the VGG model.
# Start interactive session
sess = tf.InteractiveSession()
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)