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

username: Gleb_Shishaev
cooked: <p>Hi everyone!<br>
I’ve got an error “TypeError: batch must contain tensors, numbers, dicts or lists; found object” and I don’t know what’s wrong.<br>
Checked another similar topics, but still confused.</p>
<hr>
<p><span class="hashtag">#My</span> Dataset class:</p>
<p>class GridDataset(Dataset):<br>
“”“Grids dataset.”""</p>
<pre><code>def __init__(self, csv_file, csv_file_labels, transform=None):
    '''
    Args:
        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
</code></pre>
<hr>
<p><span class="hashtag">#Transform</span> classes:</p>
<p>#<a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/data_loading_tutorial.html</a><br>
import pandas as pd<br>
import numpy as np</p>
<p>class RescaleTo3D(object):<br>
“”"Rescale the grid to a given size.</p>
<pre><code>Args:
    i_max - dimension in X
    j_max - dimension in Y
    k_max - dimension in Z

Return:
    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 &lt;= self.k_max-1:
        while j &lt;= self.j_max-1:
            while i &lt;= 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)     
</code></pre>
<p>class ToTensor(object):<br>
“”“Convert ndarrays in sample to Tensors.”""</p>
<pre><code>def __call__(self, sample):
    return (torch.from_numpy(sample))
</code></pre>
<hr>
<p><span class="hashtag">#After</span> that I try this. And it works fine:</p>
<p>scale = RescaleTo3D(24, 18, 15)<br>
totensor = ToTensor()<br>
composed = transforms.Compose([scale, totensor])</p>
<p>for i, tsfrm in enumerate([composed]):<br>
grid3D = tsfrm(grid)</p>
<p>print (grid3D) <span class="hashtag">#ok</span><br>
print(grid_name) <span class="hashtag">#ok</span><br>
print(grid3D.shape)  <span class="hashtag">#ok</span></p>
<hr>
<p><span class="hashtag">#In</span> this code I have problems:</p>
<p>path  = “…/DATA/_PERMX.csv” <span class="hashtag">#path</span> to dataset<br>
path_labels  = “…/DATA/_PERMX_labels.csv” <span class="hashtag">#path</span> to dataset labels</p>
<p>transformed_dataset = GridDataset(path,<br>
path_labels,<br>
transform=transforms.Compose([RescaleTo3D(24, 18, 15),<br>
ToTensor()<br>
])<br>
)</p>
<p>dataloader = DataLoader(dataset=transformed_dataset, batch_size=2, shuffle=True)</p>
<p>for batch_idx, (grid3D, grid_name, label) in enumerate(dataloader):<br>
print(batch_idx) <span class="hashtag">#no</span> luck</p>
<p>Please help!<br>
Thanks a lot in advance!</p>
<p>Gleb</p>

username: raymond715
cooked: <h1>[TypeError: batch must contain tensors, numbers, dicts or lists; found object]</h1>
<p>I met exactly same error and I found that the type of data I create in  <strong>init</strong>  is object. I specified it to float and solved my problem.</p>