This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgg.py
152 lines (132 loc) · 4.29 KB
/
vgg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import scipy.io
import numpy as np
import tensorflow as tf
def load(path):
'''
Loads the pre-trained VGG covnet
'''
raw = scipy.io.loadmat(path)
net = raw['layers'][0]
mean_pixels = raw['meta'][0][0][2][0][0][2][0][0]
return net, mean_pixels
def build_network(image, vgg):
'''
Builds the network by computing convolutions and activations at each stage.
Done explicitly for readability.
Network Layer Structure
conv1_1, relu1_1, conv1_2, relu1_2, pool1
conv2_1, relu2_1, conv2_2, relu2_2, pool2
conv3_1, relu3_1, conv3_2, relu3_2, conv3_3, relu3_3, conv3_4, relu3_4, pool2
conv4_1, relu4_1, conv4_2, relu4_2, conv4_3, relu4_3, conv4_4, relu4_4, pool2
conv5_1, relu5_1, conv5_2, relu5_2, conv5_3, relu5_3, conv5_4, relu5_4, pool2
'''
net = {}
net['input'] = image
# Layer 1
W, b = get_weights(vgg, 0)
net['conv1_1'] = convolution(net['input'], W, b)
net['relu1_1'] = relu(net['conv1_1'])
W, b = get_weights(vgg, 2)
net['conv1_2'] = convolution(net['relu1_1'], W, b)
net['relu1_2'] = relu(net['conv1_2'])
pooling = pooling_type(vgg, 4)
net['pool1'] = pool(net['relu1_2'], pooling)
# Layer 2
W, b = get_weights(vgg, 5)
net['conv2_1'] = convolution(net['pool1'], W, b)
net['relu2_1'] = relu(net['conv2_1'])
W, b = get_weights(vgg, 7)
net['conv2_2'] = convolution(net['relu2_1'], W, b)
net['relu2_2'] = relu(net['conv2_2'])
pooling = pooling_type(vgg, 9)
net['pool2'] = pool(net['relu2_2'], pooling)
# Layer 3
W, b = get_weights(vgg, 10)
net['conv3_1'] = convolution(net['pool2'], W, b)
net['relu3_1'] = relu(net['conv3_1'])
W, b = get_weights(vgg, 12)
net['conv3_2'] = convolution(net['relu3_1'], W, b)
net['relu3_2'] = relu(net['conv3_2'])
W, b = get_weights(vgg, 14)
net['conv3_3'] = convolution(net['relu3_2'], W, b)
net['relu3_3'] = relu(net['conv3_3'])
W, b = get_weights(vgg, 16)
net['conv3_4'] = convolution(net['relu3_3'], W, b)
net['relu3_4'] = relu(net['conv3_4'])
pooling = pooling_type(vgg, 18)
net['pool3'] = pool(net['relu3_4'], pooling)
# Layer 4
W, b = get_weights(vgg, 19)
net['conv4_1'] = convolution(net['pool3'], W, b)
net['relu4_1'] = relu(net['conv4_1'])
W, b = get_weights(vgg, 21)
net['conv4_2'] = convolution(net['relu4_1'], W, b)
net['relu4_2'] = relu(net['conv4_2'])
W, b = get_weights(vgg, 23)
net['conv4_3'] = convolution(net['relu4_2'], W, b)
net['relu4_3'] = relu(net['conv4_3'])
W, b = get_weights(vgg, 25)
net['conv4_4'] = convolution(net['relu4_3'], W, b)
net['relu4_4'] = relu(net['conv4_4'])
pooling = pooling_type(vgg, 27)
net['pool4'] = pool(net['relu4_4'], pooling)
# Layer 5
W, b = get_weights(vgg, 28)
net['conv5_1'] = convolution(net['pool4'], W, b)
net['relu5_1'] = relu(net['conv5_1'])
W, b = get_weights(vgg, 30)
net['conv5_2'] = convolution(net['relu5_1'], W, b)
net['relu5_2'] = relu(net['conv5_2'])
W, b = get_weights(vgg, 32)
net['conv5_3'] = convolution(net['relu5_2'], W, b)
net['relu5_3'] = relu(net['conv5_3'])
W, b = get_weights(vgg, 34)
net['conv5_4'] = convolution(net['relu5_3'], W, b)
net['relu5_4'] = relu(net['conv5_4'])
pooling = pooling_type(vgg, 36)
net['pool5'] = pool(net['relu5_4'], pooling)
return net
'''
Structure of the VGG net:
VGG_net[x][0][0][y][0]
x: Layer number
y = 0: Name of layer
y = 1: Type of layer {conv, relu, pool}
y = 2: Value in layer {weights and biases, 0, type of pooling}
'''
def get_weights(vgg, layer):
'''
Returns the weights and biases for each convolution layer.
'''
weights = tf.constant(vgg[layer][0][0][2][0][0])
temp = vgg[layer][0][0][2][0][1]
biases = tf.constant(np.reshape(temp, (temp.size)))
return weights, biases
def pooling_type(vgg, layer):
'''
Finds the type of pooling at each pooling layer
'''
return vgg[layer][0][0][2][0]
def convolution(layer_input, weights, biases):
'''
Performs a convolution operation
'''
conv = tf.nn.conv2d(layer_input, weights, [1, 1, 1, 1], padding='SAME')
conv = conv + biases
return conv
def relu(layer_input):
'''
Performs an activation with ReLU
'''
reLU = tf.nn.relu(layer_input)
return reLU
def pool(layer_input, pooling_type):
'''
Performs pooling operation
'''
if pooling_type == 'max':
pooling = tf.nn.max_pool(layer_input, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
else:
pooling = tf.nn.avg_pool(layer_input, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
return pooling