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

username: Heinlein
cooked: <p>TypeError: module() takes at most 2 arguments (3 given) when inheriting torch.utils.data.dataset</p>
<p>Help! I have just installed pytorch1.1 and tested my codes.<br>
When I define a class inheriting torch.utils.data.dataset, something goes wrong.</p>
<p>My codes are here:</p>
<pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto">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 "&lt;ipython-input-2-2e9363f1d3d6&gt;", line 1, in &lt;module&gt;
    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 &lt;module&gt;
    class MixDataset(data.dataset):
TypeError: module() takes at most 2 arguments (3 given)
</code></pre>
<p>I have reinstalled anaconda and run the command “conda install pytorch-cpu torchvision-cpu -c pytorch” . I can print “print(torch.<strong>version</strong>)” 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?</p>
<p>Thank you for your reading. I would be happy if you could give me some comments.</p>

username: noornk
cooked: <p>You’ve got a typo! It’s :<br>
<code>def __init__</code> ,<br>
not<br>
<code>def init</code> .</p>

username: Heinlein
cooked: <p>class MixDataset(data.dataset):<br>
def <strong>init</strong>(self,image_root,image_attribute,TrainOrValid=‘Train’):</p>
<p><strong>O(#￣▽￣)</strong><br>
It’s “def <strong>init</strong>”. Is there a typo?</p>

username: noornk
cooked: <p>Yeah, as long as it’s not a <code>__init__</code> it won’t be called in the moment of the object initiation, and the object will not be recognized as an object.</p>

username: Heinlein
cooked: <p>Oh, I get it.<br>
It’s of course def __ init __, while markdown hides the double underline. So I think maybe there is no typo.<br>
There is my code picture.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/71f311845936cd8dde8a4ac7cd5b0951a0640fb9.png" data-download-href="https://discuss.pytorch.org/uploads/default/71f311845936cd8dde8a4ac7cd5b0951a0640fb9" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/7/71f311845936cd8dde8a4ac7cd5b0951a0640fb9.png" alt="image" data-base62-sha1="gg2J0d1neCwLdPk3UtEwlpn4357" width="690" height="184" data-dominant-color="EBE9E9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">914×245 30.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: NguyenVanThanhHust
cooked: <p>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.</p>
<p>This following code should work:</p>
<pre><code class="lang-auto">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)

</code></pre>
<p>you can consult <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="nofollow noopener">Custom DataLoader</a></p>

username: Bolloo
cooked: <p>I had the same issue, and your solution worked. Thanks! <img src="https://discuss.pytorch.org/images/emoji/apple/laughing.png?v=12" title=":laughing:" class="emoji" alt=":laughing:" loading="lazy" width="20" height="20"><br>
I made the same mistake.<br>
Just change<br>
“import torch.utils.data as data”<br>
to<br>
“from torch.utils.data import Dataset”</p>