Skip to content

Commit

Permalink
examples: remove unneeded files & add defenses
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsextonMITRE committed Sep 18, 2024
1 parent 6934232 commit 247781c
Show file tree
Hide file tree
Showing 37 changed files with 757 additions and 4,398 deletions.
237 changes: 190 additions & 47 deletions examples/mnist-classifier-demo/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 80,
"metadata": {
"tags": []
},
Expand All @@ -53,7 +53,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -176,7 +176,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 131,
"metadata": {
"tags": []
},
Expand All @@ -194,9 +194,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 132,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2m2024-09-18 12:52:25\u001b[0m [\u001b[31m\u001b[1merror \u001b[0m] \u001b[1mError code 400 returned. \u001b[0m \u001b[36mdata\u001b[0m=\u001b[35m{'username': 'pluginuser', 'email': '[email protected]', 'password': 'pleasemakesuretoPLUGINthecomputer', 'confirmPassword': 'pleasemakesuretoPLUGINthecomputer'}\u001b[0m \u001b[36mmethod\u001b[0m=\u001b[35mPOST\u001b[0m \u001b[36mresponse\u001b[0m=\u001b[35m{\"message\": \"Bad Request - The username on the registration form is not available. Please select another and resubmit.\"}\n",
"\u001b[0m \u001b[36murl\u001b[0m=\u001b[35mhttp://localhost:20080/api/v1/users/\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'username': 'pluginuser', 'status': 'Login successful'}"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"try:\n",
" client.users.create('pluginuser','[email protected]','pleasemakesuretoPLUGINthecomputer','pleasemakesuretoPLUGINthecomputer')\n",
Expand All @@ -214,20 +233,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"def wait_for_job(job, job_name):\n",
"def wait_for_job(job, job_name, quiet=False):\n",
" n = 0\n",
" while job['status'] != 'finished': \n",
" job = client.jobs.get_by_id(job['id'])\n",
" time.sleep(1)\n",
" clear_output(wait=True)\n",
" display(\"Waiting for job.\" + \".\" * (n % 3) )\n",
" if not quiet:\n",
" clear_output(wait=True)\n",
" display(\"Waiting for job.\" + \".\" * (n % 3) )\n",
" n += 1\n",
" clear_output(wait=True)\n",
" display(f\"Job finished. Starting {job_name} job.\")\n",
" if not quiet:\n",
" clear_output(wait=True)\n",
" display(f\"Job finished. Starting {job_name} job.\")\n",
" "
]
},
Expand All @@ -240,14 +261,15 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 134,
"metadata": {},
"outputs": [],
"source": [
"#delete_all(client)\n",
"experiment_id, train_ep, queue_id = upload_experiment(client, 'src/train.yml','train','training a classifier on MNIST', PLUGIN_FILES, QUEUE_NAME, QUEUE_DESC, EXPERIMENT_NAME, EXPERIMENT_DESC)\n",
"experiment_id, fgm_ep, queue_id = upload_experiment(client, 'src/fgm.yml','fgm','generating examples on mnist_classifier using the fgm attack', PLUGIN_FILES, QUEUE_NAME, QUEUE_DESC, EXPERIMENT_NAME, EXPERIMENT_DESC)\n",
"experiment_id, infer_ep, queue_id = upload_experiment(client, 'src/infer.yml','infer','evaluating performance of mnist_classifier on generated fgm examples', PLUGIN_FILES, QUEUE_NAME, QUEUE_DESC, EXPERIMENT_NAME, EXPERIMENT_DESC)"
"experiment_id, infer_ep, queue_id = upload_experiment(client, 'src/infer.yml','infer','evaluating performance of mnist_classifier on generated fgm examples', PLUGIN_FILES, QUEUE_NAME, QUEUE_DESC, EXPERIMENT_NAME, EXPERIMENT_DESC)\n",
"experiment_id, defense_ep, queue_id = upload_experiment(client, 'src/defense.yml','defense','generating defended dataset', PLUGIN_FILES, QUEUE_NAME, QUEUE_DESC, EXPERIMENT_NAME, EXPERIMENT_DESC)"
]
},
{
Expand All @@ -261,7 +283,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -290,7 +312,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 137,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -328,7 +350,104 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
"def infer(experiment_id, queue_id, infer_ep, prev_job, job_time_limit='1h', defense=False):\n",
" dd = \"def_testing\" if defense else \"adv_testing\"\n",
" tn = \"testing_adversarial_def.tar.gz\" if defense else \"testing_adversarial_fgm.tar.gz\"\n",
" wait_for_job(prev_job, 'infer', quiet=False)\n",
" infer_job = client.experiments.create_jobs_by_experiment_id(\n",
" experiment_id,\n",
" f\"infer job for {experiment_id}\",\n",
" queue_id,\n",
" infer_ep,\n",
" {\"job_id\": str(prev_job['id']),\n",
" \"tar_name\": tn,\n",
" \"data_dir\": dd,\n",
" \"model_name\": MODEL_NAME, \"model_version\": str(-1)},\n",
" job_time_limit\n",
" )\n",
" return infer_job"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"from mlflow.tracking import MlflowClient\n",
"from uuid import UUID\n",
"\n",
"def get_metrics(job):\n",
" wait_for_job(job, 'metrics', quiet=True)\n",
" mlflow_client = MlflowClient()\n",
" mlflow_runid = UUID(client.jobs.get_mlflow_run_id(job['id'])['mlflowRunId']).hex\n",
" mlflow_run = mlflow_client.get_run(mlflow_runid)\n",
" return mlflow_run.data.metrics"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Job finished. Starting infer job.'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"infer_fgm = infer(experiment_id, queue_id, infer_ep, fgm_job, defense=False)"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"experiment_id, defense_ep, queue_id = upload_experiment(client, 'src/defense.yml','defense','generating defended dataset', PLUGIN_FILES, QUEUE_NAME, QUEUE_DESC, EXPERIMENT_NAME, EXPERIMENT_DESC)"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Job finished. Starting defense job.'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"job_time_limit = '1h'\n",
"wait_for_job(fgm_job, 'defense')\n",
"spatial_job = client.experiments.create_jobs_by_experiment_id(\n",
" experiment_id,\n",
" f\"defense job for {experiment_id}\",\n",
" queue_id,\n",
" defense_ep,\n",
" {\"job_id\": str(fgm_job['id']),\"def_type\":\"spatial_smoothing\"}, # -1 means get the latest\n",
" job_time_limit\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [
{
Expand All @@ -341,57 +460,81 @@
"output_type": "display_data"
}
],
"source": [
"infer_spatial = infer(experiment_id, queue_id, infer_ep, spatial_job, defense=True)"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Job finished. Starting defense job.'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"job_time_limit = '1h'\n",
"wait_for_job(fgm_job, 'infer')\n",
"infer_job = client.experiments.create_jobs_by_experiment_id(\n",
"wait_for_job(fgm_job, 'defense')\n",
"jpeg_comp_job = client.experiments.create_jobs_by_experiment_id(\n",
" experiment_id,\n",
" f\"infer job for {experiment_id}\",\n",
" f\"defense job for {experiment_id}\",\n",
" queue_id,\n",
" infer_ep,\n",
" {\"fgm_job_id\": str(fgm_job['id']), \"model_name\": MODEL_NAME, \"model_version\": str(-1)},\n",
" defense_ep,\n",
" {\n",
" \"job_id\": str(fgm_job['id']),\n",
" \"def_type\":\"jpeg_compression\"\n",
" }, # -1 means get the latest\n",
" job_time_limit\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 145,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Job finished. Starting infer job.'"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"infer_jpeg = infer(experiment_id, queue_id, infer_ep, jpeg_comp_job, defense=True)"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training accuracy:\n",
"{'accuracy': 0.9760833382606506,\n",
" 'auc': 0.9990718364715576,\n",
" 'loss': 0.07672422379255295,\n",
" 'precision': 0.9799415469169617,\n",
" 'recall': 0.9721999764442444,\n",
" 'training_time_in_minutes': 0.3090300166666667}\n",
"FGM accuracy:\n",
"{'accuracy': 0.16326121985912323,\n",
" 'auc': 0.6759902238845825,\n",
" 'loss': 2.7856907844543457,\n",
" 'precision': 0.09174499660730362,\n",
" 'recall': 0.044971954077482224}\n"
"{'training_time_in_minutes': 0.32976753333333336, 'accuracy': 0.9775166511535645, 'auc': 0.9987682700157166, 'loss': 0.07407279312610626, 'precision': 0.9809511303901672, 'recall': 0.9750000238418579}\n",
"{'accuracy': 0.11217948794364929, 'auc': 0.6169368028640747, 'precision': 0.09878776967525482, 'loss': 3.25475811958313, 'recall': 0.0546875}\n",
"{'accuracy': 0.11548477411270142, 'auc': 0.6298573613166809, 'loss': 3.010637044906616, 'precision': 0.10013880580663681, 'recall': 0.05058092996478081}\n",
"{'auc': 0.617414653301239, 'precision': 0.12656284868717194, 'accuracy': 0.1341145783662796, 'loss': 2.9532642364501953, 'recall': 0.05779246613383293}\n"
]
}
],
"source": [
"from mlflow.tracking import MlflowClient\n",
"from uuid import UUID\n",
"mlflow_client = MlflowClient()\n",
"mlflow_runid = UUID(client.jobs.get_mlflow_run_id(training_job['id'])['mlflowRunId']).hex\n",
"mlflow_run = mlflow_client.get_run(mlflow_runid)\n",
"print(\"Training metrics:\")\n",
"pprint.pprint(mlflow_run.data.metrics)\n",
" \n",
"mlflow_runid = UUID(client.jobs.get_mlflow_run_id(infer_job['id'])['mlflowRunId']).hex\n",
"mlflow_run = mlflow_client.get_run(mlflow_runid)\n",
"print(\"FGM metrics:\")\n",
"pprint.pprint(mlflow_run.data.metrics)"
"print(get_metrics(training_job))\n",
"print(get_metrics(infer_fgm))\n",
"print(get_metrics(infer_jpeg))\n",
"print(get_metrics(infer_spatial))"
]
}
],
Expand Down
Loading

0 comments on commit 247781c

Please sign in to comment.