Binary Labels and Loss function

Hi there!!
I am struggling on using CNN with BCELoss to classify with 0 or 1 images. Basically the loss struggles with the type of the labels but I really don’t understand how I should do it. Basically I have a class Dataset that opens an image and depending on having or not a feature I define a variable label as int 1 or int 0. Then, I convert it to tensor using torch.Tensor() and this class returns a sample with the image and the label. But then, when I try to train it, it always gives errors in the loss function and I really don’t understand why it doesn’t work.

Can someone help me? Thank you in advance.

Could you post the error message here, so that we could have a look? :slight_smile:

RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 185, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 74, in default_collate
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 74, in <dictcomp>
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: stack expects each tensor to be equal size, but got [0] at entry 0 and [1] at entry 1
class Dataset(object):
 #----Initializing the class----
    def __init__(self, train_names, masks_dir, img_dir,train, im_size):
        self.train_names=train_names
        self.masks_dir=masks_dir
        self.img_dir=img_dir
        self.train=train 
        self.noise = Variable(torch.zeros(299,299))
        self.data_augm=data_augm
       
        self.im_size=im_size

   
    def __len__(self):
        return len(self.train_names)
    
    #----To get a dataset item----
    def __getitem__(self, idx):

      image = Image.open(self.img_dir + self.train_names[idx])

      #target = 0

      
      if self.train == True:

        try:
          masks = Image.open(self.masks_dir + os.path.splitext(self.train_names[idx])[0]) 
          target = 1
            #----------------------------------------------------------------------- 
        except:
          target = 0
      else:
        target = 0

      image=np.array(image)
      if image.shape[2] == 4:
        #convert the image from RGBA2R
        image = cv2.cvtColor(image, cv2.COLOR_BGR2BGR)
      image=resize(image, (self.im_size , self.im_size),anti_aliasing=True)
      image = transforms.ToTensor()(image)
      image=image.float() 
      target = torch.Tensor(target)

      sample = {'image': image, 'target': target}
      
      return sample

And my CNN

class CNN(nn.Module): 
    def __init__(self, in_channels = 3, num_classes = 1):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3, stride = (1,1), padding = (1,1))
        self.pool = nn.MaxPool2d(kernel_size=2, stride = (2,2))
        self.conv2 = nn.Conv2d(in_channels=8, out_channels=16, kernel_size=3, stride = (1,1), padding = (1,1))
        self.fc1 = nn.Linear(16*75*75, num_classes)
        self.sigmoid = nn.Sigmoid()


    def forward(self, x):
        x= F.relu(self.conv1(x))
        x = self.pool(x)
        x= F.relu(self.conv2(x))
        x = self.pool(x)
        x = x.reshape(x.shape[0], -1)
        x = self.fc1(x)
        x = self.sigmoid(x)
        return x

Based on the error message it seems your data loading is creating an empty tensor and tries to concatenate it into a batch with other tensors.
Could you use batch_size=1, iterate the DataLoader in isolation without any training and check the shapes for the data and target tensors?