资源简介

这是基于VGG19网络的一个图像风格转换,需要下载VGG19的权重文件,拷入到工程目录下即可,代码直接运行就能跑。

资源截图

代码片段和文件信息

import tensorflow as tf
import numpy as np
import os
import cv2

IMAGE_W = 600
IMAGE_H = 600

Ratio = None

INI_NOISE_RATIO = 0.7
style_STRENGTH = 500
ITERATION = 2000
CONTENT_layerS = [(‘conv4_2‘ 1.)]
style_layerS = [(‘conv1_1‘ 2.) (‘conv2_1‘ 1.) (‘conv3_1‘ 0.5) (‘conv4_1‘ 0.25) (‘conv5_1‘ 0.125)]
layers = [‘conv1_1‘ ‘conv1_2‘ ‘conv2_1‘ ‘conv2_2‘ ‘conv3_1‘ ‘conv3_2‘ ‘conv3_3‘ ‘conv3_4‘
          ‘conv4_1‘ ‘conv4_2‘ ‘conv4_3‘ ‘conv4_4‘ ‘conv5_1‘ ‘conv5_2‘ ‘conv5_3‘ ‘conv5_4‘ ]


def vgg19(input model_path=None):
    ‘‘‘
    Build the VGG19 network which is initialized with the pre-trained VGG19 model.
    :param input: The input image.
    :param model_path:Which path the VGG19 model is stored.
    :return:A python dict which contains all the layers needed.
    ‘‘‘
    if model_path is None:
        model_path = ‘vgg19.npy‘

    if os.path.isfile(model_path) is False:
        raise FileNotFoundError(‘vgg19.npy cannot be found!!!‘)

    wDict = np.load(model_path encoding=“bytes“).item()

    net = {}
    net[‘input‘] = input

    # conv1_1
    weight1_1 = tf.Variable(initial_value=wDict[‘conv1_1‘][0] trainable=False)
    bias1_1 = tf.Variable(wDict[‘conv1_1‘][1] trainable=False)
    net[‘conv1_1‘] = tf.nn.relu(tf.nn.conv2d(net[‘input‘] weight1_1 [1 1 1 1] ‘SAME‘) + bias1_1)

    # conv1_2
    weight1_2 = tf.Variable(wDict[‘conv1_2‘][0] trainable=False)
    bias1_2 = tf.Variable(wDict[‘conv1_2‘][1] trainable=False)
    net[‘conv1_2‘] = tf.nn.relu(tf.nn.conv2d(net[‘conv1_1‘] weight1_2 [1 1 1 1] ‘SAME‘) + bias1_2)

    # pool1
    net[‘pool1‘] = tf.nn.avg_pool(net[‘conv1_2‘] [1 2 2 1] [1 2 2 1] ‘SAME‘)

    # conv2_1
    weight2_1 = tf.Variable(wDict[‘conv2_1‘][0] trainable=False)
    bias2_1 = tf.Variable(wDict[‘conv2_2‘][1] trainable=False)
    net[‘conv2_1‘] = tf.nn.relu(tf.nn.conv2d(net[‘pool1‘] weight2_1 [1 1 1 1] ‘SAME‘) + bias2_1)

    # conv2_2
    weight2_2 = tf.Variable(wDict[‘conv2_2‘][0] trainable=False)
    bias2_2 = tf.Variable(wDict[‘conv2_2‘][1] trainable=False)
    net[‘conv2_2‘] = tf.nn.relu(tf.nn.conv2d(net[‘conv2_1‘] weight2_2 [1 1 1 1] ‘SAME‘) + bias2_2)

    # pool2
    net[‘pool2‘] = tf.nn.avg_pool(net[‘conv2_2‘] [1 2 2 1] [1 2 2 1] ‘SAME‘)

    # conv3_1
    weight3_1 = tf.Variable(wDict[‘conv3_1‘][0] trainable=False)
    bias3_1 = tf.Variable(wDict[‘conv3_1‘][1] trainable=False)
    net[‘conv3_1‘] = tf.nn.relu(tf.nn.conv2d(net[‘pool2‘] weight3_1 [1 1 1 1] ‘SAME‘) + bias3_1)

    # conv3_2
    weight3_2 = tf.Variable(wDict[‘conv3_2‘][0] trainable=False)
    bias3_2 = tf.Variable(wDict[‘conv3_2‘][1] trainable=False)
    net[‘conv3_2‘] = tf.nn.relu(tf.nn.conv2d(net[‘conv3_1‘] weight3_2 [1 1 1 1] ‘SAME‘) + bias3_2)

    # conv3_3
    weight3_3 = tf.Variable(wDict[‘conv3_3‘][0] trainable=False)
    bias3_3 = tf.Variable(wDict[‘conv3_3‘][1] trainable=False)
  

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

     文件        153  2018-06-03 19:33  Transfer\.idea\codestyles\codestyleConfig.xml

     文件        198  2018-06-03 19:33  Transfer\.idea\codestyles\Project.xml

     文件         84  2018-06-03 19:33  Transfer\.idea\dictionaries\Yel.xml

     文件        185  2018-06-03 19:33  Transfer\.idea\misc.xml

     文件        268  2018-06-03 19:33  Transfer\.idea\modules.xml

     文件        455  2018-06-03 19:33  Transfer\.idea\Transfer.iml

     文件      13130  2018-06-13 20:59  Transfer\.idea\workspace.xml

     文件     120217  2018-03-29 16:17  Transfer\content.jpg

     文件     202404  2018-03-29 16:17  Transfer\style.jpg

     文件      10707  2018-06-03 19:40  Transfer\style_transfer.py

     目录          0  2018-06-03 19:33  Transfer\.idea\codestyles

     目录          0  2018-06-03 19:33  Transfer\.idea\dictionaries

     目录          0  2018-06-13 20:59  Transfer\.idea

     目录          0  2018-06-03 15:07  Transfer\model

     目录          0  2018-06-13 20:56  Transfer

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

               347801                    15


评论

共有 条评论