Ejemplo n.º 1
0
                        help='batch size, e.g. 16, 32, 64...', type=int)
    return parser.parse_args()


if __name__ == '__main__':
        args = parse_args()
        gpu_ids = args.gpu_id
        if isinstance(gpu_ids, int):

            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'
Ejemplo n.º 2
0
                               shuffle=True,
                               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')

model_file = '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')