Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
""" Load VGGNet weights needed for the implementation in TensorFlow
2
of the paper A Neural Algorithm of Artistic Style (Gatys et al., 2016)
3
4
Created by Chip Huyen ([email protected])
5
CS20: "TensorFlow for Deep Learning Research"
6
cs20.stanford.edu
7
8
For more details, please read the assignment handout:
9
https://docs.google.com/document/d/1FpueD-3mScnD0SJQDtwmOb1FrSwo1NGowkXzMwPoLH4/edit?usp=sharing
10
11
"""
12
import numpy as np
13
import scipy.io
14
import tensorflow as tf
15
16
import utils
17
18
# VGG-19 parameters file
19
VGG_DOWNLOAD_LINK = 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat'
20
VGG_FILENAME = 'imagenet-vgg-verydeep-19.mat'
21
EXPECTED_BYTES = 534904783
22
23
class VGG(object):
24
def __init__(self, input_img):
25
utils.download(VGG_DOWNLOAD_LINK, VGG_FILENAME, EXPECTED_BYTES)
26
self.vgg_layers = scipy.io.loadmat(VGG_FILENAME)['layers']
27
self.input_img = input_img
28
self.mean_pixels = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3))
29
30
def _weights(self, layer_idx, expected_layer_name):
31
""" Return the weights and biases at layer_idx already trained by VGG
32
"""
33
W = self.vgg_layers[0][layer_idx][0][0][2][0][0]
34
b = self.vgg_layers[0][layer_idx][0][0][2][0][1]
35
layer_name = self.vgg_layers[0][layer_idx][0][0][0][0]
36
assert layer_name == expected_layer_name
37
return W, b.reshape(b.size)
38
39
def conv2d_relu(self, prev_layer, layer_idx, layer_name):
40
""" Create a convolution layer with RELU using the weights and
41
biases extracted from the VGG model at 'layer_idx'. You should use
42
the function _weights() defined above to extract weights and biases.
43
44
_weights() returns numpy arrays, so you have to convert them to TF tensors.
45
46
Don't forget to apply relu to the output from the convolution.
47
Inputs:
48
prev_layer: the output tensor from the previous layer
49
layer_idx: the index to current layer in vgg_layers
50
layer_name: the string that is the name of the current layer.
51
It's used to specify variable_scope.
52
Hint for choosing strides size:
53
for small images, you probably don't want to skip any pixel
54
"""
55
###############################
56
## TO DO
57
out = None
58
###############################
59
setattr(self, layer_name, out)
60
61
def avgpool(self, prev_layer, layer_name):
62
""" Create the average pooling layer. The paper suggests that
63
average pooling works better than max pooling.
64
65
Input:
66
prev_layer: the output tensor from the previous layer
67
layer_name: the string that you want to name the layer.
68
It's used to specify variable_scope.
69
70
Hint for choosing strides and kszie: choose what you feel appropriate
71
"""
72
###############################
73
## TO DO
74
out = None
75
###############################
76
setattr(self, layer_name, out)
77
78
def load(self):
79
self.conv2d_relu(self.input_img, 0, 'conv1_1')
80
self.conv2d_relu(self.conv1_1, 2, 'conv1_2')
81
self.avgpool(self.conv1_2, 'avgpool1')
82
self.conv2d_relu(self.avgpool1, 5, 'conv2_1')
83
self.conv2d_relu(self.conv2_1, 7, 'conv2_2')
84
self.avgpool(self.conv2_2, 'avgpool2')
85
self.conv2d_relu(self.avgpool2, 10, 'conv3_1')
86
self.conv2d_relu(self.conv3_1, 12, 'conv3_2')
87
self.conv2d_relu(self.conv3_2, 14, 'conv3_3')
88
self.conv2d_relu(self.conv3_3, 16, 'conv3_4')
89
self.avgpool(self.conv3_4, 'avgpool3')
90
self.conv2d_relu(self.avgpool3, 19, 'conv4_1')
91
self.conv2d_relu(self.conv4_1, 21, 'conv4_2')
92
self.conv2d_relu(self.conv4_2, 23, 'conv4_3')
93
self.conv2d_relu(self.conv4_3, 25, 'conv4_4')
94
self.avgpool(self.conv4_4, 'avgpool4')
95
self.conv2d_relu(self.avgpool4, 28, 'conv5_1')
96
self.conv2d_relu(self.conv5_1, 30, 'conv5_2')
97
self.conv2d_relu(self.conv5_2, 32, 'conv5_3')
98
self.conv2d_relu(self.conv5_3, 34, 'conv5_4')
99
self.avgpool(self.conv5_4, 'avgpool5')
100