Error in ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
The input image size is 512*512,in order to suit to the input of resnet.input image
I used 

_img = Image.open(self.images[index]).convert('RGB')


in dataloader.
I used resnet50 as my network's backbone without fc.The output shape is 

[4,2048,16,16]


then used two (conv bn relu) and a interpolate 

    def forward(self, input):
        x=self.backbone(input)
        x = self.conv1(x)
        x= self.bn1(x)
        x = self.relu(x)
        x = self.conv2(x)
        x= self.bn2(x)
        x = self.relu(x)
        x = F.interpolate(x, size=[512,512], mode='bilinear', align_corners=True)
        return x


The part of training

    self.criterion=nn.CrossEntropyLoss()
    if self.args.cuda:
        image, target = image.cuda(), target.cuda()
    self.scheduler(self.optimizer, i, epoch, self.best_pred)
    self.optimizer.zero_grad()
    output = self.model(image)
    loss = self.criterion(output, target.long())
    loss.backward()


But the Error occurs

File "E:/python_workspace/1006/train.py", line 135, in training
loss = self.criterion(output, target.long())
File "E:\python_workspace\1006\utils\loss.py", line 28, in CrossEntropyLoss
loss = criterion(logit, target.long())
File "E:\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 547, in __call__
result = self.forward(*input, **kwargs)
File "E:\anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 916, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File "E:\anaconda3\lib\site-packages\torch\nn\functional.py", line 1995, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File "E:\anaconda3\lib\site-packages\torch\nn\functional.py", line 1826, in nll_loss
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Assertion `cur_target &gt;= 0 &amp;&amp; cur_target &lt; n_classes' failed.  at C:\w\1\s\tmp_conda_3.6_045031\conda\conda-bld\pytorch_1565412750030\work\aten\src\THNN/generic/SpatialClassNLLCriterion.c:111

image.shape is [4, 3, 512, 512],dtype is torch.float32
target.shape is [4, 512, 512],dtype is torch.float32
output.shape is [4, 3, 512, 512],dtype is torch.float32


target image
The target images all only have three different colors.so I set output to 3 channel.And there's Image mode is P
Where may have problems in my code?

Judging by the sizes of your ternsors, your batch_size=4. You are trying to predict one of three labels per pixel, that is n_classes=3.

The error you got:


RuntimeError: Assertion `cur_target &gt;= 0 &amp;&amp; cur_target &lt; n_classes' failed.



Means that the target.long() you provide to your loss function has values either negative or larger than n_classes.  

Check the way you read the ground truth labels. If it's a P type image, you need to read it as such and not convert it to RGB values.

PS,
Do not use align_corners=True in F.interpolate, it introduces distortions.
