コード例 #1
0
            gpu_ids = [gpu_ids]
        batch_size = args.batch_size
        target_model = args.target_model
        #inputDir = args.input_dir
        outputFile = args.output_file
    ################## Defense #######################
        m = wide_resnet(num_classes=10, depth=28, widen_factor=10,
                            dropRate=0.3)  # densenet_cifar(num_classes=110)##densenet121(num_classes=110)#wide_resnet(num_classes=110, depth=28, widen_factor=10, dropRate=0.3) ######
        m2 = wide_resnet2(num_classes=10, depth=28, widen_factor=10,
                        dropRate=0.3)  # densenet_cifar(num_classes=110)##densenet121(num_classes=110)#wide_resnet(num_classes=110, depth=28, widen_factor=10, dropRate=0.3) ######

        # Loading data for ...densenet161(num_classes=110)
        image_mean = torch.tensor([0.491, 0.482, 0.447]).view(1, 3, 1, 1)
        image_std = torch.tensor([0.247, 0.243, 0.262]).view(1, 3, 1, 1)
        device = torch.device('cuda:0' if (torch.cuda.is_available()) else 'cpu')  # torch.device('cpu')
        model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(device)
        model2 = NormalizedModel(model=m2, mean=image_mean, std=image_std).to(device)
        #model = model.to(device)
        print('loading data for defense using %s ....' % target_model)

        test_loader = load_data_for_defense()['dev_data']
        weight_norm = '/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2Norm_cifar10_ep_184_val_acc0.9515.pth'
        weight_AT = '/media/unknown/Data/PLP/fast_adv/defenses/weights/cifar10_AT/cifar10acc0.8709999859333039_45.pth'
        weight_ALP = '/media/unknown/Data/PLP/fast_adv/defenses/weights/AT+ALP/cifar10acc0.8699999809265136_50.pth'

        weight_conv_mixatten = '/media/unknown/Data/PLP/fast_adv/defenses/weights/cifar10_mixed_Attention/cifar10acc0.8759999752044678_100.pth'
        weight_025conv_mixatten='/media/unknown/Data/PLP/fast_adv/defenses/weights/best/0.25MixedAttention_mixed_attention_cifar10_ep_50_val_acc0.8720.pth'
        weight_05conv_mixatten = '/media/unknown/Data/PLP/fast_adv/defenses/weights/shape_0.5_cifar10_mixed_Attention/cifar10acc0.8434999763965607_130.pth'
        weight_1conv_mixatten = '/media/unknown/Data/PLP/fast_adv/defenses/weights/best/1MixedAttention_mixed_attention_cifar10_ep_25_val_acc0.7080.pth'

        weight_shape_alp='/media/unknown/Data/PLP/fast_adv/defenses/weights/best/shape_ALP_cifar10_ep_79_val_acc0.7625.pth'
コード例 #2
0
                               num_workers=args.workers,
                               drop_last=True,
                               pin_memory=True)
val_loader = data.DataLoader(val_set,
                             batch_size=100,
                             shuffle=False,
                             num_workers=args.workers,
                             pin_memory=True)
test_loader = data.DataLoader(test_set,
                              batch_size=100,
                              shuffle=False,
                              num_workers=args.workers,
                              pin_memory=True)

m = wide_resnet(num_classes=10, depth=28, widen_factor=10, dropRate=args.drop)
model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(
    DEVICE)  # keep images in the [0, 1] range
#model_dict = torch.load('/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2_1Attention_cifar10_ep_33_val_acc0.8890.pth')
weight_025conv_mixatten = '/media/unknown/Data/PLP/fast_adv/defenses/weights/best/0.25MixedAttention_mixed_attention_cifar10_ep_50_val_acc0.8720.pth'
'''
model_file=weight_025conv_mixatten
#'/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2AT_cifar10_ep_13_val_acc0.8770.pth'
    #'/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2_1Attention_cifar10_ep_33_val_acc0.8890.pth'
    #'/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2_2AT_cifar10_ep_29_val_acc0.8870.pth'
model_dict=model.state_dict()
save_model = torch.load(model_file,map_location=lambda storage, loc: storage)
state_dict = {k:v for k,v in save_model.items() if k in model_dict.keys()}
#print(state_dict.keys())  # dict_keys(['w', 'conv1.weight', 'conv1.bias', 'conv2.weight', 'conv2.bias'])
model_dict.update(state_dict)
model.load_state_dict(model_dict)
'''
#model_dict = torch.load('/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2_2AT_cifar10_ep_29_val_acc0.8870.pth')
コード例 #3
0
             'BinarizationRefinementAttack': BinarizationRefinementAttack,
             'ContrastReductionAttack':ContrastReductionAttack,
             'SaltAndPepperNoiseAttack':SaltAndPepperNoiseAttack,
             'SpatialAttack': SpatialAttack}
image_mean = torch.tensor([0.491, 0.482, 0.447]).view(1, 3, 1, 1)
image_std = torch.tensor([0.247, 0.243, 0.262]).view(1, 3, 1, 1)
DEVICE = torch.device( 'cpu')
test_transform = transforms.Compose([
    transforms.ToTensor(),
])
#val_set = data.Subset(CIFAR10(input, train=True, transform=test_transform, download=True),list(range(0,30000)))
#train_set = data.Subset(CIFAR10(input, train=True, transform=test_transform, download=True), list(range(48000, 50000)))#91.9

val_loader = data.DataLoader(train_set, batch_size=args.batch_size, shuffle=False, num_workers=0, pin_memory=True)
m = wide_resnet(num_classes=10, depth=28, widen_factor=10, dropRate=0.3)
model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(DEVICE)  # keep images in the [0, 1] range
model_dict = torch.load('/media/unknown/Data/PLP/fast_adv/defenses/weights/best/cifar10_80.pth')
model.load_state_dict(model_dict)
'''
classification = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
model2=NormalizedModel(model=m, mean=image_mean, std=image_std).to(DEVICE)  # keep images in the [0, 1] range
model_dict2 = torch.load('../defenses/weights/AT_cifar10_clean0.879_adv.pth')
model2.load_state_dict(model_dict2)
model3=NormalizedModel(model=m, mean=image_mean, std=image_std).to(DEVICE)  # keep images in the [0, 1] range
model_dict3 = torch.load('../defenses/weights/best/ALP_cifar10_ep_39_val_acc0.8592.pth')
model3.load_state_dict(model_dict3)
model4=NormalizedModel(model=m, mean=image_mean, std=image_std).to(DEVICE)  # keep images in the [0, 1] range
model_dict4 = torch.load('../defenses/weights/best/PLP1_cifar10_ep_29_val_acc0.8636.pth')
model4.load_state_dict(model_dict4)
'''
コード例 #4
0
ファイル: cifar10_aad.py プロジェクト: a-zuoxi/tianchi_f
                               num_workers=args.workers,
                               drop_last=True,
                               pin_memory=True)
val_loader = data.DataLoader(val_set,
                             batch_size=100,
                             shuffle=False,
                             num_workers=args.workers,
                             pin_memory=True)
test_loader = data.DataLoader(test_set,
                              batch_size=100,
                              shuffle=False,
                              num_workers=args.workers,
                              pin_memory=True)

m = wide_resnet(num_classes=10, depth=28, widen_factor=10, dropRate=args.drop)
model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(
    DEVICE)  # keep images in the [0, 1] range

weight_AT = './weights/best/2AT_cifar10_ep_13_val_acc0.8770.pth'
# model_file='/home/frankfeng/projects/researchData/AI_security/code/PLP/fast_adv/defenses/weights/cifar10_base/cifar10_valacc0.8339999794960022.pth'
model_dict = torch.load(weight_AT)
model.load_state_dict(model_dict)
if torch.cuda.device_count() > 1:
    model = torch.nn.DataParallel(model)

optimizer = SGD(model.parameters(),
                lr=args.lr,
                momentum=args.momentum,
                weight_decay=args.weight_decay)
if args.adv == 0:
    scheduler = lr_scheduler.StepLR(optimizer,
                                    step_size=args.lr_step,
コード例 #5
0
ファイル: cifar10_wsdan.py プロジェクト: a-zuoxi/tianchi_f
                             num_workers=args.workers,
                             pin_memory=True)
test_loader = data.DataLoader(test_set,
                              batch_size=100,
                              shuffle=False,
                              num_workers=args.workers,
                              pin_memory=True)

print(len(train_set), len(val_set), len(test_set))
print(len(train_loader), len(val_loader), len(test_loader))

m = WSDAN(num_classes=10,
          M=args.num_attentions,
          net=args.backbone_net,
          pretrained=True)
model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(
    DEVICE)  # keep images in the [0, 1] range
model_file = './weights/best/2AT_cifar10_ep_13_val_acc0.8770.pth'
weight_wsdan3 = './weights/cifar10_wsgan_3/cifar10_valacc0.pth'
weight_wsdan_final = './weights/cifar10_wsdan_final/cifar10_valacc0.871999979019165.pth'
weight_wsdan_best = './weights/cifar10_WSDAN_best/cifar10_valacc0.8784999758005142.pth'
weight_wsdan_best = "./weights/cifar10_WSDAN_best/cifar10_0.87_low.pth"
model_dict = torch.load(weight_wsdan_best)
model.load_state_dict(model_dict)

# finetuning cifar10_base weight
# # model_dic = model.state_dict()
# # print(model.state_dict())
# for k in model_dic.keys():
#     print(k, type(model_dic[k]), model_dic[k].shape)
# print("##########################################")
# sta_dic = torch.load(weight_wsdan)
コード例 #6
0
    outputFile = args.output_file
    ################## Defense #######################
    m = wide_resnet(
        num_classes=10, depth=28, widen_factor=10, dropRate=0.3
    )  # densenet_cifar(num_classes=110)##densenet121(num_classes=110)#wide_resnet(num_classes=110, depth=28, widen_factor=10, dropRate=0.3) ######
    # Loading data for ...densenet161(num_classes=110)
    image_mean = torch.tensor([0.491, 0.482, 0.447]).view(1, 3, 1, 1)
    image_std = torch.tensor([0.247, 0.243, 0.262]).view(1, 3, 1, 1)
    device = torch.device(
        'cuda:0' if
        (torch.cuda.is_available()) else 'cpu')  # torch.device('cpu')
    #pth_file ='/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2_1Attention_cifar10_ep_33_val_acc0.8890.pth'
    pth_file = '/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2_mixed_attention_cifar10_ep_12_val_acc0.8895.pth'
    #pth_file = '/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2_2AT_cifar10_ep_29_val_acc0.8870.pth'

    model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(device)
    #model = model.to(device)
    print('loading data for defense using %s ....' % target_model)

    test_loader = load_data_for_defense()['dev_data']

    # pth_file = os.path.join(weights_path, 'cifar10acc0.9090267625855811_40.pth')#18.24944_17.2555_ep_31__acc0.7470.pthep_36_val_acc0.9844.pthep_36_val_acc0.9844.pth#glob.glob(os.path.join(weights_path, 'cifar10_20_0.73636.pth'))#[0]
    print('loading weights from : ', pth_file)
    #model_dict = torch.load('jpeg_weight/18.5459_0.9_jpeg_WRN_DDNacc0.9553740539334037_20_0.60.pth')
    #model_dict = torch.load('jpeg_weight/ALP_smooth_p_44_val_acc0.9409.pth')
    #model_dict = torch.load('jpeg_weight/JPEG_ALP_smooth_acc0.9401_all_0.786.pth')
    model_dict = torch.load(pth_file)
    model.load_state_dict(model_dict, False)
    model.eval()
    test_accs = AverageMeter()
    test_losses = AverageMeter()
コード例 #7
0
                               num_workers=args.workers,
                               drop_last=True,
                               pin_memory=True)
val_loader = data.DataLoader(val_set,
                             batch_size=100,
                             shuffle=True,
                             num_workers=args.workers,
                             pin_memory=True)
test_loader = data.DataLoader(test_set,
                              batch_size=100,
                              shuffle=True,
                              num_workers=args.workers,
                              pin_memory=True)

m = wide_resnet(num_classes=10, depth=28, widen_factor=10, dropRate=args.drop)
model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(
    DEVICE)  # keep images in the [0, 1] range
if torch.cuda.device_count() > 1:
    model = torch.nn.DataParallel(model)

optimizer = SGD(model.parameters(),
                lr=args.lr,
                momentum=args.momentum,
                weight_decay=args.weight_decay)
if args.adv == 0:
    scheduler = lr_scheduler.StepLR(optimizer,
                                    step_size=args.lr_step,
                                    gamma=args.lr_decay)
else:
    scheduler = lr_scheduler.MultiStepLR(optimizer,
                                         milestones=[60, 120, 160],
                                         gamma=0.2)
コード例 #8
0
                             num_workers=args.workers,
                             pin_memory=True)
test_loader = data.DataLoader(test_set,
                              batch_size=100,
                              shuffle=False,
                              num_workers=args.workers,
                              pin_memory=True)

print(len(train_set), len(val_set), len(test_set))
print(len(train_loader), len(val_loader), len(test_loader))

m = WSDAN(num_classes=10,
          M=args.num_attentions,
          net=args.backbone_net,
          pretrained=True)
model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(
    DEVICE)  # keep images in the [0, 1] range
# model_file = './weights/best/2AT_cifar10_ep_13_val_acc0.8770.pth'
# if len(model_file) > 0:
#     pretext_model = torch.load(model_file)
#     model_dict = model.state_dict()
#     model_new_dict = {}
#     for k, v in pretext_model.items():
#         k = k.replace("model.conv1.weight", "model.features.0.weight")
#         k = k.replace("model.block1","model.features.1")
#         k = k.replace("model.block2", "model.features.2")
#         k = k.replace("model.block3", "model.features.3")
#         k = k.replace("model.bn1", "model.features.4")
#         if k == "model.fc.weight":
#             continue
#         model_new_dict[k] = v
#     state_dict = {k: v for k, v in model_new_dict.items() if k in model_dict.keys()}
    transforms.ToTensor(),
])

train_set = data.Subset(CIFAR10(args.data, train=True, transform=train_transform, download=True), list(range(45000)))
val_set = data.Subset(CIFAR10(args.data, train=True, transform=test_transform, download=True),
                      list(range(45000, 50000)))
test_set = CIFAR10(args.data, train=False, transform=test_transform, download=True)

train_loader = data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, num_workers=args.workers,
                               drop_last=True, pin_memory=True)
val_loader = data.DataLoader(val_set, batch_size=100, shuffle=True, num_workers=args.workers, pin_memory=True)
test_loader = data.DataLoader(test_set, batch_size=100, shuffle=True, num_workers=args.workers, pin_memory=True)

# m = wide_resnet(num_classes=10, depth=28, widen_factor=10, dropRate=args.drop)
m = resnet18(pretrained=False)
model = NormalizedModel(model=m, mean=image_mean, std=image_std).to(DEVICE)  # keep images in the [0, 1] range
# if torch.cuda.device_count() > 1:
#     model = torch.nn.DataParallel(model)

optimizer = SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
if args.adv == 0:
    scheduler = lr_scheduler.StepLR(optimizer, step_size=args.lr_step, gamma=args.lr_decay)
else:
    scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[60, 120, 160], gamma=0.2)

attacker = DDN(steps=args.steps, device=DEVICE)

max_loss = torch.log(torch.tensor(10.)).item()  # for callback
best_acc = 0
best_epoch = 0