I have multiple classes (for different datasets) that inherit from pytorch's Dataset class. They have a general structure, like so:
from torch.utils.data import Dataset
class SomeDataset(Dataset):
def __init__(self, data, labels):
super(SomeDataset, self).__init__()
self.data = data
self.labels = labels
self.__name__ = 'SomeDataset'
def __getitem__(self, index):
return {'data': self.data[index], 'label': self.labels[index]}
def __len__(self):
return len(data)
Recently I have realised that it would be beneficial to keep track of the labels passed into the Dataloader when batching, so upon googling how to do this I came across this thread, which is where I have adapted the code to write this function:
def return_indices(dataset_class):
def __getitem__(self, index):
return {'index':1, **dataset_class.__getitem__(self, index)}
return type(dataset_class.__name__, (dataset_class, ), {'__getitem__': __getitem__})
I had never seen type used like this before, but after some googling, it made some sense, so I tried it out. Unfortunately this led to this error:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
which led to a whole lot more googling, and even though I'm beginning to grasp what a metaclass is and how they're used I still can't figure out what is wrong with this approach or how to solve it - and I'm starting to think that maybe it would be easier to rewrite this functionality into my dataset classes instead of having some neat wrapper that does it for me. Can anyone weigh in with whatever it is I'm missing?