List index out of range

username: KanZa
cooked: <p>I am using a custom dataset that has the following dimensions (3, 64,64) there are 28 classes. The code works absolutely fine with pre-trained ResNet50 for cifar10 and a smaller dataset with 4 classes with similar dimensions. but for 28 classes after some cycles, it gives the following error  for ‘ct’ in data loader in def <strong>getitem</strong>(self, index):</p>
<pre><code class="lang-auto">import torch 
import torchvision 
from torch.utils.data import Dataset
import torchvision.transforms as T
from torchvision.datasets import CIFAR10, ImageFolder
from config import *

class MyDataset(Dataset):
    def __init__(self, dataset_name, train_flag, transf):
        self.dataset_name = dataset_name
        if self.dataset_name == "cifar10":
            self.cifar10 = CIFAR10('../cifar10', train=train_flag, 
                                    download=True, transform=transf)
        if self.dataset_name == "ct":
            self.ct = ImageFolder(root='/Dataset/radiology_ai/CT/Split-CT-abd/train', transform=transf)
        if self.dataset_name == "satellite":
            self.satellite = ImageFolder(root='/Dataset/Satellite/train', transform=transf)
        
    def __getitem__(self, index):
        if self.dataset_name == "cifar10":
            data, target = self.cifar10[index]
      
        if self.dataset_name == "ct":
            data, target = self.ct[index]
              
        if self.dataset_name == "satellite":

            data, target = self.satellite[index]
            
        return data, target, index

    def __len__(self):
        if self.dataset_name == "cifar10":
            return len(self.cifar10)
        
        if self.dataset_name == "ct":
            return len(self.ct)
        
        if self.dataset_name == "satellite":
            return len(self.ct)

def load_dataset(dataset):
    train_transform = T.Compose([
        T.RandomHorizontalFlip(),
        T.RandomCrop(size=64, padding=4),
        T.ToTensor(),
        T.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) # T.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) # CIFAR-100
    ])

    test_transform = T.Compose([
        T.ToTensor(),
        T.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) # T.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) # CIFAR-100
    ])


    if dataset == 'cifar10': 
        data_train = CIFAR10('../cifar10', train=True, download=True, transform=train_transform)
        data_unlabeled = MyDataset(dataset, True, test_transform)
        data_test  = CIFAR10('../cifar10', train=False, download=True, transform=test_transform)
        NO_CLASSES = 10
        adden = ADDENDUM
        no_train = NUM_TRAIN
    
    if dataset == 'ct': 
        data_train = ImageFolder(root='/Dataset/radiology_ai/CT/Split-CT-abd/train', transform=train_transform)
        data_unlabeled = MyDataset(dataset, True, test_transform)
        data_test  = ImageFolder(root='/Dataset/radiology_ai/CT/Split-CT-abd/val', transform=test_transform)
        NO_CLASSES = 28
        adden = ADDENDUM
        no_train = NUM_TRAIN
    
    if dataset == 'satellite': 
        data_train = ImageFolder(root='/Dataset/Satellite/train', transform=train_transform)
        data_unlabeled = MyDataset(dataset, True, test_transform)
        data_test  = ImageFolder(root='/Dataset/Satellite/test', transform=test_transform)
        NO_CLASSES = 4
        adden = ADDENDUM
        no_train = NUM_TRAIN
    return data_train, data_unlabeled, data_test, adden, NO_CLASSES, no_train
</code></pre>

username: ptrblck
cooked: <p>Check the stacktrace to see which operation fails and then check the length of the <code>list</code> as well as the index. This should give you more information about the out-of-bounds indexing.</p>

username: KanZa
cooked: <p>The code debugger stops here</p>
<pre><code class="lang-auto">if self.dataset_name == "ct":
            data, target = self.ct[index]
            print("Data:", data.size())
            print("Target:", target)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/4/34c55a80ff0020151079fb907d8b71aadc050e53.png" data-download-href="https://discuss.pytorch.org/uploads/default/34c55a80ff0020151079fb907d8b71aadc050e53" title="Screenshot from 2023-01-05 16-57-22"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/34c55a80ff0020151079fb907d8b71aadc050e53_2_543x500.png" alt="Screenshot from 2023-01-05 16-57-22" data-base62-sha1="7wPEHp9h8c214hXBS8gQrqf5WWn" width="543" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/34c55a80ff0020151079fb907d8b71aadc050e53_2_543x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/4/34c55a80ff0020151079fb907d8b71aadc050e53_2_814x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/4/34c55a80ff0020151079fb907d8b71aadc050e53.png 2x" data-dominant-color="362924"><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">Screenshot from 2023-01-05 16-57-22</span><span class="informations">929×854 83.6 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>