Homework — a picture as text, and your own photo
Two small jobs: the first is to see a picture with your own eyes (without any graphics library), the second is to take your own photo and see what tensor it becomes.
1. A picture as text
We do not need a plotting library (matplotlib) — a picture can be drawn with characters. Write ascii_image(image): it takes a [28, 28] tensor (0–1) and returns a string with 28 rows of 28 characters.
To start, take a brightness ramp from dark to bright:
" .:-=+*#%@"
For each pixel v pick the character by v * 10 (make sure v = 1.0 does not fall off the end of the ramp).
Print the first picture from load_fashion("train", n=100). You should recognise an ankle boot (label 9). Then print the first example of every class — how many of the ten can you recognise by eye, and which get confused?
2. Your photo as a tensor
Take any photo of your own category (from the categories you picked in lesson 1). Load it:
from PIL import Image
from torchvision.transforms.functional import to_tensor
t = to_tensor(Image.open("my_photo.jpg"))
Print t.shape, t.dtype, t.min(), t.max().
You should see three axes; the first is the channels: 3 (red, green, blue), not the height. Write down the answers to these questions:
- How many numbers are in your photo? Compare with the 784 of a FashionMNIST picture.
- Which axis is the height and which the width? (Hint: PIL says "width × height", PyTorch the other way round.)
- What would you get if you turned the photo grayscale:
Image.open(...).convert("L")? Check.
What to hand in
- the
ascii_imagecode and the pictures of two classes (e.g. a boot and a bag); - three lines from part 2: its tensor shape, dtype, minimum/maximum, and one sentence on what that means for the classifier of your categories.
Tip. The smallest thing you learn from this homework is the biggest: your photos are neither 28 × 28 nor grayscale. Every one of their numbers will have to go down the same road you just walked with FashionMNIST. How that is done — lesson 9. Next lesson — the first real neuron.