Getting 'NotImplementedError' to loading coustom dataset

I am trying to load my custom data set in Pytorch but every time getting ‘NotImplementedError’.I could not make sense where I am wrong.
here is my code

from __future__ import print_function, division
import os
from torch import nn
import torch,torchvision
import pandas as pd
import pandas
from torchvision.transforms import transforms
import numpy as np
import matplotlib.pyplot as plt
from import Dataset, DataLoader
from torchvision import transforms, utils
from PIL import Image

root_dirA= '/home/mchow/Downloads/homework/datasetA/'
root_dirB= '/home/mchow/Downloads/homework/datasetB/'

fname = '/home/mchow/Downloads/homework/kxr_sq_bu00.sas7bdat'

class OIDataset(Dataset):

   def __init__(self,fname, root_dir,transform=None):

     self.fp = pandas.read_sas( fname, format = 'sas7bdat', encoding='iso-8859-1')

    #get column names and find entries that end with 'KL'
     self.lfp = list(self.fp)
     self.matches = [x for x in self.lfp if 'KL'== x[-2:]]

     self.fp['ID'] = self.fp['ID'].astype('int')
     self.fp['SIDE'] = self.fp['SIDE'].astype('int')

#add 'ID' index
     self.fp_kl = self.fp[ ['ID'] + ['SIDE'] + self.matches]
     self.fp_kl.drop_duplicates(subset=['ID', 'SIDE'])
#change index
     self.kl_table = self.fp_kl.set_index('ID')
     self.root_dir = root_dir
     self.transform = transform

def __len__(self):
    return len(self.fname)

def __getitem__(self, idx):
    img_name = os.path.join(self.root_dir, self.ID[idx])

    # Read each 784 pixels and reshape the 1D array ([784]) to 2D array ([28,28])
    patches, p_id = np.load(img_name)
    # get right image
    right_image = Image.fromarray(patches['R'].astype('uint8'), 'L')

    # left image
    left_image = Image.fromarray(patches['L'].astype('uint8'), 'L')

    print(patches, p_id.shape)
    img_class = self.fp_kl[idx]
    img_ID = self.fp[idx]
    img_side = self.fp[idx]
    sample = {'subject id': (patches, p_id), 'side': right_image, 'side2': left_image}
    # Return image and the label
    if self.transform:
        sample = self.transform(sample)

    return sample

trans = transforms.Compose([transforms.ToTensor(),
                               transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))])

if __name__ == '__main__':

  dset_train = OIDataset(fname,root_dirA,transform=trans)

```Here is the error 

Traceback (most recent call last):
File “”, line 1, in
File “/home/mchow/miniconda3/lib/python3.6/site-packages/torch/utils/data/”, line 20, in len
raise NotImplementedError

I will appreciate any kind of help regarding my question.

__len__ and __getitem__ are at the wrong indentation, thus not part of the Dataset class.
Move them an indentation to the right and run your code again.

1 Like

Thank you @ptrblck ! Now this code is working.

I don’t see how the linked issue without any further description can be related to PyTorch as it’s using the dataset Python package.