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 goes to 0 #4

Open
jaidmin opened this issue May 9, 2018 · 5 comments
Open

Loss goes to 0 #4

jaidmin opened this issue May 9, 2018 · 5 comments

Comments

@jaidmin
Copy link

jaidmin commented May 9, 2018

Hi,

first of all thanks for publishing the source code,
I want to try this new method in a segmentation problem I have, to avoid having to weigh the classes by hand.

When I try it, the loss approaches 0 very fast, going down to less than 2e-7 in a single epoch. I use it as follows:

max_pooling_loss = mpl.MaxPoolingLoss(ratio=0.3, p=1.7, reduce=True)
def mask_loss(pred,targ):
    return max_pooling_loss(F.cross_entropy(pred, targ, reduce=False))

When I just use crossentropy, the model trains fine.
Any ideas on what might be causing this?

Cheers,
Johannes

@bes-dev
Copy link
Owner

bes-dev commented May 9, 2018

Hi,

It's works for me with latest version of PyTorch. Which version of PyTorch do you use?

Best regards,
BeS

@jaidmin
Copy link
Author

jaidmin commented May 9, 2018

Hi,

I use pytorch 0.4 and python 3.6.
On pytorch 3.1, the code doesn't work due to the breaking changes regarding Variable/Tensor in 0.4.

Is the idea correct to first apply some loss_function and the use the maxpooling?

Cheers,
Johannes

@bes-dev
Copy link
Owner

bes-dev commented May 9, 2018

This code works fine for me:

class Loss:
    def __init__(self):
        self.criterion = nn.NLLLoss(reduce=False)
        self.mpl = mpl.MaxPoolingLoss(ratio=0.3, p=2.0, reduce=True)

    def __call__(self, pred, gt):
        loss = self.criterion(pred, gt)
        loss = self.mpl(loss)
        return loss

@jaidmin
Copy link
Author

jaidmin commented May 9, 2018

Hmm, still can't make it work...

Tried a few more values for p and ratio, but didn't change anything.

Any ideas on how I could try to debug this? Intermediate values or gradients or something?

I only have 5 classes in the moment, the background is dominating by far (about 86 % percent averaged over the training set), could that be a reason why it doesnt work for me?

@bes-dev
Copy link
Owner

bes-dev commented May 9, 2018

You can try to visualize loss before and after mpl and check that it works correct.

class Loss:
    def __init__(self):
        self.criterion = nn.NLLLoss(reduce=False)
        self.mpl = mpl.MaxPoolingLoss(ratio=0.3, p=2.0, reduce=False)

    def __call__(self, pred, gt):
        loss = self.criterion(pred, gt)
        cs_vis =  loss.cpu().data[0].numpy()
        _, max_v, _, _ = cv2.minMaxLoc(cs_vis)
        cv2.imshow("cs_vis", cs_vis / max_v)
        loss = self.mpl(loss)
        mpl_vis =  loss.cpu().data[0].numpy()
        _, max_v, _, _ = cv2.minMaxLoc(mpl_vis)
        cv2.imshow("mpl_vis", mpl_vis / max_v)
        cv2.waitKey(0)
        return loss.sum()

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

2 participants