Beispiel #1
0
def aggregate_stats(model_list,
                    device,
                    shape,
                    classes=10,
                    batches=20,
                    batch_size=100,
                    out_seeds=False):
    stats = []

    if out_seeds:
        if shape[0] == 1:
            dataset = 'MNIST'
        else:
            dataset = 'CIFAR10'
        out_loader = iter(dl.TinyImages(dataset, batch_size=batch_size))

    for _ in range(batches):

        if out_seeds:
            seed = next(out_loader)[0].to(device)
        else:
            seed = torch.rand((batch_size, ) + tuple(shape), device=device)

        batch_stats = []
        for i, model in enumerate(model_list):
            model.eval()
            batch_stats.append(
                model(seed).max(1)[0].exp().detach().cpu().clone())

        batch_stats = torch.stack(batch_stats, 0)
        stats.append(batch_stats.clone())

    stats = torch.cat(stats, -1)

    return stats
Beispiel #2
0
 def __init__(self, augm_flag=True, batch_size=128):
     self.data_name = 'CIFAR10'
     self.base_model = resnet.ResNet18()
     self.train_loader = dl.CIFAR10(train=True, batch_size=batch_size, augm_flag=augm_flag)
     self.cali_loader = dl.CIFAR10(train=True, batch_size=batch_size, augm_flag=False)
     self.test_loader = dl.CIFAR10(train=False)
     self.dim = 3072
     self.loaders = [('SVHN', dl.SVHN(train=False)), 
          ('CIFAR100', dl.CIFAR100(train=False)),
          ('LSUN_CR', dl.LSUN_CR(train=False)),
          ('Imagenet-',dl.ImageNetMinusCifar10(train=False)),
     #     ('TinyImages', dl.TinyImages(self.data_name, batch_size=batch_size, train=False)),
          ('Noise', dl.Noise(dataset='CIFAR10', batch_size=batch_size)),
          ('UniformNoise', dl.UniformNoise(dataset=self.data_name, batch_size=batch_size))]
     
     self.tinyimage_loader = dl.TinyImages(self.data_name, batch_size=100)
     
     self.data_used = 50000
     self.epsilon = 0.3
     self.lr = 0.1
     self.classes = 10
Beispiel #3
0
 def __init__(self, augm_flag=True, batch_size=128):
     self.data_name = 'FMNIST'
     
     self.base_model = resnet.ResNet18(num_of_channels=1)
     
     self.train_loader = dl.FMNIST(train=True, batch_size=batch_size, augm_flag=augm_flag)
     self.cali_loader = dl.FMNIST(train=True, batch_size=batch_size, augm_flag=False)
     self.test_loader = dl.FMNIST(train=False, augm_flag=False)
     self.dim = 784
     self.loaders = [('MNIST', dl.MNIST(train=False)), 
          ('EMNIST', dl.EMNIST(train=False)),
          ('GrayCIFAR10', dl.GrayCIFAR10(train=False)),
     #     ('TinyImages', dl.TinyImages(self.data_name, batch_size=batch_size, train=False)),
          ('Noise', dl.Noise(dataset=self.data_name, batch_size=batch_size)),
          ('UniformNoise', dl.UniformNoise(dataset=self.data_name, batch_size=batch_size))]
     
     self.tinyimage_loader = dl.TinyImages(self.data_name, batch_size=100)
     
     self.data_used = 60000
     self.epsilon = 0.3
     self.lr = 0.1
     self.classes = 10
Beispiel #4
0
def aggregate_adv_stats_out(model_list, gmm, gmm_out, device, shape, classes=10, 
                            batches=10, batch_size=100, steps=200, out_seeds=None,
                            restarts=10, alpha=1., lam=1.):
    
    pca = models.MyPCA(gmm.metric.comp_vecs.t(), gmm.metric.singular_values, shape)
    
    f = 1.1
    b = lam * (f-1.) / (classes-f)

    bounds = []
    stats = []
    samples = []
    seeds = []
    
    if out_seeds is not None:
        if type(out_seeds)!=torch.Tensor:
            if shape[0]==1:
                dataset = 'MNIST'
            else:
                dataset = 'CIFAR10'
            out_loader = iter(dl.TinyImages(dataset, batch_size=batch_size, train=False))

    for idx in range(batches):
        if out_seeds is not None:
            if type(out_seeds)!=torch.Tensor:
                seed = next(out_loader)[0].to(device)
            else:
                seed = out_seeds[idx].to(device)
        else:
            seed = torch.rand((batch_size,) + tuple(shape), device=device)
        batch_bounds = []
        batch_samples = []

        for x in seed:
            a = gmm_helpers.get_b_out(0., x, gmm, gmm_out, b)
            if a>=0:
                batch_bounds.append(0.)
            else:
                batch_bounds.append( scipy.optimize.brentq(gmm_helpers.get_b_out, 0, 
                                                       10000., args = (x, gmm, gmm_out, b),
                                                          maxiter=10000) )
        batch_bounds = torch.tensor(batch_bounds, device=device)
        bounds.append(batch_bounds.clone().cpu())

        batch_stats = []
        for i, model in enumerate(model_list):
            model.eval()
            adv_noise, _ = adv.gen_pca_noise(model, device, seed, pca, 
                                             epsilon=batch_bounds, perturb=True, 
                                             restarts=restarts, steps=steps, alpha=alpha)
            out = model(adv_noise).max(1)[0].detach().cpu().clone()
            
            batch_stats.append(out)
            batch_samples.append(adv_noise.detach().cpu())
            
        seeds.append(seed.cpu())
        
        batch_samples = torch.stack(batch_samples, 0)
        batch_stats = torch.stack(batch_stats, 0)
        stats.append(batch_stats.clone())
        samples.append(batch_samples.clone())

    seeds = torch.stack(seeds, 0)
    samples = torch.stack(samples, 0)
    stats = torch.cat(stats, -1)
    bounds = torch.cat(bounds, 0)
    
    return stats, bounds, seeds, samples
Beispiel #5
0
        'params': model.parameters(),
        'lr': lr,
        'weight_decay': hps.decay
    }]

if hps.dataset == 'MNIST':
    optimizer = optim.Adam(param_groups)
else:
    optimizer = optim.SGD(param_groups, momentum=0.9)

lam = model.loglam.data.exp().item() if hps.use_gmm else np.exp(hps.loglam)

writer = SummaryWriter('logs/' + saving_string + str(datetime.datetime.now()))

if hps.fit_out:
    noise_loader = iter(dl.TinyImages(hps.dataset, shuffle=False))
else:
    noise_loader = None

for epoch in range(hps.epochs):
    if epoch + 1 in [50, 75, 90]:
        for group in optimizer.param_groups:
            group['lr'] *= .1
        #  group['weight_decay'] *= .1
    if hps.train_type == 'ACET':
        if epoch < 3:
            lam = 0.001
        else:
            lam = model.loglam.data.exp().item() if hps.use_gmm else np.exp(
                hps.loglam)
Beispiel #6
0
        gmm.mu.data = ( (gmm.mu.data * metric.singular_values_sqrt[None,:] ) 
                       @ metric.comp_vecs.t().inverse() )

    saving_string = ('SavedModels/GMM/gmm_' + hps.dataset
                     +'_n' + str(n)
                     +'_data_used' + str(hps.data_used)
                     +'_augm_flag' + str(hps.augm_flag))

    if hps.PCA:
        saving_string += '_PCA'


    torch.save(gmm, saving_string + '.pth')


out_loader = dl.TinyImages(hps.dataset)

    
X = []
for idx, (x, f) in enumerate(out_loader):
    if idx>200:
        break;
    X.append(x.view(-1,dim))
X = torch.cat(X, 0)


if hps.PCA:
    X = ( ([email protected]_vecs.t()) / metric.singular_values_sqrt[None,:] ) 

    
for n in hps.n: