Commit b8b71630 authored by Tiago Pereira's avatar Tiago Pereira

Migrated Siamease net

parent f11d453f
Pipeline #8651 failed with stages
in 20 minutes and 50 seconds
......@@ -20,42 +20,22 @@ class Siamese(Base):
def __init__(self, **kwargs):
super(Siamese, self).__init__(**kwargs)
self.data2_placeholder = None
def set_placeholders(self, data, data2, label):
self.data_placeholder = data
self.data2_placeholder = data2
self.label_placeholder = label
def get_placeholders(self, name=""):
"""
Returns a place holder with the size of your batch
def create_placeholders(self):
"""
if self.data_placeholder is None:
self.data_placeholder = tf.placeholder(tf.float32, shape=self.shape, name=name+"_right")
if self.data2_placeholder is None:
self.data2_placeholder = tf.placeholder(tf.float32, shape=self.shape, name=name+"_left")
if self.label_placeholder is None:
self.label_placeholder = tf.placeholder(tf.int64, shape=self.shape[0], name=name+"_label")
return [self.data_placeholder, self.data2_placeholder, self.label_placeholder]
Create place holder instances
def get_placeholders_forprefetch(self, name=""):
:return:
"""
Returns a place holder with the size of your batch
"""
if self.data_placeholder is None:
self.data_placeholder = tf.placeholder(tf.float32, shape=tuple([None] + list(self.shape[1:])), name=name)
if self.data2_placeholder is None:
self.data2_placeholder = tf.placeholder(tf.float32, shape=tuple([None] + list(self.shape[1:])), name=name)
if self.label_placeholder is None:
self.label_placeholder = tf.placeholder(tf.int64, shape=[None, ])
return [self.data_placeholder, self.data2_placeholder, self.label_placeholder]
with tf.name_scope("Input"):
self.data_ph = {}
self.data_ph['left'] = tf.placeholder(tf.float32, shape=self.input_shape, name="left")
self.data_ph['right'] = tf.placeholder(tf.float32, shape=self.input_shape, name="right")
self.label_ph = tf.placeholder(tf.int64, shape=[None], name="label")
# If prefetch, setup the queue to feed data
if self.prefetch:
raise ValueError("There is no prefetch for siamease networks")
def get_genuine_or_not(self, input_data, input_labels, genuine=True):
......
......@@ -45,12 +45,13 @@ class SiameseMemory(Siamese, Memory):
"""
def __init__(self, data, labels,
input_shape,
input_dtype="float",
batch_size=1,
input_shape=[None, 28, 28, 1],
input_dtype="float32",
batch_size=32,
seed=10,
data_augmentation=None,
normalizer=Linear()):
normalizer=Linear()
):
super(SiameseMemory, self).__init__(
data=data,
......@@ -64,7 +65,6 @@ class SiameseMemory(Siamese, Memory):
)
# Seting the seed
numpy.random.seed(seed)
self.data = self.data.astype(input_dtype)
def get_batch(self, zero_one_labels=True):
......@@ -76,12 +76,15 @@ class SiameseMemory(Siamese, Memory):
**Return**
"""
sample_l = numpy.zeros(shape=self.shape, dtype='float')
sample_r = numpy.zeros(shape=self.shape, dtype='float')
labels_siamese = numpy.zeros(shape=self.shape[0], dtype='float')
shape = [self.batch_size] + list(self.input_shape[1:])
sample_l = numpy.zeros(shape=shape, dtype='float32')
sample_r = numpy.zeros(shape=shape, dtype='float32')
labels_siamese = numpy.zeros(shape=shape[0], dtype='float32')
genuine = True
for i in range(self.shape[0]):
for i in range(shape[0]):
sample_l[i, ...], sample_r[i, ...] = self.get_genuine_or_not(self.data, self.labels, genuine=genuine)
if zero_one_labels:
labels_siamese[i] = not genuine
......
......@@ -23,27 +23,26 @@ Some unit tests for the datashuffler
"""
batch_size = 32
validation_batch_size = 32
validation_batch_size = 400
iterations = 300
seed = 10
def dummy_experiment(data_s, architecture):
def dummy_experiment(data_s, embedding):
"""
Create a dummy experiment and return the EER
"""
data_shuffler = object.__new__(Memory)
data_shuffler.__dict__ = data_s.__dict__.copy()
# Extracting features for enrollment
enroll_data, enroll_labels = data_shuffler.get_batch()
enroll_features = architecture(enroll_data)
enroll_features = embedding(enroll_data)
del enroll_data
# Extracting features for probing
probe_data, probe_labels = data_shuffler.get_batch()
probe_features = architecture(probe_data)
probe_features = embedding(probe_data)
del probe_data
# Creating models
......@@ -90,12 +89,6 @@ def test_cnn_trainer():
data_augmentation=data_augmentation,
normalizer=ScaleFactor())
validation_data_shuffler = Memory(validation_data, validation_labels,
input_shape=[None, 28, 28, 1],
batch_size=batch_size,
data_augmentation=data_augmentation,
normalizer=ScaleFactor())
directory = "./temp/cnn"
# Loss for the softmax
......@@ -138,45 +131,40 @@ def test_siamesecnn_trainer():
# Creating datashufflers
train_data_shuffler = SiameseMemory(train_data, train_labels,
input_shape=[28, 28, 1],
batch_size=batch_size)
input_shape=[None, 28, 28, 1],
batch_size=batch_size,
normalizer=ScaleFactor())
validation_data_shuffler = SiameseMemory(validation_data, validation_labels,
input_shape=[28, 28, 1],
batch_size=validation_batch_size)
input_shape=[None, 28, 28, 1],
batch_size=validation_batch_size,
normalizer=ScaleFactor())
directory = "./temp/siamesecnn"
# Preparing the architecture
architecture = Chopra(seed=seed, fc1_output=10)
inputs = {}
inputs['left'] = tf.placeholder(tf.float32, shape=[None, 28, 28, 1], name="input_left")
inputs['right'] = tf.placeholder(tf.float32, shape=[None, 28, 28, 1], name="input_right")
inputs['label'] = tf.placeholder(tf.int64, shape=[None], name="label")
graph = {}
graph['left'] = architecture(inputs['left'])
graph['right'] = architecture(inputs['right'])
# Loss for the Siamese
loss = ContrastiveLoss(contrastive_margin=4.)
# One graph trainer
trainer = SiameseTrainer(inputs=inputs,
graph=graph,
loss=loss,
input_pl = train_data_shuffler("data")
graph = {}
graph['left'] = architecture(input_pl['left'])
graph['right'] = architecture(input_pl['right'])
trainer = SiameseTrainer(train_data_shuffler,
iterations=iterations,
prefetch=False,
analizer=None,
learning_rate=constant(0.05, name="siamese_lr"),
optimizer=tf.train.AdamOptimizer(name="adam_siamese"),
temp_dir=directory
)
trainer.train(train_data_shuffler)
temp_dir=directory)
eer = dummy_experiment(validation_data_shuffler, architecture)
trainer.create_network_from_scratch(graph=graph,
loss=loss,
learning_rate=constant(0.01, name="regular_lr"),
optimizer=tf.train.GradientDescentOptimizer(0.01),)
trainer.train(train_data_shuffler)
embedding = Embedding(train_data_shuffler("data", from_queue=False)['left'], graph['left'])
eer = dummy_experiment(validation_data_shuffler, embedding)
# At least 80% of accuracy
assert eer < 0.25
assert eer < 0.15
shutil.rmtree(directory)
del architecture
......
......@@ -63,9 +63,7 @@ def test_cnn_pretrained():
data_augmentation=data_augmentation,
normalizer=ScaleFactor())
validation_data = numpy.reshape(validation_data, (validation_data.shape[0], 28, 28, 1))
directory = "./temp/cnn"
directory2 = "./temp/cnn2"
# Creating a random network
input_pl = train_data_shuffler("data", from_queue=True)
......
......@@ -195,9 +195,6 @@ class Trainer(object):
self.global_step = tf.get_collection("global_step")[0]
self.from_scratch = False
# Creating the variables
#tf.global_variables_initializer().run(session=self.session)
def __del__(self):
tf.reset_default_graph()
......
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