๐ŸŽ“ A1Learner
๐Ÿ”

PyTorch Images as Tensors

An image is a tensor of numbers with shape (channels, height, width). Each number is one pixel, and its value sets how bright that pixel is.

An empty image

Zeros make a black image. The shape reads channels, height, then width.

import torch

image = torch.zeros(1, 3, 4)
print(image.shape)
print(image[0, 0])

Two pixel scales

200 on the 0 to 255 scale is the same brightness as a fraction on the 0 to 1 scale.

import torch

pixel = torch.tensor(200.0)
print(f"{(pixel / 255).item():.4f}")
print(f"{(pixel / 255 * 255).item():.0f}")

Setting one pixel

Writing 1.0 into the centre makes one bright spot in a dark image.

import torch

image = torch.zeros(1, 3, 3)
image[0, 1, 1] = 1.0
print(image[0])

Example

import torch

torch.manual_seed(0)

# One channel, 16 rows, 16 columns. A soft gradient with a bright block.
image = torch.zeros(1, 16, 16)
image[0] = torch.linspace(0.0, 0.3, 16).reshape(-1, 1).expand(16, 16).clone()
image[0, 4:12, 4:12] = 1.0

print(f"Shape: {tuple(image.shape)}")
print(f"Smallest pixel: {image.min().item():.1f}")
print(f"Largest pixel: {image.max().item():.1f}")

shades = " .:-=+*#%@"
for row in image[0]:
    line = "".join(shades[min(int(v.item() * 10), 9)] for v in row)
    print(line)

Key points

  • Channel: one layer of an image, holding one value per pixel
  • Height and width: the number of pixel rows and columns
  • A pixel value of 0 is black, and larger values are brighter
  • The 0 to 1 scale stores brightness as a fraction
  • The 0 to 255 scale stores brightness as a whole number, and dividing by 255 converts it
  • An image is numbers in a grid, and the shape says how the grid is arranged

In this example, the program builds one image with a single channel and 16 rows and 16 columns. A soft gradient fills the background, rising from 0.0 at the top to 0.3 lower down. A bright block of 1.0 values sits in the middle. The printout shows the shape (1, 16, 16) first. The smallest pixel is 0.0 and the largest is 1.0. The grid then prints each value as a shade character. Spaces and dots mark the dark gradient. The @ characters mark the bright block.

Note

PyTorch uses channels first, so a photo shape reads (3, height, width)

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Python Playground

Exercise

A tensor has shape (3, 16, 16) and holds one PyTorch image. What do the three numbers mean?