Esempio n. 1
0
def main(_):
    # create folders
    mkdir_if_missing(FLAGS.checkpoint_dir)
    mkdir_if_missing(FLAGS.log_dir)
    mkdir_if_missing(FLAGS.saliency_map)
    # training
    train_models.train()
Esempio n. 2
0
def retrain_experiments(args: ExperimentArgs, train_args: RetrainingArgs,
                        final_protocol: PruneProtocol, init_protocol: PruneProtocol,
                        train_loader: DataLoader, test_loader: DataLoader,
                        use_final_subnetwork=True,
                        device=0):
    # load init model
    init_model = load_model(args.arch, args.init_model_path, device=device)
    init_model.eval()
    print('Loaded initialized model from %s' % args.init_model_path)

    (init_pruner,) = get_pruners(init_protocol,
                                 device=device,
                                 networks=(init_model,))

    if use_final_subnetwork:
        # load final model
        final_model = load_model(args.arch, args.final_model_path, device=device)
        print('Loaded final model from %s' % args.final_model_path)

        (final_pruner,) = get_pruners(final_protocol,
                                      device=device,
                                      networks=(final_model,))

        init_pruner.set_prune_masks(**final_pruner.compute_prune_masks(reset=True))
        del final_model

    with init_pruner.prune(clear_on_exit=True, recompute_masks=False):
        print('Retraining pruned subnetwork of model at initialization...')
        train(train_args, init_model, train_loader, test_loader, device=device)
def main(_):
    # create folders
    mkdir_if_missing(FLAGS.checkpoint_dir)
    mkdir_if_missing(FLAGS.log_dir)
    mkdir_if_missing(FLAGS.train_samples_dir)
    # training
    train_models.train()
Esempio n. 4
0
## Build the architectures
Models, model_names = build_models.build(Imported_data['sample_sz'],
                                         Imported_data['classes'],
                                         dr=0.5)

## Train/ load weights for the built models
# If training, will plot and save the training & validation loss curves in the 'Figures' folder
for i in range(len(model_names)):
    model = Models[i]
    model_name = model_names[i]
    train_models.train(model,
                       model_name,
                       Imported_data['X_train1'],
                       Imported_data['X_test1'],
                       Imported_data['Y_train1'],
                       Imported_data['Y_test1'],
                       tag,
                       load_weights,
                       epochs=100,
                       batch_size=Imported_data['batch_size'])

## Verify which dataset to use when testing from here on, should it belong to a different distribution than the training data
if train_test_SNR_eq == True:
    X_test = Imported_data['X_test1']
    Y_test = Imported_data['Y_test1']
else:
    X_test = Imported_data['X_test2']
    Y_test = Imported_data['Y_test2']

## Overall accuracy and confusion matrix for the corresponding models
for i in range(len(model_names)):
Esempio n. 5
0
from argument_parsing import *
from utils.hook_management import HookManager, detach_hook
from utils.helpers import find_network_modules_by_name, get_named_modules_from_network
from train_models import train, get_dataloaders


def reinit_layers(network: torch.nn.Module, start_layer):
    begin_reinit = False
    for name, module in get_named_modules_from_network(network,
                                                       include_bn=True):
        if begin_reinit:
            module.reset_parameters()
        if name == start_layer:
            begin_reinit = True


if __name__ == '__main__':
    model_args, data_args, train_args = parse_args(LoadModelArgs,
                                                   FeatureDataArgs,
                                                   DistributedTrainingArgs)
    network = load_model(model_args.arch,
                         model_args.final_model_path,
                         device=0)
    hook_manager = HookManager()
    hook_manager.register_forward_hook(detach_hook,
                                       *find_network_modules_by_name(
                                           network, [data_args.layer]),
                                       activate=True)
    dataloaders = get_dataloaders(data_args)
    train(train_args, network, *dataloaders, device=0)
Esempio n. 6
0
    if cfg["save_dir"].is_dir():
        raise Exception("Result directory already exist. Please remove first.")
    else:
        cfg["model_dir"] = cfg["save_dir"] / 'models'
        for basin in basins:
            for seed in seeds:
                run_dir = cfg["model_dir"] / basin / f"seed_{seed}"
                run_dir.mkdir(parents=True)

    # train one model at the time with each of the random seeds
    for i, basin in enumerate(basins):
        print(
            f"### {i+1}/{len(basins)} Start training LSTMs for basin {basin}")
        for seed in seeds:
            print(f"## Setting random seed to {seed}")
            train(basin, seed, cfg)

# check if run includes evaluation
if cfg["mode"] in ["eval", "both"]:

    # add path to data dir to cfg dictionary and create dir to store plots
    cfg["data_dir"] = Path(__file__).absolute().parent / 'data'
    cfg["plot_dir"] = Path(__file__).absolute().parent / 'plots'
    if not cfg["plot_dir"].is_dir():
        cfg["plot_dir"].mkdir()

    results = defaultdict(dict)

    # check if trained models exist
    if "model_dir" not in cfg.keys():
        cfg["save_dir"] = Path(__file__).absolute().parent / 'results'
Esempio n. 7
0
check_args(args)
model_params, opt_params, train_params = get_params(args)

# Load data code should be here. Vocab size function of text.
train_iter, val_iter, EN, DE = generate_iterators(MAX_LEN=args.max_len, load_data=args.load_saved_data, BATCH_SIZE=args.batch_size, embedding=args.embedding)
source_embedding = DE.vocab.vectors if args.embedding is not None else None
target_embedding = EN.vocab.vectors if args.embedding is not None else None
model_params['source_vocab_size'] = len(DE.vocab.itos)
model_params['target_vocab_size'] = len(EN.vocab.itos)

if True:
    t.backends.cudnn.enabled = True  # False necessary for memory overflows ? It seems not

# Call for different models code should be here.
# Train Model
trained_model = train(args.model,
                      train_iter,
                      val_iter=val_iter,
                      cuda=args.cuda,
                      save=args.save,
                      save_path=args.output_filename,
                      source_embedding=source_embedding,
                      target_embedding=target_embedding,
                      model_params=model_params,
                      early_stopping=args.early_stopping,
                      train_params=train_params,
                      opt_params=opt_params)

# Dummy code.
print("The model is ", args.model)
Esempio n. 8
0
    dataset, args.data_directory, samples, train_pct, train_SNR_h, train_SNR_l,
    train_SNR_step, test_SNR_h, test_SNR_l, test_SNR_step, train_test_SNR_eq)

## Build the architectures
Models, model_names = build_models.build(in_shp, classes, dr=0.5)

## Train/ load weights for the built models
# If training, will plot and save the training & validation loss curves in the 'Figures' folder
for i in range(len(model_names)):
    model = Models[i]
    model_name = model_names[i]
    train_models.train(model,
                       model_name,
                       X_train1,
                       X_test1,
                       Y_train1,
                       Y_test1,
                       tag,
                       load_weights,
                       epochs=100,
                       batch_size=1024)

## Verify which dataset to use when testing from here on, should it belong to a different distribution than the training data
if train_test_SNR_eq == True:
    X_test = X_test1
    Y_test = Y_test1
else:
    X_test = X_test2
    Y_test = Y_test2

## Overall accuracy and confusion matrix for the corresponding models
for i in range(len(model_names)):