I don't know why I'm commiting this. Suddenly everything was updated with nothing

parent 77c46255
Pipeline #13104 failed with stages
in 14 minutes and 15 seconds
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -191,10 +191,10 @@ class Base(object):
dst = numpy.reshape(dst, self.input_shape[1:4])
else:
#dst = numpy.resize(data, self.bob_shape) # Scaling with numpy, because bob is c,w,d instead of w,h,c
#dst = numpy.zeros(shape=(data.shape[0], data.shape[1], 3))
#dst[:, :, 0] = data[:, :, 0]
#dst[:, :, 1] = data[:, :, 0]
#dst[:, :, 2] = data[:, :, 0]
dst = numpy.zeros(shape=(data.shape[0], data.shape[1], 3))
dst[:, :, 0] = data[:, :, 0]
dst[:, :, 1] = data[:, :, 0]
dst[:, :, 2] = data[:, :, 0]
# TODO: LAME SOLUTION
#if data.shape[0] != 3: # GRAY SCALE IMAGES IN A RGB DATABASE
......@@ -204,8 +204,8 @@ class Base(object):
#step_data[1, ...] = data[:, :, 0]
#step_data[2, ...] = data[:, :, 0]
#data = step_data
dst = numpy.zeros(shape=(self.bob_shape))
bob.ip.base.scale(data, dst)
#dst = numpy.zeros(shape=(self.bob_shape))
#bob.ip.base.scale(data, dst)
return dst
else:
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Mon 07 Nov 2016 09:39:36 CET
import numpy
class ScaleFactor(object):
"""
......@@ -40,3 +40,16 @@ class Linear(object):
return x
class PerImageStandarization(object):
def __init__(self):
pass
def __call__(self, x):
mean = numpy.mean(x)
std = numpy.std(x)
return (x-mean)/max(std, 1/numpy.sqrt(numpy.prod(x.shape)))
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -28,7 +28,7 @@ class TFRecord(object):
input_dtype=tf.float32,
batch_size=32,
seed=10,
prefetch_capacity=50,
prefetch_capacity=1000,
prefetch_threads=5):
# Setting the seed for the pseudo random number generator
......
......@@ -39,7 +39,7 @@ class TFRecordImage(TFRecord):
input_dtype=tf.uint8,
batch_size=32,
seed=10,
prefetch_capacity=50,
prefetch_capacity=1000,
prefetch_threads=5,
shuffle=True,
normalization=False,
......@@ -73,6 +73,7 @@ class TFRecordImage(TFRecord):
self.shuffle = shuffle
self.normalization = normalization
self.random_crop = random_crop
self.random_flip = random_flip
self.gray_scale = gray_scale
def __call__(self, element, from_queue=False):
......@@ -127,6 +128,9 @@ class TFRecordImage(TFRecord):
if self.random_crop:
image = tf.image.resize_image_with_crop_or_pad(image, self.output_shape[1], self.output_shape[2])
if self.random_flip:
image = tf.image.random_flip_left_right(image)
# normalizing data
if self.normalization:
......@@ -138,7 +142,7 @@ class TFRecordImage(TFRecord):
if self.shuffle:
data_ph, label_ph = tf.train.shuffle_batch([image, label], batch_size=self.batch_size,
capacity=self.prefetch_capacity, num_threads=self.prefetch_threads,
min_after_dequeue=1, name="shuffle_batch")
min_after_dequeue=self.prefetch_capacity//2, name="shuffle_batch")
else:
data_ph, label_ph = tf.train.batch([image, label], batch_size=self.batch_size,
capacity=self.prefetch_capacity, num_threads=self.prefetch_threads, name="batch")
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -18,12 +18,13 @@ from .TripletWithSelectionDisk import TripletWithSelectionDisk
from .DataAugmentation import DataAugmentation
from .ImageAugmentation import ImageAugmentation
from .Normalizer import ScaleFactor, MeanOffset, Linear
from .Normalizer import ScaleFactor, MeanOffset, Linear, PerImageStandarization
from .DiskAudio import DiskAudio
from .TFRecord import TFRecord
from .TFRecordImage import TFRecordImage
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
"""Says object was actually declared here, an not on the import module.
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -48,10 +48,11 @@ class ContrastiveLoss(BaseLoss):
one = tf.constant(1.0)
d = compute_euclidean_distance(left_feature, right_feature)
between_class = tf.multiply(one - label, tf.square(d)) # (1-Y)*(d^2)
within_class = tf.multiply(one - label, tf.square(d)) # (1-Y)*(d^2)
max_part = tf.square(tf.maximum(self.contrastive_margin - d, 0))
within_class = tf.multiply(label, max_part) # (Y) * max((margin - d)^2, 0)
between_class = tf.multiply(label, max_part) # (Y) * max((margin - d)^2, 0)
loss = 0.5 * (within_class + between_class)
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -3,7 +3,7 @@
"""Converts Bio and PAD datasets to TFRecords file formats.
Usage:
%(prog)s <config_files>...
%(prog)s <config_files>... [--allow-missing-files]
%(prog)s --help
%(prog)s --version
......@@ -117,6 +117,7 @@ def main(argv=None):
args = docopt(docs, argv=argv, version=version)
config_files = args['<config_files>']
config = read_config_file(config_files)
allow_missing_files = args['--allow-missing-files']
# Sets-up logging
verbosity = getattr(config, 'verbose', 0)
......@@ -148,6 +149,14 @@ def main(argv=None):
path = f.make_path(data_dir, data_extension)
data = reader(path)
if data is None:
if allow_missing_files:
logger.debug("... Processing original data file '{0}' was not successful".format(path))
continue
else:
raise RuntimeError("Preprocessing of file '{0}' was not successful".format(path))
label = file_to_label(f)
if one_file_one_sample:
......
......@@ -12,7 +12,7 @@ Options:
<data-path> Path that contains the features
--extension=<arg> Default feature extension [default: .hdf5]
--protocol=<arg> One of the LFW protocols [default: view1]
--data-type=<arg> TFRecord data type [default: float32]
--data-type=<arg> TFRecord data type [default: uint8]
The possible protocol options are the following:
......@@ -49,25 +49,14 @@ def file_to_label(client_ids, f):
def get_pairs(all_pairs, match=True):
pairs = []
enroll = []
probe = []
for p in all_pairs:
if p.is_match == match:
pairs.append(p.enroll_file)
pairs.append(p.probe_file)
enroll.append(p.enroll_file)
probe.append(p.probe_file)
return pairs
def bob2skimage(bob_image):
"""
Convert bob color image to the skcit image
"""
skimage = numpy.zeros(shape=(bob_image.shape[1], bob_image.shape[2], bob_image.shape[0]))
skimage[:, :, 2] = bob_image[0, :, :]
skimage[:, :, 1] = bob_image[1, :, :]
skimage[:, :, 0] = bob_image[2, :, :]
return skimage
return enroll, probe
def main(argv=None):
......@@ -80,9 +69,6 @@ def main(argv=None):
protocol = args['--protocol']
data_type = args['--data-type']
#Setting the reader
reader = bob.io.base.load
# Sets-up logging
if args['--verbose']:
verbosity = 2
......@@ -90,28 +76,36 @@ def main(argv=None):
# Loading LFW models
database = bob.db.lfw.Database()
all_pairs = get_pairs(database.pairs(protocol=protocol), match=True)
client_ids = list(set([f.client_id for f in all_pairs]))
client_ids = dict(zip(client_ids, range(len(client_ids))))
enroll, probe = get_pairs(database.pairs(protocol=protocol), match=True)
#client_ids = list(set([f.client_id for f in all_pairs]))
create_directories_safe(os.path.dirname(output_file))
n_files = len(all_pairs)
with tf.python_io.TFRecordWriter(output_file) as writer:
for i, f in enumerate(all_pairs):
logger.info('Processing file %d out of %d', i + 1, n_files)
path = f.make_path(data_path, extension)
#data = reader(path).astype('uint8').tostring()
img = bob2skimage(reader(path)).astype(data_type)
data = img.tostring()
client_ids = list(set([f.client_id for f in enroll] + [f.client_id for f in probe]))
client_ids = dict(zip(client_ids, range(len(client_ids))))
feature = {'train/data': _bytes_feature(data),
'train/label': _int64_feature(file_to_label(client_ids, f))}
create_directories_safe(os.path.dirname(output_file))
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())
import ipdb; ipdb.set_trace()
n_files = len(enroll)
with tf.python_io.TFRecordWriter(output_file) as writer:
for e, p, i in zip(enroll, probe, range(len(enroll)) ):
logger.info('Processing pair %d out of %d', i + 1, n_files)
e_path = e.make_path(data_path, extension)
p_path = p.make_path(data_path, extension)
if os.path.exists(p_path) and os.path.exists(e_path):
for path in [e_path, p_path]:
data = bob.io.image.to_matplotlib(bob.io.base.load(path)).astype(data_type)
data = data.tostring()
feature = {'train/data': _bytes_feature(data),
'train/label': _int64_feature(file_to_label(client_ids, f))}
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())
else:
logger.debug("... Processing original data file '{0}' was not successful".format(path))
if __name__ == '__main__':
main()
File mode changed from 100644 to 100755
......@@ -87,6 +87,11 @@ def main():
if hasattr(config, 'validation_data_shuffler'):
validation_data_shuffler = config.validation_data_shuffler
prelogits = None
if hasattr(config, 'prelogits'):
prelogits = config.prelogits
trainer = config.Trainer(config.train_data_shuffler,
validation_data_shuffler=validation_data_shuffler,
validate_with_embeddings=validate_with_embeddings,
......@@ -128,6 +133,7 @@ def main():
validation_graph=validation_graph,
loss=config.loss,
learning_rate=config.learning_rate,
optimizer=config.optimizer)
optimizer=config.optimizer,
prelogits=prelogits)
trainer.train()
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -55,7 +55,7 @@ def scratch_network_embeding_example(train_data_shuffler, reuse=False, get_embed
return logits, prelogits
def test_cnn_tfrecord_embedding_validation():
def test_center_loss_tfrecord_embedding_validation():
tf.reset_default_graph()
train_data, train_labels, validation_data, validation_labels = load_mnist()
......@@ -128,31 +128,58 @@ def test_cnn_tfrecord_embedding_validation():
)
trainer.train()
os.remove(tfrecords_filename)
os.remove(tfrecords_filename_val)
"""
assert True
tf.reset_default_graph()
del trainer
assert len(tf.global_variables())==0
del train_data_shuffler
del validation_data_shuffler
##### 2 Continuing the training
# Creating the CNN using the TFRecord as input
train_data_shuffler = TFRecord(filename_queue=filename_queue,
batch_size=batch_size)
validation_data_shuffler = TFRecord(filename_queue=filename_queue_val,
batch_size=2000)
# One graph trainer
trainer = Trainer(train_data_shuffler,
validation_data_shuffler=validation_data_shuffler,
validate_with_embeddings=True,
iterations=2, #It is supper fast
analizer=None,
temp_dir=directory)
trainer.create_network_from_file(directory)
import ipdb; ipdb.set_trace();
trainer.train()
"""
# Inference. TODO: Wrap this in a package
file_name = os.path.join(directory, "model.ckp.meta")
images = tf.placeholder(tf.float32, shape=(None, 28, 28, 1))
graph = scratch_network_embeding_example(images, reuse=False)
graph ,_ = scratch_network_embeding_example(images, reuse=False)
session = tf.Session()
session.run(tf.global_variables_initializer())
saver = tf.train.import_meta_graph(file_name, clear_devices=True)
saver.restore(session, tf.train.latest_checkpoint(os.path.dirname("./temp/cnn_scratch/")))
data = numpy.random.rand(2, 28, 28, 1).astype("float32")
assert session.run(graph, feed_dict={images: data}).shape == (2, 10)
"""
assert session.run(graph['logits'], feed_dict={images: data}).shape == (2, 10)
os.remove(tfrecords_filename)
os.remove(tfrecords_filename_val)
tf.reset_default_graph()
shutil.rmtree(directory)
assert len(tf.global_variables())==0
"""
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -220,6 +220,7 @@ class SiameseTrainer(Trainer):
def fit(self, step):
feed_dict = self.get_feed_dict(self.train_data_shuffler)
_, l, bt_class, wt_class, lr, summary = self.session.run([
self.optimizer,
self.predictor['loss'], self.predictor['between_class'],
......@@ -231,6 +232,11 @@ class SiameseTrainer(Trainer):
def create_general_summary(self):
# Appending histograms for each trainable variables
#for var in tf.trainable_variables():
for var in tf.global_variables():
tf.summary.histogram(var.op.name, var)
# Train summary
tf.summary.scalar('loss', self.predictor['loss'])
tf.summary.scalar('between_class_loss', self.predictor['between_class'])
......
......@@ -84,6 +84,7 @@ class Trainer(object):
self.summaries_train = None
self.train_summary_writter = None
self.thread_pool = None
self.centers = None
# Validation data
self.validation_summary_writter = None
......@@ -98,6 +99,7 @@ class Trainer(object):
self.graph = None
self.validation_graph = None
self.prelogits = None
self.loss = None
......@@ -240,8 +242,10 @@ class Trainer(object):
# TODO: SPECIFIC HACK FOR THE CENTER LOSS. I NEED TO FIND A CLEAN SOLUTION FOR THAT
self.centers = None
if prelogits is not None:
tf.add_to_collection("prelogits", prelogits)
self.predictor, self.centers = self.loss(self.graph, prelogits, self.label_ph)
tf.add_to_collection("centers", self.centers)
tf.add_to_collection("prelogits", prelogits)
self.prelogits = prelogits
else:
self.predictor = self.loss(self.graph, self.label_ph)
......@@ -275,10 +279,6 @@ class Trainer(object):
tf.add_to_collection("summaries_train", self.summaries_train)
# Appending histograms for each trainable variables
for var in tf.trainable_variables():
tf.summary.histogram(var.op.name, var)
# Same business with the validation
if self.validation_data_shuffler is not None:
self.validation_data_ph = self.validation_data_shuffler("data", from_queue=True)
......@@ -371,6 +371,10 @@ class Trainer(object):
self.summaries_train = tf.get_collection("summaries_train")[0]
self.global_step = tf.get_collection("global_step")[0]
self.from_scratch = False
if len(tf.get_collection("centers")) > 0:
self.centers = tf.get_collection("centers")[0]
self.prelogits = tf.get_collection("prelogits")[0]
# Loading the validation bits
if self.validation_data_shuffler is not None:
......@@ -378,7 +382,7 @@ class Trainer(object):
self.validation_graph = tf.get_collection("validation_graph")[0]
self.validation_data_ph = tf.get_collection("validation_data_ph")[0]
self.validation_label = tf.get_collection("validation_label_ph")[0]
self.validation_label_ph = tf.get_collection("validation_label_ph")[0]
self.validation_predictor = tf.get_collection("validation_predictor")[0]
self.summaries_validation = tf.get_collection("summaries_validation")[0]
......@@ -480,6 +484,12 @@ class Trainer(object):
"""
Creates a simple tensorboard summary with the value of the loss and learning rate
"""
# Appending histograms for each trainable variables
#for var in tf.trainable_variables():
for var in tf.global_variables():
tf.summary.histogram(var.op.name, var)
# Train summary
tf.summary.scalar('loss', average_loss)
tf.summary.scalar('lr', self.learning_rate)
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
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