Skip to content

Commit

Permalink
examples: add graph at end of demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsextonMITRE committed Sep 19, 2024
1 parent 91c5b4e commit 4a8327a
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions examples/mnist-classifier-demo/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,69 @@
"metadata": {},
"outputs": [],
"source": [
"print(get_metrics(training_job))\n",
"print(get_metrics(infer_fgm))\n",
"print(get_metrics(infer_jpeg))\n",
"print(get_metrics(infer_spatial))\n",
"print(get_metrics(infer_gaussian))"
"import pprint\n",
"\n",
"metrics = {\n",
" \"trained\": get_metrics(training_job),\n",
" \"fgm\": get_metrics(infer_fgm),\n",
" \"jpeg\": get_metrics(infer_jpeg),\n",
" \"spatial\": get_metrics(infer_spatial),\n",
" \"gaussian\": get_metrics(infer_gaussian)\n",
"}\n",
"\n",
"pp = pprint.PrettyPrinter(depth=4)\n",
"pp.pprint(metrics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt \n",
"\n",
"scenarios = [\n",
" 'Base Model',\n",
" 'Fast Gradient Method (Attack)',\n",
" 'JPEG Compression (Defense)',\n",
" 'Spatial Smoothing (Defense)',\n",
" 'Gaussian Noise (Defense)'\n",
"]\n",
"values = [\n",
" metrics['trained']['accuracy'] * 100,\n",
" metrics['fgm']['accuracy'] * 100,\n",
" metrics['jpeg']['accuracy'] * 100,\n",
" metrics['spatial']['accuracy'] * 100,\n",
" metrics['gaussian']['accuracy'] * 100,\n",
"]\n",
"\n",
"fig, ax = plt.subplots(figsize =(16, 9))\n",
"\n",
"# Horizontal Bar Plot\n",
"ax.barh(scenarios, values)\n",
"\n",
"# Add padding between axes and labels\n",
"ax.xaxis.set_tick_params(pad = 5)\n",
"ax.yaxis.set_tick_params(pad = 10)\n",
"\n",
"# Show top values \n",
"ax.invert_yaxis()\n",
"\n",
"# Add annotation to bars\n",
"for i in ax.patches:\n",
" plt.text(i.get_width()+0.2, i.get_y()+0.5, \n",
" str(round((i.get_width()), 2)),\n",
" fontsize = 10, fontweight ='bold',\n",
" color ='grey')\n",
"\n",
"# Add Plot Title\n",
"ax.set_title('Inference Percent Accuracy',\n",
" loc ='left', )\n",
"\n",
"# Show Plot\n",
"plt.show()"
]
}
],
Expand Down

0 comments on commit 4a8327a

Please sign in to comment.