Got NotImplementedError and Custom Dataloader for Image and Mask pair

username: RadAlienware
cooked: <p>Advance sorry for being noob in Pytorch.<br>
I am trying to do image segmentation with custom data set. ( /home/images/ and /home/masks/)<br>
<strong>Home</strong><br>
<strong>train_val</strong><br>
<strong>Images</strong></p>
<ul>
<li><strong>Images1 . Jpg</strong></li>
<li><strong>…………</strong></li>
</ul>
<p><strong>Masks</strong></p>
<ul>
<li><strong>Images1 . bmp</strong></li>
<li><strong>……………….</strong></li>
</ul>
<p>I am getting the number of images and masks pairs. But the visualization is not working properly. I am pretty sure something is incorrect with my dataloader set.</p>
<p>I uploaded the full code here.</p>
<pre><code class="lang-auto">def robotfishhumanreefwrecks(mask):
    human=torch.zeros((imw,imh))
    fish=torch.zeros((imw,imh))
    robot=torch.zeros((imw,imh))
    reef=torch.zeros((imw,imh))
    wrecks=torch.zeros((imw,imh))
    for i in range(imw):
        for j in range(imh):
            if (mask[i,j,0]==0 and mask[i,j,1]==0 and mask[i,j,2]==1):
                    Human[i, j] = 1 
            elif (mask[i,j,0]==1 and mask[i,j,1]==1 and mask[i,j,2]==0):
                fish[i, j] = 1  
            elif (mask[i,j,0]==1 and mask[i,j,1]==0 and mask[i,j,2]==0):
                robot[i, j] = 1  
            elif (mask[i,j,0]==1 and mask[i,j,1]==0 and mask[i,j,2]==1):
                reef[i, j] = 1  
            elif (mask[i,j,0]==0 and mask[i,j,1]==1 and mask[i,j,2]==1):
                wreck[i, j] = 1 
            else:
                pass

    return torch.stack((robot,fish,human,reef,wreck),-1)

def getSaliency(mask):
    imgw,imgh=mask.shape[0],mask.shape[1]
    sal=torch.zeros((imgw,imgh))
    for i in range(imgw):
        for j in range(imgh):
            if (mask[i,j,0]==0 and mask[i,j,1]==0 and mask[i,j,2]==1):
                sal[i,j]=1
            elif (mask[i,j,0]==1 and mask[i,j,1]==0 and mask[i,j,2]==0):
                sal[i, j] = 1  
            elif (mask[i,j,0]==1 and mask[i,j,1]==1 and mask[i,j,2]==0):
                sal[i, j] = 1   
            elif (mask[i,j,0]==0 and mask[i,j,1]==1 and mask[i,j,2]==1):
                sal[i, j] = 0.8  
            else: pass  
        
            
    return sal.unsqueeze(1)
            
            

def processData(image,mask,sal=False):
     # scaling image data and masks
    image = image / 255
    mask = mask /255
    mask[mask &gt; 0.5] = 1
    mask[mask &lt;= 0.5] = 0
    m = []
    for i in range(mask.shape[0]):
        if sal:
            m.append(getSaliency(mask[i]))
        else:
            m.append(robotfishhumanreefwrecks(mask[i]))
            
    m=torch.tensor(m)
    return (img,m)


class SUIMData(Dataset):
    def __init__(self,folder_path,transform=None):
        
#         self.images_filenames=images_filenames
        self.images_directory=sorted(glob.glob(os.path.join(folder_path,'images','*.jpg')))
        self.masks_directory=sorted(glob.glob(os.path.join(folder_path,'masks','*.bmp')))
        self.transform=transform
    def __len__(self):
        
        return len(self.images_directory)
    
    def __getitem__(self,index):
        image = Image.open(self.img_files[index])
        mask = Image.open(self.mask_files[index])
        image=processData(image)
        mask=processData(mask)
        if self.transform is not None:
            transformed = self.transform(image=image, mask=mask)
            image = transformed["image"]
            mask = transformed["mask"]
        return image, mask


    def load_training(batch_size,image_folder,mask_folder, sal=False):
        scale=Rescale(256)
        transform = transforms.Compose(
    #         [transforms.RandomHorizontalFlip(),
            [Rescale(256),
             transforms.ToTensor()])
        image_data=torchvision.datasets.ImageFolder(root=image_folder,transform=transform)
        mask_data=torchvision.datasets.ImageFolder(root=mask_folder,transform=transform)

        image_generator=DataLoader(image_data,batch_size=batch_size,shuffle=True,num_workers=4)
        mask_generator=DataLoader(mask_data,batch_size=batch_size,shuffle=True,num_workers=4)
        for (img,mask) in zip(image_generator,mask_generator):
            img,mask_indiv=processData(img,mask,sal)
            yield img,mask_indiv

    def getPaths(data_dir):
        # read image files from directory
        exts = ['*.png','*.PNG','*.jpg','*.JPG', '*.JPEG', '*.bmp']
        image_paths = []
        for pattern in exts:
            for d, s, fList in os.walk(data_dir):
                for filename in fList:
                    if (fnmatch.fnmatch(filename, pattern)):
                        fname_ = os.path.join(d,filename)
                        image_paths.append(fname_)
        return image_paths
    
    
data=SUIMData(folder_path='home/train_val')
len(data)

import albumentations as A
from albumentations.pytorch import ToTensorV2
train_transform = A.Compose(
    [
        A.Resize(256, 256),
        A.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=30, p=0.5),
        A.RGBShift(r_shift_limit=25, g_shift_limit=25, b_shift_limit=25, p=0.5),
        A.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3, p=0.5),
        A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2(),
    ]
)

def visualize_augmentations(dataset, idx=0, samples=5):
    dataset = copy.deepcopy(dataset)
    dataset.transform = A.Compose([t for t in dataset.transform if not isinstance(t, (A.Normalize, ToTensorV2))])
    figure, ax = plt.subplots(nrows=samples, ncols=2, figsize=(10, 24))
    for i in range(samples):
        image, mask = dataset[idx]
        ax[i, 0].imshow(image)
        ax[i, 1].imshow(mask, interpolation="nearest")
        ax[i, 0].set_title("Augmented image")
        ax[i, 1].set_title("Augmented mask")
        ax[i, 0].set_axis_off()
        ax[i, 1].set_axis_off()
    plt.tight_layout()
    plt.show()

random.seed(42)
train_dataset = SUIMData(folder_path='home/train_val',transform=train_transform)

visualize_augmentations(train_dataset, idx=55)

</code></pre>
<p>This visualize augmentation is giving me NotImplementedError. Advance thanks</p>

username: ptrblck
cooked: <p>Could you post the full stacktrace so that we could check which method is not implemented?</p>

username: RadAlienware
cooked: <p>here is the full error</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_55840/168878227.py in &lt;module&gt;
      4 train_dataset = SUIMData(folder_path='home/train_val',transform=train_transform)
      5 
----&gt; 6 visualize_augmentations(train_dataset, idx=55)

~\AppData\Local\Temp/ipykernel_55840/71307043.py in visualize_augmentations(dataset, idx, samples)
      5     figure, ax = plt.subplots(nrows=samples, ncols=2, figsize=(10, 24))
      6     for i in range(samples):
----&gt; 7         image, mask = dataset[idx]
      8         ax[i, 0].imshow(image)
      9         ax[i, 1].imshow(mask, interpolation="nearest")

~\anaconda3\envs\envpytorch\lib\site-packages\torch\utils\data\dataset.py in __getitem__(self, index)
     23 
     24     def __getitem__(self, index):
---&gt; 25         raise NotImplementedError
     26 
     27     def __add__(self, other):

NotImplementedError: 
</code></pre>

username: ptrblck
cooked: <p>Are you sure you are using the posted code here?<br>
Based on the error message the <code>__getitem__</code> method cannot be found, while the definition is posted in your previous code snippet. I also can’t reproduce the issue using your code, but of course I had to remove undefined parts of your code.</p>

username: RadAlienware
cooked: <p>Yes. I am using the exact code. It will be great if for the aforementioned dataset structure of getting pair of image and mask can you give me a dataloader sample? Thanks</p>

username: ptrblck
cooked: <p>As previously mentioned your posted code snippet works fine using random data. Could you post a minimal and executable code snippet which would reproduce the issue?</p>