Debuging the triplet trainer

parent 4afda035
......@@ -10,6 +10,7 @@ from .Disk import Disk
from .SiameseMemory import SiameseMemory
from .TripletMemory import TripletMemory
from .TripletWithSelectionMemory import TripletWithSelectionMemory
from .TripletWithFastSelectionDisk import TripletWithFastSelectionDisk
from .SiameseDisk import SiameseDisk
from .TripletDisk import TripletDisk
......
......@@ -27,11 +27,11 @@ class Constant(Initialization):
self.constant_value = constant_value
super(Constant, self).__init__(seed=None, use_gpu=use_gpu)
def __call__(self, shape, name):
def __call__(self, shape, name, scope):
initializer = tf.constant(self.constant_value, shape=shape)
try:
with tf.variable_scope(name):
with tf.variable_scope(scope):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......@@ -39,7 +39,7 @@ class Constant(Initialization):
with tf.device("/cpu"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
except ValueError:
with tf.variable_scope(name, reuse=True):
with tf.variable_scope(scope, reuse=True):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......
......@@ -24,7 +24,7 @@ class Gaussian(Initialization):
self.std = std
super(Gaussian, self).__init__(seed, use_gpu=use_gpu)
def __call__(self, shape, name):
def __call__(self, shape, name, scope):
if len(shape) == 4:
in_out = shape[0] * shape[1] * shape[2] + shape[3]
......@@ -37,7 +37,7 @@ class Gaussian(Initialization):
seed=self.seed)
try:
with tf.variable_scope(name):
with tf.variable_scope(scope):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......@@ -46,7 +46,7 @@ class Gaussian(Initialization):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
except ValueError:
with tf.variable_scope(name, reuse=True):
with tf.variable_scope(scope, reuse=True):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......
......@@ -26,5 +26,5 @@ class Initialization(object):
self.seed = seed
self.use_gpu = use_gpu
def __call__(self, shape, name):
def __call__(self, shape, name, scope):
NotImplementedError("Please implement this function in derived classes")
......@@ -26,7 +26,7 @@ class SimplerXavier(Initialization):
def __init__(self, seed=10., use_gpu=False):
super(SimplerXavier, self).__init__(seed, use_gpu=use_gpu)
def __call__(self, shape, name):
def __call__(self, shape, name, scope):
if len(shape) == 4:
in_out = shape[0] * shape[1] * shape[2]
......@@ -39,7 +39,7 @@ class SimplerXavier(Initialization):
initializer = tf.truncated_normal(shape, stddev=stddev, seed=self.seed)
try:
with tf.variable_scope(name):
with tf.variable_scope(scope):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......@@ -47,7 +47,7 @@ class SimplerXavier(Initialization):
with tf.device("/cpu"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
except ValueError:
with tf.variable_scope(name, reuse=True):
with tf.variable_scope(scope, reuse=True):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......
......@@ -27,7 +27,7 @@ class Xavier(Initialization):
super(Xavier, self).__init__(seed, use_gpu=use_gpu)
def __call__(self, shape, name):
def __call__(self, shape, name, scope):
if len(shape) == 4:
in_out = shape[0] * shape[1] * shape[2] + shape[3]
......@@ -40,7 +40,7 @@ class Xavier(Initialization):
initializer = tf.truncated_normal(shape, stddev=stddev, seed=self.seed)
try:
with tf.variable_scope(name):
with tf.variable_scope(scope):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......@@ -48,7 +48,7 @@ class Xavier(Initialization):
with tf.device("/cpu"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
except ValueError:
with tf.variable_scope(name, reuse=True):
with tf.variable_scope(scope, reuse=True):
if self.use_gpu:
with tf.device("/gpu:0"):
return tf.get_variable(name, initializer=initializer, dtype=tf.float32)
......
......@@ -18,6 +18,7 @@ class Conv2D(Layer):
def __init__(self, name, activation=None,
kernel_size=3,
filters=8,
stride=[1, 1, 1, 1],
weights_initialization=Xavier(),
bias_initialization=Constant(),
use_gpu=False
......@@ -38,12 +39,15 @@ class Conv2D(Layer):
activation=activation,
weights_initialization=weights_initialization,
bias_initialization=bias_initialization,
use_gpu=use_gpu
use_gpu=use_gpu,
)
self.kernel_size = kernel_size
self.filters = filters
self.W = None
self.b = None
self.stride = stride
def create_variables(self, input_layer):
self.input_layer = input_layer
......@@ -56,16 +60,19 @@ class Conv2D(Layer):
if self.W is None:
self.W = self.weights_initialization(shape=[self.kernel_size, self.kernel_size, n_channels, self.filters],
name="w_" + str(self.name)
name="w_" + str(self.name),
scope="w_" + str(self.name)
)
self.b = self.bias_initialization(shape=[self.filters],
name="b_" + str(self.name) + "bias")
name="b_" + str(self.name) + "bias",
scope="b_" + str(self.name)
)
def get_graph(self):
with tf.name_scope(str(self.name)):
conv2d = tf.nn.conv2d(self.input_layer, self.W, strides=[1, 1, 1, 1], padding='SAME')
conv2d = tf.nn.conv2d(self.input_layer, self.W, strides=self.stride, padding='SAME')
if self.activation is not None:
output = self.activation(tf.nn.bias_add(conv2d, self.b))
......
......@@ -50,10 +50,14 @@ class FullyConnected(Layer):
if self.W is None:
input_dim = reduce(mul, self.input_layer.get_shape().as_list()[1:])
self.W = self.weights_initialization(shape=[input_dim, self.output_dim],
name="W_" + str(self.name))
name="W_" + str(self.name),
scope="W_" +str(self.name)
)
# if self.activation is not None:
self.b = self.bias_initialization(shape=[self.output_dim],
name="b_" + str(self.name))
name="b_" + str(self.name),
scope="b_" + str(self.name)
)
def get_graph(self):
......
......@@ -9,6 +9,7 @@ from .Dummy import Dummy
from .VGG import VGG
from .LenetDropout import LenetDropout
from .MLP import MLP
from .FaceNet import FaceNet
# gets sphinx autodoc done right - don't remove it
__all__ = [_ for _ in dir() if not _.startswith('_')]
......@@ -23,7 +23,7 @@ import tensorflow as tf
from .. import util
SEED = 10
from bob.learn.tensorflow.datashuffler import TripletMemory, TripletWithSelectionMemory
from bob.learn.tensorflow.network import Lenet, MLP, LenetDropout, VGG, Chopra, Dummy
from bob.learn.tensorflow.network import Lenet, MLP, LenetDropout, VGG, Chopra, Dummy, FaceNet
from bob.learn.tensorflow.trainers import TripletTrainer
from bob.learn.tensorflow.loss import TripletLoss
import numpy
......@@ -74,7 +74,8 @@ def main():
#n_classes = 200
cnn = True
if cnn:
architecture = Chopra(seed=SEED, fc1_output=n_classes, use_gpu=USE_GPU)
architecture = FaceNet(seed=SEED, use_gpu=USE_GPU)
#architecture = Chopra(seed=SEED, fc1_output=n_classes, use_gpu=USE_GPU)
#architecture = Lenet(default_feature_layer="fc2", n_classes=n_classes, conv1_output=8, conv2_output=16,use_gpu=USE_GPU)
#architecture = VGG(n_classes=n_classes, use_gpu=USE_GPU)
#architecture = Dummy(seed=SEED)
......
......@@ -22,8 +22,8 @@ from docopt import docopt
import tensorflow as tf
from .. import util
SEED = 10
from bob.learn.tensorflow.datashuffler import TripletWithSelectionDisk, TripletDisk
from bob.learn.tensorflow.network import Lenet, MLP, LenetDropout, VGG, Chopra, Dummy
from bob.learn.tensorflow.datashuffler import TripletWithSelectionDisk, TripletDisk, TripletWithFastSelectionDisk
from bob.learn.tensorflow.network import Lenet, MLP, LenetDropout, VGG, Chopra, Dummy, FaceNet
from bob.learn.tensorflow.trainers import SiameseTrainer, Trainer, TripletTrainer
from bob.learn.tensorflow.loss import ContrastiveLoss, BaseLoss, TripletLoss
import numpy
......@@ -41,11 +41,11 @@ def main():
import bob.db.mobio
db_mobio = bob.db.mobio.Database()
directory = "/idiap/temp/tpereira/DEEP_FACE/CASIA/preprocessed"
directory = "/idiap/temp/tpereira/DEEP_FACE/CASIA_WEBFACE/mobio/preprocessed/"
# Preparing train set
#train_objects = db_mobio.objects(protocol="male", groups="world")
train_objects = db_mobio.objects(protocol="male", groups="dev")
train_objects = db_mobio.objects(protocol="male", groups="world")
train_labels = [int(o.client_id) for o in train_objects]
n_classes = len(set(train_labels))
......@@ -53,9 +53,14 @@ def main():
directory=directory,
extension=".hdf5")
for o in train_objects]
train_data_shuffler = TripletWithSelectionDisk(train_file_names, train_labels,
input_shape=[125, 125, 3],
batch_size=BATCH_SIZE)
#train_data_shuffler = TripletWithSelectionDisk(train_file_names, train_labels,
# input_shape=[125, 125, 3],
# batch_size=BATCH_SIZE)
train_data_shuffler = TripletWithFastSelectionDisk(train_file_names, train_labels,
input_shape=[224, 224, 3],
batch_size=BATCH_SIZE,
total_identities=8)
# Preparing train set
validation_objects = db_mobio.objects(protocol="male", groups="dev")
......@@ -67,11 +72,12 @@ def main():
extension=".hdf5")
for o in validation_objects]
validation_data_shuffler = TripletDisk(validation_file_names, validation_labels,
input_shape=[125, 125, 3],
batch_size=VALIDATION_BATCH_SIZE)
input_shape=[224, 224, 3],
batch_size=VALIDATION_BATCH_SIZE)
# Preparing the architecture
#architecture = Chopra(seed=SEED, fc1_output=n_classes)
architecture = Chopra(seed=SEED, fc1_output=n_classes)
#architecture = Chopra(seed=SEED, fc1_output=n_classes)
architecture = FaceNet(seed=SEED, use_gpu=USE_GPU)
optimizer = tf.train.GradientDescentOptimizer(0.00000001)
......@@ -89,12 +95,13 @@ def main():
# optimizer=optimizer,
# temp_dir="./LOGS_MOBIO/siamese-cnn-prefetch")
loss = TripletLoss(margin=4.)
loss = TripletLoss(margin=1.)
#optimizer = optimizer,
trainer = TripletTrainer(architecture=architecture, loss=loss,
iterations=ITERATIONS,
prefetch=False,
optimizer=optimizer,
temp_dir="./LOGS_MOBIO/triplet-cnn")
#trainer.train(train_data_shuffler, validation_data_shuffler)
trainer.train(train_data_shuffler)
trainer.train(train_data_shuffler, validation_data_shuffler)
#trainer.train(train_data_shuffler)
......@@ -22,7 +22,7 @@ from docopt import docopt
import tensorflow as tf
from .. import util
SEED = 10
from bob.learn.tensorflow.datashuffler import TripletDisk, TripletWithSelectionDisk
from bob.learn.tensorflow.datashuffler import TripletDisk, TripletWithSelectionDisk, TripletWithFastSelectionDisk
from bob.learn.tensorflow.network import Lenet, MLP, LenetDropout, VGG, Chopra, Dummy
from bob.learn.tensorflow.trainers import SiameseTrainer, TripletTrainer
from bob.learn.tensorflow.loss import ContrastiveLoss, TripletLoss
......@@ -56,9 +56,14 @@ def main():
extension="")
for o in train_objects]
train_data_shuffler = TripletWithSelectionDisk(train_file_names, train_labels,
input_shape=[125, 125, 3],
batch_size=BATCH_SIZE)
#train_data_shuffler = TripletWithSelectionDisk(train_file_names, train_labels,
# input_shape=[125, 125, 3],
# batch_size=BATCH_SIZE)
train_data_shuffler = TripletWithFastSelectionDisk(train_file_names, train_labels,
input_shape=[125, 125, 3],
batch_size=BATCH_SIZE)
# Preparing train set
directory = "/idiap/temp/tpereira/DEEP_FACE/CASIA/preprocessed"
......@@ -85,11 +90,11 @@ def main():
# snapshot=VALIDATION_TEST,
# optimizer=optimizer)
loss = TripletLoss(margin=4.)
loss = TripletLoss(margin=1.)
trainer = TripletTrainer(architecture=architecture, loss=loss,
iterations=ITERATIONS,
prefetch=False,
temp_dir="./LOGS_CASIA/triplet-cnn")
temp_dir="./LOGS_CASIA/triplet-cnn-fast-selection")
trainer.train(train_data_shuffler, validation_data_shuffler)
......
......@@ -266,7 +266,8 @@ class Trainer(object):
# Preparing the optimizer
self.optimizer_class._learning_rate = self.learning_rate
self.optimizer = self.optimizer_class.minimize(self.training_graph, global_step=tf.Variable(0))
#self.optimizer = self.optimizer_class.minimize(self.training_graph, global_step=tf.Variable(0))
self.optimizer = self.optimizer_class.minimize(self.training_graph)
# Train summary
self.summaries_train = self.create_general_summary()
......
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