def predict(params):

    inverter = PrectNormalizer(xr.open_dataset(params['norm_fn']),
                               params['output_vars'],
                               params['input_transform'][0],
                               params['input_transform'][1],
                               params['var_cut_off'], params['model_type'])

    model = CVAE(params)
    optimizer = optim.Adam(model.parameters(), lr=0.00001, weight_decay=0.001)

    ### Load model
    checkpoint = torch.load('./runs/VAE_model_DNN_classifier_exp_VAE_Exp04.pt')
    model.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    loss = checkpoint['loss']

    model.cuda()
    model.eval()

    valid_dataset = spcamDataset(params, phase="validation")
    valid_loader = DataLoader(valid_dataset,
                              sampler=SubsetSampler(valid_dataset.indices))

    result_predicted, result_actual = [], []
    with torch.no_grad():
        for batch_idx, (data, target) in enumerate(valid_loader):

            target = target.squeeze(0).type(torch.float32).to(params['device'])

            z = torch.randn(data.shape[1], 16)

            z = torch.cat((z, data.squeeze(0)), dim=1).cuda()

            predPrecit = model.decoder(z)

            print("Batch MSE {}".format(
                metrics.mean_squared_error(
                    predPrecit.detach().cpu().numpy(),
                    target.squeeze(0).detach().cpu().numpy())))

            #val_loss = compute_loss(target, sampled_precit, mean, log_var) #.type(torch.FloatTensor).to(params['device']))
            #assert val_loss.requires_grad == False

            result_predicted.extend(predPrecit.cpu().detach().numpy())
            result_actual.extend(target.squeeze(0).cpu().detach().numpy())

    mse = metrics.mean_squared_error(np.array(result_actual),
                                     np.array(result_predicted))

    print("MSE {}".format(mse))
Exemple #2
0
    testDataset = CELEBA(root=opts.root,
                         train=False,
                         transform=transforms.ToTensor())
    testLoader = torch.utils.data.DataLoader(testDataset,
                                             batch_size=opts.batchSize,
                                             shuffle=False)
    print 'Data loaders ready.'

    ####### Create model #######
    cvae = CVAE(nz=opts.nz, imSize=64, fSize=opts.fSize)
    dis = DISCRIMINATOR(imSize=64, fSize=opts.fSize)

    if cvae.useCUDA:
        print 'using CUDA'
        cvae.cuda()
        dis.cuda()
    else:
        print '\n *** NOT USING CUDA ***\n'

    print cvae
    print dis

    ####### Define optimizer #######
    optimizerCVAE = optim.RMSprop(
        cvae.parameters(),
        lr=opts.lr)  #specify the params that are being upated
    optimizerDIS = optim.RMSprop(dis.parameters(), lr=opts.lr, alpha=opts.mom)

    ####### Create a new folder to save results and model info #######
    exDir = make_new_folder(opts.outDir)