Lin
Lin

Reputation: 47

Why is my custom dataset giving attribute error?

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.

Upvotes: 0

Views: 913

Answers (1)

DerekG
DerekG

Reputation: 3958

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__()).

Upvotes: 1

Related Questions