示例#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()
示例#2
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,
    )
示例#3
0
    # "CWL2": [CarliniL2],
}

adversarial_label = 1
normal_label = -1

class_trace_fn = lambda class_id: lenet_mnist_class_trace(
    class_id,
    threshold,
    label=model_label,
    trace_dir=trace_dir,
)

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


def filter_edge_by_key(
        trace: AttrMap,
        key: str = TraceKey.EDGE,
        graph=LENET.network_class.graph().load(),
):
    reconstruct_edge_fn = partial(
        reconstruct_edge,
        trace,
        graph,
        key,
    )
# Configs
result_dir = "result/resnet18cifar10"
# The label of LeNet model
model_label = "dropout"
model_dir = f"{result_dir}/model_{model_label}"
# Dir to save traces for this model
trace_dir = f"{model_dir}/traces_{threshold}"
# The name of traces for this experiment.
# The final dir to save traces is trace_dir/trace_name.
# Should adapt with transform_list
trace_name = "noop"
# Example number per class to extract trace
example_num = 1

resnet_18_cifar10_class_trace = class_trace(name=trace_name,
                                            model_config=RESNET_18_CIFAR10,
                                            data_config=CIFAR10_TRAIN)

# resnet_18_cifar10_class_trace_growth = class_trace_growth(
#     name=name, model_config=RESNET_18_CIFAR10, data_config=CIFAR10_TRAIN
# )

resnet_18_cifar10_class_trace_compact = class_trace_compact(
    resnet_18_cifar10_class_trace,
    name=trace_name,
    model_config=RESNET_18_CIFAR10)

# save_resnet_18_cifar10_class_traces_low_latency = save_class_traces_low_latency(
#     name=name, model_config=RESNET_18_CIFAR10, data_config=CIFAR10_TRAIN
# )