Once again "TypeError: batch must contain tensors, numbers, dicts or lists; found object"

Hi everyone!
I’ve got an error “TypeError: batch must contain tensors, numbers, dicts or lists; found object” and I don’t know what’s wrong.
Checked another similar topics, but still confused.

#My Dataset class:

class GridDataset(Dataset):
“”“Grids dataset.”""

def __init__(self, csv_file, csv_file_labels, transform=None):
        csv_file (string): Path to the .csv file which contains pd.DataFrames in 1D.
        csv_file_labels (string): Path to the .csv file which contains labels of grids.
        transform (callable, optional): Optional transform to be applied on a sample.
    self.data_set = pd.read_csv(csv_file)
    self.label = pd.read_csv(csv_file_labels)
    self.transform = transform

def __len__(self):
    return len(self.data_set.columns) #returns the size of the dataset (nomber of columnss)

def __getitem__(self, idx):
    grid = self.data_set.iloc[:, idx].values
    grid = grid.reshape(1, -1)
    grid_name = self.data_set.columns[idx]
    label = self.label.iloc[:, idx].values
    if self.transform:
        grid = self.transform(grid)
    return grid, grid_name, label

#Transform classes:

import pandas as pd
import numpy as np

class RescaleTo3D(object):
“”"Rescale the grid to a given size.

    i_max - dimension in X
    j_max - dimension in Y
    k_max - dimension in Z

    ThreeDim - 3d np.array  

def __init__(self, i_max, j_max, k_max):
    self.i_max = i_max
    self.j_max = j_max
    self.k_max = k_max
def __call__(self, sample):

    Three_Dim = np.array(np.zeros((self.i_max, self.j_max, self.k_max)))        
    i = 0
    j = 0
    k = 0
    z = 0
    while k <= self.k_max-1:
        while j <= self.j_max-1:
            while i <= self.i_max-1:
                Three_Dim[i, j ,k] = sample[:,z]
                i += 1
                z += 1
            i = 0
            j += 1
        j = 0
        k += 1

    return (Three_Dim)     

class ToTensor(object):
“”“Convert ndarrays in sample to Tensors.”""

def __call__(self, sample):
    return (torch.from_numpy(sample))

#After that I try this. And it works fine:

scale = RescaleTo3D(24, 18, 15)
totensor = ToTensor()
composed = transforms.Compose([scale, totensor])

for i, tsfrm in enumerate([composed]):
grid3D = tsfrm(grid)

print (grid3D) #ok
print(grid_name) #ok
print(grid3D.shape) #ok

#In this code I have problems:

path = “…/DATA/_PERMX.csv” #path to dataset
path_labels = “…/DATA/_PERMX_labels.csv” #path to dataset labels

transformed_dataset = GridDataset(path,
transform=transforms.Compose([RescaleTo3D(24, 18, 15),

dataloader = DataLoader(dataset=transformed_dataset, batch_size=2, shuffle=True)

for batch_idx, (grid3D, grid_name, label) in enumerate(dataloader):
print(batch_idx) #no luck

Please help!
Thanks a lot in advance!


[TypeError: batch must contain tensors, numbers, dicts or lists; found object]

I met exactly same error and I found that the type of data I create in init is object. I specified it to float and solved my problem.

1 Like