I’m new to PyTorch and this is my first segmentation attempt, so apologies in advance if there’s something basic I’m missing here.
I’m trying to segment the FloodNet dataset and am running into a strange error when I iterate over the images in a DataLoader. Specifically, I get “ValueError: not enough values to unpack (expected 2, got 1)” every 41 images for some reason… This doesn’t change when I change the batch size (e.g. with a batch size of 8 I run into trouble on the 6th batch, which includes the 41st image, etc.) and also doesn’t change when I shuffle the images; I’ve iterated through with a batch size of 1 and checked the image where I get the first error and can display the image/mask pair okay with matplotlib so it doesn’t look as though it’s the images themselves. I really have no idea why it might fail at regular intervals like this.
The code for the Dataset and DataLoader is here (
IMG_SIZE = 256) and I’m running pytorch 2.1.0 on a colab notebook:
train_transforms = A.Compose([ A.Resize(IMG_SIZE, IMG_SIZE), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5) ], is_check_shapes=False) class SegmentationDataset(Dataset): def __init__(self, df, augmentations): self.df = df self.augmentations = augmentations def __len__(self): return len(self.df) def __getitem__(self, idx): row = self.df.iloc[idx] image_path = row['images'] mask_path = row['masks'] image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image.astype('uint8') mask = cv2.imread(mask_path, cv2.IMREAD_UNCHANGED) mask = np.expand_dims(mask, axis=-1) if self.augmentations: data = self.augmentations(image=image, mask=mask) image = data['image'] mask = data['mask'] image = np.transpose(image, (2,0,1)).astype(np.float32) mask = np.transpose(mask, (2,0,1)) image = torch.Tensor(image) / 255.0 mask = torch.Tensor(mask).long() return image, mask train = SegmentationDataset(train_df, train_transforms) train_loader = DataLoader(train, batch_size=BATCH_SIZE, shuffle=False)
Thanks a lot, and as I say, it’s my first post so feel free to let me know if this could be clearer or if I’ve missed anything that would help.