|
from handler import EndpointHandler |
|
import numpy as np |
|
import shutil |
|
|
|
from pathlib import Path |
|
|
|
from sklearn.datasets import make_classification |
|
from sklearn.model_selection import train_test_split |
|
|
|
from concrete.ml.sklearn import LogisticRegression |
|
from concrete.ml.deployment import FHEModelClient, FHEModelDev |
|
|
|
|
|
path_to_model = Path("compiled_model") |
|
do_training_and_compilation = True |
|
|
|
x, y = make_classification(n_samples=1000, class_sep=2, n_features=30, random_state=42) |
|
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42) |
|
|
|
if do_training_and_compilation: |
|
model_dev = LogisticRegression() |
|
model_dev.fit(X_train, y_train) |
|
|
|
|
|
model_dev.compile(X_train) |
|
|
|
|
|
shutil.rmtree(path_to_model, ignore_errors=True) |
|
fhemodel_dev = FHEModelDev(path_to_model, model_dev) |
|
fhemodel_dev.save(via_mlir=True) |
|
|
|
|
|
my_handler = EndpointHandler(path=".") |
|
|
|
|
|
fhemodel_client = FHEModelClient(path_to_model) |
|
|
|
|
|
fhemodel_client.generate_private_and_evaluation_keys() |
|
evaluation_keys = fhemodel_client.get_serialized_evaluation_keys() |
|
|
|
|
|
nb_good = 0 |
|
nb_samples = len(X_test) |
|
verbose = False |
|
|
|
for i in range(nb_samples): |
|
|
|
|
|
encrypted_inputs = fhemodel_client.quantize_encrypt_serialize([X_test[i]]) |
|
|
|
|
|
payload = { |
|
"inputs": "fake", |
|
"encrypted_inputs": encrypted_inputs, |
|
"evaluation_keys": evaluation_keys, |
|
} |
|
|
|
|
|
encrypted_prediction = my_handler(payload) |
|
encrypted_prediction = encrypted_prediction |
|
|
|
|
|
prediction_proba = fhemodel_client.deserialize_decrypt_dequantize(encrypted_prediction)[0] |
|
prediction = np.argmax(prediction_proba) |
|
|
|
if verbose: |
|
print(f"for i-th input, {prediction=} with expected {y_test[i]}") |
|
|
|
|
|
nb_good += y_test[i] == prediction |
|
|
|
print(f"Accuracy on {nb_samples} samples is {nb_good * 1. / nb_samples}") |
|
|