Dataloader __getitem__ idx control when using csv file

I am trying to load an image file using the following form of the csv file.
This is a simplified representation of my huge csv file in the same format.

enter image description here

I actually understand the basic method of sequentially loading images using rows of csv files by code “iloc[idx]”.

However, like the attached image, it is difficult to sequentially load images containing as many images as “number of images” in the terminal folder written in the “file location.”

According to the attached image, the idx of getitem_ points to 16 images in the “./data/asd” folder sequentially, and then to the “./data/vbn” folder so that the next 16 images can be imported sequentially.

It’s a code that I’ve worked on so far, so I’d appreciate any advice you can give me here.

class CustomDataset(torch.utils.data.Dataset):

def __init__(self, root, csv_file):
    self.root = root
    self.df = pd.read_csv(csv_file)

def __len__(self):
    #The sum of "Number of Images" values will be the total length of the image dataset.
    return len(self.df.iloc[1:,1].sum())

def __getitem__(self, idx): 
    #This is where I have trouble controlling idx.
    img_name = os.path.join(self.root, self.df.iloc[idx, 2] + '.dcm')
    image = io.imread(img_name)
    image = torch.tensor(image)

    return image