style-transfer


用于图像风格转化(image style transfer)的代码实现。
资源截图
代码片段和文件信息
# # style Transfer
# In this notebook we will implement the style transfer technique from [“Image style Transfer Using Convolutional Neural Networks“ (Gatys et al. CVPR 2015)](http://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Gatys_Image_style_Transfer_CVPR_2016_paper.pdf).
#
# The general idea is to take two images and produce a new image that reflects the content of one but the artistic “style“ of the other. We will do this by first formulating a loss function that matches the content and style of each respective image in the feature space of a deep network and then performing gradient descent on the pixels of the image itself.
#
# The deep network we use as a feature extractor is [SqueezeNet](https://arxiv.org/abs/1602.07360) a small model that has been trained on ImageNet. You could use any network but we chose SqueezeNet here for its small size and efficiency.
#
# Here‘s an example of the images you‘ll be able to produce by the end of this notebook:
#
# ![caption](example_styletransfer.png)
#
#

# Set up

from scipy.misc import imread imresize
import numpy as np
import os
from scipy.misc import imread
import matplotlib.pyplot as plt

# Helper functions to deal with image preprocessing
from cs231n.image_utils import load_image preprocess_image deprocess_image

from cs231n.classifiers.squeezenet import SqueezeNet
import tensorflow as tf


def get_session():
    “““Create a session that dynamically allocates memory.“““
    # See: https://www.tensorflow.org/tutorials/using_gpu#allowing_gpu_memory_growth
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config)
    return session

def rel_error(xy):
    return np.max(np.abs(x - y) / (np.maximum(1e-8 np.abs(x) + np.abs(y))))

# Older versions of scipy.misc.imresize yield different results
# from newer versions so we check to make sure scipy is up to date.
def check_scipy():
    import scipy
    vnum = int(scipy.__version__.split(‘.‘)[1])
    assert vnum >= 16 “You must install SciPy >= 0.16.0 to complete this notebook.“

check_scipy()


# Load the pretrained SqueezeNet model. This model has been ported from PyTorch see ‘cs231n/classifiers/squeezenet.py‘ for the model architecture.
#
# To use SqueezeNet you will need to first **download the weights** by changing into the ‘cs231n/datasets‘ directory and running ‘get_squeezenet_tf.sh‘ . Note that if you ran ‘get_assignment3_data.sh‘ then SqueezeNet will already be downloaded.


tf.reset_default_graph() # remove all existing variables in the graph
sess = get_session() # start a new Session

# Load pretrained SqueezeNet model
SAVE_PATH = ‘cs231n/datasets/squeezenet_tf/squeezenet.ckpt‘
# if not os.path.exists(SAVE_PATH):
#     raise ValueError(“You need to download SqueezeNet!“)
model = SqueezeNet(save_path=SAVE_PATH sess=sess)

# Load data for testing
content_img_test = preprocess_image(load_image(‘styles/

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       9792  2018-01-22 17:45  style-transfer.ideaworkspace.xml

     文件       4741  2017-05-18 09:46  style-transfercs231nclassifierssqueezenet.py

     文件       3628  2018-01-22 17:42  style-transfercs231nclassifiers\__pycache__squeezenet.cpython-36.pyc

     文件    4941984  2017-05-04 11:57  style-transfercs231ndatasetssqueezenet_tfsqueezenet.ckpt.data-00000-of-00001

     文件       2293  2017-05-04 11:57  style-transfercs231ndatasetssqueezenet_tfsqueezenet.ckpt.index

     文件    5060877  2017-05-04 11:57  style-transfercs231ndatasetssqueezenet_tfsqueezenet.ckpt.meta

     文件       2627  2017-12-26 14:33  style-transfercs231nimage_utils.py

     文件      66274  2017-05-18 09:46  style-transferstyle-transfer-checks-tf.npz

     文件      66274  2017-05-18 09:46  style-transferstyle-transfer-checks.npz

     文件      22417  2018-01-06 18:10  style-transferstyle-Transfer.py

     文件     202426  2017-05-18 09:46  style-transferstylescomposition_vii.jpg

     文件     703587  2017-05-18 09:46  style-transferstylesmuse.jpg

     文件     613337  2017-05-18 09:46  style-transferstylesstarry_night.jpg

     文件     216723  2017-05-18 09:46  style-transferstyles he_scream.jpg

     文件     406531  2017-05-18 09:46  style-transferstyles ubingen.jpg

     目录          0  2018-01-22 17:42  style-transfercs231nclassifiers\__pycache__

     目录          0  2018-01-22 17:38  style-transfercs231ndatasetssqueezenet_tf

     目录          0  2018-01-22 17:42  style-transfercs231nclassifiers

     目录          0  2018-01-22 17:38  style-transfercs231ndatasets

     目录          0  2018-01-22 17:45  style-transfer.idea

     目录          0  2018-01-22 17:44  style-transfercs231n

     目录          0  2018-01-22 17:38  style-transferstyles

     目录          0  2018-01-22 17:45  style-transfer

----------- ---------  ---------- -----  ----

             12323511                    23


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

发表评论

评论列表(条)