Skip to content

Strange behavior in nn.NLLLoss2d() function #5452

Closed

Description

@MabinogiX
  • OS: win10
  • PyTorch version: 0.3.0
  • How you installed PyTorch (conda, pip, source): source
  • Python version: 3.6
  • CUDA/cuDNN version: 8.0
  • GPU models and configuration: GTX1080
  • GCC version (if compiling from source): VS2015

I am doing some image classification and I have a model which uses nn.NLLLoss2d as loss function.
The size of images are 1024*512.
I can run this model on GPU when I use this kind of image. But when I resize images to 512*256, the model will fail in some images.
I have located the problem in nn.NLLLoss2d() function. The function can return loss without problems, but when I try to access the loss, such as loss.data[0] or loss.backward(), the program will break down:

RuntimeError: cuda runtime error (77) : an illegal memory access was encountered at d:\pytorch\pytorch\torch\lib\thc\generic/THCStorage.cu:58

And when I move the whole model to cpu and use images of 1024*512, the model also fails in some images:

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at d:\pytorch\pytorch\torch\lib\thnn\generic/SpatialClassNLLCriterion.c:111

The size of input tensor and target are [1, 9, 512, 1024] and [1, 512, 1024] and I am pretty sure that the value of target is 0<=value<9.
I tried it on my macbook pro and I have the same problem.

Activity

  1. MabinogiX commented on Feb 28, 2018

    @MabinogiX
    Author

    I find the problem, when I resize the mask, some objects overlap with another and the values of mask have changed. But there is no bug report on GPU. It takes me a whole day to find the problem.

  2. ssnl commented on Feb 28, 2018

    @ssnl
    Collaborator

    @Mabinogiysk In general, you don't want to resize a noncontiguous tensor.

  3. zou3519 commented on Mar 5, 2018

    @zou3519
    Contributor

    At @Mabinogiysk could you provide us with an example of how you're resizing a mask please? It would be great if there was an error message for something that could turn perilous like it sounds like in your case.

  4. MabinogiX commented on Mar 15, 2018

    @MabinogiX
    Author

    This is the code:

    import torch
    import numpy as np
    
    # Three classes
    inputs = np.random.random((1, 3, 10, 10))
    label = np.random.randint(0, 3, size=(1, 10, 10))
    
    # Changing some labels' id to great than 3
    label[0, 3, 6] = 5
    label[0, 3, 8] = 4
    label[0, 6, 8] = 6
    label[0, 8, 2] = 5
    
    nllloss = torch.nn.NLLLoss2d().cuda()
    inputs = torch.autograd.Variable(torch.from_numpy(inputs).float()).cuda()
    label = torch.autograd.Variable(torch.from_numpy(label).long()).cuda()
    
    loss = nllloss(inputs, label)
    

    This code can run without any errors. If I remove .cuda(), there will be some error:

    nllloss = torch.nn.NLLLoss2d()
    inputs = torch.autograd.Variable(torch.from_numpy(inputs).float())
    label = torch.autograd.Variable(torch.from_numpy(label).long())
    
    loss = nllloss(inputs, label)
    

    outputs:

    Assertion `cur_target >= 0 && cur_target < n_classes' failed.  at d:\pytorch\pytorch\torch\lib\thnn\generic/SpatialClassNLLCriterion.c:111
    

    And I hope the message can be more specific. I had been stuck for several days and tried to figure out what cur_target is.

  5. MabinogiX commented on Mar 15, 2018

    @MabinogiX
    Author

    I am not sure what cause the problem:

    RuntimeError: cuda runtime error (77) : an illegal memory access was encountered at d:\pytorch\pytorch\torch\lib\thc\generic/THCStorage.cu:58

  6. apaszke commented on Mar 15, 2018

    @apaszke
    Contributor

    The first one says that your targets are out of range (you only have 3 input classes and put 6 into targets). How did you get the second error? This should never happen

  7. MabinogiX commented on Mar 21, 2018

    @MabinogiX
    Author

    I don't know. I think it related to my memory, which is too small.

  8. MabinogiX commented on Mar 21, 2018

    @MabinogiX
    Author

    When I fixed the first error, the second one disappeared too.

  9. added
    todo
    on May 14, 2018
  10. zou3519 commented on Aug 28, 2018

    @zou3519
    Contributor

    Going to close this due to inability to reproduce and no other similar reports. @Mabinogiysk please feel free to reopen this issue if it persists.

  11. FionaFN commented on Mar 3, 2019

    @FionaFN

    I also meet the very similar problem when trying to run DRN on Linemod Dataset... Still trying to figure it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

todo

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions