[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:
@@ -146,28 +146,39 @@ def show_image_reconstruction(model: TestModel, img_path: str):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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'
|
prj_name = 'faces_sub_image'
|
||||||
work_dir = 'results'
|
work_dir = 'results'
|
||||||
|
|
||||||
model = TestModel(prj_name, work_dir)
|
model = TestModel(prj_name, work_dir)
|
||||||
model.init(0.001)
|
model.init(0.001)
|
||||||
model.load()
|
|
||||||
|
|
||||||
print(f'Loading patches from {N_IMAGES} images...')
|
if args.load_model:
|
||||||
train_patches = load_patches(DATA_DIR, N_IMAGES)
|
model.load()
|
||||||
print(f'Training on {train_patches.shape[0]} patches ({N_VIS}-dim each)')
|
|
||||||
|
|
||||||
model.train(train_patches)
|
if args.do_train:
|
||||||
model.save()
|
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 learned weight filters
|
||||||
show_filters(model.unit1)
|
show_filters(model.unit1)
|
||||||
|
|
||||||
# Show patch-level reconstructions on held-out images
|
# 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 = 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:
|
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_reconstructions(model, test_patches)
|
||||||
|
|
||||||
# Show full image reconstruction
|
# Show full image reconstruction
|
||||||
|
|||||||
Reference in New Issue
Block a user