Skip to content
Snippets Groups Projects
Commit 9013c0b8 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Removed VGG16

Documented exponential moving average

Removed comments
parent 82cf9beb
No related branches found
No related tags found
1 merge request!45Exponential Moving Average + Batch Normalization + fixes in the losses
Pipeline #
...@@ -80,6 +80,12 @@ class Logits(estimator.Estimator): ...@@ -80,6 +80,12 @@ class Logits(estimator.Estimator):
"scopes": dict({"<SOURCE_SCOPE>/": "<TARGET_SCOPE>/"}), "scopes": dict({"<SOURCE_SCOPE>/": "<TARGET_SCOPE>/"}),
"trainable_variables": [<LIST OF VARIABLES OR SCOPES THAT YOU WANT TO RETRAIN>] "trainable_variables": [<LIST OF VARIABLES OR SCOPES THAT YOU WANT TO RETRAIN>]
} }
apply_moving_averages: bool
Apply exponential moving average in the training variables and in the loss.
https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage
By default the decay for the variable averages is 0.9999 and for the loss is 0.9
""" """
def __init__(self, def __init__(self,
...@@ -269,6 +275,13 @@ class LogitsCenterLoss(estimator.Estimator): ...@@ -269,6 +275,13 @@ class LogitsCenterLoss(estimator.Estimator):
"scopes": dict({"<SOURCE_SCOPE>/": "<TARGET_SCOPE>/"}), "scopes": dict({"<SOURCE_SCOPE>/": "<TARGET_SCOPE>/"}),
"trainable_variables": [<LIST OF VARIABLES OR SCOPES THAT YOU WANT TO TRAIN>] "trainable_variables": [<LIST OF VARIABLES OR SCOPES THAT YOU WANT TO TRAIN>]
} }
apply_moving_averages: bool
Apply exponential moving average in the training variables and in the loss.
https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage
By default the decay for the variable averages is 0.9999 and for the loss is 0.9
""" """
......
...@@ -30,7 +30,6 @@ def mean_cross_entropy_loss(logits, labels, add_regularization_losses=True): ...@@ -30,7 +30,6 @@ def mean_cross_entropy_loss(logits, labels, add_regularization_losses=True):
tf.summary.scalar('cross_entropy_loss', cross_loss) tf.summary.scalar('cross_entropy_loss', cross_loss)
tf.add_to_collection(tf.GraphKeys.LOSSES, cross_loss) tf.add_to_collection(tf.GraphKeys.LOSSES, cross_loss)
if add_regularization_losses: if add_regularization_losses:
regularization_losses = tf.get_collection( regularization_losses = tf.get_collection(
tf.GraphKeys.REGULARIZATION_LOSSES) tf.GraphKeys.REGULARIZATION_LOSSES)
......
...@@ -321,13 +321,6 @@ def inception_resnet_v2(inputs, ...@@ -321,13 +321,6 @@ def inception_resnet_v2(inputs,
[slim.batch_norm, slim.dropout], [slim.batch_norm, slim.dropout],
is_training=(mode == tf.estimator.ModeKeys.TRAIN)): is_training=(mode == tf.estimator.ModeKeys.TRAIN)):
#with slim.arg_scope(
# [slim.conv2d, slim.fully_connected],
# weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
# weights_regularizer=slim.l2_regularizer(weight_decay),
# normalizer_fn=slim.batch_norm,
# normalizer_params=batch_norm_params):
with slim.arg_scope( with slim.arg_scope(
[slim.conv2d, slim.max_pool2d, slim.avg_pool2d], [slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
stride=1, stride=1,
......
...@@ -5,7 +5,6 @@ from .MLP import mlp ...@@ -5,7 +5,6 @@ from .MLP import mlp
from .Embedding import Embedding from .Embedding import Embedding
from .InceptionResnetV2 import inception_resnet_v2, inception_resnet_v2_batch_norm from .InceptionResnetV2 import inception_resnet_v2, inception_resnet_v2_batch_norm
from .InceptionResnetV1 import inception_resnet_v1, inception_resnet_v1_batch_norm from .InceptionResnetV1 import inception_resnet_v1, inception_resnet_v1_batch_norm
from .vgg import vgg_16
from . import SimpleCNN from . import SimpleCNN
......
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Contains model definitions for versions of the Oxford VGG network.
These model definitions were introduced in the following technical report:
Very Deep Convolutional Networks For Large-Scale Image Recognition
Karen Simonyan and Andrew Zisserman
arXiv technical report, 2015
PDF: http://arxiv.org/pdf/1409.1556.pdf
ILSVRC 2014 Slides: http://www.robots.ox.ac.uk/~karen/pdf/ILSVRC_2014.pdf
CC-BY-4.0
More information can be obtained from the VGG website:
www.robots.ox.ac.uk/~vgg/research/very_deep/
Usage:
with slim.arg_scope(vgg.vgg_arg_scope()):
outputs, end_points = vgg.vgg_a(inputs)
with slim.arg_scope(vgg.vgg_arg_scope()):
outputs, end_points = vgg.vgg_16(inputs)
@@vgg_a
@@vgg_16
@@vgg_19
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.contrib import layers
from tensorflow.contrib.framework.python.ops import arg_scope
from tensorflow.contrib.layers.python.layers import layers as layers_lib
from tensorflow.contrib.layers.python.layers import regularizers
from tensorflow.contrib.layers.python.layers import utils
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import variable_scope
import tensorflow.contrib.slim as slim
import tensorflow as tf
def vgg_arg_scope(weight_decay=0.0005):
"""Defines the VGG arg scope.
Args:
weight_decay: The l2 regularization coefficient.
Returns:
An arg_scope.
"""
with arg_scope(
[layers.conv2d, layers_lib.fully_connected],
activation_fn=nn_ops.relu,
weights_regularizer=regularizers.l2_regularizer(weight_decay),
biases_initializer=init_ops.zeros_initializer()):
with arg_scope([layers.conv2d], padding='SAME') as arg_sc:
return arg_sc
def vgg_a(inputs,
num_classes=1000,
is_training=True,
dropout_keep_prob=0.5,
spatial_squeeze=True,
scope='vgg_a'):
"""Oxford Net VGG 11-Layers version A Example.
Note: All the fully_connected layers have been transformed to conv2d layers.
To use in classification mode, resize input to 224x224.
Args:
inputs: a tensor of size [batch_size, height, width, channels].
num_classes: number of predicted classes.
is_training: whether or not the model is being trained.
dropout_keep_prob: the probability that activations are kept in the dropout
layers during training.
spatial_squeeze: whether or not should squeeze the spatial dimensions of the
outputs. Useful to remove unnecessary dimensions for classification.
scope: Optional scope for the variables.
Returns:
the last op containing the log predictions and end_points dict.
"""
with variable_scope.variable_scope(scope, 'vgg_a', [inputs]) as sc:
end_points_collection = sc.original_name_scope + '_end_points'
# Collect outputs for conv2d, fully_connected and max_pool2d.
with arg_scope(
[layers.conv2d, layers_lib.max_pool2d],
outputs_collections=end_points_collection):
net = layers_lib.repeat(
inputs, 1, layers.conv2d, 64, [3, 3], scope='conv1')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool1')
net = layers_lib.repeat(net, 1, layers.conv2d, 128, [3, 3], scope='conv2')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool2')
net = layers_lib.repeat(net, 2, layers.conv2d, 256, [3, 3], scope='conv3')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool3')
net = layers_lib.repeat(net, 2, layers.conv2d, 512, [3, 3], scope='conv4')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool4')
net = layers_lib.repeat(net, 2, layers.conv2d, 512, [3, 3], scope='conv5')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool5')
# Use conv2d instead of fully_connected layers.
net = layers.conv2d(net, 4096, [7, 7], padding='VALID', scope='fc6')
net = layers_lib.dropout(
net, dropout_keep_prob, is_training=is_training, scope='dropout6')
net = layers.conv2d(net, 4096, [1, 1], scope='fc7')
net = layers_lib.dropout(
net, dropout_keep_prob, is_training=is_training, scope='dropout7')
net = layers.conv2d(
net,
num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
scope='fc8')
# Convert end_points_collection into a end_point dict.
end_points = utils.convert_collection_to_dict(end_points_collection)
if spatial_squeeze:
net = array_ops.squeeze(net, [1, 2], name='fc8/squeezed')
end_points[sc.name + '/fc8'] = net
return net, end_points
vgg_a.default_image_size = 224
def vgg_16(inputs,
mode=tf.estimator.ModeKeys.TRAIN,
reuse=None,
dropout_keep_prob=0.5,
spatial_squeeze=True,
scope='vgg_16',
trainable_variables=None,
**kwargs):
"""Oxford Net VGG 16-Layers version D Example.
Note: All the fully_connected layers have been transformed to conv2d layers.
To use in classification mode, resize input to 224x224.
Args:
inputs: a tensor of size [batch_size, height, width, channels].
num_classes: number of predicted classes.
is_training: whether or not the model is being trained.
dropout_keep_prob: the probability that activations are kept in the dropout
layers during training.
spatial_squeeze: whether or not should squeeze the spatial dimensions of the
outputs. Useful to remove unnecessary dimensions for classification.
scope: Optional scope for the variables.
Returns:
the last op containing the log predictions and end_points dict.
"""
is_training = mode == tf.estimator.ModeKeys.TRAIN
end_points = dict()
with variable_scope.variable_scope(scope, 'vgg_16', [inputs]) as sc:
end_points_collection = sc.original_name_scope + '_end_points'
# Collect outputs for conv2d, fully_connected and max_pool2d.
with arg_scope(
[layers.conv2d, layers_lib.fully_connected, layers_lib.max_pool2d],
outputs_collections=end_points_collection):
with slim.arg_scope(vgg_arg_scope()):
name = "conv1"
net = layers_lib.repeat(
inputs, 2, layers.conv2d, 64, [3, 3], scope=name, reuse=reuse)
end_points[name] = net
net = layers_lib.max_pool2d(net, [2, 2], scope='pool1')
name = "conv2"
net = layers_lib.repeat(net, 2, layers.conv2d, 128, [3, 3], scope=name, reuse=reuse)
end_points[name] = net
net = layers_lib.max_pool2d(net, [2, 2], scope='pool2')
name = "conv3"
net = layers_lib.repeat(net, 3, layers.conv2d, 256, [3, 3], scope=name, reuse=reuse)
end_points[name] = net
net = layers_lib.max_pool2d(net, [2, 2], scope='pool3')
name = "conv4"
net = layers_lib.repeat(net, 3, layers.conv2d, 512, [3, 3], scope=name, reuse=reuse)
end_points[name] = net
net = layers_lib.max_pool2d(net, [2, 2], scope='pool4')
name = "conv5"
net = layers_lib.repeat(net, 3, layers.conv2d, 512, [3, 3], scope=name, reuse=reuse)
end_points[name] = net
net = layers_lib.max_pool2d(net, [2, 2], scope='pool5')
# Tiago: Make things flat
net = layers_lib.flatten(net)
# Use conv2d instead of fully_connected layers.
# net = layers.conv2d(net, 4096, [7, 7], padding='VALID', scope='fc6', reuse=reuse)
# net = layers.conv2d(net, 4096, [7, 7], padding='VALID', scope='fc6', reuse=reuse)
name = "fc6"
net = layers.fully_connected(net, 4096, activation_fn=tf.nn.relu, scope=name, reuse=reuse)
end_points[name] = net
net = layers_lib.dropout(
net, dropout_keep_prob, is_training=is_training, scope='dropout6')
# net = layers.conv2d(net, 4096, [1, 1], scope='fc7', reuse=reuse)
name = "fc7"
net = layers.fully_connected(net, 4096, activation_fn=tf.nn.relu, scope=name, reuse=reuse)
end_points[name] = net
net = layers_lib.dropout(
net, dropout_keep_prob, is_training=is_training, scope='dropout7')
return net, end_points
vgg_16.default_image_size = 224
def vgg_19(inputs,
num_classes=1000,
is_training=True,
dropout_keep_prob=0.5,
spatial_squeeze=True,
scope='vgg_19'):
"""Oxford Net VGG 19-Layers version E Example.
Note: All the fully_connected layers have been transformed to conv2d layers.
To use in classification mode, resize input to 224x224.
Args:
inputs: a tensor of size [batch_size, height, width, channels].
num_classes: number of predicted classes.
is_training: whether or not the model is being trained.
dropout_keep_prob: the probability that activations are kept in the dropout
layers during training.
spatial_squeeze: whether or not should squeeze the spatial dimensions of the
outputs. Useful to remove unnecessary dimensions for classification.
scope: Optional scope for the variables.
Returns:
the last op containing the log predictions and end_points dict.
"""
with variable_scope.variable_scope(scope, 'vgg_19', [inputs]) as sc:
end_points_collection = sc.name + '_end_points'
# Collect outputs for conv2d, fully_connected and max_pool2d.
with arg_scope(
[layers.conv2d, layers_lib.fully_connected, layers_lib.max_pool2d],
outputs_collections=end_points_collection):
with slim.arg_scope(vgg_arg_scope()):
net = layers_lib.repeat(
inputs, 2, layers.conv2d, 64, [3, 3], scope='conv1')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool1')
net = layers_lib.repeat(net, 2, layers.conv2d, 128, [3, 3], scope='conv2')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool2')
net = layers_lib.repeat(net, 4, layers.conv2d, 256, [3, 3], scope='conv3')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool3')
net = layers_lib.repeat(net, 4, layers.conv2d, 512, [3, 3], scope='conv4')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool4')
net = layers_lib.repeat(net, 4, layers.conv2d, 512, [3, 3], scope='conv5')
net = layers_lib.max_pool2d(net, [2, 2], scope='pool5')
# Use conv2d instead of fully_connected layers.
net = layers.conv2d(net, 4096, [7, 7], padding='VALID', scope='fc6')
net = layers_lib.dropout(
net, dropout_keep_prob, is_training=is_training, scope='dropout6')
net = layers.conv2d(net, 4096, [1, 1], scope='fc7')
net = layers_lib.dropout(
net, dropout_keep_prob, is_training=is_training, scope='dropout7')
net = layers.conv2d(
net,
num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
scope='fc8')
# Convert end_points_collection into a end_point dict.
end_points = utils.convert_collection_to_dict(end_points_collection)
if spatial_squeeze:
net = array_ops.squeeze(net, [1, 2], name='fc8/squeezed')
end_points[sc.name + '/fc8'] = net
return net, end_points
vgg_19.default_image_size = 224
# Alias
vgg_d = vgg_16
vgg_e = vgg_19
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment