Skip to content

Commit

Permalink
Update notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrinkiani committed Jan 25, 2024
1 parent a647a2b commit bed3d4c
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 158 deletions.
109 changes: 82 additions & 27 deletions notebooks/keras_fashion_mnist.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"modelscan, version 0.5.0\n"
]
}
],
"source": [
"!pip install -q modelscan"
"!pip install -q modelscan\n",
"!modelscan -v"
]
},
{
Expand Down Expand Up @@ -58,35 +67,35 @@
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"1875/1875 [==============================] - 2s 773us/step - loss: 0.5034 - accuracy: 0.8228\n",
"1875/1875 [==============================] - 1s 726us/step - loss: 0.4988 - accuracy: 0.8246\n",
"Epoch 2/10\n",
"1875/1875 [==============================] - 1s 738us/step - loss: 0.3762 - accuracy: 0.8640\n",
"1875/1875 [==============================] - 1s 754us/step - loss: 0.3765 - accuracy: 0.8641\n",
"Epoch 3/10\n",
"1875/1875 [==============================] - 1s 760us/step - loss: 0.3358 - accuracy: 0.8769\n",
"1875/1875 [==============================] - 1s 729us/step - loss: 0.3408 - accuracy: 0.8752\n",
"Epoch 4/10\n",
"1875/1875 [==============================] - 1s 733us/step - loss: 0.3114 - accuracy: 0.8854\n",
"1875/1875 [==============================] - 1s 772us/step - loss: 0.3138 - accuracy: 0.8853\n",
"Epoch 5/10\n",
"1875/1875 [==============================] - 1s 743us/step - loss: 0.2949 - accuracy: 0.8909\n",
"1875/1875 [==============================] - 1s 716us/step - loss: 0.2953 - accuracy: 0.8916\n",
"Epoch 6/10\n",
"1875/1875 [==============================] - 1s 731us/step - loss: 0.2816 - accuracy: 0.8962\n",
"1875/1875 [==============================] - 1s 744us/step - loss: 0.2817 - accuracy: 0.8964\n",
"Epoch 7/10\n",
"1875/1875 [==============================] - 1s 743us/step - loss: 0.2687 - accuracy: 0.9000\n",
"1875/1875 [==============================] - 1s 770us/step - loss: 0.2699 - accuracy: 0.8995\n",
"Epoch 8/10\n",
"1875/1875 [==============================] - 1s 762us/step - loss: 0.2567 - accuracy: 0.9044\n",
"1875/1875 [==============================] - 1s 736us/step - loss: 0.2590 - accuracy: 0.9025\n",
"Epoch 9/10\n",
"1875/1875 [==============================] - 1s 766us/step - loss: 0.2464 - accuracy: 0.9075\n",
"1875/1875 [==============================] - 1s 720us/step - loss: 0.2475 - accuracy: 0.9072\n",
"Epoch 10/10\n",
"1875/1875 [==============================] - 1s 737us/step - loss: 0.2372 - accuracy: 0.9113\n",
"313/313 [==============================] - 0s 490us/step - loss: 0.3440 - accuracy: 0.8827\n",
"1875/1875 [==============================] - 1s 747us/step - loss: 0.2403 - accuracy: 0.9109\n",
"313/313 [==============================] - 0s 480us/step - loss: 0.3441 - accuracy: 0.8793\n",
"\n",
"Model trained with test accuracy: 0.8827000260353088\n"
"Model trained with test accuracy: 0.8792999982833862\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/mehrinkiani/mambaforge/envs/notebooks-xgb-2/lib/python3.9/site-packages/keras/src/engine/training.py:3000: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n",
"/Users/mehrinkiani/mambaforge/envs/py310/lib/python3.10/site-packages/keras/src/engine/training.py:3000: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.\n",
" saving_api.save_model(\n"
]
}
Expand Down Expand Up @@ -117,9 +126,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 0s 35ms/step\n",
"1/1 [==============================] - 0s 34ms/step\n",
"\n",
"The model predicts: ['Ankle boot', 'Pullover', 'Trouser'] with probabilities: [ 98.059 99.993996 100. ]\n",
"The model predicts: ['Ankle boot', 'Pullover', 'Trouser'] with probabilities: [ 99.396 99.94 100. ]\n",
"\n",
"The true labels are ['Ankle boot', 'Pullover', 'Trouser']\n"
]
Expand Down Expand Up @@ -159,7 +168,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/KerasModels/safe_model.h5 using hdf5 model scan\n",
"No settings file detected at /Users/mehrinkiani/Documents/modelscan/notebooks/modelscan-settings.toml. Using defaults. \n",
"\n",
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/KerasModels/safe_model.h5 using modelscan.scanners.H5LambdaDetectScan model scan\n",
"\n",
"\u001b[34m--- Summary ---\u001b[0m\n",
"\n",
Expand Down Expand Up @@ -244,9 +255,9 @@
"aws_secret_access_key=<aws_secret_key>\n",
"aws_access_key_id=<access_key_id>\n",
"aws_secret_access_key=<aws_secret_key>\n",
"1/1 [==============================] - 0s 29ms/step\n",
"1/1 [==============================] - 0s 28ms/step\n",
"\n",
"The model predicts: ['Ankle boot', 'Pullover', 'Trouser'] with probabilities: [ 98.059 99.993996 100. ]\n",
"The model predicts: ['Ankle boot', 'Pullover', 'Trouser'] with probabilities: [ 99.396 99.94 100. ]\n",
"\n",
"The true labels are ['Ankle boot', 'Pullover', 'Trouser']\n"
]
Expand Down Expand Up @@ -289,7 +300,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/KerasModels/unsafe_model.h5 using hdf5 model scan\n",
"No settings file detected at /Users/mehrinkiani/Documents/modelscan/notebooks/modelscan-settings.toml. Using defaults. \n",
"\n",
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/KerasModels/unsafe_model.h5 using modelscan.scanners.H5LambdaDetectScan model scan\n",
"\n",
"\u001b[34m--- Summary ---\u001b[0m\n",
"\n",
Expand Down Expand Up @@ -317,17 +330,59 @@
"!modelscan -p KerasModels/unsafe_model.h5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reporting Format\n",
"ModelScan can report scan results in console (default), JSON, or custom report (to be defined by user in settings-file). For mode details, please see: ` modelscan -h` "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## JSON Report\n",
"\n",
"For JSON reporting: `modelscan -p ./path-to/file -r json -o output-file-name.json` \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No settings file detected at /Users/mehrinkiani/Documents/modelscan/notebooks/modelscan-settings.toml. Using defaults. \n",
"\n",
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt:unsafe_model/data.pkl using modelscan.scanners.PickleUnsafeOpScan model scan\n",
"\u001b[1m{\u001b[0m\u001b[32m\"modelscan_version\"\u001b[0m: \u001b[32m\"0.5.0\"\u001b[0m, \u001b[32m\"timestamp\"\u001b[0m: \u001b[32m\"2024-01-25T17:11:52.483755\"\u001b[0m, \n",
"\u001b[32m\"input_path\"\u001b[0m: \n",
"\u001b[32m\"/Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt\"\u001b[0m\n",
", \u001b[32m\"total_issues\"\u001b[0m: \u001b[1;36m1\u001b[0m, \u001b[32m\"summary\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"total_issues_by_severity\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"LOW\"\u001b[0m: \u001b[1;36m0\u001b[0m, \n",
"\u001b[32m\"MEDIUM\"\u001b[0m: \u001b[1;36m0\u001b[0m, \u001b[32m\"HIGH\"\u001b[0m: \u001b[1;36m0\u001b[0m, \u001b[32m\"CRITICAL\"\u001b[0m: \u001b[1;36m1\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m, \u001b[32m\"issues_by_severity\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"CRITICAL\"\u001b[0m: \n",
"\u001b[1m[\u001b[0m\u001b[1m{\u001b[0m\u001b[32m\"description\"\u001b[0m: \u001b[32m\"Use of unsafe operator 'system' from module 'posix'\"\u001b[0m, \n",
"\u001b[32m\"operator\"\u001b[0m: \u001b[32m\"system\"\u001b[0m, \u001b[32m\"module\"\u001b[0m: \u001b[32m\"posix\"\u001b[0m, \u001b[32m\"source\"\u001b[0m: \n",
"\u001b[32m\"/Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt:\u001b[0m\n",
"\u001b[32munsafe_model/data.pkl\"\u001b[0m, \u001b[32m\"scanner\"\u001b[0m: \u001b[32m\"modelscan.scanners.PickleUnsafeOpScan\"\u001b[0m\u001b[1m}\u001b[0m\u001b[1m]\u001b[0m\u001b[1m}\u001b[0m, \n",
"\u001b[32m\"errors\"\u001b[0m: \u001b[1m[\u001b[0m\u001b[1m]\u001b[0m, \u001b[32m\"scanned\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"total_scanned\"\u001b[0m: \u001b[1;36m1\u001b[0m, \u001b[32m\"scanned_files\"\u001b[0m: \n",
"\u001b[1m[\u001b[0m\u001b[32m\"/Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt\u001b[0m\n",
"\u001b[32m:unsafe_model/data.pkl\"\u001b[0m\u001b[1m]\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m\n"
]
}
],
"source": [
"# This will save the scan results in file: keras-model-scan-results.json\n",
"!modelscan --path ./PyTorchModels/unsafe_model.pt -r json -o keras-model-scan-results.json"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.16 ('notebooks-xgb-2')",
"display_name": "Python 3.10.13 ('py310')",
"language": "python",
"name": "python3"
},
Expand All @@ -341,12 +396,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "8792b9cba3843e3778a70fb92fd4a6e11f0e29f4d97b60d08b903ca0b490cce6"
"hash": "bd638e2064d9001d4ca93bc8e56e039dad230900dd235e8a6196f1614960903a"
}
}
},
Expand Down
100 changes: 66 additions & 34 deletions notebooks/pytorch_sentiment_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installing modelscan"
"## Installing ModelScan"
]
},
{
Expand All @@ -23,34 +23,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting modelscan\n",
" Obtaining dependency information for modelscan from https://files.pythonhosted.org/packages/bf/6e/dda8f775b53c307bd59042a7d20143b3f0d068e9b9c6776cf07a28ce208d/modelscan-0.1.1-py3-none-any.whl.metadata\n",
" Using cached modelscan-0.1.1-py3-none-any.whl.metadata (5.1 kB)\n",
"Collecting click<9.0.0,>=8.1.3 (from modelscan)\n",
" Obtaining dependency information for click<9.0.0,>=8.1.3 from https://files.pythonhosted.org/packages/1a/70/e63223f8116931d365993d4a6b7ef653a4d920b41d03de7c59499962821f/click-8.1.6-py3-none-any.whl.metadata\n",
" Using cached click-8.1.6-py3-none-any.whl.metadata (3.0 kB)\n",
"Collecting numpy==1.24.0 (from modelscan)\n",
" Using cached numpy-1.24.0-cp39-cp39-macosx_11_0_arm64.whl (13.9 MB)\n",
"Collecting rich<14.0.0,>=13.4.2 (from modelscan)\n",
" Obtaining dependency information for rich<14.0.0,>=13.4.2 from https://files.pythonhosted.org/packages/8d/5f/21a93b2ec205f4b79853ff6e838e3c99064d5dbe85ec6b05967506f14af0/rich-13.5.2-py3-none-any.whl.metadata\n",
" Using cached rich-13.5.2-py3-none-any.whl.metadata (18 kB)\n",
"Collecting markdown-it-py>=2.2.0 (from rich<14.0.0,>=13.4.2->modelscan)\n",
" Obtaining dependency information for markdown-it-py>=2.2.0 from https://files.pythonhosted.org/packages/42/d7/1ec15b46af6af88f19b8e5ffea08fa375d433c998b8a7639e76935c14f1f/markdown_it_py-3.0.0-py3-none-any.whl.metadata\n",
" Using cached markdown_it_py-3.0.0-py3-none-any.whl.metadata (6.9 kB)\n",
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/mehrinkiani/mambaforge/envs/notebooks-pt/lib/python3.9/site-packages (from rich<14.0.0,>=13.4.2->modelscan) (2.15.1)\n",
"Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich<14.0.0,>=13.4.2->modelscan)\n",
" Using cached mdurl-0.1.2-py3-none-any.whl (10.0 kB)\n",
"Using cached modelscan-0.1.1-py3-none-any.whl (22 kB)\n",
"Using cached click-8.1.6-py3-none-any.whl (97 kB)\n",
"Using cached rich-13.5.2-py3-none-any.whl (239 kB)\n",
"Using cached markdown_it_py-3.0.0-py3-none-any.whl (87 kB)\n",
"Installing collected packages: numpy, mdurl, click, markdown-it-py, rich, modelscan\n",
"Successfully installed click-8.1.6 markdown-it-py-3.0.0 mdurl-0.1.2 modelscan-0.1.1 numpy-1.24.0 rich-13.5.2\n"
"modelscan, version 0.5.0\n"
]
}
],
"source": [
"!pip install modelscan"
"!pip install -q modelscan\n",
"!modelscan -v"
]
},
{
Expand All @@ -73,7 +52,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/mehrinkiani/mambaforge/envs/notebooks-pt/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
"/Users/mehrinkiani/mambaforge/envs/py310/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
},
Expand Down Expand Up @@ -147,7 +126,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Scan the Safe Model\n",
"# Scan Safe Model\n",
"\n",
"The scan results include information on the files scanned, and any issues if found. For the safe model scanned, modelscan finds no model serialization attacks, as expected."
]
Expand All @@ -161,7 +140,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/safe_model.pt:safe_model/data.pkl using pickle model scan\n",
"No settings file detected at /Users/mehrinkiani/Documents/modelscan/notebooks/modelscan-settings.toml. Using defaults. \n",
"\n",
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/safe_model.pt:safe_model/data.pkl using modelscan.scanners.PickleUnsafeOpScan model scan\n",
"\n",
"\u001b[34m--- Summary ---\u001b[0m\n",
"\n",
Expand Down Expand Up @@ -237,7 +218,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Scanning Unsafe Model\n",
"# Scan Unsafe Model\n",
"\n",
"The scan results include information on the files scanned, and any issues if found. In this case, a critical severity level issue is found in the unsafe model scanned. \n",
"\n",
Expand All @@ -253,7 +234,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt:unsafe_model/data.pkl using pickle model scan\n",
"No settings file detected at /Users/mehrinkiani/Documents/modelscan/notebooks/modelscan-settings.toml. Using defaults. \n",
"\n",
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt:unsafe_model/data.pkl using modelscan.scanners.PickleUnsafeOpScan model scan\n",
"\n",
"\u001b[34m--- Summary ---\u001b[0m\n",
"\n",
Expand All @@ -278,7 +261,56 @@
}
],
"source": [
"!modelscan --path ./PyTorchModels/unsafe_model.pt"
"!modelscan --path ./PyTorchModels/unsafe_model.pt "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reporting Format\n",
"ModelScan can report scan results in console (default), json, or custom report (to be defined by user in settings-file). For mode details, please see: ` modelscan -h` "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## JSON Report\n",
"\n",
"For JSON reporting: `modelscan -p ./path-to/file -r json -o output-file-name.json` "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No settings file detected at /Users/mehrinkiani/Documents/modelscan/notebooks/modelscan-settings.toml. Using defaults. \n",
"\n",
"Scanning /Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt:unsafe_model/data.pkl using modelscan.scanners.PickleUnsafeOpScan model scan\n",
"\u001b[1m{\u001b[0m\u001b[32m\"modelscan_version\"\u001b[0m: \u001b[32m\"0.5.0\"\u001b[0m, \u001b[32m\"timestamp\"\u001b[0m: \u001b[32m\"2024-01-25T17:10:54.306065\"\u001b[0m, \n",
"\u001b[32m\"input_path\"\u001b[0m: \n",
"\u001b[32m\"/Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt\"\u001b[0m\n",
", \u001b[32m\"total_issues\"\u001b[0m: \u001b[1;36m1\u001b[0m, \u001b[32m\"summary\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"total_issues_by_severity\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"LOW\"\u001b[0m: \u001b[1;36m0\u001b[0m, \n",
"\u001b[32m\"MEDIUM\"\u001b[0m: \u001b[1;36m0\u001b[0m, \u001b[32m\"HIGH\"\u001b[0m: \u001b[1;36m0\u001b[0m, \u001b[32m\"CRITICAL\"\u001b[0m: \u001b[1;36m1\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m, \u001b[32m\"issues_by_severity\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"CRITICAL\"\u001b[0m: \n",
"\u001b[1m[\u001b[0m\u001b[1m{\u001b[0m\u001b[32m\"description\"\u001b[0m: \u001b[32m\"Use of unsafe operator 'system' from module 'posix'\"\u001b[0m, \n",
"\u001b[32m\"operator\"\u001b[0m: \u001b[32m\"system\"\u001b[0m, \u001b[32m\"module\"\u001b[0m: \u001b[32m\"posix\"\u001b[0m, \u001b[32m\"source\"\u001b[0m: \n",
"\u001b[32m\"/Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt:\u001b[0m\n",
"\u001b[32munsafe_model/data.pkl\"\u001b[0m, \u001b[32m\"scanner\"\u001b[0m: \u001b[32m\"modelscan.scanners.PickleUnsafeOpScan\"\u001b[0m\u001b[1m}\u001b[0m\u001b[1m]\u001b[0m\u001b[1m}\u001b[0m, \n",
"\u001b[32m\"errors\"\u001b[0m: \u001b[1m[\u001b[0m\u001b[1m]\u001b[0m, \u001b[32m\"scanned\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[32m\"total_scanned\"\u001b[0m: \u001b[1;36m1\u001b[0m, \u001b[32m\"scanned_files\"\u001b[0m: \n",
"\u001b[1m[\u001b[0m\u001b[32m\"/Users/mehrinkiani/Documents/modelscan/notebooks/PyTorchModels/unsafe_model.pt\u001b[0m\n",
"\u001b[32m:unsafe_model/data.pkl\"\u001b[0m\u001b[1m]\u001b[0m\u001b[1m}\u001b[0m\u001b[1m}\u001b[0m\n"
]
}
],
"source": [
"# This will save the scan results in file: pytorch-model-scan-results.json\n",
"!modelscan --path ./PyTorchModels/unsafe_model.pt -r json -o pytorch-model-scan-results.json"
]
},
{
Expand All @@ -291,7 +323,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.16 ('notebooks-pt')",
"display_name": "Python 3.10.13 ('py310')",
"language": "python",
"name": "python3"
},
Expand All @@ -305,11 +337,11 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.13"
},
"vscode": {
"interpreter": {
"hash": "95ff33cfa0d19b4e14b9fe5f5453ae67cda9106db59c9cf5735c1bf946b2200c"
"hash": "bd638e2064d9001d4ca93bc8e56e039dad230900dd235e8a6196f1614960903a"
}
}
},
Expand Down
Loading

0 comments on commit bed3d4c

Please sign in to comment.