Commit 17b460f6 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

tf-slim update

parent 2c26f7da
Pipeline #8149 failed with stages
in 5 minutes and 42 seconds
......@@ -3,16 +3,10 @@
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 11 May 2016 09:39:36 CEST
"""
"""
import tensorflow as tf
from .SequenceNetwork import SequenceNetwork
#from bob.learn.tensorflow.initialization import Xavier
#from bob.learn.tensorflow.initialization import Constant
class Chopra(SequenceNetwork):
class Chopra(object):
"""Class that creates the architecture presented in the paper:
Chopra, Sumit, Raia Hadsell, and Yann LeCun. "Learning a similarity metric discriminatively, with application to
......@@ -69,24 +63,49 @@ class Chopra(SequenceNetwork):
pooling2_size=[4, 3],
fc1_output=250,
default_feature_layer="fc1",
seed=10,
use_gpu=False,
device="/cpu:0",
batch_norm=False):
self.conv1_kernel_size = conv1_kernel_size
self.conv1_output = conv1_output
self.pooling1_size = pooling1_size
self.conv2_output = conv2_output
self.conv2_kernel_size = conv2_kernel_size
self.pooling2_size = pooling2_size
self.fc1_output = fc1_output
self.seed = seed
self.device = device
self.batch_norm = batch_norm
def __call__(self, inputs):
slim = tf.contrib.slim
graph = slim.conv2d(data_placeholder, conv1_output, conv1_kernel_size, activation_fn=tf.nn.relu,
stride=2, scope='conv1')
graph = slim.max_pool2d(graph, pooling1_size, scope='pool1')
initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=self.seed)
with tf.device(self.device):
initializer = tf.contrib.layers.xavier_initializer(uniform=False, dtype=tf.float32, seed=self.seed)
graph = slim.conv2d(inputs, self.conv1_output, self.conv1_kernel_size, activation_fn=tf.nn.relu,
stride=2,
weights_initializer=initializer,
scope='conv1')
graph = slim.max_pool2d(graph, self.pooling1_size, scope='pool1')
graph = slim.conv2d(graph, conv2_output, conv2_kernel_size, activation_fn=tf.nn.relu,
stride=2, scope='conv2')
graph = slim.max_pool2d(graph, pooling2_size, scope='pool2')
graph = slim.conv2d(graph, self.conv2_output, self.conv2_kernel_size, activation_fn=tf.nn.relu,
stride=2,
weights_initializer=initializer,
scope='conv2')
graph = slim.max_pool2d(graph, self.pooling2_size, scope='pool2')
graph = slim.flatten(graph, scope='flatten1')
graph = slim.flatten(graph, scope='flatten1')
graph = slim.fully_connected(graph, fc1_output, scope='fc1')
graph = slim.fully_connected(graph, self.fc1_output,
weights_initializer=initializer,
scope='fc1')
super(Chopra, self).__init__(default_feature_layer=default_feature_layer,
use_gpu=use_gpu)
return graph
......@@ -9,7 +9,7 @@ from bob.learn.tensorflow.network import Chopra, SequenceNetwork
from bob.learn.tensorflow.loss import BaseLoss, ContrastiveLoss, TripletLoss
from bob.learn.tensorflow.trainers import Trainer, SiameseTrainer, TripletTrainer, constant
from .test_cnn_scratch import validate_network
import pkg_resources
from bob.learn.tensorflow.network import Embedding
from bob.learn.tensorflow.utils import load_mnist
import tensorflow as tf
......@@ -22,9 +22,9 @@ import bob.measure
Some unit tests for the datashuffler
"""
batch_size = 16
batch_size = 32
validation_batch_size = 400
iterations = 50
iterations = 1000
seed = 10
......@@ -90,32 +90,42 @@ def test_cnn_trainer():
directory = "./temp/cnn"
# Preparing the architecture
architecture = Chopra(seed=seed, fc1_output=10)
# Loss for the softmax
loss = BaseLoss(tf.nn.sparse_softmax_cross_entropy_with_logits, tf.reduce_mean)
inputs = {}
inputs['data'] = tf.placeholder(tf.float32, shape=[None, 28, 28, 1], name="train_data")
inputs['label'] = tf.placeholder(tf.int64, shape=[None], name="train_label")
# Preparing the architecture
architecture = Chopra(seed=seed,
conv1_kernel_size=[3,3],
conv2_kernel_size=[3,3],
fc1_output=10)
graph = architecture(inputs['data'])
embedding = Embedding(inputs['data'], graph)
# One graph trainer
trainer = Trainer(architecture=architecture,
trainer = Trainer(inputs=inputs,
graph=graph,
loss=loss,
iterations=iterations,
analizer=None,
prefetch=False,
learning_rate=constant(0.05, name="regular_lr"),
optimizer=tf.train.AdamOptimizer(name="adam_softmax"),
learning_rate=constant(0.1, name="regular_lr"),
optimizer=tf.train.GradientDescentOptimizer(0.1),
temp_dir=directory
)
trainer.train(train_data_shuffler)
accuracy = validate_network(validation_data, validation_labels, architecture)
accuracy = validate_network(embedding, validation_data, validation_labels)
#import ipdb; ipdb.set_trace()
# At least 80% of accuracy
assert accuracy > 80.
shutil.rmtree(directory)
#shutil.rmtree(directory)
del trainer
del architecture
del graph
def test_siamesecnn_trainer():
......
......@@ -103,5 +103,5 @@ def test_cnn_trainer_scratch():
accuracy = validate_network(embedding, validation_data, validation_labels)
assert accuracy > 80
#shutil.rmtree(directory)
shutil.rmtree(directory)
del trainer
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment