def test_truncatedsvd_filtered_model_is_loaded_correctly():
    model = load_from_file("/tmp/model/truncatedsvd-filtered-model-10-components")
    assert type(model.sklearn_transformer) is TruncatedSVD
def test_incrementalpca_filtered_model_is_loaded_correctly():
    model = load_from_file("/tmp/model/incrementalpca-filtered-model-10-components")
    assert type(model.sklearn_transformer) is IncrementalPCA
def test_nmf_filtered_model_is_loaded_correctly():
    model = load_from_file("/tmp/model/nmf-filtered-model-10-components")
    assert type(model.sklearn_transformer) is NMF
def test_fastica_filtered_model_is_loaded_correctly():
    model = load_from_file("/tmp/model/fastica-filtered-model-10-components")
    assert type(model.sklearn_transformer) is FastICA
Beispiel #5
0
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('--model',
                             nargs="+",
                             dest="model_paths",
                             help="models to test the accuracy of")

args = argument_parser.parse_args()

if not args.model_paths:
    model_paths = glob("model/*")  # compute accuracy for all the found models
else:
    model_paths = args.model_paths

result = []
_, _, X_test, y_test = mnist()
for path in model_paths:
    if not os.path.exists(path):
        continue

    print(f"Computing for {path}...", end="")
    sys.stdout.flush()
    model = load_from_file(path)
    test_set_accuracy = accuracy(model, X_test, y_test)
    result.append((path, test_set_accuracy))
    print()

result.sort(key=lambda pair: pair[1], reverse=True)
for pair in result:
    path, test_set_accuracy = pair
    print(f"{path} -> {test_set_accuracy}")
Beispiel #6
0
import sys

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from datasets import mnist
from models import fc_100_100_10, pca_filtered_model, train, load_from_file
from attacks import adversarial_example, adversarial_score
from utils import dump_json_to_file

argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('--model', nargs="+", dest="model_paths",
                             help='path to models to attack')
argument_parser.add_argument('--eta', nargs='+', type=float, dest="eta_list",
                             default=np.arange(0, 0.25, 0.01),
                             help='values of eta for generating adv/ examples')
args = argument_parser.parse_args()

PREFIX = os.environ.get('PREFIX', '.')

X_train, y_train, X_test, y_test = mnist()

for model_path in args.model_paths:
    model = load_from_file(model_path)
    print(f"Computing adversarial score against {model.name}...", file=sys.stderr)
    adversarial_score_dictionary = {}
    for eta in args.eta_list:
        score = round(adversarial_score(model, X_test, y_test, eta), 3)
        adversarial_score_dictionary[eta] = score

    print(json.dumps(adversarial_score_dictionary))
    dump_json_to_file(adversarial_score_dictionary, f"{PREFIX}/attack/{model.name}/score.json")
Beispiel #7
0
argument_parser = ArgumentParser()
argument_parser.add_argument('--eta',
                             required=True,
                             type=float,
                             help="value of eta parameter for FGS")
argument_parser.add_argument(
    "--model",
    dest="model_path",
    required=True,
    help="path to the model to generate input against")

args = argument_parser.parse_args()

_, _, X_test, y_test = mnist()

original_input = X_test[0]

model = load_from_file(args.model_path)
adversarial_input = adversarial_example(model, [original_input], [7],
                                        eta=args.eta)[0]

plt.imshow(adversarial_input)
figure_name_eta = "".join(f"{args.eta}".split("."))
figure_name = f"adversarial_input.{model.name}.{figure_name_eta}.png"
print(f"Saving {figure_name}...", end="")
plt.savefig(figure_name)
print()

plt.imshow(adversarial_input)
plt.show()