Example #1
0
model = vae_models[config['model_params']['name']](**config['model_params'])
experiment = VAEXperiment(model, config['exp_params'])

runner = Trainer(default_save_path=f"{tt_logger.save_dir}",
                 min_nb_epochs=1,
                 logger=tt_logger,
                 log_save_interval=100,
                 train_percent_check=1.,
                 val_percent_check=1.,
                 num_sanity_val_steps=5,
                 early_stop_callback=False,
                 **config['trainer_params'])

print(f"======= Training {config['model_params']['name']} =======")
load_dict = torch.load(config.ckpt_path)
experiment.load_state_dict(load_dict['state_dict'])
experiment.cuda()
experiment.eval()
sample_dataloader = experiment.train_dataloader()
test_input, test_label = next(iter(sample_dataloader))
#test_input = test_input.to('cuda')
test_label = test_label.to('cuda')
#imgs = experiment.model.sample(num_samples=64, current_device=0)
test_input = scio.loadmat('./cifar10_index.mat')
test_input = torch.Tensor(test_input['data']).to('cuda')
imgs_recon = experiment.model.generate(test_input, labels=test_label)

FID_IS_tf = build_GAN_metric(config.GAN_metric)


class SampleFunc(object):
Example #2
0
    device = 'cuda'
else:
    device = 'cpu'
with open(args.filename, 'r') as file:
    try:
        config = yaml.safe_load(file)
    except yaml.YAMLError as exc:
        print(exc)

with torch.no_grad():
    model = vae_models[config['model_params']['name']](
        **config['model_params'])
    test = VAEXperiment(model, config['exp_params'])
    checkpoint = torch.load(args.ckpt,
                            map_location=lambda storage, loc: storage)
    test.load_state_dict(checkpoint['state_dict'])
    test = test.model
    if args.gpu:
        test = test.cuda()
    if args.eval:
        test.eval()
    if args.parallel:
        test = torch.nn.DataParallel(test)
        test = test.module

xsize = args.xsize
ysize = args.ysize
zsize = args.zsize
blocksize = args.blocksize
dim = args.dimension
error_bound = args.error
Example #3
0
experiment = VAEXperiment(model, config['exp_params'])

model_path = None
if config['model_params']['only_auxillary_training'] or config['model_params'][
        'memory_leak_training'] or resume:
    weights = [
        os.path.join(model_save_path, x) for x in os.listdir(model_save_path)
        if '.ckpt' in x
    ]
    weights.sort(key=lambda x: os.path.getmtime(x))
    if len(weights) > 0:
        model_path = weights[-1]
        print('loading: ', weights[-1])
        if config['model_params']['only_auxillary_training']:
            checkpoint = torch.load(model_path)
            experiment.load_state_dict(checkpoint['state_dict'])
            model_path = None
    # experiment = VAEXperiment.load_from_checkpoint(model_path, vae_model = model, params=config['exp_params'])

checkpoint_callback = ModelCheckpoint(model_save_path,
                                      verbose=True,
                                      save_last=True)
# monitor='loss',
# mode='min',)
# save_top_k=5,)

print(config['exp_params'],
      config['logging_params']['save_dir'] + config['logging_params']['name'])
runner = Trainer(
    checkpoint_callback=checkpoint_callback,
    resume_from_checkpoint=model_path,
Example #4
0
experiment = VAEXperiment(model, config['exp_params'])

# Han: above mostly unchanged, now substituting the trainer with influence analysis

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# currently not working for model loading because of some issues over pytorch_lightning's version?
if args.model_ckpt_dir:
    checkpoints = list(
        sorted(
            glob.glob(os.path.join(args.model_ckpt_dir, "*.ckpt"),
                      recursive=True)))
    #     trained_ckpt = pl_load(trained_ckpt_file, map_location=lambda storage, loc: storage)
    #     model.load_state_dict(trained_ckpt['state_dict'])
    trained_ckpt = torch.load(checkpoints[-1])
    experiment.load_state_dict(trained_ckpt['state_dict'])

model.to(device)

print('++++++++++ number of params +++++++++',
      sum(p.numel() for n, p in model.named_parameters()))

# prepare parameters
param_influence = []
frozen = []
# frozen = ['decoder']
for n, p in list(model.named_parameters()):
    if (not any(fr in n for fr in frozen)):
        param_influence.append(p)
    else:
        p.requires_grad = False