Taget and input must have the same number of elements

RegularGuY: <p>My input label size is <code>torch.size([30, 2, 96, 96, 96])</code></p>
<p>My labels size is <code>torch.size([30, 96, 96, 96])</code>  ,Im feeding them to my loss function which goes like this</p>
<pre><code>loss = F.binary_cross_entropy(F.sigmoid(output),labels,torch.FloatTensor(CLASS_WEIGHTS).cuda())
</code></pre>
<p>when i run this though i get</p>
<pre><code>Value error:Target and input must have the same number of elements.target nelement(26542080) != input nelement(53084160)
</code></pre>
<p>Im a little confused here, i get the input value is twice the target value because it multiplies <code>[30,96,96]</code> by num classes,But im not sure why is that, and how to rectify it, Any suggestions will be helpful,Thanks in advance.</p>

ptrblck: <p><code>nn.BCELoss</code> needs inputs and targets of the same shape, since each element individually could predict the occurrence of the class.<br>
Probably you are looking for <a href="https://pytorch.org/docs/master/nn.html#torch.nn.NLLLoss">nn.NLLLoss</a> or <code>nn.CrossEntropyLoss</code>.</p>

samin_hamidi: <p>I have the same issue. I tried with nn.CrossEntropyLoss and then BCELoss and got errors in both case. Just tried nn.NLLLoss and got this error:<br>
“bool value of Tensor with more than one value is ambiguous”</p>
<p>I cannot understand what is the problem. My images are read in as PIL images in RGB mode and then transformed this way:</p>
<p>data_transforms = {<br>
‘train’: transforms.Compose([<br>
transforms.RandomResizedCrop(input_size),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
‘val’: transforms.Compose([<br>
transforms.Resize(input_size),<br>
transforms.CenterCrop(input_size),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
}</p>
<p>Masks are .PNG images and read with PIL library and the mode is L (black and white images). I applied these transformations:</p>
<p>class Train_Dataset(Dataset):</p>
<pre><code>def __init__(self, imgarray, maskarray, transform=None):

    
    self.images = imgarray
    self.masks = maskarray
    self.transform = transform

    
def __getitem__(self, index):
    
    img = self.images[index]
    if self.transform is not None:
        img = self.transform(img)  
    
    #label = torch.from_numpy(self.masks[index])
    trnsf1 = transforms.RandomResizedCrop(input_size)
    trnsf2 = transforms.ToTensor()
    label = trnsf2(trnsf1(self.masks[index]))
    
   
    return img, label

def __len__(self):
    return len(self.images) 
</code></pre>
<p>#<span class="hashtag">#-----------------------------------------------------------------------------------</span></p>
<p>class val_Dataset(Dataset):</p>
<pre><code>def __init__(self, imgarray_val, maskarray_val, transform=None):

    
    self.images = imgarray_val
    self.masks = maskarray_val
    self.transform = transform

    
def __getitem__(self, index):
   
    img = self.images[index]
    if self.transform is not None:
        img = self.transform(img)
        
    #label = torch.from_numpy(self.masks[index])
    trnsf1 = transforms.RandomResizedCrop(input_size)
    trnsf2 = transforms.ToTensor()
    label = trnsf2(trnsf1(self.masks[index]))
  
    return img, label

def __len__(self):
    return len(self.images)
</code></pre>
<p>So basically resizing the image and then converting it into a tensor.<br>
I am not certain if converting a mask image into a tensor is reasonable as my mask label contains class indexes (each pixel gets discrete values between 0 and 3). When I transform this image into a tensor, the values will be between 0 and 1. I wonder how the computations will take place?<br>
Also, My mask tensors get the shape [N, C, W, H]=[2, 1, 224,224] and the output of the model has the shape of [2, 4, 224, 224]. When using CrossEntropy, I get an error telling me that the dimension of my target should be of 3 and not 4. How should I change this? should I drop the C  value and create a tensor of shape [N, W, H].</p>

ptrblck: <aside class="quote no-group" data-username="samin_hamidi" data-post="3" data-topic="19090">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/a698b9/48.png" class="avatar"> samin_hamidi:</div>
<blockquote>
<p>I am not certain if converting a mask image into a tensor is reasonable as my mask label contains class indexes (each pixel gets discrete values between 0 and 3). When I transform this image into a tensor, the values will be between 0 and 1.</p>
</blockquote>
</aside>
<p>That’s a correct assumption and therefore I would transform the mask to a tensor via <code>torch.from_numpy</code> to keep the class indices. Some transformations, e.g. resizing or <code>ToTensor</code> might destroy your class indices.</p>
<aside class="quote no-group" data-username="samin_hamidi" data-post="3" data-topic="19090">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/a698b9/48.png" class="avatar"> samin_hamidi:</div>
<blockquote>
<p>should I drop the C value and create a tensor of shape [N, W, H].</p>
</blockquote>
</aside>
<p>Yes, that’s also correct. <code>nn.CrossEntropyLoss</code> as well as <code>nn.NLLLoss</code> expect a target without the channel dimension, which contains just the class indices.<br>
In you case you could just call <code>target = target.squeeze(1)</code>.</p>

samin_hamidi: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="19090">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>That’s a correct assumption and therefore I would transform the mask to a tensor via <code>torch.from_numpy</code> to keep the class indices. Some transformations, e.g. resizing or <code>ToTensor</code> might destroy your class indices.</p>
</blockquote>
</aside>
<p>Thank you for the answer.<br>
I have another question now, if I am resizing the image so it is suitable as my network input, isn’t it mandatory then to resize the mask too?<br>
also as my mask images do not have the same size as my input images, what is the correct approach to choose here?</p>

ptrblck: <p>Yes, if you are resizing your input images, you should also resize the masks, too.<br>
However, make sure to use <code>interpolation=PIL.Image.NEAREST</code>, otherwise your class indices might be distorted.<br>
Also, if you are planning on applying random data transformations, I would recommend to use the functional API as described in <a href="https://discuss.pytorch.org/t/torchvision-transfors-how-to-perform-identical-transform-on-both-image-and-target/10606/7">this post</a> to make sure both the image and mask are using the same random values.</p>

samin_hamidi: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="19090">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>interpolation=PIL.Image.NEAREST</p>
</blockquote>
</aside>
<p>Must I use <code>interpolation=PIL.Image.NEAREST</code> for both labels and input images?<br>
Another issue that comes to  mind here is that:<br>
My labels are read as json files, converted into nparrays and saved into a list. What can be the order of applying the changes?</p>
<ol>
<li>First convert the label into a PIL image,</li>
<li>Resize using ‘interpolation=PIL.Image.NEAREST’,</li>
<li>then convert the resized image into nparray again</li>
<li>and then apply torch.to_numpy?</li>
</ol>
<p>If I change this order somehow, will it affect the final result?</p>

ptrblck: <p>No, you can apply any interpolation on the image (as long as your training pipeline works fine, as some interpolation methods might work better than others). The main concern is to keep your mask with class indices.</p>
<p><code>PIL</code> uses numpy arrays internally, if I’m not mistaken, so your workflow should work fine.</p>
<p>Once you have created your <code>Dataset</code> just make sure that your mask and image tensors are still valid. I would simply read some random samples and e.g. visualize the mask.</p>

samin_hamidi: <p>Thank you. I proceeded this way:<br>
'class val_Dataset(Dataset):</p>
<pre><code>def __init__(self, imgarray_val, labelarray_val, transform=None):

    
    self.images = imgarray_val
    self.labels = labelarray_val
    self.transform = transform

    
def __getitem__(self, index):
   
    img = self.images[index]
    if self.transform is not None:
        img = self.transform(img)
        
    
    Img = Image.fromarray(self.labels[index]) #numpy array to PIL
    PilImg = Img.resize((224, 224),Image.NEAREST) #resize the PIL image
    label = torch.from_numpy(np.asarray(PilImg)) # convert PIL to numpy and then convert the numpyarray to tensor
    
    #trnsf1 = transforms.RandomResizedCrop(input_size)
    #trnsf2 = transforms.ToTensor()
    #label = trnsf2(trnsf1(self.masks[index]))
          
    print(img.size())
    print(label.size()) 
          
    return img, label

def __len__(self):
    return len(self.images) '
</code></pre>
<p>Now I encounter this error:<br>
‘RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 231 and 228 in dimension 3 at …\aten\src\TH/generic/THTensor.cpp:711’</p>
<p>what I get as output is this:</p>
<h2><a name="epoch-00-1" class="anchor" href="#epoch-00-1"></a>'Epoch 0/0</h2>
<p>torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
image shape: torch.Size([4, 3, 224, 224])<br>
label shape: torch.Size([4, 224, 224])<br>
squeezed label shape:torch.Size([4, 224, 224])<br>
model output shape:torch.Size([4, 4, 224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
image shape: torch.Size([4, 3, 224, 224])<br>
label shape: torch.Size([4, 224, 224])<br>
squeezed label shape:torch.Size([4, 224, 224])<br>
model output shape:torch.Size([4, 4, 224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
image shape: torch.Size([4, 3, 224, 224])<br>
label shape: torch.Size([4, 224, 224])<br>
squeezed label shape:torch.Size([4, 224, 224])<br>
model output shape:torch.Size([4, 4, 224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
torch.Size([3, 224, 224]) torch.Size([224, 224])<br>
image shape: torch.Size([4, 3, 224, 224])<br>
label shape: torch.Size([4, 224, 224])<br>
squeezed label shape:torch.Size([4, 224, 224])<br>
model output shape:torch.Size([4, 4, 224, 224])<br>
train Loss: 1.7062 Acc: 29483.5625<br>
torch.Size([3, 224, 231])<br>
torch.Size([224, 224])<br>
torch.Size([3, 224, 228])<br>
torch.Size([224, 224])<br>
torch.Size([3, 224, 232])<br>
torch.Size([224, 224])<br>
torch.Size([3, 224, 229])<br>
torch.Size([224, 224])<br>
’</p>
<p>and the training function is as follows:</p>
<p>def train_model(model, dataloaders, criterion, optimizer, num_epochs=25, has_aux = True):<br>
since = time.time()</p>
<pre><code>val_acc_history = []

best_model_wts = copy.deepcopy(model.state_dict())#??????
best_acc = 0.0

for epoch in range(num_epochs):
    print('Epoch {}/{}'.format(epoch, num_epochs - 1))
    print('-' * 10)

    # Each epoch has a training and validation phase
    for phase in ['train', 'val']:
        if phase == 'train':
            model.train()  # Set model to training mode
        else:
            model.eval()   # Set model to evaluate mode

        running_loss = 0.0
        running_corrects = 0

        # Iterate over data.
        
        for inputs, labels in dataloaders[phase]:
            inputs, labels = Variable(inputs), Variable(labels)
            inputs = inputs.to('cpu')
            labels = labels.to('cpu')
            
            print('image shape: {}'.format(inputs.shape))
            print('label shape: {}'.format(labels.shape))
            #labels = labels.squeeze(1)
            #print('squeezed label shape:{}'.format(labels.shape))

            # zero the parameter gradients
            optimizer.zero_grad()

            # forward
            # track history if only in train
            with torch.set_grad_enabled(phase == 'train'):
                # Get model outputs and calculate loss
                # Special case for inception because in training it has an auxiliary output. In train
                #   mode we calculate the loss by summing the final output and the auxiliary output
                #   but in testing we only consider the final output.
                if has_aux and phase == 'train':
                    # From https://discuss.pytorch.org/t/how-to-optimize-inception-model-with-auxiliary-classifiers/7958
                    outputs = model(inputs)
                    print('model output shape:{}'.format(outputs['out'].shape))
                    loss1 = criterion(outputs['out'], labels.long())
                    loss2 = criterion(outputs['aux'], labels.long())
                    #loss2 = output['aux']
                    loss = loss1 + 0.4*loss2
                #else:
                 #   outputs = model(inputs)
                  #  loss = criterion(outputs, labels.long())

                _, preds = torch.max(outputs['out'], 1)

                # backward + optimize only if in training phase
                if phase == 'train':
                    loss.backward()
                    optimizer.step()

            # statistics
            running_loss += loss.item() * inputs.size(0)
            running_corrects += torch.sum(preds == labels.data.long())

        epoch_loss = running_loss / len(dataloaders[phase].dataset)
        epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)

        print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))

        # deep copy the model
        if phase == 'val' and epoch_acc &gt; best_acc:
            best_acc = epoch_acc
            best_model_wts = copy.deepcopy(model.state_dict())
        if phase == 'val':
            val_acc_history.append(epoch_acc)

    print()

time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))

# load best model weights
model.load_state_dict(best_model_wts)
return model, val_acc_history
</code></pre>
<p>What I understood so far is that transformation works fine for the train images and not the validation images but I am checking my code and I cannot find the error!<br>
this is the  data set function and data loaders:</p>
<blockquote>
<p>data_transforms = {<br>
‘train’: transforms.Compose([<br>
transforms.Resize(input_size, input_size),<br>
<span class="hashtag-raw">#transforms</span>.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
‘val’: transforms.Compose([<br>
transforms.Resize(input_size, input_size),<br>
<span class="hashtag-raw">#transforms</span>.CenterCrop(input_size),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
}<br>
print(“Initializing Datasets and Dataloaders…”)</p>
</blockquote>
<blockquote>
<p>class Train_Dataset(Dataset):</p>
<pre><code>def __init__(self, imgarray, labelarray, transform=None):

    
    self.images = imgarray
    self.labels = labelarray
    self.transform = transform

    
def __getitem__(self, index):
    
    img = self.images[index]
    if self.transform is not None:
        img = self.transform(img)  
    
   
    Img = Image.fromarray(self.labels[index]) #numpy array to PIL
    PilImg = Img.resize((224, 224), Image.NEAREST ) #resize the PIL image
    label = torch.from_numpy(np.asarray(PilImg)) # convert PIL to numpy and then convert the numpyarray to tensor
    

    print(img.size(), label.size())
    return img, label

def __len__(self):
    return len(self.images) 
</code></pre>
<p>class val_Dataset(Dataset):</p>
<pre><code>def __init__(self, imgarray_val, labelarray_val, transform=None):

    
    self.images = imgarray_val
    self.labels = labelarray_val
    self.transform = transform

    
def __getitem__(self, index):
   
    img = self.images[index]
    if self.transform is not None:
        img = self.transform(img)
        
    
    Img = Image.fromarray(self.labels[index]) #numpy array to PIL
    PilImg = Img.resize((224, 224),Image.NEAREST) #resize the PIL image
    label = torch.from_numpy(np.asarray(PilImg)) # convert PIL to numpy and then convert the numpyarray to tensor
    
    #trnsf1 = transforms.RandomResizedCrop(input_size)
    #trnsf2 = transforms.ToTensor()
    #label = trnsf2(trnsf1(self.masks[index]))
          
    print(img.size(), label.size())

          
    return img, label

def __len__(self):
    return len(self.images) 
</code></pre>
</blockquote>
<p>I markeddown two lines of code in transformation code block:<br>
for train set<br>
<span class="hashtag-raw">#transforms</span>.RandomHorizontalFlip(),<br>
and for val set:<br>
<span class="hashtag-raw">#transforms</span>.CenterCrop(input_size),<br>
might it be this is the problem?</p>

ptrblck: <p>Could you try to pass the size as a tuple to <code>Resize</code>?</p>
<pre><code class="lang-python">transforms.Resize((input_size, input_size))
# instead of
transforms.Resize(input_size, input_size)
</code></pre>
<p>The second argument should be used as the <code>interpolation</code> argument, and should throw an error, if you are trying to resize the input. If one side of the passed image has already the desired shape, this might be a no-op, thus now throwing the error.</p>

samin_hamidi: <aside class="quote no-group" data-username="ptrblck" data-post="10" data-topic="19090">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>d you try to pass the size as a tuple to <code>Resiz</code></p>
</blockquote>
</aside>
<p>I changed the argument into a tuple and now it is working. Thank you very much <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>