Example #1
0
def attack_transform_overlap(
        attack_name,
        transform_name,
        transforms,
        reduce_mode="none",
        result_dir="result/lenet/9transform_attack_overlap"):

    name = attack_name + '_' + transform_name

    lenet_mnist_class_trace = class_trace(
        trace_name,
        model_config=LENET,
        data_config=data_config,
    )

    threshold = 0.5

    # DeepFool will shutdown when num_gpu<0.2
    num_gpus = 0.2

    overlap_fn = calc_trace_side_overlap
    per_channel = False

    path = os.path.join(result_dir, f"{name}_overlap.csv")

    # print(f"Computing {name}")

    # lenet_overlap_ratio = attack_reduced_edge_detection_count_violation(
    lenet_overlap_ratio = attack_reduced_edge_detection(
        attack_name=attack_name,
        attack_fn=attacks[attack_name][0],
        generate_adversarial_fn=cw_generate_adversarial_example if
        attack_name.startswith("CW") else foolbox_generate_adversarial_example,
        class_trace_fn=lambda class_id: lenet_mnist_class_trace(
            class_id,
            threshold,
            label=model_label,
            trace_dir=trace_dir,
        ),
        select_fn=lambda input: arg_approx(input, threshold),
        overlap_fn=overlap_fn,
        path=path,
        per_channel=per_channel,
        preprocessing=(0.1307, 0.3081),
        image_size=28,
        class_num=10,
        norm_fn=mnist.normalize,
        data_format="channels_first",
        **(attacks[attack_name][1] if len(attacks[attack_name]) == 2 else {}),
        images_per_class=images_per_class,
        model_dir=model_dir,
        num_gpus=num_gpus,
        transforms=transforms,
        transform_name=transform_name,
        reduce_mode=reduce_mode,
    )

    lenet_overlap_ratio.save()

    return lenet_overlap_ratio.load()
Example #2
0
def transform_overlap(name, transforms=None):
    label = "augmentation"
    result_dir = "result/lenet/transform_overlap"
    if not os.path.exists(result_dir):
        os.makedirs(result_dir, exist_ok=True)

    threshold = 0.5

    overlap_fn = calc_trace_side_overlap
    per_channel = False

    path = os.path.join(result_dir, f"{name}_overlap.csv")

    print(f"Computing {name}")

    lenet_overlap_ratio = translation_overlap_ratio(
        class_trace_fn=lambda class_id: lenet_mnist_class_trace(
            class_id, threshold, label=label),
        # class_trace_fn=lambda class_id: lenet_mnist_class_trace(class_id, threshold),
        select_fn=lambda input: arg_approx(input, threshold),
        overlap_fn=overlap_fn,
        path=path,
        per_channel=per_channel,
        preprocessing=(0.1307, 0.3081),
        image_size=28,
        class_num=10,
        norm_fn=mnist.normalize,
        data_format="channels_first",
        transforms=transforms,
        name=name,
        num_gpus=0.1,
        images_per_class=100)

    lenet_overlap_ratio.save(header=1)
    return lenet_overlap_ratio.load(header=0, index_col=0)
Example #3
0
def clean_overlap():

    label = "dropout"
    result_dir = "result/lenet"

    images_per_class = 1
    num_gpus = 0.2

    threshold = 0.5

    overlap_fn = calc_trace_side_overlap
    # overlap_fn = calc_trace_side_overlap_both_compact
    per_channel = False
    # per_channel = True

    path_template = (os.path.join(result_dir,
                                  "class_overlap_ratio_{0:.1f}_{1}.csv"))

    lenet_overlap_ratio = clean_overlap_ratio(
        class_trace_fn=lambda class_id: lenet_mnist_class_trace(
            class_id, threshold, label=label),
        # class_trace_fn=lambda class_id: lenet_mnist_class_trace(class_id, threshold),
        select_fn=lambda input: arg_approx(input, threshold),
        overlap_fn=overlap_fn,
        path=path_template.format(threshold, label),
        per_channel=per_channel,
        preprocessing=(0.1307, 0.3081),
        image_size=28,
        class_num=10,
        norm_fn=mnist.normalize,
        data_format="channels_first",
        num_gpus=num_gpus,
        images_per_class=images_per_class,
    )

    lenet_overlap_ratio.save()

    summary_mean_path_template = os.path.join(
        result_dir, "mean_overlap_ratio_summary_{threshold:.1f}.csv")
    key = TraceKey.EDGE
    # key = TraceKey.WEIGHT

    overlay_ratio_mean = lenet_overlap_ratio.load().mean()
    summary_file = summary_mean_path_template.format(
        threshold=threshold,
        label=label,
    )
    overlay_ratio_mean.to_csv(summary_file)
Example #4
0
def visualize_features(
    transforms,
    result_dir = "result/lenet/model_augmentation/visualize"
):

    name = attack_name

    lenet_mnist_class_trace = class_trace(
                                trace_name,
                                model_config=LENET,
                                data_config=data_config,
                            )

    threshold = 0.5

    # DeepFool will shutdown when num_gpu<0.2
    num_gpus = 0.2

    overlap_fn = calc_trace_side_overlap
    per_channel = False

    path = os.path.join(result_dir, f"{name}_overlap.csv")

    print(f"Computing {name}")

    lenet_overlap_ratio = test_data_forward(
        class_trace_fn=lambda class_id: lenet_mnist_class_trace(
            class_id,
            threshold,
            label=model_label,
            trace_dir = trace_dir,
        ),
        select_fn=lambda input: arg_approx(input, threshold),
        overlap_fn=overlap_fn,
        path=path,
        per_channel=per_channel,
        preprocessing=(0.1307, 0.3081),
        image_size=28,
        class_num=10,
        norm_fn=mnist.normalize,
        data_format="channels_first",
        **(attacks[attack_name][1] if len(attacks[attack_name]) == 2 else {}),
        images_per_class=images_per_class,
        model_dir=model_dir,
        num_gpus = num_gpus,
        transforms = transforms,
    )
Example #5
0
num_gpus = 0.5

per_channel = False

use_class_trace = True
if use_class_trace:
    sum_precision_error = 0.2
    threshold_bar_ratio = 0.01
    select_fn = lambda input, output: arg_abs_approx(
        input,
        output,
        sum_precision_error=sum_precision_error,
        threshold_bar_ratio=threshold_bar_ratio,
    )
else:
    select_fn = lambda input: arg_approx(input, threshold)


# Compute the mean overlap ratio of attacked image
def save_trace(
    class_id,
    image_id,
    select_fn: Callable[[np.ndarray], np.ndarray],
    class_dir,
    graph_dir,
    create_model,
    graph,
    per_node: bool = False,
    images_per_class: int = 1,
    num_gpus: float = 1,
    model_dir=resnet18_dir,