22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

For each individual image, it may be either the first dimension (before HW) or the

last dimension (after HW).

There are endless discussions about which format is better, faster, or whatever.

We’re not getting into that here. Nonetheless, we need to address this difference

because it is a common source of confusion and error since each package or

framework uses a different one:

• PyTorch uses NCHW.

• TensorFlow uses NHWC.

• PIL images are HWC.

Annoying, right? I think so, too. But it is only a matter of paying close attention to

which format is coming in, and which format is going out after an operation. Let’s

work our way through it.

Our dataset generates images following the PyTorch format; that is, NCHW.

What’s the shape of our dataset, then?

images.shape

Output

(300, 1, 5, 5)

As expected, 300 images, single-channel, five pixels wide, five pixels high. Let’s take

a closer look at one image, say, image #7:

example = images[7]

example

Output

array([[[ 0, 255, 0, 0, 0],

[ 0, 0, 255, 0, 0],

[ 0, 0, 0, 255, 0],

[ 0, 0, 0, 0, 255],

[ 0, 0, 0, 0, 0]]], dtype=uint8)

272 | Chapter 4: Classifying Images

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

Saved successfully!

Ooh no, something went wrong!