0

I am trying the create a custom dataset and if I call neither the getitem or len functions as object.len() or object.getitem(index) I get this error

File ~/Library/Python/3.9/lib/python/site-packages/torch/utils/data/dataset.py:83, in Dataset.__getattr__(self, attribute_name)
     81     return function
     82 else:
---> 83     raise AttributeError

the code itself is like the following,

class CustomDataset(Dataset):
def __init__(self,
             path_to_images,
             transform_extra=None,
             img_size=128):

    self.images= createimages(path, img_size)

def __len__(self):
    return len(self.images)
    
def __getitem__(self, idx):
    return self.images[idx][0], self.images[idx][1]

Images is a 2d list of Pillow images and string labels and the createimages function works outside of the class. I am not very familiar with Python so I was wondering what is going wrong here.

Lin
  • 47
  • 5
  • Can you share swith us createimages function ? – Maxime D. Feb 20 '22 at 18:57
  • Correct `path` to `path_to_images` in __ intit __ – Hamzah Feb 20 '22 at 22:24
  • I prefer if you can add the external function inside the class, otherwise follow this guideline https://stackoverflow.com/questions/15937507/binding-class-methods-to-external-functions/15937667#15937667 – Hamzah Feb 20 '22 at 22:31

1 Answers1

1

These functions are part of a subset of special functions in python colloquially called "magic methods" which are denoted by the leading and trailing double underscore. The exact meaning of this differs slightly for different functions but in general the double underscore is meant to distinguish these functions from others with similar names as they have special usage in python. Other examples include __next__(), __dir__, etc. See similar question.

Let cd be an instance of CustomDataset. As somewhat of a peculiarity, the __len__ function is called as len(cd) (or you can do cd.__len__().) The __getitem__ function is generally called as cd[i] where i indexes is passed as the indexing variable to __getitem__, (but likewise you could do cd.__getitem__()).

DerekG
  • 3,555
  • 1
  • 11
  • 21