TypeError: module() takes at most 2 arguments (3 given) when inheriting torch.utils.data.dataset

TypeError: module() takes at most 2 arguments (3 given) when inheriting torch.utils.data.dataset

Help! I have just installed pytorch1.1 and tested my codes.
When I define a class inheriting torch.utils.data.dataset, something goes wrong.

My codes are here:

import os
from PIL import Image
import torch.utils.data as data
from torchvision import transforms, utils
import pandas as pd

transformer = transforms.Compose([
    transforms.CenterCrop((590,730)),
    transforms.Resize(448),
    transforms.Grayscale(3),
    transforms.ToTensor(), #Convert a PIL image or numpy.array to tensor and normalize in range[0.0, 1.0]
    transforms.Normalize(mean = (0.5, 0.5, 0.5), std = (0.5, 0.5, 0.5))

class MixDataset(data.dataset):
    def __init__(self,image_root,image_attribute,TrainOrValid='Train'):
        # imgs = os.listdir(root)
        # imgs_id = [os.path.splitext(i)[0] for i in imgs]
        imgs = [i+'.jpg' for i in image_attribute['UltrasoundImageID']]
        self.labels = [i for i in image_attribute[['TypeOfPlaque_1', 'TypeOfPlaque_2', 'TypeOfPlaque_3', 'TypeOfPlaque_4']]]
        self.imgs_fullpath = [os.path.join(image_root,i) for i in imgs]
        self.transforms = transformer
        self.image_attributes = image_attribute

    def __getitem__(self, index):
        img_fullpath = self.imgs_fullpath[index]
        pil_img = Image.open(img_fullpath)
        img = self.transforms(pil_img)
        label = self.labels[index]
        return img, label

    def __len__(self):
        return len(self.imgs_fullpath)
Traceback (most recent call last):
  File "D:\Coding\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-2e9363f1d3d6>", line 1, in <module>
    runfile('D:/Workspace/TNInet/Data/DataLoader.py', wdir='D:/Workspace/TNInet/Data')
  File "D:\Coding\Jet Pycharm\PyCharm 2019.1.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\Coding\Jet Pycharm\PyCharm 2019.1.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/Workspace/TNInet/Data/DataLoader.py", line 66, in <module>
    class MixDataset(data.dataset):
TypeError: module() takes at most 2 arguments (3 given)

I have reinstalled anaconda and run the command “conda install pytorch-cpu torchvision-cpu -c pytorch” . I can print “print(torch.version)” and get “1.1.0”. So I think my environment looks well. Howerver, nothing changed. Maybe I need to reinstall again to fix it? Or what should I do?

Thank you for your reading. I would be happy if you could give me some comments.

You’ve got a typo! It’s :
def __init__ ,
not
def init .

class MixDataset(data.dataset):
def init(self,image_root,image_attribute,TrainOrValid=‘Train’):

O(# ̄▽ ̄)
It’s “def init”. Is there a typo?

Yeah, as long as it’s not a __init__ it won’t be called in the moment of the object initiation, and the object will not be recognized as an object.

Oh, I get it.
It’s of course def __ init __, while markdown hides the double underline. So I think maybe there is no typo.
There is my code picture.

1 Like

Maybe you inherit wrong things. You’re inheriting torch.utils.data.dataset which is a file ( dataset.py in folder data). You should inherit from a class, in this case is class Dataset.

This following code should work:

import os
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import pandas as pd

transformer = transforms.Compose([
    transforms.CenterCrop((590,730)),
    transforms.Resize(448),
    transforms.Grayscale(3),
    transforms.ToTensor(), #Convert a PIL image or numpy.array to tensor and normalize in range[0.0, 1.0]
    transforms.Normalize(mean = (0.5, 0.5, 0.5), std = (0.5, 0.5, 0.5))

class MixDataset(Dataset):
    def __init__(self,image_root,image_attribute,TrainOrValid='Train'):
        # imgs = os.listdir(root)
        # imgs_id = [os.path.splitext(i)[0] for i in imgs]
        imgs = [i+'.jpg' for i in image_attribute['UltrasoundImageID']]
        self.labels = [i for i in image_attribute[['TypeOfPlaque_1', 'TypeOfPlaque_2', 'TypeOfPlaque_3', 'TypeOfPlaque_4']]]
        self.imgs_fullpath = [os.path.join(image_root,i) for i in imgs]
        self.transforms = transformer
        self.image_attributes = image_attribute

    def __getitem__(self, index):
        img_fullpath = self.imgs_fullpath[index]
        pil_img = Image.open(img_fullpath)
        img = self.transforms(pil_img)
        label = self.labels[index]
        return img, label

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

you can consult Custom DataLoader

1 Like

I had the same issue, and your solution worked. Thanks! :laughing:
I made the same mistake.
Just change
“import torch.utils.data as data”
to
“from torch.utils.data import Dataset”