-
Notifications
You must be signed in to change notification settings - Fork 937
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
Comments
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. |
@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 |
@vkherde Thanks. I may misunderstand the concept. 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 |
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
The text was updated successfully, but these errors were encountered: