Fixe issue #1

parent 94b53903
......@@ -58,6 +58,7 @@ class Base(object):
self.label_placeholder = None
self.data_augmentation = data_augmentation
self.deployment_shape = [-1] + list(input_shape)
def get_placeholders(self, name=""):
"""
......@@ -89,9 +90,7 @@ class Base(object):
skimage = numpy.zeros(shape=(bob_image.shape[1], bob_image.shape[2], bob_image.shape[0]))
for i in range(bob_image.shape[0]):
skimage[:, :, i] = bob_image[i, :, :] # Copying red
#skimage[:, :, 1] = bob_image[1, :, :] # Copying green
#skimage[:, :, 2] = bob_image[2, :, :] # Copying blue
skimage[:, :, i] = bob_image[i, :, :]
return skimage
......@@ -105,11 +104,6 @@ class Base(object):
for i in range(bob_image.shape[0]):
bob_image[i, :, :] = sk_image[:, :, i] # Copying red
#bob_image[0, :, :] = sk_image[:, :, 0] # Copying red
#if bob_image.shape[0] > 1:
# bob_image[1, :, :] = sk_image[:, :, 1]# Copying green
# bob_image[2, :, :] = sk_image[:, :, 2] # Copying blue
return bob_image
def rescale(self, data):
......
......@@ -8,7 +8,7 @@ import tensorflow as tf
import abc
import six
import numpy
import os
import pickle
from collections import OrderedDict
from bob.learn.tensorflow.layers import Layer, MaxPooling, Dropout, Conv2D, FullyConnected
......@@ -35,6 +35,9 @@ class SequenceNetwork(six.with_metaclass(abc.ABCMeta, object)):
self.input_subtract = 0.
self.use_gpu = use_gpu
self.pickle_architecture = None# The trainer triggers this
self.deployment_shape = None# The trainer triggers this
def add(self, layer):
"""
Add a :py:class:`bob.learn.tensorflow.layers.Layer` in the sequence network
......@@ -44,6 +47,10 @@ class SequenceNetwork(six.with_metaclass(abc.ABCMeta, object)):
raise ValueError("Input `layer` must be an instance of `bob.learn.tensorflow.layers.Layer`")
self.sequence_net[layer.name] = layer
def pickle_net(self, shape):
self.pickle_architecture = pickle.dumps(self.sequence_net)
self.deployment_shape = shape
def compute_graph(self, input_data, feature_layer=None, training=True):
"""Given the current network, return the Tensorflow graph
......@@ -108,10 +115,9 @@ class SequenceNetwork(six.with_metaclass(abc.ABCMeta, object)):
return feature
def dump_variables(self):
"""Return all the tensorflow `variables <https://www.tensorflow.org/versions/r0.11/api_docs/python/state_ops.html#Variable>`_ used in the graph
"""
Return all the tensorflow `variables <https://www.tensorflow.org/versions/r0.11/api_docs/python/state_ops.html#Variable>`_ used in the graph
"""
variables = {}
for k in self.sequence_net:
# TODO: IT IS NOT SMART TESTING ALONG THIS PAGE
......@@ -178,15 +184,7 @@ class SequenceNetwork(six.with_metaclass(abc.ABCMeta, object)):
return samples_per_sample
#if self.saver is None:
# variables = self.dump_variables()
# variables['input_divide'] = self.input_divide
# variables['input_subtract'] = self.input_subtract
# self.saver = tf.train.Saver(variables)
#self.saver.restore(session, path)
def save(self, hdf5, step=None):
def save(self, hdf5):
"""
Save the state of the network in HDF5 format
......@@ -204,24 +202,21 @@ class SequenceNetwork(six.with_metaclass(abc.ABCMeta, object)):
p = split_path[i]
hdf5.create_group(p)
# Saving the architecture
if self.pickle_architecture is not None:
hdf5.set('architecture', self.pickle_architecture)
hdf5.set('deployment_shape', self.deployment_shape)
# Directory that stores the tensorflow variables
hdf5.create_group('/tensor_flow')
hdf5.cd('/tensor_flow')
if step is not None:
group_name = '/step_{0}'.format(step)
hdf5.create_group(group_name)
hdf5.cd(group_name)
# Iterating the variables of the model
for v in self.dump_variables().keys():
create_groups(v)
hdf5.set(v, self.dump_variables()[v].eval())
hdf5.cd('..')
if step is not None:
hdf5.cd('..')
hdf5.set('input_divide', self.input_divide)
hdf5.set('input_subtract', self.input_subtract)
......@@ -245,6 +240,10 @@ class SequenceNetwork(six.with_metaclass(abc.ABCMeta, object)):
self.input_divide = hdf5.read('input_divide')
self.input_subtract = hdf5.read('input_subtract')
# Saving the architecture
self.sequence_net = pickle.loads(hdf5.read('architecture'))
self.deployment_shape = hdf5.read('deployment_shape')
# Loading variables
place_holder = tf.placeholder(tf.float32, shape=shape, name="load")
self.compute_graph(place_holder)
......
......@@ -76,9 +76,8 @@ def main():
input_shape=[112, 112, 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 = FaceNet(seed=SEED, use_gpu=USE_GPU)
architecture = FaceNet(seed=SEED, use_gpu=USE_GPU)
#optimizer = tf.train.GradientDescentOptimizer(0.0005)
......@@ -103,7 +102,8 @@ def main():
iterations=ITERATIONS,
base_learning_rate=0.1,
prefetch=False,
temp_dir="./LOGS_MOBIO/triplet-cnn")
snapshot=200,
temp_dir="/idiap/temp/tpereira/CNN_MODELS/triplet-cnn-all-mobio")
#trainer.train(train_data_shuffler, validation_data_shuffler)
trainer.train(train_data_shuffler)
......@@ -23,7 +23,7 @@ import tensorflow as tf
from .. import util
SEED = 10
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.network import Lenet, MLP, LenetDropout, VGG, Chopra, Dummy, FaceNet
from bob.learn.tensorflow.trainers import SiameseTrainer, TripletTrainer
from bob.learn.tensorflow.loss import ContrastiveLoss, TripletLoss
import numpy
......@@ -61,7 +61,7 @@ def main():
# batch_size=BATCH_SIZE)
train_data_shuffler = TripletWithFastSelectionDisk(train_file_names, train_labels,
input_shape=[224, 224, 3],
input_shape=[112, 112, 3],
batch_size=BATCH_SIZE)
......@@ -76,11 +76,12 @@ def main():
for o in validation_objects]
validation_data_shuffler = TripletDisk(validation_file_names, validation_labels,
input_shape=[224, 224, 3],
input_shape=[112, 112, 3],
batch_size=VALIDATION_BATCH_SIZE)
# Preparing the architecture
# LENET PAPER CHOPRA
architecture = Chopra(seed=SEED)
#architecture = Chopra(seed=SEED)
architecture = FaceNet(seed=SEED, use_gpu=USE_GPU)
#loss = ContrastiveLoss(contrastive_margin=50.)
#optimizer = tf.train.GradientDescentOptimizer(0.00001)
......@@ -90,9 +91,10 @@ def main():
# snapshot=VALIDATION_TEST,
# optimizer=optimizer)
loss = TripletLoss(margin=1.)
loss = TripletLoss(margin=0.5)
trainer = TripletTrainer(architecture=architecture, loss=loss,
iterations=ITERATIONS,
base_learning_rate=0.1,
prefetch=False,
temp_dir="./LOGS_CASIA/triplet-cnn-fast-selection")
......
......@@ -55,8 +55,9 @@ class SiameseTrainer(Trainer):
###### training options ##########
convergence_threshold=0.01,
iterations=5000,
snapshot=100,
snapshot=500,
prefetch=False,
validation_snapshot=100,
## Analizer
analizer=ExperimentAnalizer(),
......@@ -79,6 +80,7 @@ class SiameseTrainer(Trainer):
convergence_threshold=convergence_threshold,
iterations=iterations,
snapshot=snapshot,
validation_snapshot=validation_snapshot,
prefetch=prefetch,
## Analizer
......
......@@ -14,8 +14,8 @@ from tensorflow.core.framework import summary_pb2
import time
from bob.learn.tensorflow.datashuffler.OnlineSampling import OnLineSampling
#os.environ["CUDA_VISIBLE_DEVICES"] = "1,2,3,0"
os.environ["CUDA_VISIBLE_DEVICES"] = ""
os.environ["CUDA_VISIBLE_DEVICES"] = "3,2,0,1"
#os.environ["CUDA_VISIBLE_DEVICES"] = ""
logger = bob.core.log.setup("bob.learn.tensorflow")
......@@ -58,7 +58,8 @@ class Trainer(object):
###### training options ##########
convergence_threshold=0.01,
iterations=5000,
snapshot=100,
snapshot=500,
validation_snapshot=100,
prefetch=False,
## Analizer
......@@ -81,6 +82,7 @@ class Trainer(object):
self.iterations = iterations
self.snapshot = snapshot
self.validation_snapshot = validation_snapshot
self.convergence_threshold = convergence_threshold
self.prefetch = prefetch
......@@ -259,6 +261,9 @@ class Trainer(object):
bob.io.base.create_directories_safe(self.temp_dir)
self.train_data_shuffler = train_data_shuffler
# Pickle the architecture to save
self.architecture.pickle_net(train_data_shuffler.deployment_shape)
# TODO: find an elegant way to provide this as a parameter of the trainer
self.global_step = tf.Variable(0, trainable=False)
self.learning_rate = tf.train.exponential_decay(
......@@ -274,18 +279,14 @@ class Trainer(object):
self.optimizer_class._learning_rate = self.learning_rate
self.optimizer = self.optimizer_class.minimize(self.training_graph, global_step=self.global_step)
# Train summary
self.summaries_train = self.create_general_summary()
logger.info("Initializing !!")
# Training
hdf5 = bob.io.base.HDF5File(os.path.join(self.temp_dir, 'model.hdf5'), 'w')
config = tf.ConfigProto(log_device_placement=True)
config.gpu_options.allow_growth = True
with tf.Session(config=config) as session:
tf.initialize_all_variables().run()
if isinstance(train_data_shuffler, OnLineSampling):
......@@ -307,19 +308,29 @@ class Trainer(object):
summary = summary_pb2.Summary.Value(tag="elapsed_time", simple_value=float(end-start))
self.train_summary_writter.add_summary(summary_pb2.Summary(value=[summary]), step)
if validation_data_shuffler is not None and step % self.snapshot == 0:
# Running validation
if validation_data_shuffler is not None and step % self.validation_snapshot == 0:
self.compute_validation(session, validation_data_shuffler, step)
if self.analizer is not None:
self.validation_summary_writter.add_summary(self.analizer(
validation_data_shuffler, self.architecture, session), step)
# Taking snapshot
if step % self.snapshot == 0:
logger.info("Taking snapshot")
hdf5 = bob.io.base.HDF5File(os.path.join(self.temp_dir, 'model_snapshot{0}.hdf5'.format(step)), 'w')
self.architecture.save(hdf5)
del hdf5
logger.info("Training finally finished")
self.train_summary_writter.close()
if validation_data_shuffler is not None:
self.validation_summary_writter.close()
# Saving the final network
hdf5 = bob.io.base.HDF5File(os.path.join(self.temp_dir, 'model.hdf5'), 'w')
self.architecture.save(hdf5)
del hdf5
......
......@@ -55,7 +55,8 @@ class TripletTrainer(Trainer):
###### training options ##########
convergence_threshold=0.01,
iterations=5000,
snapshot=100,
snapshot=500,
validation_snapshot=100,
prefetch=False,
## Analizer
......@@ -79,6 +80,7 @@ class TripletTrainer(Trainer):
convergence_threshold=convergence_threshold,
iterations=iterations,
snapshot=snapshot,
validation_snapshot=validation_snapshot,
prefetch=prefetch,
## Analizer
......@@ -190,8 +192,6 @@ class TripletTrainer(Trainer):
self.within_class_graph_train,
self.learning_rate, self.summaries_train],
feed_dict=feed_dict)
print "LEARNING {0}".format(lr)
logger.info("Loss training set step={0} = {1}".format(step, l))
self.train_summary_writter.add_summary(summary, step)
......
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