Beispiel #1
0
def iter_l2_adversarial_sticks(model, x, y, params):
    epsilon = float(params["epsilon"])
    n = int(params["n"])
    top_k = int(params["top_k"])
    sigma = int(params["sigma"])

    epsilon = epsilon / float(n)
    tree = PerturbProjTree(x)
    x_perturb = x

    for i in range(n):
        grad = model.grad_fn(x_perturb, y)
        perturb = epsilon * grad / np.sqrt(np.sum(grad**2))
        x_perturb = x_perturb + perturb

    sort_idx = np.argsort(np.linalg.norm(x_perturb - x, axis=1))
    perturbed_idx = sort_idx[-top_k:]
    perturbed = x_perturb[perturbed_idx]
    x_proj = tree.project(perturbed, perturbed - x[perturbed_idx])
    x_sample = sample_on_line_segments(x_proj, perturbed, sigma)
    x_max = np.empty((len(x_perturb), 3))

    for i in range(len(sort_idx)):
        if i < sigma:
            x_max[sort_idx[i]] = x_sample[i]
        elif i < len(sort_idx) - top_k:
            x_max[sort_idx[i]] = x[sort_idx[i]]
        else:
            x_max[sort_idx[i]] = x_perturb[sort_idx[i]]

    return x_max
Beispiel #2
0
def normal_jitter(model, x, y, params):
    epsilon = float(params["epsilon"])
    tau = float(params["tau"])

    tree = PerturbProjTree(x, thickness=tau)
    perturb = np.random.normal(size=x.shape)
    perturb = epsilon * perturb / np.sqrt(np.sum(perturb**2))
    x_perturb = x + perturb
    x_perturb = tree.project(x_perturb, perturb)

    return x_perturb
def iter_l2_adversarial_sticks2(model, x, y, params):
    n = int(params["n"])
    top_k = int(params["top_k"])
    sigma = int(params["sigma"])
    eta = float(params["eta"])
    alpha = float(params["alpha"])
    lambda_ = float(params["lambda_"])
    density = float(params["density"])

    sort_idx = np.argsort(np.linalg.norm(model.grad_fn(x, y), axis = 1))
    perturbed_idx = sort_idx[-top_k:]
    mask = np.zeros(x.shape)
    mask[perturbed_idx, :] = 1.0

    tree = PerturbProjTree(x)

    pred_idx = np.argmax(model.pred_fn(x))
    
    lo = 0.0
    hi = float(alpha)
    res = x

    # binary search for alpha
    for _ in range(20):
        mid = (lo + hi) / 2.0
        
        model.reset_sticks_fn(x + 0.0001 * np.random.normal(size = x.shape))

        for i in range(n):
            model.train_sticks_fn(x, y, mask, mid, lambda_, eta)
        
        x_perturb = model.x_perturb_sticks_fn(x, mask)
        
        if pred_idx == np.argmax(model.pred_fn(x_perturb)):
            hi = mid
        else:
            lo = mid
            res = x_perturb

    dists = np.linalg.norm(x[:, np.newaxis, :] - x[np.newaxis, :, :], axis = 2)
    dists = np.where(np.eye(len(x)) > 0.0, np.full(dists.shape, np.inf), dists)
    avg_min_dist = np.mean(np.min(dists, axis = 1))

    perturbed = res[perturbed_idx]
    x_proj = tree.project(perturbed, perturbed - x[perturbed_idx])
    sticks_len = np.sum(np.linalg.norm(perturbed - x_proj, axis = 1))
    num_resample = min(int(sticks_len / avg_min_dist * density), sigma)
    x_sample = sample_on_line_segments(x_proj, perturbed, num_resample)
    x_unperturbed = x[sort_idx[:-top_k]]
    x_farthest_idx = farthest_point_idx(x_unperturbed, x[perturbed_idx], len(x) - top_k - num_resample)
    x_max = np.concatenate((x_sample, x_unperturbed[x_farthest_idx], perturbed))

    return x_max
Beispiel #4
0
def iter_l2_attack_n_proj(model, x, y, params):
    epsilon = float(params["epsilon"])
    n = int(params["n"])
    tau = float(params["tau"])

    epsilon = epsilon / float(n)
    tree = PerturbProjTree(x, thickness=tau)
    x_perturb = x

    for i in range(n):
        grad = model.grad_fn(x_perturb, y)
        perturb = epsilon * grad / np.sqrt(np.sum(grad**2))
        x_perturb = x_perturb + perturb
        x_perturb = tree.project(x_perturb, perturb)

    return x_perturb
Beispiel #5
0
    def iter_l2_attack_n_proj(model, x, y):
        epsilon = 1.0
        n = 20
        tau = 0.05
        #pdb.set_trace()
        epsilon = epsilon / float(n)
        tree = PerturbProjTree(x.detach().cpu().numpy(), thickness=tau)
        x_perturb = x

        for i in range(n):
            grad = model.grad_fn(x_perturb, y).detach().cpu().numpy()
            perturb = epsilon * grad / np.sqrt(np.sum(grad ** 2))
            x_perturb = x_perturb.detach().cpu().numpy() + perturb
            x_perturb = tree.project(x_perturb, perturb)

        return x_perturb.cuda()
Beispiel #6
0
def mom_l2_attack_n_proj(model, x, y, params):
    epsilon = params["epsilon"]
    mu = params["mu"]
    n = params["n"]
    tau = params["tau"]

    epsilon = epsilon / float(n)
    tree = PerturbProjTree(x, thickness=tau)
    x_perturb = x
    grad = np.zeros(x.shape)

    for i in range(n):
        curr_grad = model.grad_fn(x_perturb, y)
        grad = mu * grad + curr_grad / np.mean(np.abs(curr_grad))
        perturb = epsilon * grad / np.sqrt(np.sum(grad**2))
        x_perturb = x_perturb + perturb
        x_perturb = tree.project(x_perturb, perturb)

    return x_perturb
print("Number of triangles in alpha shape:", alpha_triangles.shape[0])

plt.gca().plot_trisurf(*alpha_points.T, triangles=alpha_triangles)

rand_perturb = (np.random.random(view_pc.shape) * 0.03 +
                0.03) * np.random.choice(np.array([-1, 1]), size=view_pc.shape)
perturbed = view_pc + rand_perturb

colors = np.random.random(num_points)
plt.gca().scatter(*perturbed.T, zdir="y", s=300, c=colors, cmap="rainbow")

scale_plot()

plt.subplot(122, projection="3d")

plt.gca().plot_trisurf(*alpha_points.T, triangles=alpha_triangles)

print("Projection started.")
start_time = time.time()
tree = PerturbProjTree(view_pc, thickness=0.01)
projected = tree.project(perturbed, rand_perturb)
end_time = time.time()
print("Projection ended in %f seconds." % (end_time - start_time))

plt.gca().scatter(*projected.T, zdir="y", s=300, c=colors, cmap="rainbow")

scale_plot()

plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
plt.show()
def example_perturb_proj_tree(label="chair"):

    np.random.seed(1234)

    view_label = label
    offset_idx = 0
    num_points = 1024
    f = h5py.File("../data/point_clouds.hdf5", "r")
    shape_names = [line.rstrip() for line in open("../data/shape_names.txt")]

    pc = f["points"][:][:, :num_points]
    labels = f["labels"][:]

    f.close()

    print("Shape:", pc.shape)
    print("Number of points:", num_points)
    print("Labels:", [shape_names[idx] for idx in np.unique(labels)])
    print("Selected label:", view_label)

    match_idx = np.where(labels == shape_names.index(view_label))[0]
    view_pc = pc[match_idx[offset_idx]]

    print("Shape index:", match_idx[offset_idx])

    plt.figure(figsize=(30, 15))

    plt.subplot(121, projection="3d")

    alpha_points, alpha_triangles = alpha_shape_border(view_pc)
    alpha_points = alpha_points[:, (0, 2, 1)]

    print("Number of points in alpha shape:", alpha_points.shape[0])
    print("Number of triangles in alpha shape:", alpha_triangles.shape[0])

    plt.gca().plot_trisurf(*alpha_points.T, triangles=alpha_triangles)

    rand_perturb = (np.random.random(view_pc.shape) * 0.03 +
                    0.03) * np.random.choice(np.array([-1, 1]),
                                             size=view_pc.shape)
    perturbed = view_pc + rand_perturb

    colors = np.random.random(num_points)
    plt.gca().scatter(*perturbed.T, zdir="y", s=300, c=colors, cmap="rainbow")

    scale_plot()

    plt.subplot(122, projection="3d")

    plt.gca().plot_trisurf(*alpha_points.T, triangles=alpha_triangles)

    print("Projection started.")
    start_time = time.time()
    tree = PerturbProjTree(view_pc, thickness=0.01)
    projected = tree.project(perturbed, rand_perturb)
    end_time = time.time()
    print("Projection ended in %f seconds." % (end_time - start_time))

    plt.gca().scatter(*projected.T, zdir="y", s=300, c=colors, cmap="rainbow")

    scale_plot()

    plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
    plt.show()