📚 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:8https://docs.google.com/document/d/1FpueD-3mScnD0SJQDtwmOb1FrSwo1NGowkXzMwPoLH4/edit?usp=sharing910"""11import numpy as np12import scipy.io13import tensorflow as tf1415import utils1617# VGG-19 parameters file18VGG_DOWNLOAD_LINK = 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat'19VGG_FILENAME = 'imagenet-vgg-verydeep-19.mat'20EXPECTED_BYTES = 5349047832122class VGG(object):23def __init__(self, input_img):24utils.download(VGG_DOWNLOAD_LINK, VGG_FILENAME, EXPECTED_BYTES)25self.vgg_layers = scipy.io.loadmat(VGG_FILENAME)['layers']26self.input_img = input_img27self.mean_pixels = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3))2829def _weights(self, layer_idx, expected_layer_name):30""" Return the weights and biases at layer_idx already trained by VGG31"""32W = self.vgg_layers[0][layer_idx][0][0][2][0][0]33b = self.vgg_layers[0][layer_idx][0][0][2][0][1]34layer_name = self.vgg_layers[0][layer_idx][0][0][0][0]35assert layer_name == expected_layer_name36return W, b.reshape(b.size)3738def conv2d_relu(self, prev_layer, layer_idx, layer_name):39""" Create a convolution layer with RELU using the weights and40biases extracted from the VGG model at 'layer_idx'. You should use41the function _weights() defined above to extract weights and biases.4243_weights() returns numpy arrays, so you have to convert them to TF tensors.4445Don't forget to apply relu to the output from the convolution.46Inputs:47prev_layer: the output tensor from the previous layer48layer_idx: the index to current layer in vgg_layers49layer_name: the string that is the name of the current layer.50It's used to specify variable_scope.51Hint for choosing strides size:52for small images, you probably don't want to skip any pixel53"""54###############################55## TO DO56out = None57###############################58setattr(self, layer_name, out)5960def avgpool(self, prev_layer, layer_name):61""" Create the average pooling layer. The paper suggests that62average pooling works better than max pooling.6364Input:65prev_layer: the output tensor from the previous layer66layer_name: the string that you want to name the layer.67It's used to specify variable_scope.6869Hint for choosing strides and kszie: choose what you feel appropriate70"""71###############################72## TO DO73out = None74###############################75setattr(self, layer_name, out)7677def load(self):78self.conv2d_relu(self.input_img, 0, 'conv1_1')79self.conv2d_relu(self.conv1_1, 2, 'conv1_2')80self.avgpool(self.conv1_2, 'avgpool1')81self.conv2d_relu(self.avgpool1, 5, 'conv2_1')82self.conv2d_relu(self.conv2_1, 7, 'conv2_2')83self.avgpool(self.conv2_2, 'avgpool2')84self.conv2d_relu(self.avgpool2, 10, 'conv3_1')85self.conv2d_relu(self.conv3_1, 12, 'conv3_2')86self.conv2d_relu(self.conv3_2, 14, 'conv3_3')87self.conv2d_relu(self.conv3_3, 16, 'conv3_4')88self.avgpool(self.conv3_4, 'avgpool3')89self.conv2d_relu(self.avgpool3, 19, 'conv4_1')90self.conv2d_relu(self.conv4_1, 21, 'conv4_2')91self.conv2d_relu(self.conv4_2, 23, 'conv4_3')92self.conv2d_relu(self.conv4_3, 25, 'conv4_4')93self.avgpool(self.conv4_4, 'avgpool4')94self.conv2d_relu(self.avgpool4, 28, 'conv5_1')95self.conv2d_relu(self.conv5_1, 30, 'conv5_2')96self.conv2d_relu(self.conv5_2, 32, 'conv5_3')97self.conv2d_relu(self.conv5_3, 34, 'conv5_4')98self.avgpool(self.conv5_4, 'avgpool5')99100