Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to implement focal loss on ssd300 model #248

Closed
vkherde opened this issue May 3, 2019 · 4 comments
Closed

[Question] How to implement focal loss on ssd300 model #248

vkherde opened this issue May 3, 2019 · 4 comments
Labels

Comments

@vkherde
Copy link

vkherde commented May 3, 2019

Hi @pierluigiferrari

Thanks for this exceptional tutorial, it provides a great learning. I wanted to implement Focal loss on ssd300 model which i trained on my own dataset. Can you suggest a way how can I do that. I tried implementing using below code but it's not working upto the mark:

def focal_loss(y_true, y_pred, gamma=2.0, alpha = 1.0):
#y_pred (tensor of shape (batch size, num_boxes, num_classes)
eps = K.epsilon()
y_pred = K.clip(y_pred, eps, 1.-eps)
pt = tf.where(tf.equal(y_true, 1), y_pred, 1- y_pred)
loss = -K.pow(1.-pt, gamma) * K.log(pt)
loss = alpha * loss
return tf.reduce_sum(loss, axis = -1)

If you get a chance please suggest, many thanks indeed

Regards

@stale
Copy link

stale bot commented May 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 10, 2019
@stale stale bot closed this as completed May 17, 2019
@DonghoonPark12
Copy link

@vkherde Hi. I also trying what you want to do and trying to understand what I need to change.
Let's try and share some intuition.

By the way, make sure that When you apply focal loss, you need to remove 'hard negative mining' parts.
Refer to it. links

@vkherde
Copy link
Author

vkherde commented May 27, 2019

@DonghunP Hi, sorry for delayed response, while implementing focal loss I just changed the log loss function with above code to fit focal loss. I am not sure whether we need to remove the hard negative mining part because it is used to find appropriate bounding box for the object whereas the focal loss would only improve the classification loss for imbalanced classes. Please refer this link

@DonghoonPark12
Copy link

DonghoonPark12 commented Jun 5, 2019

@vkherde Thanks. I may misunderstand the concept.
Anyway when I implement below code(when alpha is not 0) loss become infinite, but when I use your code above(when alpha is 1) loss doesn't go infinite. Thanks anyway.

def focal_loss(gamma=2, alpha=0.75):
	def focal_loss_fixed(y_true, y_pred):
		eps = K.epsilon()
		y_pred=K.clip(y_pred,eps,1.-eps)
        	pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
        	pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
        	return -K.sum(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1))-K.sum((1-alpha) * K.pow( pt_0, gamma) * K.log(1. - pt_0))
    return focal_loss_fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants