Пример #1
0
parser.add_argument('--test_batch', type=int, default=10,
                    help='test batch number')
parser.add_argument('--model_dir', type=str,  help='model loading directory')


args = parser.parse_args()
np.random.seed(args.seed)
torch.manual_seed(args.seed)



if args.dataset == "MNIST":
    net = MNIST()
    net = torch.nn.DataParallel(net, device_ids=[0])
    load_model(net,'model/mnist_gpu.pt')
    train_loader, test_loader, train_dataset, test_dataset = load_mnist_data(args.test_batch_size)
elif args.dataset == 'CIFAR10':
    net = CIFAR10() 
    #net = VGG_plain('VGG16', 10, img_width=32)
    net = torch.nn.DataParallel(net, device_ids=[0])
    load_model(net,args.model_dir)

    #device = torch.device("cuda")
    #net = WideResNet().to(device)
    #load_model(net, 'model/cifar10_gpu.pt')

    train_loader, test_loader, train_dataset, test_dataset = load_cifar10_data(args.test_batch_size)
elif args.dataset == 'Imagenet':
    net = CIFAR10() 
    net = torch.nn.DataParallel(net, device_ids=[0])
    load_model(net, 'cifar10_gpu.pt')
Пример #2
0
def attack(algorithm,
           dataset,
           targeted,
           norm='l2',
           num=50,
           stopping_criteria=None,
           query_limit=40000,
           start_from=0,
           gpu=0):

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu)

    print("Attacking:".format(num))
    print("    Number of samples - {0}".format(num))
    print("    Dataset - {0}".format(dataset.upper()))
    print("    Targeted - {0}".format(targeted))
    print("    Norm - {0}".format(norm))
    print("    Query Limit - {0}".format(query_limit))
    print("    GPU - {0}".format(gpu))
    print()
    if stopping_criteria is not None:
        print("    Stopping criteria - {0}".format(stopping_criteria))
    if start_from > 0:
        print("    Start from {0}".format(start_from))

    if dataset == 'mnist':
        net = MNIST()
        net.cuda()
        net = torch.nn.DataParallel(net, device_ids=[0])
        load_model(net, '../mnist_gpu.pt')
        train_loader, test_loader, train_dataset, test_dataset = load_mnist_data(
        )
    elif dataset == 'cifar':
        net = CIFAR10()
        net.cuda()
        net = torch.nn.DataParallel(net, device_ids=[0])
        load_model(net, '../cifar10_gpu.pt')
        train_loader, test_loader, train_dataset, test_dataset = load_cifar10_data(
        )
    elif dataset == 'imagenet':
        net = models.__dict__["resnet50"](pretrained=True)
        net.cuda()
        net = torch.nn.DataParallel(net, device_ids=[0])
        train_loader, test_loader, train_dataset, test_dataset = load_imagenet_data(
        )
    else:
        print("Invalid dataset")
        return

    net.eval()
    model = net.module if torch.cuda.is_available() else net
    #amodel = PytorchModel(model, bounds=[0,1], num_classes=10)

    fmodel = foolbox.models.PyTorchModel(model, bounds=[0, 1], num_classes=10)
    #if targeted:
    #    criterion = TargetClass(target)
    #    attack = foolbox.attacks.BoundaryAttack(fmodel,criterion)
    #else:
    #    attack = foolbox.attacks.BoundaryAttack(fmodel)

    print("using BoundaryAttack from foolbox")
    #print("Invalid algorithm")

    np.random.seed(0)
    seeds = np.random.randint(10000, size=[2 * num])
    count = 0
    for i, (xi, yi) in enumerate(test_loader):
        if i < start_from:
            continue
        if count == num:
            break
        image, label = xi[0].numpy(), yi.item()
        seed_index = i - start_from
        np.random.seed(seeds[seed_index])
        target = np.random.randint(10) * torch.ones(
            1, dtype=torch.long).cuda() if targeted else None
        print("Attacking Source: {0} Target: {1} Seed: {2} Number {3}".format(
            yi.item(), target, seeds[seed_index], i))

        if targeted:
            target = target.item()
            criterion = TargetClass(target)
            attack = foolbox.attacks.BoundaryAttack(fmodel, criterion)
            for i, (xi, yi) in enumerate(train_loader):
                if i == 1:
                    break
                #print(yi, target)
                index = (yi == target).nonzero()
                image_t = xi[index[0]][0]
            image_t = image_t.numpy()
            adv = attack(image,
                         label,
                         iterations=6000,
                         verbose=False,
                         unpack=False,
                         log_every_n_steps=100,
                         starting_point=image_t)
        else:
            attack = foolbox.attacks.BoundaryAttack(fmodel)
            adv = attack(image,
                         label,
                         iterations=6000,
                         verbose=False,
                         unpack=False,
                         log_every_n_steps=100)
        if adv.image is None:
            continue
        dis = LA.norm(adv.image - image)
        #print("Norm L2 and queries of image {} is {} and {}".format(dis, adv._total_prediction_calls))
        print(
            "adversarial Example Found Successfully: distortion {} target {} queries {}"
            .format(dis, np.argmax(fmodel.predictions(adv.image)),
                    adv._total_prediction_calls))
        #adv, dist = attack(xi.cuda(), yi.cuda(), target=target,
        #                   seed=seeds[seed_index], query_limit=query_limit)
        #if dist > 1e-8 and dist != float('inf'):
        #    count += 1
        print()
Пример #3
0
                lbd_hi = lbd_mid
            else:
                lbd_lo = lbd_mid
        return lbd_hi, nquery


    def __call__(self, input_xi, label_or_target, TARGETED=False):
        if TARGETED:
            print("Not Implemented.")
        else:
            adv = self.genattack_untargeted(input_xi, label_or_target)
        return adv   
    
        
if __name__ == '__main__':
    #timestart = time.time()
    random.seed(0)
    net = MNIST()
    net.cuda()
    net = torch.nn.DataParallel(net, device_ids=[0])
    load_model(net,'mnist_gpu.pt')
    net.eval()
    model = net.module if torch.cuda.is_available() else net
    amodel = PytorchModel(model, bounds=[0,1], num_classes=10)
    attack = OPT_genattack(amodel)
    train_loader, test_loader, train_dataset, test_dataset = load_mnist_data()
    for i, (xi,yi) in enumerate(test_loader):
        if i==1:
            break
    xi = xi.numpy()
    attack(xi,yi)
Пример #4
0
def attack(algorithm,
           dataset,
           targeted,
           norm='l2',
           num=50,
           stopping_criteria=None,
           query_limit=40000,
           start_from=0,
           gpu=0):

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu)

    print("Attacking:".format(num))
    print("    Number of samples - {0}".format(num))
    print("    Dataset - {0}".format(dataset.upper()))
    print("    Targeted - {0}".format(targeted))
    print("    Norm - {0}".format(norm))
    print("    Query Limit - {0}".format(query_limit))
    print("    GPU - {0}".format(gpu))
    print()
    if stopping_criteria is not None:
        print("    Stopping criteria - {0}".format(stopping_criteria))
    if start_from > 0:
        print("    Start from {0}".format(start_from))

    if dataset == 'mnist':
        net = MNIST()
        net.cuda()
        net = torch.nn.DataParallel(net, device_ids=[0])
        load_model(net, 'mnist_gpu.pt')
        train_loader, test_loader, train_dataset, test_dataset = load_mnist_data(
        )
    elif dataset == 'cifar':
        net = CIFAR10()
        net.cuda()
        net = torch.nn.DataParallel(net, device_ids=[0])
        load_model(net, 'cifar10_gpu.pt')
        train_loader, test_loader, train_dataset, test_dataset = load_cifar10_data(
        )
    elif dataset == 'imagenet':
        net = models.__dict__["resnet50"](pretrained=True)
        net.cuda()
        net = torch.nn.DataParallel(net, device_ids=[0])
        train_loader, test_loader, train_dataset, test_dataset = load_imagenet_data(
        )
    else:
        print("Invalid dataset")
        return

    net.eval()
    model = net.module if torch.cuda.is_available() else net
    amodel = PytorchModel(model, bounds=[0, 1], num_classes=10)

    attack_type = None
    if algorithm == 'opt':
        if norm == 'l2':
            attack_type = OPT_attack
        elif norm == 'linf':
            attack_type = OPT_attack_lf
    elif algorithm == 'sign_sgd':
        if norm == 'l2':
            attack_type = OPT_attack_sign_SGD
        elif norm == 'linf':
            attack_type = OPT_attack_sign_SGD_lf
    else:
        print("Invalid algorithm")

    if attack_type is None:
        print("Invalid norm")

    if targeted:
        attack = attack_type(amodel, train_dataset=train_dataset)
    else:
        attack = attack_type(amodel)

    np.random.seed(0)
    seeds = np.random.randint(10000, size=[2 * num])
    count = 0
    for i, (xi, yi) in enumerate(test_loader):
        if i < start_from:
            continue
        if count == num:
            break

        seed_index = i - start_from
        np.random.seed(seeds[seed_index])
        target = np.random.randint(10) * torch.ones(
            1, dtype=torch.long).cuda() if targeted else None
        print("Attacking Source: {0} Target: {1} Seed: {2} Number {3}".format(
            yi.item(), target, seeds[seed_index], i))
        adv, dist = attack(xi.cuda(),
                           yi.cuda(),
                           target=target,
                           seed=seeds[seed_index],
                           query_limit=query_limit)
        if dist > 1e-8 and dist != float('inf'):
            count += 1
        print()