Ch 1 ▸ Why not a dense layer? ▸ Module 1/4
Images are numbers.
A digital image is a grid of numbers. A pixel is one square of that grid, and it holds the brightness of the image at that spot. In this book 0 is black, 1 is white, and anything in between is a shade of gray. An image in which every pixel holds a single brightness is called a grayscale image. Color images store three numbers per pixel, and they wait until chapter 4, so every image before then is grayscale.
Pixels are named by their row and column, both counted from 0, starting at the top-left. So pixel (2, 5) is in the third row from the top and the sixth column from the left.
The duck in figure 1.1 is 16 pixels wide and 16 pixels tall, so it’s 256 numbers. Point at any pixel to read its value.
Flattening
The layers in this chapter don’t take a grid. They take a list. Flattening turns a grid into a list by laying its rows end to end: row 0 first, then row 1, then row 2, and so on. The 16×16 duck becomes a list of 256 numbers.
Every pixel lands at a predictable position. Row 0 fills positions 0 to 15, row 1 fills 16 to 31, and in general a pixel at row and column of an image pixels wide lands at
Pixel (2, 5) of the duck lands at .
What happens to neighbors
Point at a pixel in the middle of the duck. Its left and right neighbors land right next to it in the list, one position on either side. But the pixel above it lands 16 positions earlier, and the pixel below lands 16 positions later. Switch the image to 32×32, and those neighbors are 32 positions away.
Nothing is lost. If you know the width, you can cut the list back into rows and rebuild the image exactly. The list itself, though, has no record of which numbers were neighbors, and two pixels that touched in the image can end up far apart in it. The last module of this chapter is about what that costs.
Knowledge check ▸ 4 questions
Before you move on.
Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.
1A 28×28 grayscale image is flattened. How many numbers are in the list?
2A 16×16 image is flattened. Counting from 0, at what position does the pixel at row 3, column 4 land?
3In a flattened 32×32 image, how many positions apart are a pixel and the pixel directly below it?
4What does flattening lose?