Skip to content

Fliptate Problem


Given an 8x8x3 tensor which represents an image of the number 4,

Flip the image horizontally.

Then rotate the image clockwise 90 degrees.

import torch

r = torch.tensor([
    [0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0],
    [0,0,1,0,0,1,0,0],
    [0,0,1,0,0,1,0,0],
    [0,0,1,1,1,1,0,0],
    [0,0,0,0,0,1,0,0],
    [0,0,0,0,0,1,0,0],
    [0,0,0,0,0,1,0,0]
], dtype=torch.float32)
g = 0.8*r
b = 0.5*r
img = torch.moveaxis(torch.stack((r,g,b)), 0, -1)

print(img.shape)
# torch.Size([8, 8, 3]) (1)
  1. img is an 8x8 image with 3 color channels (RGB).
How to plot img

Use matplotlib.pyplot.imshow().

import matplotlib.pyplot as plt
plt.imshow(img)

Try with Google Colab See the solution