Commit 2b86a999 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

remove the network folder

parent c75e4792
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
import tensorflow as tf
def chopra(
inputs,
conv1_kernel_size=[7, 7],
conv1_output=15,
pooling1_size=[2, 2],
conv2_kernel_size=[6, 6],
conv2_output=45,
pooling2_size=[4, 3],
fc1_output=250,
seed=10,
reuse=False,
):
"""Class that creates the architecture presented in the paper:
Chopra, Sumit, Raia Hadsell, and Yann LeCun. "Learning a similarity metric discriminatively, with application to
face verification." 2005 IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR'05). Vol. 1. IEEE, 2005.
This is modifield version of the original architecture.
It is inspired on https://gitlab.idiap.ch/bob/xfacereclib.cnn/blob/master/lua/network.lua
-- C1 : Convolutional, kernel = 7x7 pixels, 15 feature maps
-- M2 : MaxPooling, 2x2
-- HT : Hard Hyperbolic Tangent
-- C3 : Convolutional, kernel = 6x6 pixels, 45 feature maps
-- M4 : MaxPooling, 4x3
-- HT : Hard Hyperbolic Tangent
-- R : Reshaping layer HT 5x5 => 25 (45 times; once for each feature map)
-- L5 : Linear 25 => 250
**Parameters**
conv1_kernel_size:
conv1_output:
pooling1_size:
conv2_kernel_size:
conv2_output:
pooling2_size
fc1_output:
seed:
"""
slim = tf.contrib.slim
end_points = dict()
initializer = tf.contrib.layers.xavier_initializer(
uniform=False, dtype=tf.float32, seed=seed)
graph = slim.conv2d(
inputs,
conv1_output,
conv1_kernel_size,
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='conv1',
reuse=reuse)
end_points['conv1'] = graph
graph = slim.max_pool2d(graph, pooling1_size, scope='pool1')
end_points['pool1'] = graph
graph = slim.conv2d(
graph,
conv2_output,
conv2_kernel_size,
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope='conv2',
reuse=reuse)
end_points['conv2'] = graph
graph = slim.max_pool2d(graph, pooling2_size, scope='pool2')
end_points['pool2'] = graph
graph = slim.flatten(graph, scope='flatten1')
end_points['flatten1'] = graph
graph = slim.fully_connected(
graph,
fc1_output,
weights_initializer=initializer,
activation_fn=None,
scope='fc1',
reuse=reuse)
end_points['fc1'] = graph
return graph, end_points
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
import tensorflow as tf
from .utils import is_trainable
def dummy(inputs,
reuse=False,
mode=tf.estimator.ModeKeys.TRAIN,
trainable_variables=None,
**kwargs):
"""
Create all the necessary variables for this CNN
Parameters
----------
inputs:
reuse:
mode:
trainable_variables:
"""
slim = tf.contrib.slim
end_points = dict()
# Here is my choice to shutdown the whole scope
trainable = is_trainable("Dummy", trainable_variables)
with tf.variable_scope('Dummy', reuse=reuse):
initializer = tf.contrib.layers.xavier_initializer()
name = 'conv1'
graph = slim.conv2d(
inputs,
10, [3, 3],
activation_fn=tf.nn.relu,
stride=1,
scope=name,
weights_initializer=initializer,
trainable=trainable)
end_points[name] = graph
graph = slim.max_pool2d(graph, [4, 4], scope='pool1')
end_points['pool1'] = graph
graph = slim.flatten(graph, scope='flatten1')
end_points['flatten1'] = graph
name = 'fc1'
graph = slim.fully_connected(
graph,
50,
weights_initializer=initializer,
activation_fn=None,
scope=name,
trainable=trainable)
end_points[name] = graph
return graph, end_points
This diff is collapsed.
This diff is collapsed.
from .InceptionResnetV2 import inception_resnet_v2_batch_norm
from .InceptionResnetV1 import inception_resnet_v1_batch_norm
from .SimpleCNN import base_architecture as simplecnn_arch
import numpy as np
import tensorflow as tf
def architecture(faces, mode, face_arch='InceptionResnetV2', **kwargs):
# construct patches inside the model
ksizes = strides = [1, 28, 28, 1]
rates = [1, 1, 1, 1]
patches = tf.extract_image_patches(faces, ksizes, strides, rates, 'VALID')
n_blocks = int(np.prod(patches.shape[1:3]))
# n_blocks should be 25 for 160x160 faces
patches = tf.reshape(patches, [-1, n_blocks, 28, 28, 3])
simplecnn_kwargs = {
'kernerl_size': (3, 3),
'data_format': 'channels_last',
'add_batch_norm': True,
'use_bias_with_batch_norm': False,
}
simplecnn_kwargs.update(kwargs)
endpoints = {}
# construct simplecnn from patches
for i in range(n_blocks):
if i == 0:
reuse = False
else:
reuse = True
with tf.variable_scope('SimpleCNN', reuse=reuse):
net, temp = simplecnn_arch(patches[:, i], mode, **simplecnn_kwargs)
if i == 0:
simplecnn_embeddings = net
endpoints.update(temp)
else:
simplecnn_embeddings += net
# average the embeddings of patches
simplecnn_embeddings /= n_blocks
# construct inception_resnet_v1 or 2 from faces
if face_arch == 'InceptionResnetV2':
face_embeddings, temp = inception_resnet_v2_batch_norm(
faces, mode=mode, **kwargs)
elif face_arch == 'InceptionResnetV1':
face_embeddings, temp = inception_resnet_v1_batch_norm(
faces, mode=mode, **kwargs)
endpoints.update(temp)
embeddings = tf.concat([simplecnn_embeddings, face_embeddings], 1)
endpoints['final_embeddings'] = embeddings
return embeddings, endpoints
def model_fn(features, labels, mode, params, config):
"""The model function for join face and patch PAD. The input to the model
is 160x160 faces."""
faces = features['data']
key = features['key']
# organize the parameters
params = params or {}
learning_rate = params.get('learning_rate', 1e-4)
apply_moving_averages = params.get('apply_moving_averages', True)
n_classes = params.get('n_classes', 2)
add_histograms = params.get('add_histograms')
face_arch = params.get('face_arch', 'InceptionResnetV2')
embeddings, _ = architecture(faces, mode, face_arch=face_arch)
# Logits layer
logits = tf.layers.dense(inputs=embeddings, units=n_classes, name='logits')
# # restore the model from an extra_checkpoint
# if extra_checkpoint is not None and mode == tf.estimator.ModeKeys.TRAIN:
# tf.train.init_from_checkpoint(
# ckpt_dir_or_file=extra_checkpoint["checkpoint_path"],
# assignment_map=extra_checkpoint["scopes"],
# )
predictions = {
# Generate predictions (for PREDICT and EVAL mode)
"classes": tf.argmax(input=logits, axis=1),
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the
# `logging_hook`.
"probabilities": tf.nn.softmax(logits, name="softmax_tensor"),
'key': key,
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
accuracy = tf.metrics.accuracy(
labels=labels, predictions=predictions["classes"])
metrics = {'accuracy': accuracy}
global_step = tf.train.get_or_create_global_step()
# Compute the moving average of all individual losses and the total loss.
if apply_moving_averages and mode == tf.estimator.ModeKeys.TRAIN:
variable_averages = tf.train.ExponentialMovingAverage(
0.9999, global_step)
variable_averages_op = variable_averages.apply(
tf.trainable_variables())
else:
variable_averages_op = tf.no_op(name='noop')
if mode == tf.estimator.ModeKeys.TRAIN:
# for batch normalization to be updated as well:
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
else:
update_ops = []
with tf.control_dependencies([variable_averages_op] + update_ops):
# Calculate Loss (for both TRAIN and EVAL modes)
cross_loss = tf.losses.sparse_softmax_cross_entropy(
logits=logits, labels=labels)
regularization_losses = tf.get_collection(
tf.GraphKeys.REGULARIZATION_LOSSES)
loss = tf.add_n(
[cross_loss] + regularization_losses, name="total_loss")
if apply_moving_averages and mode == tf.estimator.ModeKeys.TRAIN:
# Compute the moving average of all individual losses and the total
# loss.
loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
loss_averages_op = loss_averages.apply(
tf.get_collection(tf.GraphKeys.LOSSES))
else:
loss_averages_op = tf.no_op(name='noop')
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=learning_rate)
train_op = tf.group(
optimizer.minimize(loss, global_step=global_step),
variable_averages_op, loss_averages_op)
# Log accuracy and loss
with tf.name_scope('train_metrics'):
tf.summary.scalar('accuracy', accuracy[1])
tf.summary.scalar('cross_entropy_loss', cross_loss)
tf.summary.scalar('loss', loss)
if apply_moving_averages:
for l in tf.get_collection(tf.GraphKeys.LOSSES):
tf.summary.scalar(l.op.name + "_averaged",
loss_averages.average(l))
# add histograms summaries
if add_histograms == 'all':
for v in tf.all_variables():
tf.summary.histogram(v.name, v)
elif add_histograms == 'train':
for v in tf.trainable_variables():
tf.summary.histogram(v.name, v)
else:
train_op = None
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
loss=loss,
train_op=train_op,
eval_metric_ops=metrics)
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
import tensorflow as tf
from bob.learn.tensorflow.layers import maxout
from .utils import is_trainable
def light_cnn9(inputs,
seed=10,
reuse=False,
trainable_variables=None,
**kwargs):
"""Creates the graph for the Light CNN-9 in
Wu, Xiang, et al. "A light CNN for deep face representation with noisy labels." arXiv preprint arXiv:1511.02683 (2015).
"""
slim = tf.contrib.slim
with tf.variable_scope('LightCNN9', reuse=reuse):
initializer = tf.contrib.layers.xavier_initializer(
uniform=False, dtype=tf.float32, seed=seed)
end_points = dict()
name = "Conv1"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
inputs,
96, [5, 5],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
end_points[name] = graph
graph = maxout(graph, num_units=48, name='Maxout1')
graph = slim.max_pool2d(
graph, [2, 2], stride=2, padding="SAME", scope='Pool1')
####
name = "Conv2a"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
96, [1, 1],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
graph = maxout(graph, num_units=48, name='Maxout2a')
name = "Conv2"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
192, [3, 3],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
end_points[name] = graph
graph = maxout(graph, num_units=96, name='Maxout2')
graph = slim.max_pool2d(
graph, [2, 2], stride=2, padding="SAME", scope='Pool2')
#####
name = "Conv3a"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
192, [1, 1],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
graph = maxout(graph, num_units=96, name='Maxout3a')
name = "Conv3"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
384, [3, 3],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
end_points[name] = graph
graph = maxout(graph, num_units=192, name='Maxout3')
graph = slim.max_pool2d(
graph, [2, 2], stride=2, padding="SAME", scope='Pool3')
#####
name = "Conv4a"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
384, [1, 1],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
graph = maxout(graph, num_units=192, name='Maxout4a')
name = "Conv4"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
256, [3, 3],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
end_points[name] = graph
graph = maxout(graph, num_units=128, name='Maxout4')
#####
name = "Conv5a"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
256, [1, 1],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
graph = maxout(graph, num_units=128, name='Maxout5a')
name = "Conv5"
trainable = is_trainable(name, trainable_variables)
graph = slim.conv2d(
graph,
256, [3, 3],
activation_fn=tf.nn.relu,
stride=1,
weights_initializer=initializer,
scope=name,
trainable=trainable,
reuse=reuse)
end_points[name] = graph
graph = maxout(graph, num_units=128, name='Maxout5')
graph = slim.max_pool2d(
graph, [2, 2], stride=2, padding="SAME", scope='Pool4')
graph = slim.flatten(graph, scope='flatten1')
end_points['flatten1'] = graph
graph = slim.dropout(graph, keep_prob=0.5, scope='dropout1')
name = "fc1"
trainable = is_trainable(name, trainable_variables)
prelogits = slim.fully_connected(
graph,
512,
weights_initializer=initializer,
activation_fn=tf.nn.relu,
scope=name,
trainable=trainable,
reuse=reuse)
end_points['fc1'] = prelogits
return prelogits, end_points
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
import tensorflow as tf
from bob.learn.tensorflow.network.utils import is_trainable
slim = tf.contrib.slim
def mlp(
inputs,
output_shape,
hidden_layers=[10],
hidden_activation=tf.nn.tanh,
output_activation=None,
seed=10,
**kwargs
):
"""An MLP is a representation of a Multi-Layer Perceptron.
This implementation is feed-forward and fully-connected.
The implementation allows setting a global and the output activation functions.
References to fully-connected feed-forward networks: Bishop's Pattern Recognition and Machine Learning, Chapter 5. Figure 5.1 shows what is programmed.
MLPs normally are multi-layered systems, with 1 or more hidden layers.
**Parameters**
output_shape: number of neurons in the output.
hidden_layers: :py:class:`list` that contains the amount of hidden layers, where each element is the number of neurons
hidden_activation: Activation function of the hidden layers. Possible values can be seen
`here <https://www.tensorflow.org/versions/r0.11/api_docs/python/nn.html#activation-functions>`_.
If you set to ``None``, the activation will be linear.
output_activation: Activation of the output layer. If you set to `None`, the activation will be linear
seed:
"""
initializer = tf.contrib.layers.xavier_initializer(
uniform=False, dtype=tf.float32, seed=seed
)
graph = inputs
for i in range(len(hidden_layers)):
weights = hidden_layers[i]
graph = slim.fully_connected(
graph,
weights,
weights_initializer=initializer,
activation_fn=hidden_activation,
scope="fc_{0}".format(i),
)
graph = slim.fully_connected(