Homework

Homework — your categories, a borrowed network

What to hand in

A table, a short report and your own transfer-learning run on your own categories.

1. Prepare the originals

transfer.py reads colour originals and shrinks them to 224 × 224 itself — it does not need your 28 × 28 data/. But a 12-megapixel phone photograph is decoded again every epoch, and that is slow. Before you start, shrink the originals to ~256 px:

from pathlib import Path

from PIL import Image, ImageOps

for path in Path("photos").glob("*/*"):
    if path.suffix.lower() not in {".jpg", ".jpeg", ".png", ".webp"}:
        continue
    out = Path("photos_small") / path.parent.name / (path.stem + ".jpg")
    out.parent.mkdir(parents=True, exist_ok=True)
    img = ImageOps.exif_transpose(Image.open(path)).convert("RGB")
    img.thumbnail((256, 256))  # longest side at most 256, aspect ratio kept
    img.save(out)

(You need not declare the file — it is a one-off tool, not part of the project.) You will get photos_small/<category>/*.jpg.

2. Train

python transfer.py --data photos_small --epochs 5 --lr 0.01
python transfer.py --data photos_small --epochs 5 --lr 0.001 --finetune

Add lesson 9's CNN from scratch to the table (--data data, the same set of categories):

model trainable params train val time
CNN from scratch (lesson 9, 28 × 28)
ResNet-18 frozen
ResNet-18 + layer4

3. Check yourself before writing the report

  • Does the trainable parameters: line show what you expected? (Drill 1.)
  • Is the "before training" accuracy about 1 / the number of classes?
  • Is the training accuracy 100 % while validation is much lower? If so — lesson 7.

4. The report (6–8 sentences)

  • Did transfer learning give you a jump? Compare with our two results: ~0 points (clothes, frozen) and ~25 points (CIFAR). Where did your categories end up, and why (how much do they resemble ImageNet photographs)?
  • How many validation pictures does your smallest class have? How many points is one picture? Is the difference between the models bigger than that?
  • One sentence: what these numbers do not prove.

5. Do not touch the test folder

Your separate test folder (~20 pictures per class) — still do not touch it. In lesson 11 you will use it for the first time — and learn whether the validation number was too optimistic. In our case it was: 95.6 % on validation, 93.5 % on 400 new pictures.