mia
A library for running membership inference attacks (MIA) against machine learning models. Check out the documentation.
These are attacks against privacy of the training data. In MIA, an attacker tries to guess whether a given example was used during training of a target model or not, only by querying the model. See more in the paper by Shokri et al. Currently, you can use the library to evaluate the robustness of your Keras or PyTorch models to MIA.
Features:
Implements the original shadow model attack
Is customizable, can use any scikit learn’s Estimator-like object as a shadow or attack model
Is tested with Keras and PyTorch
Getting started
You can install mia from PyPI:
pip install mia
Usage
Shokri et al. attack
See the full runnable example. Read the details of the attack in the paper.
Let target_model_fn() return the target model architecture as a scikit-like classifier. The attack is white-box, meaning the attacker is assumed to know the architecture. Let NUM_CLASSES be the number of classes of the classification problem.
First, the attacker needs to train several shadow models —that mimick the target model— on different datasets sampled from the original data distribution. The following code snippet initializes a shadow model bundle, and runs the training of the shadows. For each shadow model, 2 * SHADOW_DATASET_SIZE examples are sampled without replacement from the full attacker’s dataset. Half of them will be used for control, and the other half for training of the shadow model.
from mia.estimators import ShadowModelBundle
smb = ShadowModelBundle(
target_model_fn,
shadow_dataset_size=SHADOW_DATASET_SIZE,
num_models=NUM_MODELS,
)
X_shadow, y_shadow = smb.fit_transform(attacker_X_train, attacker_y_train)
fit_transform returns attack data X_shadow, y_shadow. Each row in X_shadow is a concatenated vector consisting of the prediction vector of a shadow model for an example from the original dataset, and the example’s class (one-hot encoded). Its shape is hence (2 * SHADOW_DATASET_SIZE, 2 * NUM_CLASSES). Each label in y_shadow is zero if a corresponding example was “out” of the training dataset of the shadow model (control), or one, if it was “in” the training.
mia provides a class to train a bundle of attack models, one model per class. attack_model_fn() is supposed to return a scikit-like classifier that takes a vector of model predictions (NUM_CLASSES, ), and returns whether an example with these predictions was in the training, or out.
from mia.estimators import AttackModelBundle
amb = AttackModelBundle(attack_model_fn, num_classes=NUM_CLASSES)
amb.fit(X_shadow, y_shadow)
In place of the AttackModelBundle one can use any binary classifier that takes (2 * NUM_CLASSES, )-shape examples (as explained above, the first half of an input is the prediction vector from a model, the second half is the true class of a corresponding example).
To evaluate the attack, one must encode the data in the above-mentioned format. Let target_model be the target model, data_in the data (tuple X, y) that was used in the training of the target model, and data_out the data that was not used in the training.
from mia.estimators import prepare_attack_data
attack_test_data, real_membership_labels = prepare_attack_data(
target_model, data_in, data_out
)
attack_guesses = amb.predict(attack_test_data)
attack_accuracy = np.mean(attack_guesses == real_membership_labels)
Citing
@misc{mia,
author = {Bogdan Kulynych and
Mohammad Yaghini},
title = {{mia: A library for running membership inference
attacks against ML models}},
month = sep,
year = 2018,
doi = {10.5281/zenodo.1433744},
url = {https://doi.org/10.5281/zenodo.1433744}
}
Metadata
Release files for mia 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mia-0.1.2.tar.gz | 17.2 kB | Without provenance | Details for mia-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mia-0.1.2-py2-none-any.whl | Python 2 | none | any | Details for mia-0.1.2-py2-none-any.whl |
Total release size: 30.7 kB
Release files / mia-0.1.2.tar.gz
| Download URL | mia-0.1.2.tar.gz |
|---|---|
| Size | 17.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
118606dfe9f8cbd8ecb6edcd1231d5360decbf4ff02d90a1696df73958580a55
|
|
BLAKE2b-256 checksum How to use checksums |
7d12f149a7cd43e49725921e9884363aa3cbfea8a49c319a944eb71d48973fa9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
|
Release files / mia-0.1.2-py2-none-any.whl
| Download URL | mia-0.1.2-py2-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
f170384300f1e08898237cf7f43582fb2b95c0ea1ac56c2c8198e18f58633154
|
|
BLAKE2b-256 checksum How to use checksums |
ff2c9027be252e23650230be310f9b7886fc2f9f5bed77afe89542441a44c8ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6
|
Security for mia 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.