Example #1
0
        CALLBACK.scalar('Val_Acc', epoch + 1, val_accs.last_avg)

    print('Epoch {} | Validation | Loss: {:.4f}, Accs: {:.4f}'.format(
        epoch, val_losses.avg, val_accs.avg))

    save_path = 'weights/best/'
    if val_accs.avg >= best_acc:  # args.adv is None and
        best_acc = val_accs.avg
        best_epoch = epoch
        best_dict = deepcopy(model.state_dict())
        files2remove = glob.glob(os.path.join(save_path, 'shape_ALP_*'))
        for _i in files2remove:
            os.remove(_i)
        strsave = "shape_ALP_cifar10_ep_%d_val_acc%.4f.pth" % (epoch, best_acc)
        torch.save(model.cpu().state_dict(), os.path.join(save_path, strsave))
        model.to(DEVICE)

    if args.adv is None and val_accs.avg >= best_acc:
        best_acc = val_accs.avg
        best_epoch = epoch
        best_dict = deepcopy(model.state_dict())

    if not (epoch + 1) % args.save_freq:
        save_checkpoint(model.state_dict(),
                        os.path.join(
                            args.save_folder, args.save_name +
                            'acc{}_{}.pth'.format(val_accs.avg, (epoch + 1))),
                        cpu=True)

if args.adv is None:
    model.load_state_dict(best_dict)
Example #2
0
    ################## 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) ######
    #
    m = WSDAN(num_classes=10, M=32, net='wide_resnet', pretrained=True)
    # device = torch.device('cuda:0' if (torch.cuda.is_available()) else 'cpu')
    # model = model.to(device)

    # 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)
    model = model.to(device)
    print('loading data for defense using %s ....' % target_model)

    # weight_norm = '/media/unknown/Data/PLP/fast_adv/defenses/weights/best/2Norm_cifar10_ep_184_val_acc0.9515.pth'
    # weight_AT = './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'
    # weight_attention = '/media/unknown/Data/PLP/fast_adv/defenses/weights/cifar10_Attention/cifar10acc0.8729999780654907_120.pth'
    #
    # weight_025conv_mixatten_ALP = '/media/unknown/Data/PLP/fast_adv/defenses/weights/best/0.25Mixed+ALP_cifar10_ep_85_val_acc0.8650.pth'