Default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class 'NoneType'>

in dataloader files, it’s showing error.
Here is the code

class CDataset(Dataset):

def __init__(self, dataset, img_dir, df, transform=None): 
    self.dataset = dataset
    self.img_dir = img_dir
    self.df=df
    self.transform = transforms.Compose(transform) if transform else None
    
def __len__(self): 
    return len(self.dataset)

def return_image_mask(self, file):
    ds=dcmread(self.img_dir+file)
    arr=ds.pixel_array
    pat_id=file[:-4]
    if self.df[self.df['patientId']==pat_id].iloc[0]['Target']==0:
        mask=None
    else:
        mask=np.zeros(arr.shape,dtype='int')
        temp=self.df[self.df['patientId']==pat_id]
        for i in range(len(temp)):
            x=temp.iloc[i]['x']
            y=temp.iloc[i]['y']
            w=temp.iloc[i]['width']
            h=temp.iloc[i]['height']
            cv2.rectangle(mask, (int(x), int(y)), (int(x+w), int(y+h)),255,-1)
    return arr,mask
    
def __getitem__(self, i):
    img,mask = self.return_image_mask(self.dataset[i])
    if self.transform:
        img = self.transform(img)
        if mask is not None:
            mask = self.transform(mask)
    temp = self.df[self.df['patientId'] == self.dataset[i][:-4]]
    return img,mask,temp.iloc[0].Target

transform = [transforms.ToPILImage(),transforms.Resize(448), transforms.ToTensor()]
train_data=CDataset(train, img_folder,train_df,transform)
test_data=CDataset(test, img_folder,train_df,transform)

train_dl = DataLoader(train_data, batch_size=64)
test_dl = DataLoader(test_data, batch_size=64)

The dataloader has a function which generates batches of elements from the output of getitem.
As the error says, the outputs of that function are rectricted to those.

But I don’t see any restrictions as such

Default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class ‘NoneType’>

At some point you are either returning a mask, which is a np array, and None.
When the system tries to make a batch it fails (cannot compose arrays with None)

The simplest case is you to write your own collate function to return a list of elements instead of trying to batchify your output “mask”. Or return a mask of -1 values or something meaningful to you