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);--outand--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-dirthe script stops within seconds, not after minutes of training; - at the end the demo shows up to five random
test/photos, in any formatImageFolderrecognises.
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.ptwas saved with the classes and size, andsummary.jsonwith five keys;- a missing
test/folder stops the script before training; - a
test/with different classes is refused with the messagetest folder must have the same class folders; - augmentation is off by default.
A trap this test catches. A demo that only looks for
*.pngcrashes on a folder of.jpgphotos (phone photos usually are) withValueError: Sample larger than population or is negative— after training and evaluation have finished. That is why the test'stest/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 intest/do not matchdata/. Check the spelling and whether an extra folder appeared.FileNotFoundErrorfortest/— 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*.pngand your photos are.jpg.