Exemplo n.º 1
0
def run(args, kwargs):
    args.model_signature = str(datetime.datetime.now())[0:19]

    model_name = args.dataset_name + '_' + args.model_name + '_' + args.prior + '(K_' + str(args.number_components) + ')' + '_wu(' + str(args.warmup) + ')' + '_z1_' + str(args.z1_size) + '_z2_' + str(args.z2_size)

    # DIRECTORY FOR SAVING
    snapshots_path = 'snapshots/'
    dir = snapshots_path + args.model_signature + '_' + model_name +  '/'

    if not os.path.exists(dir):
        os.makedirs(dir)

    # LOAD DATA=========================================================================================================
    print('load data')

    # loading data
    train_loader, val_loader, test_loader, args = load_dataset(args, **kwargs)

    # CREATE MODEL======================================================================================================
    print('create model')
    # importing model
    if args.model_name == 'vae':
        from models.VAE import VAE
    elif args.model_name == 'hvae_2level':
        from models.HVAE_2level import VAE
    elif args.model_name == 'convhvae_2level':
        from models.convHVAE_2level import VAE
    elif args.model_name == 'pixelhvae_2level':
        from models.PixelHVAE_2level import VAE
    else:
        raise Exception('Wrong name of the model!')

    model = VAE(args)
    if args.cuda:
        model.cuda()

    optimizer = AdamNormGrad(model.parameters(), lr=args.lr)
#    optimizer = optim.Adam(model.parameters(), lr=args.lr)

    # ======================================================================================================================
    print(args)
    with open('vae_experiment_log.txt', 'a') as f:
        print(args, file=f)

    # ======================================================================================================================
    print('perform experiment')
    from utils.perform_experiment import experiment_vae
    experiment_vae(args, train_loader, val_loader, test_loader, model, optimizer, dir, model_name = args.model_name)
    # ======================================================================================================================
    print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-')
    with open('vae_experiment_log.txt', 'a') as f:
        print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n', file=f)
Exemplo n.º 2
0
def run(args, kwargs):
    args.model_signature = str(datetime.datetime.now())[0:19]

    model_name = args.dataset_name + '_' + args.model_name + '_' + args.prior + '(K_' + str(args.number_components) + ')' + '_wu(' + str(args.warmup) + ')' + '_z1_' + str(args.z1_size) + '_z2_' + str(args.z2_size)

    # DIRECTORY FOR SAVING
    snapshots_path = 'snapshots/'
    dir = snapshots_path + args.model_signature + '_' + model_name +  '/'

    if not os.path.exists(dir):
        os.makedirs(dir)

    # LOAD DATA=========================================================================================================
    print('load data')

    # loading data
    train_loader, val_loader, test_loader, args = load_dataset(args, **kwargs)

    # CREATE MODEL======================================================================================================
    print('create model')
    # importing model
    if args.model_name == 'vae':
        from models.VAE import VAE
    elif args.model_name == 'hvae_2level':
        from models.HVAE_2level import VAE
    elif args.model_name == 'convhvae_2level':
        from models.convHVAE_2level import VAE
    elif args.model_name == 'pixelhvae_2level':
        from models.PixelHVAE_2level import VAE
    else:
        raise Exception('Wrong name of the model!')

    model = VAE(args)
    if args.cuda:
        model.cuda()

    optimizer = AdamNormGrad(model.parameters(), lr=args.lr)

    # ======================================================================================================================
    print(args)
    with open('vae_experiment_log.txt', 'a') as f:
        print(args, file=f)

    # ======================================================================================================================
    print('perform experiment')
    from utils.perform_experiment import experiment_vae
    experiment_vae(args, train_loader, val_loader, test_loader, model, optimizer, dir, model_name = args.model_name)
    # ======================================================================================================================
    print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-')
    with open('vae_experiment_log.txt', 'a') as f:
        print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n', file=f)
Exemplo n.º 3
0
        train_dataset,
        batch_size=train_args.batch_size,
        shuffle=True,
        num_workers=train_args.num_workers,
        pin_memory=True,
    )
    net = VAE(
        label="vgg",
        image_size=train_args.image_size,
        channel_num=3,
        kernel_num=128,
        z_size=train_args.z_dim,
    )

    if cuda:
        net.cuda()

    # run a test or a training process.
    train_model(
        net,
        dataset="vgg",
        epochs=train_args.epochs,
        batch_size=train_args.batch_size,
        sample_size=32,
        lr=3e-05,
        weight_decay=1e-06,
        checkpoint_dir=train_args.ckpt_dir,
        loss_log_interval=train_args.log_interval,
        image_log_interval=500,
        resume=False,
        cuda=cuda,
Exemplo n.º 4
0
def run(args, kwargs):
    args.model_signature = str(datetime.datetime.now())[0:19]

    model_name = args.dataset_name + '_' + args.model_name + '_' + args.prior + '(M_' + str(
        args.M) + ')' + '(F_' + str(args.F) + ')' + '_wu(' + str(
            args.warmup) + ')' + '_z1_' + str(args.z1_size) + '_hidden_' + str(
                args.number_hidden) + '_ksi_' + str(args.ksi)

    if args.FI is True:
        model_name += '_FI'
    else:
        args.F = 0

    if args.MI is True:
        model_name += '_MI'
    else:
        args.M = 0

    # DIRECTORY FOR SAVING
    #snapshots_path = 'snapshots/'

    snapshots_path = args.snapshot_dir
    if not os.path.exists(snapshots_path):
        os.makedirs(snapshots_path)

    dir = snapshots_path + args.model_signature + '_' + model_name + '/'

    if not os.path.exists(dir):
        os.makedirs(dir)

    # LOAD DATA=========================================================================================================
    print('load data')

    # loading data
    train_loader, val_loader, test_loader, args = load_dataset(args, **kwargs)

    # CREATE MODEL======================================================================================================
    print('create model')
    if args.dataset_name == "celeba":
        args.model_name = "celebavae"
    # importing model
    if args.model_name == 'vae':
        from models.VAE import VAE
    elif args.model_name == 'hvae_2level':
        from models.HVAE_2level import VAE
    elif args.model_name == 'convhvae_2level':
        from models.convHVAE_2level import VAE
    elif args.model_name == 'convvae':
        from models.convVAE import VAE
    elif args.model_name == 'pixelhvae_2level':
        from models.PixelHVAE_2level import VAE
    elif args.model_name == 'pixelvae':
        from models.pixelVAE import VAE
    elif args.model_name == 'iaf_vae':
        from models.VAE_ccLinIAF import VAE
    elif args.model_name == 'rev_vae':
        from models.REV_VAE import VAE
    elif args.model_name == 'rev_pixelvae':
        from models.REV_pixelVAE import VAE
    elif args.model_name == 'celebavae':
        from models.CelebaVAE import VAE
    else:
        raise Exception('Wrong name of the model!')

    model = VAE(args)
    if args.cuda:
        model.cuda()

    optimizer = AdamNormGrad(model.parameters(), lr=args.lr)

    # ======================================================================================================================
    print(args)
    with open(dir + 'vae_experiment_log.txt', 'a') as f:
        print(args, file=f)

    # ======================================================================================================================
    print('perform experiment')
    from utils.perform_experiment import experiment_vae
    experiment_vae(args,
                   train_loader,
                   val_loader,
                   test_loader,
                   model,
                   optimizer,
                   dir,
                   model_name=args.model_name)
    # ======================================================================================================================
    print(
        '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-'
    )
    with open(dir + '/vae_experiment_log.txt', 'a') as f:
        print(
            '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n',
            file=f)
Exemplo n.º 5
0
def run(args, kwargs):

    # LOAD DATA=========================================================================================================
    print('load data')

    checkpoints_dir, results_dir = create_dirNames(args)

    # loading data
    train_loader, val_loader, test_loader, args = load_dataset(args, **kwargs)

    # CREATE MODEL======================================================================================================
    print('create model')
    # importing model
    if args.model_name == 'vae':
        from models.VAE import VAE
        model = VAE(args)
    elif args.model_name == 'conv_vae':
        from models.conv_vae import VAE
        model = VAE(args)
    elif args.model_name == 'hvae_2level':
        from models.HVAE_2level import VAE
        model = VAE(args)
    elif args.model_name == 'convhvae_2level':
        from models.convHVAE_2level import VAE
        model = VAE(args)
    elif args.model_name == 'convhvae_2level-smim':
        from models.convHVAE_2level import SymMIM as VAE
        model = VAE(args)
    elif args.model_name == 'MLP_wae':
        from models.MLP_wae import WAE
        model = WAE(args)
    elif args.model_name == 'conv_wae':
        from models.conv_wae import WAE
        model = WAE(args)
    else:
        raise Exception('Wrong name of the model!')

    #model = VAE(args)
    if args.cuda:
        model.cuda()

    optimizer = AdamNormGrad(model.parameters(),
                             lr=args.lr,
                             betas=(args.beta1, 0.999))

    # ======================================================================================================================
    print(args)

    # ======================================================================================================================
    print('perform experiment')

    if args.trainflag:
        if args.model_name == 'MLP_wae' or args.model_name == 'conv_wae' or args.model_name == 'Pixel_wae' or args.model_name == 'conv_wae_2level':
            train_wae(args, train_loader, val_loader, model, optimizer,
                      checkpoints_dir, results_dir)
        else:
            train_vae(args, train_loader, val_loader, test_loader, model,
                      optimizer, checkpoints_dir, results_dir)

    if args.testflag:
        test(args, train_loader, test_loader, model, checkpoints_dir,
             results_dir)
Exemplo n.º 6
0
print('accuracy {}'.format(acc.item()))

if not os.path.exists('joint_models/'):
    os.mkdir('joint_models/')
torch.save(
    classifier.state_dict(), 'joint_models/joint_classifier_' +
    arguments.dataset_name + 'accuracy_{}'.format(acc) + '.t')

pdb.set_trace()

#
### generator
model = VAE(arguments)
if arguments.cuda:
    model = model.cuda()

if 0 & os.path.exists(model_path):
    print('loading model...')

    model.load_state_dict(torch.load(model_path))
    model = model.cuda()
else:
    print('training model...')
    optimizer = AdamNormGrad(model.parameters(), lr=arguments.lr)
    tr.experiment_vae(arguments, train_loader, val_loader, test_loader, model,
                      optimizer, dr, arguments.model_name)

results = ev.evaluate_vae(arguments, model, train_loader, test_loader, 0,
                          results_path, 'test')
pickle.dump(results, open(results_path + results_name + '.pk', 'wb'))
Exemplo n.º 7
0
def run():
    model_name = args.model_name
    if model_name == 'vae_HF':
        args.number_combination = 0
    elif model_name == 'vae_ccLinIAF':
        args.number_of_flows = 1

    if args.model_name == 'vae_HF':
        model_name = model_name + '(T_' + str(args.number_of_flows) + ')'
    elif args.model_name == 'vae_ccLinIAF':
        model_name = model_name + '(K_' + str(args.number_combination) + ')'

    model_name = model_name + '_wu(' + str(args.warmup) + ')' + '_z1_' + str(args.z1_size)

    if args.z2_size > 0:
        model_name = model_name + '_z2_' + str(args.z2_size)

    print(args)

    with open('vae_experiment_log.txt', 'a') as f:
        print(args, file=f)

    # DIRECTORY FOR SAVING
    snapshots_path = 'snapshots/'
    dir = snapshots_path + model_name + '/'

    if not os.path.exists(dir):
        os.makedirs(dir)

    # LOAD DATA=========================================================================================================
    print('load data')
    if args.dataset_name == 'dynamic_mnist':
        args.dynamic_binarization = True
    else:
        args.dynamic_binarization = False

    # loading data
    train_loader, val_loader, test_loader = load_dataset(args)

    # CREATE MODEL======================================================================================================
    print('create model')
    # importing model
    if args.model_name == 'vae':
        from models.VAE import VAE
    elif args.model_name == 'vae_HF':
        from models.VAE_HF import VAE
    elif args.model_name == 'vae_ccLinIAF':
        from models.VAE_ccLinIAF import VAE
    else:
        raise Exception('Wrong name of the model!')

    model = VAE(args)
    if args.cuda:
        model.cuda()

    optimizer = optim.Adam(model.parameters(), lr=args.lr)

    # ======================================================================================================================
    print('perform experiment')
    from utils.perform_experiment import experiment_vae
    experiment_vae(args, train_loader, val_loader, test_loader, model, optimizer, dir, model_name = args.model_name)
    # ======================================================================================================================
    print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-')
    with open('vae_experiment_log.txt', 'a') as f:
        print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n', file=f)