'CIFAR10 - LSUN': [], 'CIFAR10 - FarAway': [], 'CIFAR10 - Adversarial': [], 'CIFAR10 - FarAwayAdv': [] } tab_cal = {'DKL': ([], [])} delta = 2000 np.random.seed(args.randseed) torch.manual_seed(args.randseed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False model_dkl, likelihood = dkl.get_dkl_model(dataset='CIFAR10') model_dkl.cuda() likelihood.cuda() state = torch.load(f'./pretrained_models/CIFAR10_dkl.pt') model_dkl.load_state_dict(state['model']) likelihood.load_state_dict(state['likelihood']) model_dkl.eval() likelihood.eval() # In-distribution py_in, time_pred = timing( lambda: predict_dkl(test_loader, model_dkl, likelihood).cpu().numpy()) acc_in = np.mean(np.argmax(py_in, 1) == targets) conf_in = get_confidence(py_in)
'MNIST - FMNIST': [], 'MNIST - FarAway': [], 'MNIST - Adversarial': [], 'MNIST - FarAwayAdv': [] } tab_cal = {'DKL': ([], [])} delta = 2000 np.random.seed(args.randseed) torch.manual_seed(args.randseed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False model_dkl, likelihood = dkl.get_dkl_model(dataset='MNIST') model_dkl.cuda() likelihood.cuda() state = torch.load(f'./pretrained_models/MNIST_dkl.pt') model_dkl.load_state_dict(state['model']) likelihood.load_state_dict(state['likelihood']) model_dkl.eval() likelihood.eval() # In-distribution py_in, time_pred = timing( lambda: predict_dkl(test_loader, model_dkl, likelihood).cpu().numpy()) acc_in = np.mean(np.argmax(py_in, 1) == targets) conf_in = get_confidence(py_in)
'SVHN - LSUN': [], 'SVHN - FarAway': [], 'SVHN - Adversarial': [], 'SVHN - FarAwayAdv': [] } tab_cal = {'DKL': ([], [])} delta = 2000 np.random.seed(args.randseed) torch.manual_seed(args.randseed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False model_dkl, likelihood = dkl.get_dkl_model(dataset='SVHN') model_dkl.cuda() likelihood.cuda() state = torch.load(f'./pretrained_models/SVHN_dkl.pt') model_dkl.load_state_dict(state['model']) likelihood.load_state_dict(state['likelihood']) model_dkl.eval() likelihood.eval() # In-distribution py_in, time_pred = timing( lambda: predict_dkl(test_loader, model_dkl, likelihood).cpu().numpy()) acc_in = np.mean(np.argmax(py_in, 1) == targets) conf_in = get_confidence(py_in)
elif args.dataset == 'CIFAR10': train_loader = dl.CIFAR10(train=True) val_loader, test_loader = dl.CIFAR10(train=False, augm_flag=False, val_size=2000) elif args.dataset == 'SVHN': train_loader = dl.SVHN(train=True) val_loader, test_loader = dl.SVHN(train=False, augm_flag=False, val_size=2000) elif args.dataset == 'CIFAR100': train_loader = dl.CIFAR100(train=True) val_loader, test_loader = dl.CIFAR100(train=False, augm_flag=False, val_size=2000) targets = torch.cat([y for x, y in test_loader], dim=0).numpy() targets_val = torch.cat([y for x, y in val_loader], dim=0).numpy() print(len(train_loader.dataset), len(val_loader.dataset), len(test_loader.dataset)) model, likelihood = dkl.get_dkl_model(dataset=args.dataset) model = model.cuda() likelihood = likelihood.cuda() if args.dataset == 'MNIST': lr = 1e-3 opt = torch.optim.Adam([ {'params': model.feature_extractor.parameters(), 'weight_decay': 5e-4}, {'params': model.gp_layer.hyperparameters(), 'lr': lr}, {'params': model.gp_layer.variational_parameters()}, {'params': likelihood.parameters()}, ], lr=lr, weight_decay=0) else: lr = 0.1