首页 资料正文

机器学习进阶笔记之六 | 深入理解Fast Neural Style

woniu 资料 2020-03-22 23:13:00 78612 0 | 文章出自:用户投稿

Transform Network网络结构

  1. line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; list-style-position: outside !important;">import tensorflow as tf

  2. def conv2d(x, input_filters, output_filters, kernel, strides, padding=’SAME’):
  3. with tf.variable_scope('conv') as scope:

  4.     shape = [kernel, kernel, input_filters, output_filters]
  5.     weight = tf.Variable(tf.truncated_normal(shape, stddev=0.1), name='weight')
  6.     convolved = tf.nn.conv2d(x, weight, strides=[1, strides, strides, 1], padding=padding, name='conv')

  7.     normalized = batch_norm(convolved, output_filters)

  8.     return normalized

  9. def conv2d_transpose(x, input_filters, output_filters, kernel, strides, padding=’SAME’):
  10. with tf.variable_scope('conv_transpose') as scope:

  11.     shape = [kernel, kernel, output_filters, input_filters]
  12.     weight = tf.Variable(tf.truncated_norm
  13. al(shape, stddev=0.1), name='weight')

  14.     batch_size = tf.shape(x)[0]
  15.     height = tf.shape(x)[1] * strides
  16.     width = tf.shape(x)[2] * strides
  17.     output_shape = tf.pack([batch_size, height, width, output_filters])
  18.     convolved = tf.nn.conv2d_transpose(x, weight, output_shape, strides=[1, strides, strides, 1], padding=padding, name='conv_transpose')

  19.     normalized = batch_norm(convolved, output_filters)
  20.     return normalized

  21. def batch_norm(x, size):

  22. batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2], keep_dims=True)
  23. beta = tf.Variable(tf.zeros([size]), name='beta')
  24. scale = tf.Variable(tf.ones([size]), name='scale')
  25. epsilon = 1e-3
  26. return tf.nn.batch_normalization(x, batch_mean, batch_var, beta, scale, epsilon, name='batch')

  27. def residual(x, filters, kernel, strides, padding=’SAME’):
  28. with tf.variable_scope('residual') as scope:
  29.   
  30.     conv1 = conv2d(x, filters, filters, kernel, strides, padding=padding)
  31.     conv2 = conv2d(tf.nn.relu(conv1), filters, filters, kernel, strides, padding=padding)

  32.     residual = x + conv2

  33.     return residual

  34. def net(image):

  35. with tf.variable_scope('conv1'):
  36.     conv1 = tf.nn.relu(conv2d(image, 3, 32, 9, 1))
  37. with tf.variable_scope('conv2'):
  38.     conv2 = tf.nn.relu(conv2d(conv1, 32, 64, 3, 2))
  39. with tf.variable_scope('conv3'):
  40.     conv3 = tf.nn.relu(conv2d(conv2, 64, 128, 3, 2))
  41. with tf.variable_scope('res1'):
  42.     res1 = residual(conv3, 128, 3, 1)
  43. with tf.variable_scope('res2'):
  44.     res2 = residual(res1, 128, 3, 1)
  45. with tf.variable_scope('res3'):
  46.     res3 = residual(res2, 128, 3, 1)
  47. with tf.variable_scope('res4'):
  48.     res4 = residual(res3, 128, 3, 1)
  49. with tf.variable_scope('res5'):
  50.     res5 = residual(res4, 128, 3, 1)
  51. with tf.variable_scope('deconv1'):
  52.     deconv1 = tf.nn.relu(conv2d_transpose(res5, 128, 64, 3, 2))
  53. with tf.variable_scope('deconv2'):
  54.     deconv2 = tf.nn.relu(conv2d_transpose(deconv1, 64, 32, 3, 2))
  55. with tf.variable_scope('deconv3'):
  56.     deconv3 = tf.nn.tanh(conv2d_transpose(deconv2, 32, 3, 9, 1))

  57. y = deconv3 * 127.5

  58. return y

       使用deep residual network来训练COCO数据集,能够在保证性能的前提下,训练更深的模型。 而Loss Network是有pretrained的VGG网络来计算,网络结构:

  1.  import tensorflow as tf
  2.  import numpy as np
  3.  import scipy.io
  4.  from scipy import misc


  5.  def net(data_path, input_image):
  6.      layers = (
  7.          'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',

  8.          'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',

  9.          'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
  10.          'relu3_3', 'conv3_4', 'relu3_4', 'pool3',

  11.          'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
  12.          'relu4_3', 'conv4_4', 'relu4_4', 'pool4',

  13.          'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
  14.          'relu5_3', 'conv5_4', 'relu5_4'
  15.      )

  16.      data = scipy.io.loadmat(data_path)
  17.      mean = data['normalization'][0][0][0]
  18.      mean_pixel = np.mean(mean, axis=(0, 1))
  19.      weights = data['layers'][0]

  20.      net = {}
  21.      current = input_image
  22.      for i, name in enumerate(layers):
  23.          kind = name[:4]
  24.          if kind == 'conv':
  25.              kernels, bias = weights[i][0][0][0][0]
  26.              # matconvnet: weights are [width, height, in_channels, out_channels]
  27.              # tensorflow: weights are [height, width, in_channels, out_channels]
  28.              kernels = np.transpose(kernels, (1, 0, 2, 3))
  29.              bias = bias.reshape(-1)
  30.              current = _conv_layer(current, kernels, bias, name=name)
  31.          elif kind == 'relu':
  32.              current = tf.nn.relu(current, name=name)
  33.          elif kind == 'pool':
  34.              current = _pool_layer(current, name=name)
  35.          net[name] = current

  36.      assert len(net) == len(layers)
  37.      return net, mean_pixel


  38.  def _conv_layer(input, weights, bias, name=None):
  39.      conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1, 1, 1, 1),
  40.                          padding='SAME', name=name)
  41.      return tf.nn.bias_add(conv, bias)


  42.  def _pool_layer(input, name=None):
  43.      return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1),
  44.                            padding='SAME', name=name)


  45.  def preprocess(image, mean_pixel):
  46.      return image - mean_pixel


  47.  def unprocess(image, mean_pixel):
  48.      return image + mean_pixel

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接:https://www.chinaai.com/ziliao/575.html

评论