Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.5k stars 1.01k forks source link

How to get image file names from DataLoader #7850

Closed karllandheer closed 3 days ago

karllandheer commented 2 weeks ago

Hello, I would like to get the image filenames from the DataLoader. What's the easiest way to do this? Is it safe to assume the order of the images is the same as input when accessing the data by iterating through the data loader object as long as shuffle = False like this. I.e., will all_labels and local_train_images match in ordering?

train_ds = ImageDataset(image_files=local_train_images, labels=train_labels, transform=train_transforms) 
train_loader = DataLoader(train_ds, batch_size=8, shuffle=False, num_workers=2, pin_memory=pin_memory) 

all_labels = []
for batch_data in train_loader:
            step += 1
            inputs, labels = batch_data[0].to(device), batch_data[1].to(device)
            all_labels.append(labels)
KumoLiu commented 3 days ago

Hi @karllandheer, you can get the file name by using inputs.meta["filename_or_obj"]. The info should be saved in the meta information.

Hope it helps, move to discussion for now. Feel free to create another one if you have more issues. Thanks.