Skip to content
Snippets Groups Projects
Commit 4d563c2d authored by tgupta6's avatar tgupta6
Browse files

margin based loss for object training

parent 5a257b12
No related branches found
No related tags found
No related merge requests found
...@@ -57,21 +57,33 @@ def margin_loss(y, y_pred, margin): ...@@ -57,21 +57,33 @@ def margin_loss(y, y_pred, margin):
keep_dims=True, name='correct_score') keep_dims=True, name='correct_score')
return tf.reduce_mean(tf.maximum(0.0, y_pred + margin - correct_score)) return tf.reduce_mean(tf.maximum(0.0, y_pred + margin - correct_score))
def multilabel_margin_loss(y, y_pred, margin):
y_list = tf.unpack(y) def multilabel_margin_loss(y, y_pred, margin, num_samples):
y_pred_list = tf.unpack(y) y_list = tf.unpack(y, num_samples)
y_pred_list = tf.unpack(y_pred, num_samples)
loss = 0.0 loss = 0.0
for y_, y_pred_ in zip(y_list, y_pred_list): for i in xrange(num_samples):
partition = tf.dynamic_partition(y_pred_, y_, 2) y_ = y_list[i]
pos_labels_scores = tf.expand_dims(tf.transpose(partition[1]),1) y_pred_ = y_pred_list[i]
neg_labels_scores = partition[0] k = tf.reduce_sum(y_)
margin_violation = tf.maximum( loss += tf.cond(
0, neg-labels_scores + margin - pos_labels_scores) k > 0.5,
loss += tf.reduce_mean(margin_violation) lambda: multilabel_margin_loss_inner(y_,y_pred_,margin),
lambda: tf.constant(0.0))
loss /= len(y_list) loss /= float(num_samples)
return loss return loss
def multilabel_margin_loss_inner(y_,y_pred_,margin):
partition_ids = tf.cast(y_>0.5,tf.int32)
partition = tf.dynamic_partition(y_pred_, partition_ids, 2)
pos_labels_scores = tf.expand_dims(partition[1],1)
neg_labels_scores = partition[0]
margin_violation = tf.maximum(
0.0, neg_labels_scores + margin - pos_labels_scores)
return tf.reduce_mean(margin_violation)
def mil_loss(scores, y, type='obj', epsilon=1e-5): def mil_loss(scores, y, type='obj', epsilon=1e-5):
if type=='obj': if type=='obj':
log_prob = tf.nn.log_softmax(scores) log_prob = tf.nn.log_softmax(scores)
...@@ -82,6 +94,7 @@ def mil_loss(scores, y, type='obj', epsilon=1e-5): ...@@ -82,6 +94,7 @@ def mil_loss(scores, y, type='obj', epsilon=1e-5):
loss = -tf.reduce_sum(max_region_scores)/tf.maximum(tf.reduce_sum(y),epsilon) loss = -tf.reduce_sum(max_region_scores)/tf.maximum(tf.reduce_sum(y),epsilon)
return loss return loss
if __name__=='__main__': if __name__=='__main__':
scores = tf.constant([[0.2, 0.3, 0.7],[0.8, 0.2, 0.9]]) scores = tf.constant([[0.2, 0.3, 0.7],[0.8, 0.2, 0.9]])
labels = tf.constant([[1.0, 0.0, 0.0],[0.0, 1.0, 0.0]]) labels = tf.constant([[1.0, 0.0, 0.0],[0.0, 1.0, 0.0]])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment