📚 The CoCalc Library - books, templates and other resources
License: OTHER
""" Load VGGNet weights needed for the implementation in TensorFlow1of the paper A Neural Algorithm of Artistic Style (Gatys et al., 2016)23Created by Chip Huyen ([email protected])4CS20: "TensorFlow for Deep Learning Research"5cs20.stanford.edu67For more details, please read the assignment handout:89"""10import numpy as np11import scipy.io12import tensorflow as tf1314import utils1516# VGG-19 parameters file17VGG_DOWNLOAD_LINK = 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat'18VGG_FILENAME = 'imagenet-vgg-verydeep-19.mat'19EXPECTED_BYTES = 5349047832021class VGG(object):22def __init__(self, input_img):23utils.download(VGG_DOWNLOAD_LINK, VGG_FILENAME, EXPECTED_BYTES)24self.vgg_layers = scipy.io.loadmat(VGG_FILENAME)['layers']25self.input_img = input_img26self.mean_pixels = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3))2728def _weights(self, layer_idx, expected_layer_name):29""" Return the weights and biases at layer_idx already trained by VGG30"""31W = self.vgg_layers[0][layer_idx][0][0][2][0][0]32b = self.vgg_layers[0][layer_idx][0][0][2][0][1]33layer_name = self.vgg_layers[0][layer_idx][0][0][0][0]34assert layer_name == expected_layer_name35return W, b.reshape(b.size)3637def conv2d_relu(self, prev_layer, layer_idx, layer_name):38""" Return the Conv2D layer with RELU using the weights,39biases from the VGG model at 'layer_idx'.40Don't forget to apply relu to the output from the convolution.41Inputs:42prev_layer: the output tensor from the previous layer43layer_idx: the index to current layer in vgg_layers44layer_name: the string that is the name of the current layer.45It's used to specify variable_scope.464748Note that you first need to obtain W and b from from the corresponding VGG's layer49using the function _weights() defined above.50W and b returned from _weights() are numpy arrays, so you have51to convert them to TF tensors. One way to do it is with tf.constant.5253Hint for choosing strides size:54for small images, you probably don't want to skip any pixel55"""56###############################57## TO DO58with tf.variable_scope(layer_name) as scope:59W, b = self._weights(layer_idx, layer_name)60W = tf.constant(W, name='weights')61b = tf.constant(b, name='bias')62conv2d = tf.nn.conv2d(prev_layer,63filter=W,64strides=[1, 1, 1, 1],65padding='SAME')66out = tf.nn.relu(conv2d + b)67###############################68setattr(self, layer_name, out)6970def avgpool(self, prev_layer, layer_name):71""" Return the average pooling layer. The paper suggests that72average pooling works better than max pooling.73Input:74prev_layer: the output tensor from the previous layer75layer_name: the string that you want to name the layer.76It's used to specify variable_scope.7778Hint for choosing strides and kszie: choose what you feel appropriate79"""80###############################81## TO DO82with tf.variable_scope(layer_name):83out = tf.nn.avg_pool(prev_layer,84ksize=[1, 2, 2, 1],85strides=[1, 2, 2, 1],86padding='SAME')87###############################88setattr(self, layer_name, out)8990def load(self):91self.conv2d_relu(self.input_img, 0, 'conv1_1')92self.conv2d_relu(self.conv1_1, 2, 'conv1_2')93self.avgpool(self.conv1_2, 'avgpool1')94self.conv2d_relu(self.avgpool1, 5, 'conv2_1')95self.conv2d_relu(self.conv2_1, 7, 'conv2_2')96self.avgpool(self.conv2_2, 'avgpool2')97self.conv2d_relu(self.avgpool2, 10, 'conv3_1')98self.conv2d_relu(self.conv3_1, 12, 'conv3_2')99self.conv2d_relu(self.conv3_2, 14, 'conv3_3')100self.conv2d_relu(self.conv3_3, 16, 'conv3_4')101self.avgpool(self.conv3_4, 'avgpool3')102self.conv2d_relu(self.avgpool3, 19, 'conv4_1')103self.conv2d_relu(self.conv4_1, 21, 'conv4_2')104self.conv2d_relu(self.conv4_2, 23, 'conv4_3')105self.conv2d_relu(self.conv4_3, 25, 'conv4_4')106self.avgpool(self.conv4_4, 'avgpool4')107self.conv2d_relu(self.avgpool4, 28, 'conv5_1')108self.conv2d_relu(self.conv5_1, 30, 'conv5_2')109self.conv2d_relu(self.conv5_2, 32, 'conv5_3')110self.conv2d_relu(self.conv5_3, 34, 'conv5_4')111self.avgpool(self.conv5_4, 'avgpool5')112113