__getitem__ is the Python special method that lets a PyTorch Dataset return one item when accessed with subscription syntax such as dataset[index]. A returned item may contain a sample and label, or any other data point. section 7.1.2
A Dataset implementation uses __getitem__ to:
- Accept an index, typically an integer from 0 through N−1 when __len__ returns N. section 12.5
- Retrieve or construct the corresponding sample and format it into the data types and dimensions required downstream. section 12.5
- Return a problem-specific value, such as an image-label pair or a four-item training tuple. section 7.1.2 section 12.5
Indexing CIFAR-10
img, label = cifar10[99] retrieves a PIL RGB image and integer label 1; interpreting that label through class_names gives “automobile.” section 7.1.2
Building a custom sample
A custom implementation can select candidateInfo_list[ndx], load a candidate, convert it to a float tensor with an added dimension, create a label tensor, and return those values with identifiers and coordinates. section 12.5
Figure 7.2 Concept of a PyTorch Dataset object. It doesn’t necessarily hold the data, but it provides uniform access to it through __len__ and __getitem__.
Shows Dataset as a uniform access interface built around __len__ and __getitem__, without requiring the object itself to hold all data. section 7.1.2

