Mickey
Mickey

Reputation: 1505

Error reading images with pandas (+pyTorch,scikit)

I'm trying to read images to work with a CNN, but I'm getting a pandas error while trying to load the images. This is some of the code (omitted imports and irrelevant nn class for clarity):

file_name = "annotation.csv"
image_files = pd.read_csv(file_name)

class SimpsonsDataset(Dataset):

    def __init__(self, csv_file, root_dir, transform=None):
        self.image_file = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

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

    def __getitem__(self, idx):
        img_name = os.path.join(self.root_dir, self.image_file.iloc[idx,0][1:])
        image = io.imread(img_name)
        sample = {'image': image}
        if self.transform:
            sample = self.transform(sample)
        return sample

simpsons = SimpsonsDataset(csv_file=image_files,root_dir="folder/")

I use the iloc[idx,0][1:] to format the filepath, and the filepath is joined, with the folders and filenames matching.

However, when I try to run the file, I get the following error:

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/.../image_extractor.py", line 41, in <module>
    simpsons = SimpsonsDataset(csv_file=image_files,root_dir="folder/")

  File "C:/.../image_extractor.py", line 26, in __init__
    self.image_file = pd.read_csv(csv_file)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 655, in parser_f
    return _read(filepath_or_buffer, kwds)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 392, in _read
    filepath_or_buffer, encoding, compression)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\common.py", line 210, in get_filepath_or_buffer
    raise ValueError(msg.format(_type=type(filepath_or_buffer)))

ValueError: Invalid file path or buffer object type: <class 'pandas.core.frame.DataFrame'>

Would love some insights on why this is happening. Thanks!

Upvotes: 0

Views: 171

Answers (1)

Peter Leimbigler
Peter Leimbigler

Reputation: 11105

Your variable image_files is a pandas DataFrame, since it holds the return value of pd.read_csv(), which returns a DataFrame. Try deleting the line

image_files = pd.read_csv(file_name)

and changing the last line to this:

simpsons = SimpsonsDataset(csv_file=file_name, root_dir="folder/")

Upvotes: 1

Related Questions