PrivacyRaven allows you to perform model extraction attacks by providing a query function that wraps your target model. The ModelExtractionAttack class orchestrates the attack using a query function, query limits, input shapes, and seed data.
Key components required:
- Query Function: A function that takes input data and returns the target model's output. You can use
privacyraven.utils.query.get_target to wrap a model. - Seed Data: Public or known data used during the extraction process.
- Substitute Model: An architecture used to synthesize the extracted knowledge.
import privacyraven as pr
from privacyraven.utils.data import get_emnist_data
from privacyraven.extraction.core import ModelExtractionAttack
from privacyraven.utils.query import get_target
from privacyraven.models.victim import train_four_layer_mnist_victim
from privacyraven.models.four_layer import FourLayerClassifier
# 1. Setup the target model
model = train_four_layer_mnist_victim()
# 2. Define the query function
def query_mnist(input_data):
return get_target(model, input_data, (1, 28, 28, 1))
# 3. Obtain seed data
emnist_train, emnist_test = get_emnist_data()
# 4. Configure and run the attack
attack = ModelExtractionAttack(
query_mnist, # query function
200, # query limit
(1, 28, 28, 1), # victim input shape
10, # number of targets
(3, 1, 28, 28), # substitute input shape
"copycat", # synthesizer name
FourLayerClassifier, # substitute model architecture
784, # substitute input size
emnist_train, # seed train data
emnist_test, # seed test data
)