[faces_sub_image] - add --load_model and --do_train CLI args

Reconstruction always runs. Default: load saved weights, skip training.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-30 15:25:22 +02:00
co-authored by Claude Sonnet 4.6
parent 56f881d155
commit feaf96417a
+19 -8
View File
@@ -146,28 +146,39 @@ def show_image_reconstruction(model: TestModel, img_path: str):
if __name__ == '__main__':
from argparse import ArgumentParser
ap = ArgumentParser()
ap.add_argument('--load_model', type=lambda s: s.lower() != 'false', default=True,
help='Load saved model weights (default: true)')
ap.add_argument('--do_train', type=lambda s: s.lower() != 'false', default=False,
help='Train the model (default: false)')
args = ap.parse_args()
prj_name = 'faces_sub_image'
work_dir = 'results'
model = TestModel(prj_name, work_dir)
model.init(0.001)
model.load()
print(f'Loading patches from {N_IMAGES} images...')
train_patches = load_patches(DATA_DIR, N_IMAGES)
print(f'Training on {train_patches.shape[0]} patches ({N_VIS}-dim each)')
if args.load_model:
model.load()
model.train(train_patches)
model.save()
if args.do_train:
print(f'Loading patches from {N_IMAGES} images...')
train_patches = load_patches(DATA_DIR, N_IMAGES)
print(f'Training on {train_patches.shape[0]} patches ({N_VIS}-dim each)')
model.train(train_patches)
model.save()
# Show learned weight filters
show_filters(model.unit1)
# Show patch-level reconstructions on held-out images
print('Loading test patches...')
test_patches = load_patches(DATA_DIR, n_images=N_IMAGES + 10)
test_patches = test_patches[train_patches.shape[0]:] # use only unseen patches
test_patches = test_patches[N_IMAGES * 80:] # approximate held-out offset
if test_patches.shape[0] < 10:
test_patches = train_patches
test_patches = load_patches(DATA_DIR, n_images=10)
show_reconstructions(model, test_patches)
# Show full image reconstruction