Does a custom dataset need to always inherit the Dataset class?

It may be a trivial question but while reading this tutorial: TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 1.7.1 documentation

I found this custom Dataset

class PennFudanDataset(object):
    def __init__(self, root, transforms):
        #random code

Which does not inherit torch.utils.data.Dataset (even though pytorch itself recommends us to "inherit Dataset ")

My question is simple, can I just create a custom dataset with just ‘object’ and not ‘Dataset’? if so is there a drawback for using ‘object’ instead of ‘Dataset’?

And since we are already here, why do we need to inherit ‘torch.utils.data.Dataset’ to create a custom dataset?
I’ve checked the source code and it only contains getitem and add, do we do it only because of “good practices” or is there something about this class I’m missing here??

Thanks for any help!!

It’s not strictly necessary, as the base Dataset implements the __add__ method for you and defines the __getitem__, but for the sake of code clarity I would still recommend to derive your custom class from Dataset.

1 Like