Error when iterating over train_loader

Naina_K: <p>I’m training my ResNet 50 with the following code:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/5/65e6984f3d176fed75a6581640ea44b7132a13d2.png" data-download-href="https://discuss.pytorch.org/uploads/default/65e6984f3d176fed75a6581640ea44b7132a13d2" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/5/65e6984f3d176fed75a6581640ea44b7132a13d2.png" alt="image" data-base62-sha1="exsgRk7EuAUcArJMok1h2FFkKKC" width="690" height="488" data-dominant-color="EBEEEF"><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">718×508 39.8 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><br>
However, I’m running into an IndexError when I try to iterate over train_loader</p>
<p>Because my image and labels data were separate, I created an array with the image and labels (not sure if this is where the error lies):</p>
<pre><code class="lang-auto">train_data = []
for i in range(len(train_files)):
   train_data.append([train_files[i], train_y[i]])

train_loader = DataLoader(train_data, batch_size = batch_size, 
                         sampler = train_sampler, num_workers = num_workers)
</code></pre>
<p>Any suggestions on how to deal with this error?</p>

ptrblck: <p>Could you post the entire error message as I’m currently unsure how your <code>DataLoader</code> would work.<br>
It seems you might be passing a list of lists containing file paths, which the <code>DataLoader</code> should not be able to handle. If <code>train_files</code> contains paths to the actual data, you would need to load and process each sample in a <code>Dataset</code> first before passing it to the <code>DataLoader</code>.</p>

Naina_K: <p>train_files does contains paths to the actual data. When you say load and process each sample, what do you exactly mean - would I need to pass each sample into say an open_img function which loads each individual image?</p>
<p>full error message:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/a/3a61afe6b699f6178ea90f10393ce971d4938d1d.png" data-download-href="https://discuss.pytorch.org/uploads/default/3a61afe6b699f6178ea90f10393ce971d4938d1d" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/a/3a61afe6b699f6178ea90f10393ce971d4938d1d.png" alt="image" data-base62-sha1="8ksZbSSZYJQUhQVPW7vtD3igNk1" width="690" height="436" data-dominant-color="F5D7D6"><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">801×507 24.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>

ptrblck: <p>Yes, you could use e.g. <a href="https://pytorch.org/vision/main/generated/torchvision.datasets.ImageFolder.html#torchvision.datasets.ImageFolder"><code>ImageFolder</code></a> if the data structure meets the requirements or implement a custom <code>Dataset</code> as described <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">here</a>.</p>

Naina_K: <p>I created my custom Dataset using the <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="noopener nofollow ugc">tutorial</a> mentioned above and my train_loader</p>
<pre><code class="lang-auto">transformed_dataset = BreastCancerImages(csv_file='/kaggle/input/rsna-breast-cancer-detection/train.csv',
                                           root_dir='/kaggle/input/rsna-mammography-images-as-pngs/images_as_pngs/train_images_processed',
                                           transform= train_transform)

</code></pre>
<pre><code class="lang-auto">train_loader = DataLoader(transformed_dataset, batch_size=batch_size
                          ,num_workers=num_workers, shuffle = True)
</code></pre>
<p>But when I go to train my model:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/9/591ef094e0d9277113ec627b1230804eb305e888.png" data-download-href="https://discuss.pytorch.org/uploads/default/591ef094e0d9277113ec627b1230804eb305e888" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/9/591ef094e0d9277113ec627b1230804eb305e888.png" alt="image" data-base62-sha1="cIoMxWOhPHMbVe3Ghm7n46x7KKk" width="690" height="450" data-dominant-color="EBEEEF"><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">728×475 38.5 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><br>
it gives me the following error: <strong>pic should be PIL Image or ndarray. Got &lt;class ‘dict’&gt;</strong></p>
<p>How should I adjust my code to fix this error?</p>

ptrblck: <p>I don’t know how you’ve defined your custom <code>Dataset</code> but based on the error message a transformation fails as you are passing a <code>dict</code> to it instead of a <code>PIL.Image</code> or numpy array as seen here:</p>
<pre><code class="lang-python">transform = transforms.ToTensor()

# fails
transform(dict())
# TypeError: pic should be PIL Image or ndarray. Got &lt;class 'dict'&gt;

# works
x = np.random.randint(0, 256, (224, 224, 3)).astype(np.uint8)
out = transform(x)
</code></pre>

Naina_K: <p>This is my custom dataset, so would it still work if I converted it to a Tensor object?</p>
<p>class BreastCancerImages(Dataset):</p>
<pre><code>def __init__(self, csv_file, root_dir, transform=None):
    """
    Args:
        csv_file (string): Path to the csv file with annotations.
        root_dir (string): Directory with all the images.
        transform (callable, optional): Optional transform to be applied
            on a sample.
    """
    self.d_train = pd.read_csv(csv_file)
    self.root_dir = root_dir
    self.transform = transform

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

def __getitem__(self, idx):
    if torch.is_tensor(idx):
        idx = idx.tolist()

    img_name = list(glob(os.path.join(imageFilesDir, "**", "*.png")))[idx]
    image = Image.open(img_name)
    labels = self.d_train.iloc[idx, 6]
    labels = np.array([labels])
    labels = torch.tensor(labels.astype('float'))

    return image, labels
</code></pre>

ptrblck: <p>I don’t know where the <code>dict</code> is created and why the error is raised.<br>
However, you should transform the <code>PIL.Image</code> to a tensor before returning it in the <code>__getitem__</code> method.</p>