图像风格迁移原始论文完整实现代码


图像风格迁移原始论文完整实现代码,可以实现内容图片和风格图片的转化,https://blog.csdn.net/kevinoop/article/details/79827782 这个博客有代码详细介绍。
资源截图
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Sun Jan 21 11:14:40 2018

@author: ASUS
“““
import os
import sys
import scipy.io
import scipy.misc
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
from PIL import Image
from nst_utils import *
import numpy as np
import tensorflow as tf

#matplotlib inline
def compute_content_cost(a_C a_G):
    “““
    Computes the content cost
    
    Arguments:
    a_C -- tensor of dimension (1 n_H n_W n_C) hidden layer activations representing content of the image C 
    a_G -- tensor of dimension (1 n_H n_W n_C) hidden layer activations representing content of the image G
    
    Returns: 
    J_content -- scalar that you compute using equation 1 above.
    “““
    
    # Retrieve dimensions from a_G (≈1 line)
    m n_H n_W n_C = a_G.get_shape().as_list()
    # Reshape a_C and a_G (≈2 lines)
    a_C_unrolled = tf.reshape(tf.transpose(a_C perm=[3 2 1 0]) [n_C n_H*n_W -1])
    a_G_unrolled = tf.reshape(tf.transpose(a_G perm=[3 2 1 0]) [n_C n_H*n_W -1])
    # compute the cost with tensorflow (≈1 line)
    J_content = tf.reduce_sum(tf.square(tf.subtract(a_C_unrolled a_G_unrolled))) / (4 * n_H * n_W * n_C)
    
    return J_content

def gram_matrix(A):
    “““
    Argument:
    A -- matrix of shape (n_C n_H*n_W)
    
    Returns:
    GA -- Gram matrix of A of shape (n_C n_C)
    “““   
    GA = tf.matmul(Atf.transpose(A)) 
    return GA

def compute_layer_style_cost(a_S a_G):
    “““
    Arguments:
    a_S -- tensor of dimension (1 n_H n_W n_C) hidden layer activations representing style of the image S 
    a_G -- tensor of dimension (1 n_H n_W n_C) hidden layer activations representing style of the image G
    
    Returns: 
    J_style_layer -- tensor representing a scalar value style cost defined above by equation (2)
    “““
    # Retrieve dimensions from a_G (≈1 line)
    m n_H n_W n_C = a_G.get_shape().as_list()
    # Reshape the images to have them of shape (n_C n_H*n_W) (≈2 lines)
    a_S = tf.reshape(tf.transpose(a_S perm=[3 1 2 0]) [n_C n_W*n_H])
    a_G = tf.reshape(tf.transpose(a_G perm=[3 1 2 0]) [n_C n_W*n_H])
    # Computing gram_matrices for both images S and G (≈2 lines)
    GS = gram_matrix(a_S)
    GG = gram_matrix(a_G)
    # Computing the loss (≈1 line)
    J_style_layer = tf.reduce_sum(tf.square(tf.subtract(GS GG))) / (4 * n_C**2 * (n_W * n_H)**2)
    return J_style_layer

style_layerS = [
    (‘conv1_1‘ 0.2)
    (‘conv2_1‘ 0.2)
    (‘conv3_1‘ 0.2)
    (‘conv4_1‘ 0.2)
    (‘conv5_1‘ 0.2)]

def compute_style_cost(model style_layerS):
    “““
    Computes the overall style cost from several chosen layers
    
    Arguments:
    model -- our tensorflow model
    style_layerS -- A python list containing:
                        - the names of the layers we would like to extract style from
                        - a coefficient for eac

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件          68  2018-05-12 10:40  pretrained-model预训练数据下载网址.txt
     文件        7669  2018-01-21 11:33  NST(Gaty).py
     目录           0  2018-01-21 11:25  images
     文件      720646  2017-11-26 03:30  imagesNST_GM.png
     文件      868690  2017-11-26 03:30  imagesNST_LOSS.png
     文件      533504  2017-12-28 23:38  imagesThumbs.db
     文件       76975  2017-11-26 03:30  imagescamp-nou.jpg
     文件       33239  2017-11-26 03:30  imagescat.jpg
     文件      930639  2017-11-26 03:30  imagescircle_abstract.png
     文件       29674  2017-11-26 03:30  imagesclaude-monet.jpg
     文件       90349  2017-11-26 03:30  imagescontent.jpeg
     文件       21296  2017-11-26 03:30  imagescontent300.jpg
     文件      232594  2017-11-26 03:30  imagescontent_plus_style.png
     文件       74758  2017-11-26 03:30  imagesdrop-of-water.jpg
     文件      320270  2017-11-26 03:30  imagesgram.png
     文件      368783  2017-11-26 03:30  imageshidden_layers.png
     文件      168901  2017-11-26 03:30  imageslouvre.jpg
     文件      897986  2017-11-26 03:30  imageslouvre_generated.png
     文件       50296  2017-11-26 03:30  imageslouvre_small.jpg
     文件       48146  2017-11-26 03:30  imagesmonet.jpg
     文件      172643  2017-11-26 03:30  imagesmonet_800600.jpg
     文件      858141  2017-11-26 03:30  imagespasargad_kashi.png
     文件      238928  2017-11-26 03:30  imagespersian_cat.jpg
     文件       30492  2017-11-26 03:30  imagespersian_cat_content.jpg
     文件      820820  2017-11-26 03:30  imagesperspolis_vangogh.png
     文件      329708  2017-11-26 03:30  images
eshape_loss.png
     文件      893421  2017-11-26 03:30  images
esult.png
     文件       66303  2017-11-26 03:30  imagessandstone.jpg
     文件       53165  2017-11-26 03:30  imagesstone_style.jpg
     文件       29674  2017-11-26 03:30  imagesstyle300.jpg
     文件        6716  2017-12-20 23:45  nst_utils.py
............此处省略4个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)