Skip to content

Instantly share code, notes, and snippets.

@sidgan
Last active September 4, 2017 19:03
Show Gist options
  • Save sidgan/e8d8d3fe2ceb686be867e3ca8cd5dca8 to your computer and use it in GitHub Desktop.
Save sidgan/e8d8d3fe2ceb686be867e3ca8cd5dca8 to your computer and use it in GitHub Desktop.
nn4 architecture for facenet implementation by David Sandberg
# MIT License
#
# Copyright (c) 2016 David Sandberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# pylint: disable=missing-docstring
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import models.network as network
'''
CHANGES:
Changed max pooling to LP pooling according to facenet paper
Average pooling of 5x5 added
Fully connected layer added to give 128 outputs
'''
def inference(images, keep_probability, phase_train=True, bottleneck_layer_size=128, weight_decay=0.0):
""" Define an inference network for face recognition based
on inception modules using batch normalization
Args:
images: The images to run inference on, dimensions batch_size x height x width x channels
phase_train: True if batch normalization should operate in training mode
"""
endpoints = {}
net = network.conv(images, 3, 64, 7, 7, 2, 2, 'SAME', 'conv1_7x7', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['conv1'] = net
net = network.mpool(net, 3, 3, 2, 2, 'SAME', 'pool1')
endpoints['pool1'] = net
net = network.conv(net, 64, 64, 1, 1, 1, 1, 'SAME', 'conv2_1x1', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['conv2_1x1'] = net
net = network.conv(net, 64, 192, 3, 3, 1, 1, 'SAME', 'conv3_3x3', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['conv3_3x3'] = net
net = network.mpool(net, 3, 3, 2, 2, 'SAME', 'pool3')
endpoints['pool3'] = net
net = network.inception(net, 192, 1, 64, 96, 128, 16, 32, 3, 32, 1, 'MAX', 'incept3a', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept3a'] = net
#Changed max pooling to LP pooling according to facenet paper
net = network.inception(net, 256, 1, 64, 96, 128, 32, 64, 3, 64, 1, 'L2', 'incept3b', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept3b'] = net
net = network.inception(net, 320, 2, 0, 128, 256, 32, 64, 3, 0, 2, 'MAX', 'incept3c', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept3c'] = net
#Changed max pooling to LP pooling according to facenet paper
net = network.inception(net, 640, 1, 256, 96, 192, 32, 64, 3, 128, 1, 'L2', 'incept4a', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept4a'] = net
#Changed max pooling to LP pooling according to facenet paper
net = network.inception(net, 640, 1, 224, 112, 224, 32, 64, 3, 128, 1, 'L2', 'incept4b', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept4b'] = net
#Changed max pooling to LP pooling according to facenet paper
net = network.inception(net, 640, 1, 192, 128, 256, 32, 64, 3, 128, 1, 'L2', 'incept4c', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept4c'] = net
#Changed max pooling to LP pooling according to facenet paper
net = network.inception(net, 640, 1, 160, 144, 288, 32, 64, 3, 128, 1, 'L2', 'incept4d', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept4d'] = net
net = network.inception(net, 640, 2, 0, 160, 256, 64, 128, 3, 0, 2, 'MAX', 'incept4e', phase_train=phase_train, use_batch_norm=True)
endpoints['incept4e'] = net
#Changed max pooling to LP pooling according to facenet paper
net = network.inception(net, 1024, 1, 384, 192, 384, 48, 128, 3, 128, 1, 'L2', 'incept5a', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept5a'] = net
net = network.inception(net, 1024, 1, 384, 192, 384, 48, 128, 3, 128, 1, 'MAX', 'incept5b', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
endpoints['incept5b'] = net
#Average pooling of 5x5 added
net = network.apool(net, 5, 5, 1, 1, 'VALID', 'pool6')
endpoints['pool6'] = net
#Fully connected layer added to give 128 outputs
net = tf.contrib.layers.fully_connected(net, num_outputs=128,activation_fn=tf.nn.relu,trainable=True)
endpoints['prelogits'] = net
net = tf.reshape(net, [-1,128])
endpoints['prelogits'] = net
net = tf.nn.dropout(net, keep_probability)
endpoints['dropout'] = net
return net, endpoints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment