Attribute Error on adding new attributes to a custom dataset

I am attempting to add two new attributes to my custom dataset that can give me the shape of input data and a class map of unique classes. I am getting a AttributeError , seemingly because the new attributes are not part of the base class. Is there any way I can achieve this ?

def __getattr__(self, attribute_name):
    if attribute_name in Dataset.functions:
        function = functools.partial(Dataset.functions[attribute_name], self)
        return function
    else:
        raise AttributeError <== ERROR THROWN

My custom dataset class :
from torch.utils.data import Dataset

class CustomDataset(Dataset):
def init(self, data_path, img_dir, labels_csv, transform=None):
self.datapath = data_path
self.img_dir = img_dir
self.labels_csv = labels_csv
self.transform = None
self.labels =
self.labels_df, self.labels_map = load_labels(
os.path.join(data_path, labels_csv)
)
self.filelist = os.listdir(os.path.join(data_path, img_dir))

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

def __getitem__(self, idx):
    item = self.labels_df["Packet_Number"][idx]
    subs = "PT" + str(item) + "_"
    filename = "".join(filter(lambda x: subs in x, self.filelist))
    image = get_features(os.path.join(self.datapath, self.img_dir, filename))
    class_idx = self.labels_map[self.labels_df["Classification"][idx]
    self.image_shape = image.shape
    if self.transform:
        return self.transform(image), class_idx
    else:
        return torch.from_numpy(image), torch.tensor([class_idx])

methods to get two new attributes

def get_classes(self):
    return self.labels_map

def get_shape(self):
    return self.image_shape

Is there any other way by which new attributes and methods can be added to the base Datasets Class ?

Thank you in advance.

Your code works fine for me:

class CustomDataset(Dataset):
    def __init__(self):
        self.data = torch.randn(10, 3, 24, 24)
        self.labels_map = {
            "a": 1,
            "b": 2,
        }
        self.image_shape = self.data.shape
        
    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, idx):
        return self.data[idx]

    def get_classes(self):
        return self.labels_map
    
    def get_shape(self):
        return self.image_shape
    
dataset = CustomDataset()
print(len(dataset))
# 10
print(dataset.get_classes())
# {'a': 1, 'b': 2}
print(dataset.get_shape())
# torch.Size([10, 3, 24, 24])
1 Like

Thank you so very much, it was my mistake. I was trying to use image.shape after instantiating the Dataset object, but it was getting assigned in getitem().