Challenge¶
Given an 8x8x3 tensor which represents an image of the number 4,
Flip the image horizontally.
Then rotate the image clockwise 90 degrees.
img
is an 8x8 image with 3 color channels (RGB).
How to plot img
Solution¶
Explanation¶
-
First we flip the image using
torch.flip()
.dims
identifies which dimensions (axes) to flip. Flipping the image horizontally means flipping the column indices which are stored in dimension 1. (Remember, the image dimensions represent rows by columns by color channels.) -
Next we rotate the image using
torch.rot90()
.The docs for
rot90
explain thek
parameter as follows:number of times to rotate. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0.
The axes for
img
look like this:k = 1
means rotate once from axis 0 towards axis 1, which corresponds to a 90-degree counter-clockwise rotation.k = -1
means rotate once from axis 1 towards axis 0, which corresponds to a 90-degree clockwise rotation.