コード例 #1
0
def run():
    torch.multiprocessing.freeze_support()

    use_cuda=True
    image_nc=1
    epochs = 60
    batch_size = 128
    BOX_MIN = 0
    BOX_MAX = 1

    # Define what device we are using
    print("CUDA Available: ",torch.cuda.is_available())
    device = torch.device("cuda" if (use_cuda and torch.cuda.is_available()) else "cpu")

    pretrained_model = "./MNIST_target_model.pth"
    targeted_model = MNIST_target_net().to(device)
    targeted_model.load_state_dict(torch.load(pretrained_model))
    targeted_model.eval()
    model_num_labels = 10

    # MNIST train dataset and dataloader declaration
    mnist_dataset = torchvision.datasets.MNIST('./dataset', train=True, transform=transforms.ToTensor(), download=True)
    dataloader = DataLoader(mnist_dataset, batch_size=batch_size, shuffle=True, num_workers=1)
    advGAN = AdvGAN_Attack(device,
                            targeted_model,
                            model_num_labels,
                            image_nc,
                            BOX_MIN,
                            BOX_MAX)

    advGAN.train(dataloader, epochs)   
コード例 #2
0
    
    use_cuda=True
    image_nc=1
    batch_size = 128

    gen_input_nc = image_nc

    # Define what device we are using
    print("CUDA Available: ",torch.cuda.is_available())
    device = torch.device("cuda" if (use_cuda and torch.cuda.is_available()) else "cpu")

    # load the pretrained model
    pretrained_model = "./MNIST_target_model.pth"
    target_model = MNIST_target_net().to(device)
    target_model.load_state_dict(torch.load(pretrained_model))
    target_model.eval()

    # load the generator of adversarial examples
    pretrained_generator_path = './models/netG.pth.tar'
    pretrained_G = models.Generator(gen_input_nc, image_nc).to(device)
    pretrained_G.load_state_dict(torch.load(pretrained_generator_path))
    pretrained_G.eval()

    # test adversarial examples in MNIST training dataset
    mnist_dataset = torchvision.datasets.MNIST('./dataset', train=True, transform=transforms.ToTensor(), download=True)
    train_dataloader = DataLoader(mnist_dataset, batch_size=batch_size, shuffle=False, num_workers=1)
    num_correct = 0
    num_all = 0
    for i, data in enumerate(train_dataloader, 0):
        test_img, test_label = data
        test_img, test_label = test_img.to(device), test_label.to(device)
コード例 #3
0
def run():
    use_cuda = True
    image_nc = 1
    batch_size = 128

    gen_input_nc = image_nc

    # Define what device we are using
    print("CUDA Available: ", torch.cuda.is_available())
    device = torch.device("cuda" if (
        use_cuda and torch.cuda.is_available()) else "cpu")

    # load the pretrained model
    pretrained_model = "./MNIST_target_model_2.pth"
    target_model = MNIST_target_net().to(device)
    target_model.load_state_dict(torch.load(pretrained_model))
    target_model.eval()

    # load the generator of adversarial examples
    pretrained_generator_path = './models/netG_epoch_40.pth'
    pretrained_G = models.Generator(gen_input_nc, image_nc).to(device)
    pretrained_G.load_state_dict(torch.load(pretrained_generator_path))
    pretrained_G.eval()

    # test adversarial examples in MNIST training dataset
    mnist_dataset = torchvision.datasets.MNIST('./dataset',
                                               train=True,
                                               transform=transforms.ToTensor(),
                                               download=True)
    train_dataloader = DataLoader(mnist_dataset,
                                  batch_size=batch_size,
                                  shuffle=False,
                                  num_workers=1)
    num_correct = 0
    for i, data in enumerate(train_dataloader, 0):
        test_img, test_label = data
        test_img, test_label = test_img.to(device), test_label.to(device)
        perturbation = pretrained_G(test_img)
        perturbation = torch.clamp(perturbation, -0.3, 0.3)
        adv_img = perturbation + test_img
        adv_img = torch.clamp(adv_img, 0, 1)
        pred_lab = torch.argmax(target_model(adv_img), 1)
        num_correct += torch.sum(pred_lab == test_label, 0)

    print('MNIST training dataset:')
    print('num_correct: ', num_correct.item())
    print('accuracy of adv imgs in training set: %f\n' %
          (num_correct.item() / len(mnist_dataset)))

    # test adversarial examples in MNIST testing dataset
    mnist_dataset_test = torchvision.datasets.MNIST(
        './dataset',
        train=False,
        transform=transforms.ToTensor(),
        download=True)
    test_dataloader = DataLoader(mnist_dataset_test,
                                 batch_size=batch_size,
                                 shuffle=False,
                                 num_workers=1)
    num_correct = 0
    for i, data in enumerate(test_dataloader, 0):
        test_img, test_label = data
        test_img, test_label = test_img.to(device), test_label.to(device)
        perturbation = pretrained_G(test_img)
        perturbation = torch.clamp(perturbation, -0.3, 0.3)
        adv_img = perturbation + test_img
        adv_img = torch.clamp(adv_img, 0, 1)
        pred_lab = torch.argmax(target_model(adv_img), 1)
        num_correct += torch.sum(pred_lab == test_label, 0)

    print('num_correct: ', num_correct.item())
    print('accuracy of adv imgs in testing set: %f\n' %
          (num_correct.item() / len(mnist_dataset_test)))
コード例 #4
0
ファイル: main.py プロジェクト: dssrgu/atk_def_gan
    writer = None

if not os.path.exists(models_path):
    os.makedirs(models_path)
if not os.path.exists(models_path + model_name):
    os.makedirs(models_path + model_name)

# Define what device we are using print("CUDA Available: ",torch.cuda.is_available())
device = torch.device("cuda" if (
    use_cuda and torch.cuda.is_available()) else "cpu")

pretrained_model = "./MNIST_target_model.pth"
targeted_model = MNIST_target_net().to(device)
targeted_model.load_state_dict(
    torch.load(pretrained_model, map_location=device))
targeted_model.eval()
model_num_labels = 10

# MNIST train dataset and dataloader declaration
mnist_dataset = torchvision.datasets.MNIST('./dataset',
                                           train=True,
                                           transform=transforms.ToTensor(),
                                           download=True)
dataloader = DataLoader(mnist_dataset,
                        batch_size=batch_size,
                        shuffle=True,
                        num_workers=1)
advGAN = AdvGAN_Attack(device, targeted_model, model_num_labels, image_nc,
                       BOX_MIN, BOX_MAX, eps, pgd_iter, models_path, out_path,
                       model_name, writer, args.E_lr, args.defG_lr)
コード例 #5
0
ファイル: main.py プロジェクト: Scintillare/AIGan
    use_cuda=True
    image_nc=1
    epochs = 600
    batch_size = 128
    C_TRESH =  0.3
    BOX_MIN = 0
    BOX_MAX = 1

    # Define what device we are using
    print("CUDA Available: ",torch.cuda.is_available())
    device = torch.device("cuda" if (use_cuda and torch.cuda.is_available()) else "cpu")

    pretrained_model = "./MNIST_target_model.pth"
    model = MNIST_target_net().to(device)
    model.load_state_dict(torch.load(pretrained_model))
    model.eval()
    model_num_labels = 10
    stop_epoch = 10


    # MNIST train dataset and dataloader declaration
    # transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(0.5, 0.5) ]) # WARN don't do it
    transform = transforms.ToTensor()
    mnist_dataset = torchvision.datasets.MNIST('./dataset', train=True, transform=transform, download=True)
    dataloader = DataLoader(mnist_dataset, batch_size=batch_size, shuffle=True, num_workers=1, drop_last=True)

    # target = 4
    # labels = dataloader.dataset.targets
    # targets = torch.zeros_like(labels) + target 
    # dataloader.dataset.targets = targets
コード例 #6
0
use_cuda=True
image_nc=1
epochs = 100
batch_size = 128
BOX_MIN = 0
BOX_MAX = 1

# Define what device we are using
print("CUDA Available: ",torch.cuda.is_available())
device = torch.device("cuda" if (use_cuda and torch.cuda.is_available()) else "cpu")

pretrained_model1 = "./MNIST_target_model.pth"
pretrained_model2 = "./MNIST_target_modelA.pth"
targeted_model1 = MNIST_target_net().to(device)
targeted_model1.load_state_dict(torch.load(pretrained_model1))
targeted_model1.eval()
targeted_model2 = MNIST_target_netA().to(device)
targeted_model2.load_state_dict(torch.load(pretrained_model2))
targeted_model2.eval()


model_num_labels = 10

# MNIST train dataset and dataloader declaration
mnist_dataset = torchvision.datasets.MNIST('./dataset', train=True, transform=transforms.ToTensor(), download=True)
dataloader = DataLoader(mnist_dataset, batch_size=batch_size, shuffle=True, num_workers=1)

start_time = time.time()

advGAN = AdvGAN_Attack(device,
                          targeted_model1,