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
10
"""
11
import numpy as np
12
import scipy.io
13
import tensorflow as tf
14
15
import utils
16
17
# VGG-19 parameters file
18
VGG_DOWNLOAD_LINK = 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat'
19
VGG_FILENAME = 'imagenet-vgg-verydeep-19.mat'
20
EXPECTED_BYTES = 534904783
21
22
class VGG(object):
23
def __init__(self, input_img):
24
utils.download(VGG_DOWNLOAD_LINK, VGG_FILENAME, EXPECTED_BYTES)
25
self.vgg_layers = scipy.io.loadmat(VGG_FILENAME)['layers']
26
self.input_img = input_img
27
self.mean_pixels = np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3))
28
29
def _weights(self, layer_idx, expected_layer_name):
30
""" Return the weights and biases at layer_idx already trained by VGG
31
"""
32
W = self.vgg_layers[0][layer_idx][0][0][2][0][0]
33
b = self.vgg_layers[0][layer_idx][0][0][2][0][1]
34
layer_name = self.vgg_layers[0][layer_idx][0][0][0][0]
35
assert layer_name == expected_layer_name
36
return W, b.reshape(b.size)
37
38
def conv2d_relu(self, prev_layer, layer_idx, layer_name):
39
""" Return the Conv2D layer with RELU using the weights,
40
biases from the VGG model at 'layer_idx'.
41
Don't forget to apply relu to the output from the convolution.
42
Inputs:
43
prev_layer: the output tensor from the previous layer
44
layer_idx: the index to current layer in vgg_layers
45
layer_name: the string that is the name of the current layer.
46
It's used to specify variable_scope.
47
48
49
Note that you first need to obtain W and b from from the corresponding VGG's layer
50
using the function _weights() defined above.
51
W and b returned from _weights() are numpy arrays, so you have
52
to convert them to TF tensors. One way to do it is with tf.constant.
53
54
Hint for choosing strides size:
55
for small images, you probably don't want to skip any pixel
56
"""
57
###############################
58
## TO DO
59
with tf.variable_scope(layer_name) as scope:
60
W, b = self._weights(layer_idx, layer_name)
61
W = tf.constant(W, name='weights')
62
b = tf.constant(b, name='bias')
63
conv2d = tf.nn.conv2d(prev_layer,
64
filter=W,
65
strides=[1, 1, 1, 1],
66
padding='SAME')
67
out = tf.nn.relu(conv2d + b)
68
###############################
69
setattr(self, layer_name, out)
70
71
def avgpool(self, prev_layer, layer_name):
72
""" Return the average pooling layer. The paper suggests that
73
average pooling works better than max pooling.
74
Input:
75
prev_layer: the output tensor from the previous layer
76
layer_name: the string that you want to name the layer.
77
It's used to specify variable_scope.
78
79
Hint for choosing strides and kszie: choose what you feel appropriate
80
"""
81
###############################
82
## TO DO
83
with tf.variable_scope(layer_name):
84
out = tf.nn.avg_pool(prev_layer,
85
ksize=[1, 2, 2, 1],
86
strides=[1, 2, 2, 1],
87
padding='SAME')
88
###############################
89
setattr(self, layer_name, out)
90
91
def load(self):
92
self.conv2d_relu(self.input_img, 0, 'conv1_1')
93
self.conv2d_relu(self.conv1_1, 2, 'conv1_2')
94
self.avgpool(self.conv1_2, 'avgpool1')
95
self.conv2d_relu(self.avgpool1, 5, 'conv2_1')
96
self.conv2d_relu(self.conv2_1, 7, 'conv2_2')
97
self.avgpool(self.conv2_2, 'avgpool2')
98
self.conv2d_relu(self.avgpool2, 10, 'conv3_1')
99
self.conv2d_relu(self.conv3_1, 12, 'conv3_2')
100
self.conv2d_relu(self.conv3_2, 14, 'conv3_3')
101
self.conv2d_relu(self.conv3_3, 16, 'conv3_4')
102
self.avgpool(self.conv3_4, 'avgpool3')
103
self.conv2d_relu(self.avgpool3, 19, 'conv4_1')
104
self.conv2d_relu(self.conv4_1, 21, 'conv4_2')
105
self.conv2d_relu(self.conv4_2, 23, 'conv4_3')
106
self.conv2d_relu(self.conv4_3, 25, 'conv4_4')
107
self.avgpool(self.conv4_4, 'avgpool4')
108
self.conv2d_relu(self.avgpool4, 28, 'conv5_1')
109
self.conv2d_relu(self.conv5_1, 30, 'conv5_2')
110
self.conv2d_relu(self.conv5_2, 32, 'conv5_3')
111
self.conv2d_relu(self.conv5_3, 34, 'conv5_4')
112
self.avgpool(self.conv5_4, 'avgpool5')
113