Skip to content

Commit 3e7fe8a

Browse files
author
miguelCalado
committed
Fixed pylint and added docstrings
1 parent b219bbf commit 3e7fe8a

File tree

3 files changed

+103
-63
lines changed

3 files changed

+103
-63
lines changed

official/legacy/image_classification/classifier_trainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from official.modeling import performance
3737
from official.utils import hyperparams_flags
3838
from official.utils.misc import keras_utils
39-
from official.vision.image_classification.vgg16 import vgg_model
39+
from official.legacy.image_classification.vgg16 import vgg_model
4040

4141

4242
def get_models() -> Mapping[str, tf.keras.Model]:

official/legacy/image_classification/vgg16/vgg_config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
from __future__ import print_function
2020

2121
import dataclasses
22-
22+
from official.legacy.image_classification.configs import base_configs
2323
from official.modeling.hyperparams import base_config
24-
from official.vision.image_classification.configs import base_configs
2524

2625

2726
@dataclasses.dataclass

official/legacy/image_classification/vgg16/vgg_model.py

Lines changed: 101 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""VGG16 model for Keras.
16+
17+
Adapted from tf.keras.applications.vgg16.VGG16().
18+
19+
Related papers/blogs:
20+
- https://arxiv.org/abs/1409.1556
21+
22+
"""
123
from __future__ import absolute_import
224
from __future__ import division
325
from __future__ import print_function
@@ -6,16 +28,30 @@
628

729
layers = tf.keras.layers
830

31+
932
def _gen_l2_regularizer(use_l2_regularizer=True, l2_weight_decay=1e-4):
1033
return tf.keras.regularizers.L2(
1134
l2_weight_decay) if use_l2_regularizer else None
1235

36+
1337
def vgg16(num_classes,
1438
batch_size=None,
1539
use_l2_regularizer=True,
1640
batch_norm_decay=0.9,
1741
batch_norm_epsilon=1e-5):
42+
"""Instantiates the VGG16 architecture
43+
44+
Args:
45+
num_classes: `int` number of classes for image classification.
46+
batch_size: Size of the batches for each step.
47+
use_l2_regularizer: whether to use L2 regularizer on Conv/Dense layer.
48+
batch_norm_decay: Moment of batch norm layers.
49+
batch_norm_epsilon: Epsilon of batch borm layers.
50+
51+
Returns:
52+
A Keras model instance.
1853
54+
"""
1955
input_shape = (224, 224, 3)
2056
img_input = layers.Input(shape=input_shape, batch_size=batch_size)
2157

@@ -26,22 +62,23 @@ def vgg16(num_classes,
2662
bn_axis = 1
2763
else: # channels_last
2864
bn_axis = 3
29-
3065
# Block 1
31-
x = layers.Conv2D(64, (3, 3),
32-
padding='same',
33-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
34-
name='block1_conv1')(x)
66+
x = layers.Conv2D(
67+
64, (3, 3),
68+
padding='same',
69+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
70+
name='block1_conv1')(x)
3571
x = layers.BatchNormalization(
3672
axis=bn_axis,
3773
momentum=batch_norm_decay,
3874
epsilon=batch_norm_epsilon,
3975
name='bn_conv1')(x)
4076
x = layers.Activation('relu')(x)
41-
x = layers.Conv2D(64, (3, 3),
42-
padding='same',
43-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
44-
name='block1_conv2')(x)
77+
x = layers.Conv2D(
78+
64, (3, 3),
79+
padding='same',
80+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
81+
name='block1_conv2')(x)
4582
x = layers.BatchNormalization(
4683
axis=bn_axis,
4784
momentum=batch_norm_decay,
@@ -51,20 +88,22 @@ def vgg16(num_classes,
5188
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
5289

5390
# Block 2
54-
x = layers.Conv2D(128, (3, 3),
55-
padding='same',
56-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
57-
name='block2_conv1')(x)
91+
x = layers.Conv2D(
92+
128, (3, 3),
93+
padding='same',
94+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
95+
name='block2_conv1')(x)
5896
x = layers.BatchNormalization(
5997
axis=bn_axis,
6098
momentum=batch_norm_decay,
6199
epsilon=batch_norm_epsilon,
62100
name='bn_conv3')(x)
63101
x = layers.Activation('relu')(x)
64-
x = layers.Conv2D(128, (3, 3),
65-
padding='same',
66-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
67-
name='block2_conv2')(x)
102+
x = layers.Conv2D(
103+
128, (3, 3),
104+
padding='same',
105+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
106+
name='block2_conv2')(x)
68107
x = layers.BatchNormalization(
69108
axis=bn_axis,
70109
momentum=batch_norm_decay,
@@ -74,30 +113,33 @@ def vgg16(num_classes,
74113
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
75114

76115
# Block 3
77-
x = layers.Conv2D(256, (3, 3),
78-
padding='same',
79-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
80-
name='block3_conv1')(x)
116+
x = layers.Conv2D(
117+
256, (3, 3),
118+
padding='same',
119+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
120+
name='block3_conv1')(x)
81121
x = layers.BatchNormalization(
82122
axis=bn_axis,
83123
momentum=batch_norm_decay,
84124
epsilon=batch_norm_epsilon,
85125
name='bn_conv5')(x)
86126
x = layers.Activation('relu')(x)
87-
x = layers.Conv2D(256, (3, 3),
88-
padding='same',
89-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
90-
name='block3_conv2')(x)
127+
x = layers.Conv2D(
128+
256, (3, 3),
129+
padding='same',
130+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
131+
name='block3_conv2')(x)
91132
x = layers.BatchNormalization(
92133
axis=bn_axis,
93134
momentum=batch_norm_decay,
94135
epsilon=batch_norm_epsilon,
95136
name='bn_conv6')(x)
96137
x = layers.Activation('relu')(x)
97-
x = layers.Conv2D(256, (3, 3),
98-
padding='same',
99-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
100-
name='block3_conv3')(x)
138+
x = layers.Conv2D(
139+
256, (3, 3),
140+
padding='same',
141+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
142+
name='block3_conv3')(x)
101143
x = layers.BatchNormalization(
102144
axis=bn_axis,
103145
momentum=batch_norm_decay,
@@ -107,30 +149,33 @@ def vgg16(num_classes,
107149
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
108150

109151
# Block 4
110-
x = layers.Conv2D(512, (3, 3),
111-
padding='same',
112-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
113-
name='block4_conv1')(x)
152+
x = layers.Conv2D(
153+
512, (3, 3),
154+
padding='same',
155+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
156+
name='block4_conv1')(x)
114157
x = layers.BatchNormalization(
115158
axis=bn_axis,
116159
momentum=batch_norm_decay,
117160
epsilon=batch_norm_epsilon,
118161
name='bn_conv8')(x)
119162
x = layers.Activation('relu')(x)
120-
x = layers.Conv2D(512, (3, 3),
121-
padding='same',
122-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
123-
name='block4_conv2')(x)
163+
x = layers.Conv2D(
164+
512, (3, 3),
165+
padding='same',
166+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
167+
name='block4_conv2')(x)
124168
x = layers.BatchNormalization(
125169
axis=bn_axis,
126170
momentum=batch_norm_decay,
127171
epsilon=batch_norm_epsilon,
128172
name='bn_conv9')(x)
129173
x = layers.Activation('relu')(x)
130-
x = layers.Conv2D(512, (3, 3),
131-
padding='same',
132-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
133-
name='block4_conv3')(x)
174+
x = layers.Conv2D(
175+
512, (3, 3),
176+
padding='same',
177+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
178+
name='block4_conv3')(x)
134179
x = layers.BatchNormalization(
135180
axis=bn_axis,
136181
momentum=batch_norm_decay,
@@ -140,30 +185,33 @@ def vgg16(num_classes,
140185
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
141186

142187
# Block 5
143-
x = layers.Conv2D(512, (3, 3),
144-
padding='same',
145-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
146-
name='block5_conv1')(x)
188+
x = layers.Conv2D(
189+
512, (3, 3),
190+
padding='same',
191+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
192+
name='block5_conv1')(x)
147193
x = layers.BatchNormalization(
148194
axis=bn_axis,
149195
momentum=batch_norm_decay,
150196
epsilon=batch_norm_epsilon,
151197
name='bn_conv11')(x)
152198
x = layers.Activation('relu')(x)
153-
x = layers.Conv2D(512, (3, 3),
154-
padding='same',
155-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
156-
name='block5_conv2')(x)
199+
x = layers.Conv2D(
200+
512, (3, 3),
201+
padding='same',
202+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
203+
name='block5_conv2')(x)
157204
x = layers.BatchNormalization(
158205
axis=bn_axis,
159206
momentum=batch_norm_decay,
160207
epsilon=batch_norm_epsilon,
161208
name='bn_conv12')(x)
162209
x = layers.Activation('relu')(x)
163-
x = layers.Conv2D(512, (3, 3),
164-
padding='same',
165-
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
166-
name='block5_conv3')(x)
210+
x = layers.Conv2D(
211+
512, (3, 3),
212+
padding='same',
213+
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
214+
name='block5_conv3')(x)
167215
x = layers.BatchNormalization(
168216
axis=bn_axis,
169217
momentum=batch_norm_decay,
@@ -174,23 +222,17 @@ def vgg16(num_classes,
174222

175223
x = layers.Flatten(name='flatten')(x)
176224
x = layers.Dense(4096,
177-
#kernel_initializer=tf.initializers.random_normal(stddev=0.01),
178225
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
179-
#bias_regularizer=_gen_l2_regularizer(use_l2_regularizer),
180226
name='fc1')(x)
181227
x = layers.Activation('relu')(x)
182228
x = layers.Dropout(0.5)(x)
183229
x = layers.Dense(4096,
184-
#kernel_initializer=tf.initializers.random_normal(stddev=0.01),
185230
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
186-
#bias_regularizer=_gen_l2_regularizer(use_l2_regularizer),
187231
name='fc2')(x)
188232
x = layers.Activation('relu')(x)
189233
x = layers.Dropout(0.5)(x)
190234
x = layers.Dense(num_classes,
191-
#kernel_initializer=tf.initializers.random_normal(stddev=0.01),
192235
kernel_regularizer=_gen_l2_regularizer(use_l2_regularizer),
193-
#bias_regularizer=_gen_l2_regularizer(use_l2_regularizer),
194236
name='fc1000')(x)
195237

196238
# A softmax that is followed by the model loss must be done cannot be done
@@ -199,4 +241,3 @@ def vgg16(num_classes,
199241

200242
# Create model.
201243
return tf.keras.Model(img_input, x, name='vgg16')
202-

0 commit comments

Comments
 (0)