Commit 5fb18ca1 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Merge branch 'organizing-transfer-learning' into 'master'

Organizing transfer learning

See merge request !32
parents 467d4d05 3770d596
Pipeline #13921 failed with stages
in 31 minutes and 43 seconds
......@@ -130,13 +130,34 @@ class Logits(estimator.Estimator):
data = features['data']
key = features['key']
# Building one graph, by default everything is trainable
if self.extra_checkpoint is None:
is_trainable = True
else:
is_trainable = is_trainable_checkpoint(self.extra_checkpoint)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
# Building one graph, by default everything is trainable
if self.extra_checkpoint is None:
is_trainable = True
else:
is_trainable = is_trainable_checkpoint(self.extra_checkpoint)
# Building the training graph
prelogits = self.architecture(data, mode=mode, trainable_variables=is_trainable)[0]
logits = append_logits(prelogits, n_classes)
# Compute Loss (for both TRAIN and EVAL modes)
self.loss = self.loss_op(logits, labels)
if self.extra_checkpoint is not None:
tf.contrib.framework.init_from_checkpoint(self.extra_checkpoint["checkpoint_path"],
self.extra_checkpoint["scopes"])
global_step = tf.contrib.framework.get_or_create_global_step()
train_op = self.optimizer.minimize(self.loss, global_step=global_step)
return tf.estimator.EstimatorSpec(mode=mode, loss=self.loss,
train_op=train_op)
prelogits = self.architecture(data, is_trainable=is_trainable)[0]
# Building the training graph for PREDICTION OR VALIDATION
prelogits = self.architecture(data, mode=mode, trainable_variables=False)[0]
logits = append_logits(prelogits, n_classes)
if self.embedding_validation:
......@@ -157,22 +178,9 @@ class Logits(estimator.Estimator):
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# Compute Loss (for both TRAIN and EVAL modes)
# IF Validation
self.loss = self.loss_op(logits, labels)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
if self.extra_checkpoint is not None:
tf.contrib.framework.init_from_checkpoint(self.extra_checkpoint["checkpoint_path"],
self.extra_checkpoint["scopes"])
global_step = tf.contrib.framework.get_or_create_global_step()
train_op = self.optimizer.minimize(self.loss, global_step=global_step)
return tf.estimator.EstimatorSpec(mode=mode, loss=self.loss,
train_op=train_op)
# Validation
if self.embedding_validation:
predictions_op = predict_using_tensors(predictions["embeddings"], labels, num=validation_batch_size)
eval_metric_ops = {"accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions_op)}
......@@ -277,18 +285,39 @@ class LogitsCenterLoss(estimator.Estimator):
data = features['data']
key = features['key']
# Building one graph, by default everything is trainable
if self.extra_checkpoint is None:
is_trainable = True
else:
is_trainable = is_trainable_checkpoint(self.extra_checkpoint)
prelogits = self.architecture(data)[0]
logits = append_logits(prelogits, n_classes)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
# Compute Loss (for both TRAIN and EVAL modes)
loss_dict = mean_cross_entropy_center_loss(logits, prelogits, labels, self.n_classes,
alpha=self.alpha, factor=self.factor)
# Building one graph, by default everything is trainable
if self.extra_checkpoint is None:
is_trainable = True
else:
is_trainable = is_trainable_checkpoint(self.extra_checkpoint)
# Building the training graph
prelogits = self.architecture(data, mode=mode, trainable_variables=is_trainable)[0]
logits = append_logits(prelogits, n_classes)
# Compute Loss (for TRAIN mode)
loss_dict = mean_cross_entropy_center_loss(logits, prelogits, labels, self.n_classes,
alpha=self.alpha, factor=self.factor)
self.loss = loss_dict['loss']
centers = loss_dict['centers']
if self.extra_checkpoint is not None:
tf.contrib.framework.init_from_checkpoint(self.extra_checkpoint["checkpoint_path"],
self.extra_checkpoint["scopes"])
global_step = tf.contrib.framework.get_or_create_global_step()
train_op = tf.group(self.optimizer.minimize(self.loss, global_step=global_step),
centers)
return tf.estimator.EstimatorSpec(mode=mode, loss=self.loss,
train_op=train_op)
# Building the training graph for PREDICTION OR VALIDATION
prelogits = self.architecture(data, mode=mode, trainable_variables=False)[0]
logits = append_logits(prelogits, n_classes)
if self.embedding_validation:
# Compute the embeddings
......@@ -308,31 +337,16 @@ class LogitsCenterLoss(estimator.Estimator):
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
self.loss = loss_dict['loss']
centers = loss_dict['centers']
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
# Loading variables from some model just in case
if self.extra_checkpoint is not None:
tf.contrib.framework.init_from_checkpoint(self.extra_checkpoint["checkpoint_path"],
self.extra_checkpoint["scopes"])
global_step = tf.contrib.framework.get_or_create_global_step()
# backprop and updating the centers
train_op = tf.group(self.optimizer.minimize(self.loss, global_step=global_step),
centers)
return tf.estimator.EstimatorSpec(mode=mode, loss=self.loss,
train_op=train_op)
# IF Validation
loss_dict = mean_cross_entropy_center_loss(logits, prelogits, labels, self.n_classes,
alpha=self.alpha, factor=self.factor)
self.loss = loss_dict['loss']
if self.embedding_validation:
predictions_op = predict_using_tensors(predictions["embeddings"], labels, num=validation_batch_size)
eval_metric_ops = {"accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions_op)}
return tf.estimator.EstimatorSpec(mode=mode, loss=self.loss, eval_metric_ops=eval_metric_ops)
else:
# Add evaluation metrics (for EVAL mode)
eval_metric_ops = {
......@@ -344,4 +358,4 @@ class LogitsCenterLoss(estimator.Estimator):
super(LogitsCenterLoss, self).__init__(model_fn=_model_fn,
model_dir=model_dir,
config=config)
......@@ -119,7 +119,8 @@ class Siamese(estimator.Estimator):
def _model_fn(features, labels, mode, params, config):
if mode == tf.estimator.ModeKeys.TRAIN:
if mode == tf.estimator.ModeKeys.TRAIN:
# Building one graph, by default everything is trainable
if self.extra_checkpoint is None:
is_trainable = True
......@@ -131,8 +132,8 @@ class Siamese(estimator.Estimator):
raise ValueError("The input function needs to contain a dictionary with the keys `left` and `right` ")
# Building one graph
prelogits_left = self.architecture(features['left'], is_trainable=is_trainable)[0]
prelogits_right = self.architecture(features['right'], reuse=True, is_trainable=is_trainable)[0]
prelogits_left, end_points_left = self.architecture(features['left'], mode=mode, trainable_variables=is_trainable)
prelogits_right, end_points_right = self.architecture(features['right'], reuse=True, mode=mode, trainable_variables=is_trainable)
if self.extra_checkpoint is not None:
tf.contrib.framework.init_from_checkpoint(self.extra_checkpoint["checkpoint_path"],
......@@ -152,7 +153,7 @@ class Siamese(estimator.Estimator):
data = features['data']
# Compute the embeddings
prelogits = self.architecture(data)[0]
prelogits = self.architecture(data, mode=mode, trainable_variables=False)[0]
embeddings = tf.nn.l2_normalize(prelogits, 1)
predictions = {"embeddings": embeddings}
......
......@@ -121,9 +121,9 @@ class Triplet(estimator.Estimator):
is_trainable = is_trainable_checkpoint(self.extra_checkpoint)
# Building one graph
prelogits_anchor = self.architecture(features['anchor'], is_trainable=is_trainable)[0]
prelogits_positive = self.architecture(features['positive'], reuse=True, is_trainable=is_trainable)[0]
prelogits_negative = self.architecture(features['negative'], reuse=True, is_trainable=is_trainable)[0]
prelogits_anchor = self.architecture(features['anchor'], mode=mode)[0]
prelogits_positive = self.architecture(features['positive'], reuse=True, mode=mode)[0]
prelogits_negative = self.architecture(features['negative'], reuse=True, mode=mode)[0]
if self.extra_checkpoint is not None:
tf.contrib.framework.init_from_checkpoint(self.extra_checkpoint["checkpoint_path"],
......@@ -141,7 +141,7 @@ class Triplet(estimator.Estimator):
data = features['data']
# Compute the embeddings
prelogits = self.architecture(data)[0]
prelogits = self.architecture(data, mode=mode)[0]
embeddings = tf.nn.l2_normalize(prelogits, 1)
predictions = {"embeddings": embeddings}
......
......@@ -5,7 +5,7 @@
import tensorflow as tf
def dummy(inputs, reuse=False, is_trainable=True):
def dummy(inputs, reuse=False, mode = tf.estimator.ModeKeys.TRAIN, trainable_variables=True):
"""
Create all the necessary variables for this CNN
......@@ -24,7 +24,7 @@ def dummy(inputs, reuse=False, is_trainable=True):
graph = slim.conv2d(inputs, 10, [3, 3], activation_fn=tf.nn.relu, stride=1, scope='conv1',
weights_initializer=initializer,
trainable=is_trainable)
trainable=trainable_variables)
end_points['conv1'] = graph
graph = slim.max_pool2d(graph, [4, 4], scope='pool1')
......@@ -37,7 +37,7 @@ def dummy(inputs, reuse=False, is_trainable=True):
weights_initializer=initializer,
activation_fn=None,
scope='fc1',
trainable=is_trainable)
trainable=trainable_variables)
end_points['fc1'] = graph
......
......@@ -94,7 +94,7 @@ def main(argv=None):
# Evaluate
evaluations = estimator.evaluate(
input_fn=eval_input_fn,
steps=steps,
steps=1,
hooks=hooks,
checkpoint_path=checkpoint_path,
name=name,
......
......@@ -37,7 +37,7 @@ epochs = 2
steps = 5000
def dummy_adapted(inputs, reuse=False, is_trainable=False):
def dummy_adapted(inputs, reuse=False, mode = tf.estimator.ModeKeys.TRAIN, trainable_variables=True):
"""
Create all the necessary variables for this CNN
......@@ -48,7 +48,7 @@ def dummy_adapted(inputs, reuse=False, is_trainable=False):
"""
slim = tf.contrib.slim
graph, end_points = dummy(inputs, reuse=reuse, is_trainable=is_trainable)
graph, end_points = dummy(inputs, reuse=reuse, mode = mode, trainable_variables=trainable_variables)
initializer = tf.contrib.layers.xavier_initializer()
with tf.variable_scope('Adapted', reuse=reuse):
......
......@@ -67,7 +67,6 @@ def test_triplet_estimator():
trainer = Triplet(model_dir=model_dir,
architecture=dummy,
optimizer=tf.train.GradientDescentOptimizer(learning_rate),
n_classes=10,
loss_op=triplet_loss,
validation_batch_size=validation_batch_size)
run_triplet_estimator(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