inst = 'objects/instantiations/MNT159.inst' vector_length = 784 k = 250 classes = 10 model = 'objects/ml_models/final.mlm' mnist = fetch_openml('mnist_784') X, y = mnist['data'].astype('float'), mnist['target'].astype('float') X_test, y_test = X[60000:], y[60000:] index = random.randint(0, 9999) X, y = X_test[index], y_test[index] print(f'Will test on MNIST test instance #{index}, which is a {int(y)}.') print('Importing model.') ml = models.MLModel(source=model) biased = np.ones(785) biased[1:] = X print('Done!\n') results = ml.evaluate(biased) print(f'Expected output:\n{results}\n') print('Importing scheme.') scheme = scheme.ML_DGP(inst) print('Done!\n') print('Loading discrete logarithm solver.') dlog = discretelogarithm.PreCompBabyStepGiantStep(
""" Generates the confusion matrix for the provided model using cleartext evaluation. """ import setup from core import models import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sn from sklearn.datasets import fetch_mldata model = models.MLModel(source="objects/ml_models/final.mlm") mnist = fetch_mldata('MNIST original') X, y = mnist["data"], mnist["target"].astype('int') X_test_, y_test = X[60000:], y[60000:] X_test = np.ones((10000, 785)) X_test[:, 1:] = X_test_ predictions = [np.array(model.evaluate(x)).argmax() for x in X_test] M = [[0 for i in range(10)] for j in range(10)] for i in range(10000): value = y_test[i] pred = predictions[i] M[value][pred] += 1 conf_mat = pd.DataFrame( M,
from utils import fix_precision, float_precision model = SimpleNN(10) print("Loading model ...") path = "/home/sukhad/Workspace/GithHub/reading-in-the-dark/mnist/objects/ml_models/simple_char.pt" model.load_state_dict(torch.load(path)) model.eval() print("Done") proj_prec = 7 diag_prec = 5 data_prec = 3 model.proj1.weight = fix_precision(model.proj1.weight, proj_prec, 100) model.proj1.bias = fix_precision(model.proj1.bias, proj_prec, 100) model.diag1.weight = fix_precision(model.diag1.weight, diag_prec, 100) proj_param = torch.cat((model.proj1.bias.reshape((1,-1))/2**data_prec, model.proj1.weight.t())).long().tolist() diag_param = model.diag1.weight.t().long().tolist() print("Loading proj and forms ...") proj = models.Projection(proj_param) forms = models.DiagonalQuadraticForms(diag_param) print("Done") model_path = "/home/sukhad/Workspace/GithHub/reading-in-the-dark/mnist/objects/ml_models" ml = models.MLModel(proj, forms) ml.toFile(model_path, "simple_nn_quad")