-
Notifications
You must be signed in to change notification settings - Fork 18
/
vgg.py
183 lines (153 loc) · 7.99 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from models.imgclfmodel import ImgClfModel
from dataset.dataset import Dataset
import tensorflow as tf
from tensorflow.contrib.layers import conv2d
from tensorflow.contrib.layers import max_pool2d
from tensorflow.contrib.layers import flatten
from tensorflow.contrib.layers import fully_connected
"""
Implementation of VGGs from ILSVRC 2014. The original architecture is invented by VGG (Visual Geometry Group) @Oxford.
This one didnt' win the ILSVRC 2014, but it took the 2nd place. It is very popular and well-known to lots of new comers in deep learning area.
The main technical contributions from this architecture are "3x3 filters", and very simple architecture with deeper depth.
"""
class VGG(ImgClfModel):
def __init__(self, model_type='D'):
ImgClfModel.__init__(self, scale_to_imagenet=True, model_type=model_type)
"""
types
A : 11 weight layers
A-LRN : 11 weight layers with Local Response Normalization
B : 13 weight layers
C : 16 weight layers with 1D conv layers
D : 16 weight layers
E : 19 weight layers
"""
def create_model(self, input):
self.group1 = []
self.group2 = []
self.group3 = []
self.group4 = []
self.group5 = []
with tf.variable_scope('group1'):
# LAYER GROUP #1
group_1 = conv2d(input, num_outputs=64,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group1.append(group_1)
if self.model_type == 'A-LRN':
group_1 = tf.nn.local_response_normalization(group_1,
bias=2, alpha=0.0001, beta=0.75)
self.group1.append(group_1)
if self.model_type != 'A' and self.model_type == 'A-LRN':
group_1 = conv2d(group_1, num_outputs=64,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group1.append(group_1)
group_1 = max_pool2d(group_1, kernel_size=[2,2], stride=2)
self.group1.append(group_1)
with tf.variable_scope('group2'):
# LAYER GROUP #2
group_2 = conv2d(group_1, num_outputs=128,
kernel_size=[3, 3], padding='SAME',
activation_fn=tf.nn.relu)
self.group2.append(group_2)
if self.model_type != 'A' and self.model_type == 'A-LRN':
group_2 = conv2d(group_2, num_outputs=128,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group2.append(group_2)
group_2 = max_pool2d(group_2, kernel_size=[2,2], stride=2)
self.group2.append(group_2)
with tf.variable_scope('group3'):
# LAYER GROUP #3
group_3 = conv2d(group_2, num_outputs=256,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group3.append(group_3)
group_3 = conv2d(group_3, num_outputs=256,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group3.append(group_3)
if self.model_type == 'C':
group_3 = conv2d(group_3, num_outputs=256,
kernel_size=[1,1], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group3.append(group_3)
if self.model_type == 'D' or self.model_type == 'E':
group_3 = conv2d(group_3, num_outputs=256,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group3.append(group_3)
if self.model_type == 'E':
group_3 = conv2d(group_3, num_outputs=256,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group3.append(group_3)
group_3 = max_pool2d(group_3, kernel_size=[2,2], stride=2)
self.group3.append(group_3)
with tf.variable_scope('group4'):
# LAYER GROUP #4
group_4 = conv2d(group_3, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group4.append(group_4)
group_4 = conv2d(group_4, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group4.append(group_4)
if self.model_type == 'C':
group_4 = conv2d(group_4, num_outputs=512,
kernel_size=[1,1], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group4.append(group_4)
if self.model_type == 'D' or self.model_type == 'E':
group_4 = conv2d(group_4, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group4.append(group_4)
if self.model_type == 'E':
group_4 = conv2d(group_4, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group4.append(group_4)
group_4 = max_pool2d(group_4, kernel_size=[2,2], stride=2)
self.group4.append(group_4)
with tf.variable_scope('group5'):
# LAYER GROUP #5
group_5 = conv2d(group_4, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group5.append(group_5)
group_5 = conv2d(group_5, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group5.append(group_5)
if self.model_type == 'C':
group_5 = conv2d(group_5, num_outputs=512,
kernel_size=[1,1], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group5.append(group_5)
if self.model_type == 'D' or self.model_type == 'E':
group_5 = conv2d(group_5, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group5.append(group_5)
if self.model_type == 'E':
group_5 = conv2d(group_5, num_outputs=512,
kernel_size=[3,3], stride=1, padding='SAME',
activation_fn=tf.nn.relu)
self.group5.append(group_5)
group_5 = max_pool2d(group_5, kernel_size=[2,2], stride=2)
self.group5.append(group_5)
with tf.variable_scope('fcl'):
# 1st FC 4096
self.flat = flatten(group_5)
self.fcl1 = fully_connected(self.flat, num_outputs=4096, activation_fn=tf.nn.relu)
self.dr1 = tf.nn.dropout(self.fcl1, 0.5)
# 2nd FC 4096
self.fcl2 = fully_connected(self.dr1, num_outputs=4096, activation_fn=tf.nn.relu)
self.dr2 = tf.nn.dropout(self.fcl2, 0.5)
with tf.variable_scope('final'):
# 3rd FC 1000
self.out = fully_connected(self.dr2, num_outputs=self.num_classes, activation_fn=None)
return [self.out]