explain this: OK, clearly that is the digit 5. Since we are learning how images are…
- User
explain this:
OK, clearly that is the digit 5. Since we are learning how images are presented as tensors, let’s do a color version. If we stack three copies of the same digit on top of each other, we will have a tensor of shape (3; 28; 28). Because the structure of the tensor has meaning, this instantaneously makes it a color image by virtue of having three channels. The following code does exactly that, stacking the first grayscale image three times and printing its shape:Spatial structural prior beliefs 79 x_as_color = torch.stack([x_example[0,:], x_example[0,:], x_example[0,:]], dim=0) print(x_as_color.shape) torch.Size([3, 28, 28]) Now let’s visualize the color version. Here we need to be a little careful. In PyTorch, an image is represented as (N; C; W; H),2 but imshow expects a single image as (W; H; C). So we need to permute the dimensions when using imshow. If our tensor has r dimensions, the permute function takes r inputs: the indexes 0; 1; : : : ; r − 1 of the original tensor in the new order in which we want them to appear. Since our image has (C; W; H) right now, maintaining that order means (0; 1; 2). We want the channel at index 0 to become the last dimension, width first, and height second, which is (1; 2; 0). Let’s try it: imshow(x_as_color.permute(1,2,0)) [9]: <matplotlib.image.AxesImage at 0x7f6b681c60d0> 2 Different frameworks support different orderings for a variety of nuanced reasons that we are not going to get into. Let’s focus on the basics and default PyTorch behavior.80 CHAPTER 3 Convolutional neural networks Why is this color image still black and white? Because the original image was black and white. We have the same value copied in the red, green, and blue channels, which is how you represent a black-and-white image in color. If we zero out the red and blue channels, we get a green number: x_as_color = torch.stack([x_example[0,:], x_example[0,:], x_example[0,:]]) x_as_color[0,:] = 0 No Red. We’re leaving Green alone. x_as_color[2,:] = 0 No Blue imshow(x_as_color.permute(1,2,0)) [10]: <matplotlib.image.AxesImage at 0x7f6a1fc8b810> Changing the color of this image is an example of How the different channels impact what the data represents What it means for this to be a structured data representation Just to make sure these two points are clear, let’s stack three different images together into one color image. We will reuse the same numeral 5 as in the first image in the stack. It will go into the red channel, so we should see a red 5 mixed with two other digits that are green and blue, respectively:
- Assistant