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

Loss calculated on wrong dimension #30

Open
gergopool opened this issue Jul 4, 2022 · 0 comments
Open

Loss calculated on wrong dimension #30

gergopool opened this issue Jul 4, 2022 · 0 comments

Comments

@gergopool
Copy link

Hi,

When you've written the loss functions, did you calculate with batch data?
E.g. reading your tversky loss implementation, I think you've accidentally summed up everything aggregated over the batch instead of the data points.

    true_pos = K.sum(y_true_pos * y_pred_pos)
    false_neg = K.sum(y_true_pos * (1-y_pred_pos))
    false_pos = K.sum((1-y_true_pos)*y_pred_pos)

When you do this, you sum up every prediction, but you actually want to calculate the loss for each image input.
Let me suggest the following:

def tversky():
    y_true_pos = tf.reshape(y_true, (batch_size, -1))
    # [...]
    true_pos = K.sum(y_true_pos * y_pred_pos, axis=1)
    # [...]

def tversky_loss():
    return K.sum(1 - tversky())

For this reason your focal loss should make no difference to the simple tversky loss, because you raised the aggregated sum to the power of gamma. Instead, I believe you should raise the outputs to the power of gamma before the sum.

Of course if somehow your loss is calculated per a sample, than everything looks fine and keras made an automatic aggregation later, then everything looks fine.

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

No branches or pull requests

1 participant