def main():
    args = Args().get_args()
    kwargs = vars(args)
    checkpoint = torch.load(args.checkpoint)
    base_classifier = get_architecture(checkpoint["arch"], args.dataset)
    base_classifier.load_state_dict(checkpoint['state_dict'])

    attacker = SmoothAttack(base_classifier)
    smoothed_classifier = Smooth(base_classifier,
                                 get_num_classes(args.dataset), args.sigma)

    dataset = get_dataset(args.dataset, 'test')
    average_nat = []
    average_adv = []

    j_header('index', 'nat_y', 'adv_y', 'nat_rad', 'adv_rad', 'success')
    figure = FigureSaver()
    for i in range(0, len(dataset), args.skip):
        (x, label) = dataset[i]
        x = x.cuda()
        first_x = x.data

        nat_pred, nat_rad = smoothed_classifier.certify(
            x, args.N0, args.N, args.alpha, args.batch)
        if nat_pred is -1:
            continue
        if args.dataset == DATASETS[0]:  # ImageNet
            targets = [j for j in range(0, 1000, 100) if j is not label]
        else:
            targets = [j for j in range(10) if j is not label]
        best_rad = -10.0
        best_image = None
        best_target = -1

        for target in targets:
            adv_x = attacker.perturb(x=first_x, y=target, **kwargs)
            # If you want to do wasserstein attack, uncomment the following and change the attacker to wasserstein
            # adv_x = attacker.perturb(x=first_x, y=target, eps=args.sigma, steps=args.steps, batch=args.batch)
            adv_pred, adv_rad = smoothed_classifier.certify(
                adv_x, args.N0, 2 * args.N0, args.alpha, args.batch)
            adv_suc = (adv_pred != label) and (adv_pred != -1) and (nat_pred !=
                                                                    -1)
            adv_rad = adv_rad if adv_suc else -adv_rad

            if adv_rad > best_rad:
                best_rad = adv_rad
                best_image = adv_x.data
                best_target = target

        figure.save(best_image, i, 'best={}'.format(best_target))
        figure.save(first_x, i, 'natural')
        best_pred, best_rad = smoothed_classifier.certify(
            best_image, args.N0, args.N, args.alpha, args.batch)
        j_print(i, label, best_target, nat_rad, best_rad)
        average_adv.append(best_rad)
        average_nat.append(nat_rad)
    average_nat = np.array(average_nat)
    average_adv = np.array(average_adv)
    print('Average nat radii {}, Average adv radii {}'.format(
        average_nat.mean(), average_adv.mean()))
    if not os.path.exists(os.path.dirname(args.outfile)):
        os.makedirs(os.path.dirname(args.outfile))
    f = open(args.outfile, 'w')
    print("idx\tlabel\tpredict\tradius\tcorrect\ttime", file=f, flush=True)

    # iterate through the dataset
    dataset = get_dataset(args.dataset, args.split)
    for i in range(len(dataset)):

        # only certify every args.skip examples, and stop after args.max examples
        if i % args.skip != 0:
            continue
        if i == args.max:
            break

        (x, label) = dataset[i]

        before_time = time()
        # certify the prediction of g around x
        x = x.cuda()
        prediction, radius = smoothed_classifier.certify(x, args.N0, args.N, args.alpha, args.batch)
        after_time = time()
        correct = int(prediction == label)

        time_elapsed = str(datetime.timedelta(seconds=(after_time - before_time)))
        print("{}\t{}\t{}\t{:.3}\t{}\t{}".format(
            i, label, prediction, radius, correct, time_elapsed), file=f, flush=True)
        print(i)

    f.close()
Example #3
0
    dataset = get_dataset(args.dataset, args.split)
    for i in range(len(dataset)):

        # only certify every args.skip examples, and stop after args.max examples
        if i % args.skip != 0:
            continue
        if i == args.max:
            break

        (x, label) = dataset[i]

        before_time = time()
        # certify the prediction of g around x
        x = x.cuda()
        prediction, exp_cdf_00, exp_cdf_25, exp_cdf_50, exp_cdf_75, exp_cdf_100, exp_cdf_125, exp_cdf_150, \
            exp_00, exp_25, exp_50, exp_75, exp_100, exp_125, exp_150 = smoothed_classifier.certify(x, args.N0, args.N, args.alpha, args.batch)
        after_time = time()
        correct = int(prediction == label)

        time_elapsed = str(
            datetime.timedelta(seconds=(after_time - before_time)))
        print(
            "{}\t{}\t{}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{}\t{}"
            .format(i, label, prediction, exp_cdf_00, exp_cdf_25, exp_cdf_50,
                    exp_cdf_75, exp_cdf_100, exp_cdf_125, exp_cdf_150, exp_00,
                    exp_25, exp_50, exp_75, exp_100, exp_125, exp_150, correct,
                    time_elapsed),
            file=f,
            flush=True)

    f.close()
Example #4
0
    # prepare output file
    f = open(args.outfile, 'a')
    print(
        "idx\tlabel\tcount\tpredict\tradiusR\tradiusG\tradiusB\tradius_L1\tradius_LInf\tradius_L2\tradius_cohen\tcorrect\ttime",
        file=f,
        flush=True)

    # iterate through the dataset
    dataset = get_dataset(args.dataset, args.split)
    for i in range(args.start, args.start + (args.skip * args.max), args.skip):

        (x, label) = dataset[i]
        before_time = time()
        # certify the prediction of g around x
        x = x.cuda()
        count, maxC, meanC, meanR, meanG, meanB, prediction, radiusR, radiusG, radiusB, radiusL1, radiusLInf, radiusL2, radiusOp, radiusCohen = smoothed_classifier.certify(
            x, args.N0, args.N, args.alpha, args.batch, label)
        after_time = time()
        correct = int(prediction == label)
        time_elapsed = str(
            datetime.timedelta(seconds=(after_time - before_time)))
        print(
            "{}\t{}\t{}\t{}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{:.3}\t{}\t{}"
            .format(i, label, count, prediction, radiusR, radiusG, radiusB,
                    radiusL1, radiusLInf, radiusL2, radiusCohen, correct,
                    time_elapsed),
            file=f,
            flush=True)

    f.close()