Beispiel #1
0
def adv_attack(args, path):
    if args.hyper:
        paths = glob(path + '/*.pt')
        path = [x for x in paths if 'hypermnist_mean50_0.9866.pt' in x][0]
        path = './hypermnist_0_0.984871875.pt'
        #path = './hypermnist_disc_0.97311875.pt'
        #path = './hypermnist_disc_0.97581875.pt'
        path = './hypermnist_disc_0.8857125.pt'
        hypernet = utils.load_hypernet(path)
        run_adv_hyper(args, hypernet)
    else:
        paths = ['mnist_model_small2_0.pt',
                 'mnist_model_small2_1.pt',
                 'mnist_model_small2_2.pt',
                 'mnist_model_small2_3.pt',
                 'mnist_model_small2_4.pt',
                 'mnist_model_small2_5.pt',
                 'mnist_model_small2_6.pt',
                 'mnist_model_small2_7.pt',
                 'mnist_model_small2_8.pt',
                 'mnist_model_small2_9.pt'
                 ]
        models = []
        for path in paths:
            model = get_network(args)
            model.load_state_dict(torch.load(path))
            models.append(model.eval())
        run_adv_model(args, models)
Beispiel #2
0
def adv_attack(args, path):
    if args.hyper:
        paths = glob(path + '/*.pt')
        path = [x for x in paths if 'hypermnist_0_0.984390625.pt' in x][0]
        hypernet = utils.load_hypernet(path)
        run_adv_hyper(args, hypernet)
    else:
        paths = ['mnist_model_small2_0.pt',
                'mnist_model_small2_1.pt',
                'mnist_model_small2_2.pt',
                'mnist_model_small2_3.pt',
                'mnist_model_small2_4.pt',]
        models = []
        for path in paths:
            model = get_network(args)
            model.load_state_dict(torch.load(path))
            models.append(model.eval())
        run_adv_model(args, models)
def run_anomaly(args, path):
    if args.hyper:
        paths = glob(path + '/*.pt')
        path = [x for x in paths if 'hypermnist_0_0.984390625.pt' in x][0]
        hypernet = utils.load_hypernet(path)
        if args.dataset == 'omniglot':
            run_anomaly_omni(args, hypernet)
        elif args.dataset == 'notmnist':
            run_anomaly_notmnist(args, hypernet)
        elif args.dataset == 'mnist':
            run_anomaly_mnist(args, hypernet)
        else:
            raise NotImplementedError
    else:
        model = get_network(args)
        path = 'mnist_clf.pt'
        model.load_state_dict(torch.load(path))
        run_adv_model(args, model)
Beispiel #4
0
def run_measure(args, path):
    paths = glob(path + '/*.pt')
    path = './hypermnist_0_0.984871875.pt'
    #path = './hypermnist_disc_0.97311875.pt'
    #path = './hypermnist_disc_0.97581875.pt'
    path = './hypermnist_disc_0.8857125.pt'
    hypernet = utils.load_hypernet(path)
    mpaths = [
        'mnist_model_small2_0.pt', 'mnist_model_small2_1.pt',
        'mnist_model_small2_2.pt', 'mnist_model_small2_3.pt',
        'mnist_model_small2_4.pt', 'mnist_model_small2_5.pt',
        'mnist_model_small2_6.pt', 'mnist_model_small2_7.pt',
        'mnist_model_small2_8.pt', 'mnist_model_small2_9.pt'
    ]
    models = []
    for path in mpaths:
        model = get_network(args)
        models.append(torch.load(path))
    measure_models(args, hypernet, models)
Beispiel #5
0
def load_models(args, path):

    model = get_network(args)
    paths = glob(path + '*.pt')
    print(path)
    paths = [path for path in paths if 'mnist' in path]
    natpaths = natsort.natsorted(paths)
    accs = []
    losses = []
    natpaths = [x for x in natpaths if 'hypermnist_mi_0.987465625' in x]
    for i, path in enumerate(natpaths):
        print("loading model {}".format(path))
        if args.hyper:
            hn = utils.load_hypernet(path)
            for i in range(10):
                samples = utils.sample_hypernet(hn)
                print('sampled a batches of {} networks'.format(len(
                    samples[0])))
                for i, sample in enumerate(
                        zip(samples[0], samples[1], samples[2])):
                    model = utils.weights_to_clf(sample, model,
                                                 args.stat['layer_names'])
                    acc, loss = test(args, model)
                    print(i, ': Test Acc: {}, Loss: {}'.format(acc, loss))
                    accs.append(acc)
                    losses.append(loss)
                    #acc, loss = train(args, model)
                    #print ('Test1 Acc: {}, Loss: {}'.format(acc, loss))
                    #extract_weights_all(args, model, i)
            print(accs, losses)
        else:
            ckpt = torch.load(path)
            state = ckpt['state_dict']
            try:
                model.load_state_dict()
            except RuntimeError:
                model_dict = model.state_dict()
                filtered = {k: v for k, v in state.items() if k in model_dict}
                model_dict.update(filtered)
                model.load_state_dict(filtered)
Beispiel #6
0
def run_acc(args, path):
    path = './exp_models/hypermnist_acc_2_0.988115.pt'
    hypernet = utils.load_hypernet(path)
    model = get_network(args)
    measure_acc(args, hypernet, model)
Beispiel #7
0
from foolbox.criteria import Misclassification
foolbox_logger = logging.getLogger('foolbox')
logging.disable(logging.CRITICAL)
import warnings
warnings.filterwarnings("ignore")
get_ipython().run_line_magic('load_ext', 'autoreload')
get_ipython().run_line_magic('autoreload', '2')
import torch
import matplotlib.pyplot as plt
import adversarial_test as adv
import adversarial_test as adv
args = adv.load_args()
arch = adv.get_network(args)
get_ipython().run_line_magic('clear', '')
import utils
hypernet = utils.load_hypernet('hypermnist_0_0.984390625.pt')
import netdef
args.stat = netdef.nets()[args.net]
model_base, fmodel_base = adv.sample_fmodel(args, hypernet, arch)
criterion = Misclassification()
fgs = foolbox.attacks.BIM(fmodel_base, criterion)
import datagen
_, test = datagen.load_mnist(args)
batch = []
y = []
for (data, target) in test:
    batch.append(data)
    y.append(target)
    if len(batch) == 32:
        break