Exemple #1
0
def test_feasible(Constraint, alpha):
    """Tests if prox and LMO yield feasible points"""
    # TODO: implement feasibility check method in each constraint.

    if Constraint == chop.constraints.GroupL1Ball:
        groups = group_patches(x_patch_size=2, y_patch_size=2, x_image_size=6, y_image_size=6)
        constraint = Constraint(alpha, groups)
    elif Constraint == chop.constraints.Cone:
        directions = torch.rand(2, 3, 6, 6)
        cos_alpha = .2
        constraint = Constraint(directions, cos_alpha)
    elif Constraint == chop.constraints.Box:
        constraint = Constraint(-1., 10.)
    else:
        constraint = Constraint(alpha)
    for _ in range(10):
        try:
            data = (alpha + 1) * torch.rand(2, 3, 6, 6)
            assert constraint.is_feasible(constraint.prox(data)).all()
        except AttributeError:  # Constraint doesn't have a prox operator
            pass
        try:
            grad = (alpha + 1) * torch.rand(2, 3, 6, 6)
            update_dir, _ = constraint.lmo(-grad, data)
            s = update_dir + data
            assert constraint.is_feasible(s).all()
        except AttributeError:  # Constraint doesn't have an LMO
            pass
Exemple #2
0
def test_groupL1Prox():
    batch_size = 2
    alpha = 10
    groups = group_patches(x_patch_size=2, y_patch_size=2, x_image_size=6, y_image_size=6)
    constraint = chop.constraints.GroupL1Ball(alpha, groups)
    data = torch.rand(batch_size, 3, 6, 6)

    constraint.prox(-data, step_size=.3)
Exemple #3
0
def test_GroupL1LMO():
    batch_size = 2
    alpha = 1.
    groups = group_patches(x_patch_size=2, y_patch_size=2, x_image_size=6, y_image_size=6)
    constraint = chop.constraints.GroupL1Ball(alpha, groups)
    data = torch.rand(batch_size, 3, 6, 6)
    grad = torch.rand(batch_size, 3, 6, 6)

    constraint.lmo(-grad, data)
Exemple #4
0
def test_projections(constraint, alpha):
    """Tests that projections are true projections:
    ..math::
        p\circ p = p
    """
    batch_size = 8
    if constraint == chop.constraints.GroupL1Ball:
        groups = group_patches()
        prox = constraint(alpha, groups).prox
    elif constraint == chop.constraints.Cone:
        directions = torch.rand(batch_size, 3, 32, 32)
        prox = constraint(directions, cos_angle=.2).prox
    else:
        prox = constraint(alpha).prox

    for _ in range(10):
        data = torch.rand(batch_size, 3, 32, 32)

        proj_data = prox(data)
        # SVD reconstruction doesn't do better than 1e-5
        double_proj = prox(proj_data)
        assert double_proj.allclose(proj_data, atol=1e-5), (double_proj, proj_data)
Exemple #5
0
                        normalize=True,
                        title=f'L{constraint.p}',
                        negative=False)

print(
    f"F1 score: {f1_score(target.detach().cpu(), adv_labels.detach().cpu(), average='macro'):.3f}"
    f" for alpha={alpha:.4f}")

print("GroupL1 constraint.")

# CIFAR-10
# groups = group_patches(x_patch_size=8, y_patch_size=8, x_image_size=32, y_image_size=32)

# Imagenet
groups = group_patches(x_patch_size=28,
                       y_patch_size=28,
                       x_image_size=224,
                       y_image_size=224)

for eps in [5e-2]:
    alpha = eps * len(groups)
    constraint_group = chop.constraints.GroupL1Ball(alpha, groups)
    adversary_group = chop.Adversary(chop.optim.minimize_frank_wolfe)

    # callback_group = Trace(callable=lambda kw: criterion(model(data + kw['x']), target))
    callback_group = Trace()

    _, delta_group = adversary_group.perturb(data,
                                             target,
                                             model,
                                             criterion,
                                             lmo=constraint_group.lmo,