liveBook · Manning

Chapter 12: Combining data sources into a unified dataset

What you will learn in chapter 12 of Deep Learning with PyTorch, Second Edition

This introduction frames data loading as the work of turning raw CT scans and their annotations into training samples.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction frames data loading as the work of turning raw CT scans and their annotations into training samples.

Key terms: raw CT scan data, annotations, training sample, data loading

12.1 Raw CT data files

This section identifies the raw LUNA CT files and sketches how scan data and annotations become cropped PyTorch Dataset samples, while noting key preprocessing trade-offs.

Key terms: subset directories, .mhd file, .raw file, series UID, annotation data, patient coordinate system, (I,R,C), Dataset subclass, sample tuple, cropping

12.2 Parsing LUNA’s annotation data

The section starts loading LUNA data by examining its CSV files, explaining what candidate labels and nodule sizes mean and why representative data matters.

Key terms: candidates.csv, seriesuid, series_uid, (X,Y,Z) coordinates, class, annotations.csv, diameter_mm, representative spread, training and validation data, CSV files

12.2.1 Training and validation sets

The section explains how to make training and validation sets representative of expected CT data and illustrates a coordinate mismatch between two nodule data sources.

Key terms: training and validation sets, representative spread, annotations.csv, candidates.csv, center, data mismatch

12.2.2 Unifying our annotation and candidate data

The section builds a cleaned list of CT nodule candidates by combining candidate and annotation data, then caches and sorts the result for later use.

Key terms: NoduleInfoTuple, nodule status, diameter, series UID, candidate center, annotations.csv, candidates.csv, series_uid, placeholder 0.0 diameter, noduleInfo_list

12.3 Loading individual CT scans

This section explains how LUNA CT scan files become addressable 3D arrays and how individual scans are identified.

Key terms: MetaIO, SimpleITK, NumPy arrays, .mhd, .raw, ct_a, three-dimensional array, intensity channel, series instance UID, series_uid

12.3.1 Hounsfield Units

Explains what CT voxel values mean in Hounsfield units and why their range is clipped and considered when adding other data channels.

Key terms: Hounsfield units, HU, voxels, density, field of view, outlier values, batch normalization, –1,000 HU, 1,000 HU, raw HU values

12.4 Locating a nodule using the patient coordinate system

Explains why the classifier should receive a fixed-size CT crop with the candidate centered.

Key terms: fixed-size inputs, candidate, crop, classifier

12.4.1 The patient coordinate system

This section explains why nodule locations in patient coordinates must be transformed before they can index a CT voxel array.

Key terms: candidate center, (X,Y,Z), (I,R,C), voxels, patient coordinate system, LPS, index, row, column, origin

12.4.2 CT scan shape and voxel sizes

This subsection explains how CT voxel sizes and scan shapes vary, and why noncubic voxels can make a scan look distorted when displayed.

Key terms: voxels, row and column dimensions, index dimension, scaling factor, 512 rows by 512 columns, file metadata, voxel size

12.4.3 Converting between millimeters and voxel addresses

This section explains how CT metadata supports conversion between patient coordinates in millimeters and voxel addresses in an array.

Key terms: patient coordinates, IRC, XYZ, voxel sizes, directions matrix, origin, SimpleITK, Image, MetaIO, mhd

12.4.4 Extracting a nodule from a CT scan

The section explains why and how to crop a small CT volume around each nodule candidate instead of examining an entire scan.

Key terms: candidate, voxels, CT scan, getRawCandidate, patient coordinate system, array coordinates, width in voxels, cubic chunk of CT

12.5 Straightforward dataset implementation

This section explains how a custom LunaDataset presents candidates from many CT scans as indexed training or validation samples.

Key terms: LunaDataset, Ct instance, samples, flattening, class imbalance, Dataset, __len__, __getitem__, index, sample tuple

12.5.1 Caching candidate arrays with the getCtRawCandidate function

This section explains how caching CT scans and extracted candidate arrays makes LunaDataset faster, and when its disk cache must be cleared.

Key terms: getCt, getCtRawCandidate, diskcache, cache misses, LunaDataset, on-disk caching, I/O times, cached values, data-unversioned/cache

12.5.2 Constructing our dataset in LunaDataset.__init__

This subsection describes how LunaDataset initialization selects training or validation samples and can restrict the dataset to one CT series.

Key terms: val_stride, isValSet_bool, training set, validation set, series_uid, nodules, CT scan

12.5.3 A training/validation split

This section explains how LunaDataset keeps training and validation candidates separate and what makes those splits useful for evaluating a model.

Key terms: Dataset, isValSet_bool, self.candidateInfo_list, getCandidateInfoList, training set, validation set, leak, patients, CT scans, nodules

12.5.4 Rendering the data

The section explains why rendering CT samples helps inspect the dataset and warns that sample positions can change when more subsets are added.

Key terms: rendering your data, CT and nodule slices, subsets, LunaDataset.candidateInfo_list, noduleSample_list

12.6 Conclusion

The chapter turns raw CT data into tensors for PyTorch and emphasizes that dataset design choices can affect the project's success.

Key terms: DICOM-via-meta-image raw data, tensors, model, training loop, size of our inputs, structure of our caching, training and validation sets

12.7 Exercises

12.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 11: Using PyTorch to fight cancer Chapter 13: Training a classification model to detect suspected tumors →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.