Code

capstone.py — the baseline

What you just watched is capstone.py. Here you get it as a starting point: complete, working, and with defaults we managed to justify.

Your job

Create capstone.py and test_capstone.py (on the right). Both are complete.

All the important parts are old acquaintances: make_loaders (lesson 9), build_resnet (lesson 10), fit (lesson 7), predict_all, confusion_matrix, print_report and predict_image (lesson 11). Only these things are new:

  • --augment / --no-augment — off by default. Why — in the next step;
  • --patience — how many epochs to wait when the validation loss stops improving (0 = never stop);
  • --out and --summary — where to save the model and the summary. Different runs must get different names, otherwise one overwrites the other;
  • the test/ folder is opened at the very start, before training: with a wrong --test-dir the script stops within seconds, not after minutes of training;
  • at the end the demo shows up to five random test/ photos, in any format ImageFolder recognises.

The test

test_capstone.py does not measure whether the model is good (its data is random noise). It checks the plumbing:

$ python -m unittest test_capstone -v
test_augmentation_can_be_switched_on_and_off ... ok
test_augmentation_is_off_by_default ... ok
test_a_missing_test_folder_fails_before_any_training ... ok
test_a_test_folder_with_other_classes_is_refused ... ok
test_runs_end_to_end_and_writes_its_outputs ... ok
test_the_demo_prints_one_line_per_picture_up_to_five ... ok

Ran 6 tests in 4.208s

OK

It creates a tiny folder (2 classes, 10 training images and 4 test/ images each, the latter .jpg), runs the whole script with --epochs 1 --size 64, and checks that:

  • model.pt was saved with the classes and size, and summary.json with five keys;
  • a missing test/ folder stops the script before training;
  • a test/ with different classes is refused with the message test folder must have the same class folders;
  • augmentation is off by default.

A trap this test catches. A demo that only looks for *.png crashes on a folder of .jpg photos (phone photos usually are) with ValueError: Sample larger than population or is negative — after training and evaluation have finished. That is why the test's test/ photos are .jpg: we checked that version, and the test catches it.

Run it on your data

python capstone.py --data data --test-dir test --out noaug.pt --summary noaug.json

On our stand-in it took 151.5 seconds (another time — 221 s). Yours depends on the number of images and your computer; on a CPU it is a few minutes. Compare with the previous step and do not expect the same numbers: other classes, other images, another split.

When it does not pass

  • AssertionError: test folder must have the same class folders — the folder names in test/ do not match data/. Check the spelling and whether an extra folder appeared.
  • FileNotFoundError for test/ — a wrong --test-dir. You now see it at once, not after training.
  • ValueError: Sample larger than population or is negative — if you changed the demo's file search back to *.png and your photos are .jpg.