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.