Hi everyone, I am aware that this sort of questions are already discussed but I couldnt make it work, so I have 3 channel 256x256 images and 7 classes for a semantic segmentation task.
RuntimeError: Expected target size [4, 256], got [4, 256, 256, 1] I am getting this error write now.
class CustomImageDataset(Dataset):
def __init__(self, mask, img, transform=None):
self.mask = mask
self.img = img
self.transform = transform
def __len__(self):
return np.unique(mask).size
def __getitem__(self, idx):
image = self.img[idx]
mask = torch.from_numpy(self.mask[idx])
mask = mask.type(torch.LongTensor)
if self.transform:
image = self.transform()(image)
return image, mask
Here is my dataset creator code. My inputs to this dataset creator is (num_img, 256,256,3) and (num_masks, 256,256,1) .
Edit : I was also getting “RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4” this error and I handled it with argmax operation (RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4) however it is causing 0 gradient error too. I feel quite lost…