PyTorch
DataLoader
filename extraction
machine learning
Python programming

How to get the filename of a sample from a DataLoader?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In PyTorch, DataLoader returns batches built from your dataset’s __getitem__ output. If you need each sample’s filename, the dataset must return it explicitly. Many users expect DataLoader to expose source file paths automatically, but it only collates what __getitem__ provides. The clean solution is to include metadata like filename/path in each sample and handle batching with a compatible collate function.

Core Sections

1. Return filename from dataset

python
1from pathlib import Path
2from torch.utils.data import Dataset
3from PIL import Image
4
5class ImageDataset(Dataset):
6    def __init__(self, paths, transform=None):
7        self.paths = [Path(p) for p in paths]
8        self.transform = transform
9
10    def __len__(self):
11        return len(self.paths)
12
13    def __getitem__(self, idx):
14        path = self.paths[idx]
15        img = Image.open(path).convert("RGB")
16        if self.transform:
17            img = self.transform(img)
18        label = 0
19        return img, label, path.name

Now filenames are part of each sample tuple.

2. Iterate DataLoader with filenames

python
1from torch.utils.data import DataLoader
2
3loader = DataLoader(dataset, batch_size=8, shuffle=False)
4
5for images, labels, filenames in loader:
6    print(filenames)

Default collate stacks tensors and keeps string lists for filenames.

3. Return full path if needed

Replace path.name with str(path) when downstream steps need absolute/relative paths.

python
return img, label, str(path)

4. Dictionary-based sample format

For readability in larger projects:

python
1return {
2    "image": img,
3    "label": label,
4    "filename": path.name,
5}

Then batch loop uses dictionary keys.

5. Custom collate edge cases

If sample structure is complex (variable shapes, nested metadata), implement collate_fn to control batch assembly and preserve metadata exactly.

6. Logging and reproducibility

Persist filenames for misclassified samples and evaluation outputs. This enables traceability from model predictions back to source files.

Validation and production readiness

A working snippet is only the first step. To make the solution dependable, validate behavior under representative inputs and operating conditions. Build a small test matrix that includes normal cases, boundary values, and malformed data so failure modes are explicit. If the topic involves time, concurrency, or networking, add at least one test that simulates delayed execution and one test that verifies timeout handling. This catches race conditions and environment-specific bugs that rarely appear in local happy-path runs.

Operational clarity matters as much as correctness. Document assumptions near the implementation: runtime version, required dependencies, expected timezone or locale rules, and platform limitations. Ambiguous assumptions are a major source of production incidents because teammates run the same logic under different defaults. Use structured logs around critical branches and external calls so debugging does not require ad hoc reproduction. Logs should include identifiers and concise context, but avoid sensitive payloads.

For recurring jobs or frequently executed code paths, add observability and guardrails. Define simple success metrics, retry boundaries, and explicit rollback or fallback behavior. Silent retries with no upper limit can hide systemic failures and increase downstream impact. Keep a lightweight pre-deploy checklist in source control so changes remain auditable and repeatable across environments.

text
1release_checklist:
2  - tests cover edge cases and failure paths
3  - runtime and dependency versions documented
4  - logs/metrics confirm expected execution path
5  - retries and timeouts are bounded
6  - rollback or fallback plan is defined

Teams that treat these checks as part of the default implementation workflow usually spend less time on incident triage and more time shipping stable improvements.

Common Pitfalls

  • Expecting DataLoader to expose filenames automatically.
  • Returning non-collatable metadata structures without custom collate_fn.
  • Dropping filenames during transform wrappers or dataset adapters.
  • Using shuffled loaders when deterministic filename order is required.
  • Storing only basename when duplicate names exist across directories.

Summary

To get filenames from a DataLoader, include them in dataset __getitem__ output. DataLoader will batch whatever your dataset returns, including strings and dictionaries. For advanced metadata needs, define a custom collate function. This pattern keeps training and evaluation pipelines traceable and debuggable.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.