TypeError: object() takes no parameters

Hi all,

I have two folders, the first folder contain Original Images and the second folder contain the same Images with noise and I have no label. When I added path and loading images from paths in the code I get this error:

1 train_dataset = Dataset(path_input_1 = path_input_1
----> 2 ,path_input_2 = path_input_2)

TypeError: object() takes no parameters

Can someone please tell me where the errors are and how to fix it. Here is the code:

class Dataset(data.Dataset):
	def __int__(self, path_input_1, path_input_2):
		"""
		Initialize data set as a list of paths corresponding to each item of data set

        Args:
		    path_inputs: a list of paths for each data point in data set
		"""

        self.filenames_OI = []
		self.filenames_NI = []

	# list of paths to original images and noise images
		self.path_input_1 = path_input_1     # list of paths to original images        
		self.path_input_2 = path_input_2     # list of paths to images with noise

		filenames_OI = glob.glob(osp.join(path_input_1, '*.jpg'))
		filenames_NI = glob.glob(osp.join(path_input_2, '*.jpg'))

		for fn_OI, fn_NI in filenames_OI, filenames_NI:
    			self.filenames_OI.append(fn_OI)
    			self.filenames_NI.append(fn_NI)

	def __len__(self):
		"""
		Return the length of data set using list of paths
    	:return: number of samples in data set
		"""
		return len(self.path_input_1) 

	def __getitem__(self, index):

    	# Code to load data using Image.open
		x = Image.open(self.filenames_OI[index])
		y = Image.open(self.filenames_NI[index])

    	return X, y


train_dataset = Dataset(path_input_1 = path_input_1
                       ,path_input_2 = path_input_2)

Hi,

I ran your code and it works fine.
Although there was a lot of problem with indentation. You may wannt to reformat your code using a framework or use a proper IDE/editor.

class Dataset(data.Dataset):
    def __init__(self, path_input_1, path_input_2):
        self.filenames_OI = []
        self.filenames_NI = []
        self.path_input_1 = path_input_1
        self.path_input_2 = path_input_2

        filenames_OI = glob.glob(osp.join(path_input_1, '*.jpg'))
        filenames_NI = glob.glob(osp.join(path_input_2, '*.jpg'))

        for fn_OI, fn_NI in filenames_OI, filenames_NI:
            self.filenames_OI.append(fn_OI)
            self.filenames_NI.append(fn_NI)

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

    def __getitem__(self, index):
        x = Image.open(self.filenames_OI[index])
        y = Image.open(self.filenames_NI[index])
        return X, y


train_dataset = Dataset(path_input_1 = 'path' ,path_input_2 = 'another/path')

Bests

Hi @Nikronic,

First, thanks a lot for your precious help.

You said I may want to reformat my code using a framework or use a proper IDE/editor. I am using Jupyter notebook (anaconda software) and the Preview (Nightly) pytorch version.

You are welcome,
Ok, nightly builds may have some bugs or differences which I am not aware of. I ran your code and there was no problem using the stable build. Can you try to run your code using exact same pytorch version on google colab and share it?
Can you also print the full stack trace?

Hi @Nikronic

First thanks for your precious help.

Here are all the steps but I get another error. Do you know why?

I am using the Preview (Nightly) pytorch version because I am interesting by using
from torch.cuda import amp which exist only on the Nightly version.

my following folders are:

C:/DataSets/SimplifyData/imagesForTrain/OriginalImages/
and
C:/DataSets/SimplifyData/imagesForTrain/imagesForTrain/NoiseImages/

I have put in each folder (OriginalImages and NoiseImages) three .jpg images only for test:

Here is the complete code:

import os
import glob

import torch
import torch.cuda as cuda
import torch.nn as nn
from torch.nn import functional as F
from torch.utils import data

import matplotlib.pyplot as plt
import matplotlib.image as matpltimg

import torchvision
from torchvision import datasets
from torchvision import transforms

import numpy as np

from torch.cuda import amp

datadir  = '/DataSets/SimplifyData'

traindir_OI = datadir + '/imagesForTrain/OriginalImages/'
traindir_NI = datadir + '/imagesForTrain/NoiseImages/'

class Dataset(data.Dataset):
    def __init__(self, path_input_1, path_input_2):
    	self.filenames_OI = []
    	self.filenames_NI = []
    	self.path_input_1 = path_input_1
    	self.path_input_2 = path_input_2

        filenames_OI = glob.glob(osp.join(path_input_1, '*.jpg'))
    	filenames_NI = glob.glob(osp.join(path_input_2, '*.jpg'))

        for fn_OI, fn_NI in filenames_OI, filenames_NI:
    	        self.filenames_OI.append(fn_OI)
        		self.filenames_NI.append(fn_NI)

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

    def __getitem__(self, index):
    	x = Image.open(self.filenames_OI[index])
    	y = Image.open(self.filenames_NI[index])
    	return X, y

import os
import glob
import os.path as osp
from PIL import Image

path_labels = traindir_OI
path_inputs = traindir_NI

train_dataset = Dataset(path_input_1 = path_labels
    	               ,path_input_2 = path_inputs)

Now I get this error:


ValueError Traceback (most recent call last)
in
1 train_dataset = Dataset(path_input_1 = path_labels
----> 2 ,path_input_2 = path_inputs)

in init(self, path_input_1, path_input_2)
9 filenames_NI = glob.glob(osp.join(path_input_2, ‘*.jpg’))
10
—> 11 for fn_OI, fn_NI in filenames_OI, filenames_NI:
12 self.filenames_OI.append(fn_OI)
13 self.filenames_NI.append(fn_NI)

ValueError: not enough values to unpack (expected 2, got 0)

This means your path is wrong or there is no image in the used path. Literally, filenames_NI and filenames_OI are empty. Make sure you are using correct path, for instance, put a print(len(filenames_NI)) before the error loop.

@Nikronic thanks I will try

Hi @Nikronic

I have checked if my folder are empty as follow;

import os
import glob

import torch
import torch.cuda as cuda
import torch.nn as nn
from torch.nn import functional as F
from torch.utils import data

import matplotlib.pyplot as plt
import matplotlib.image as matpltimg

import torchvision
from torchvision import datasets
from torchvision import transforms

import numpy as np

from torch.cuda import amp

datadir  = '/DataSets/SimplifyData'

traindir_OI = datadir + '/imagesForTrain/OriginalImages/'
traindir_NI = datadir + '/imagesForTrain/NoiseImages/'

list1 = os.listdir(traindir_OI) 
number_files1 = len(list1)
print(number_files1)

list2 = os.listdir(traindir_NI) 
number_files2 = len(list2)
print(number_files2)

I get as result :
6
6

which is correct because in each folder I have put now 6 images.

I have also tested the folders as follow:

class Dataset(data.Dataset):
    def __init__(self, path_input_1, path_input_2):
        self.filenames_OI = []
        self.filenames_NI = []
        self.path_input_1 = path_input_1
        self.path_input_2 = path_input_2

        filenames_OI = glob.glob(osp.join(path_input_1, '*.jpg'))
        filenames_NI = glob.glob(osp.join(path_input_2, '*.jpg'))
        print(len(filenames_OI)) 
        print(len(filenames_NI))
        
        for fn_OI, fn_NI in filenames_OI, filenames_NI:
            self.filenames_OI.append(fn_OI)
            self.filenames_NI.append(fn_NI)

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

    def __getitem__(self, index):
        x = Image.open(self.filenames_OI[index])
        y = Image.open(self.filenames_NI[index])
        return X, y

I get also:
6
6

So I don’t known why I have the error. ValueError: not enough values to unpack (expected 2, got 0)

Perhaps I have done something wrong in the code, but I don’t known what.

Ow I am really sorry, I forgot many things. In python when you want to loop over two things, you need to use zip like this:

for fn_OI, fn_NI in zip(filenames_OI, filenames_NI):

Let me know if it works.

Hi @Nikronic

Thank you very much, everything works very well.

1 Like