Search code examples
pythondatasetconv-neural-network

Getting TypeError: LesionDataset() takes no arguments error


Keep getting the no arguments error and I'm not sure what I've got wrong.

I have search for it but could only find answer where there was a spelling mistake or the "init" function only had one underscore.

This is the current code i have (i took out the imports to fit character limit).:

    ds = datasets.LesionDataset('/content/data/img',
                            '/content/data/img/train.csv')
    input, label = ds[0]
    print(input)
    print(label)

    class LesionDataset(dataset):
        def __init__(self, img_dir, labels_fname):
        self.img_dir = img_dir
        self.labels_fname = labels_fname

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

        def __getiitem__(self, idx):
            image = self.img_dir[idx]
            label = self.labels_fname[idx]
            return image, label

Full error message: 
TypeError                                 Traceback (most recent call last)
<ipython-input-15-a9cb5c2b8a95> in <cell line: 58>()
     56 import datasets
     57 
---> 58 ds = datasets.LesionDataset('/content/data/img',
     59                             '/content/data/img/train.csv')
     60 input, label = ds[0]

TypeError: LesionDataset() takes no arguments

Solution

  • In your code, there is a typo in the constructor method name, and also the indentation seems to be off. You wrote getiitem instead of getitem.

    Here's a corrected version of your code:

    class LesionDataset(dataset):
        def __init__(self, img_dir, labels_fname):
            self.img_dir = img_dir
            self.labels_fname = labels_fname
    
        def __len__(self):
            return len(self.img_dir)
    
        def __getitem__(self, idx):
            image = self.img_dir[idx]
            label = self.labels_fname[idx]
            return image, label
    

    Make sure to use getitem instead of getiitem. This should resolve the "TypeError" issue you were facing.