20.03.2021 Views

Deep-Learning-with-PyTorch

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A dataset of tiny images

167

ACTUAL

DATA

DATASET Q: HOW MANY ELEMENTS ?

HUMAN DOG HUMAN

DOG

HUMAN

4

5

Q: MAY I GET ITEM 4 ?

4

“HUMAN”

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__.

Similarly, since the dataset is equipped with the __getitem__ method, we can use the

standard subscript for indexing tuples and lists to access individual items. Here, we get

a PIL (Python Imaging Library, the PIL package) image with our desired output—an

integer with the value 1, corresponding to “automobile”:

# In[6]:

img, label = cifar10[99]

img, label, class_names[label]

# Out[6]:

(<PIL.Image.Image image mode=RGB size=32x32 at 0x7FB383657390>,

1,

'automobile')

So, the sample in the data.CIFAR10 dataset is an instance of an RGB PIL image. We

can plot it right away:

# In[7]:

plt.imshow(img)

plt.show()

This produces the output shown in figure 7.3. It’s a red car! 3

3

It doesn’t translate well to print; you’ll have to take our word for it, or check it out in the eBook or the Jupyter

Notebook.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!