Skip to content

Commit

Permalink
Fix error in getting started notebook (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
cassidylaidlaw committed May 31, 2022
1 parent 2ffd0f4 commit c030c00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 63 deletions.
16 changes: 4 additions & 12 deletions getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,12 @@
"outputs": [],
"source": [
"attack = get_attack_from_name('recoloradv+stadv+delta', model, normalizer, verbose=True)\n",
"perturbation = attack.attack(examples, labels)\n",
"adv_examples = attack.attack(examples, labels)[0].detach()\n",
"\n",
"# Display the successful adversarial examples.\n",
"successful_advs, successful_origs = perturbation.collect_successful(model, normalizer)\n",
"successful_diffs = ((successful_advs - successful_origs) * 3 + 0.5).clamp(0, 1)\n",
"img_utils.show_images([successful_origs, successful_advs, successful_diffs])"
"# Display the adversarial examples.\n",
"diffs = ((adv_examples - examples) * 3 + 0.5).clamp(0, 1)\n",
"img_utils.show_images([examples, adv_examples, diffs])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
101 changes: 50 additions & 51 deletions getting_started_colab.ipynb
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3.7",
"language": "python",
"name": "python3.7"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"colab": {
"name": "getting_started.ipynb",
"provenance": [],
"toc_visible": true
}
},
"cells": [
{
"cell_type": "markdown",
Expand All @@ -48,9 +22,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6pWDR8eO_3Ch"
},
"outputs": [],
"source": [
"!pip install recoloradv\n",
"\n",
Expand Down Expand Up @@ -84,9 +60,7 @@
"\n",
"# Set up CIFAR-10\n",
"!python -m recoloradv.mister_ed.scripts.setup_cifar"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -117,9 +91,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "DoqMhAhp_3Cm"
},
"outputs": [],
"source": [
"cifar_valset = cifar_loader.load_cifar_data('val', batch_size=16)\n",
"examples, labels = next(iter(cifar_valset))\n",
Expand All @@ -130,9 +106,7 @@
" examples = examples.cuda()\n",
" labels = labels.cuda() \n",
" model.cuda()"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -145,14 +119,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jnjBiqHr_3Cs"
},
"outputs": [],
"source": [
"img_utils.show_images(examples)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -166,9 +140,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "nc48oTVS_3Cw"
},
"outputs": [],
"source": [
"# This threat model defines the regularization parameters of the attack.\n",
"recoloradv_threat = ap.ThreatModel(pt.ReColorAdv, {\n",
Expand Down Expand Up @@ -208,9 +184,7 @@
"successful_advs, successful_origs = perturbation.collect_successful(model, normalizer)\n",
"successful_diffs = ((successful_advs - successful_origs) * 3 + 0.5).clamp(0, 1)\n",
"img_utils.show_images([successful_origs, successful_advs, successful_diffs])"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -233,9 +207,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KghrZOmv_3C0"
},
"outputs": [],
"source": [
"# First, we define the additive threat model.\n",
"additive_threat = ap.ThreatModel(ap.DeltaAddition, {\n",
Expand Down Expand Up @@ -267,9 +243,7 @@
"successful_advs, successful_origs = perturbation.collect_successful(model, normalizer)\n",
"successful_diffs = ((successful_advs - successful_origs) * 3 + 0.5).clamp(0, 1)\n",
"img_utils.show_images([successful_origs, successful_advs, successful_diffs])"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -292,20 +266,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-QwYSgtd_3C5"
},
"outputs": [],
"source": [
"attack = get_attack_from_name('recoloradv+stadv+delta', model, normalizer, verbose=True)\n",
"perturbation = attack.attack(examples, labels)\n",
"adv_examples = attack.attack(examples, labels)[0].detach()\n",
"\n",
"# Display the successful adversarial examples.\n",
"successful_advs, successful_origs = perturbation.collect_successful(model, normalizer)\n",
"successful_diffs = ((successful_advs - successful_origs) * 3 + 0.5).clamp(0, 1)\n",
"img_utils.show_images([successful_origs, successful_advs, successful_diffs])"
],
"execution_count": null,
"outputs": []
"# Display the adversarial examples.\n",
"diffs = ((adv_examples - examples) * 3 + 0.5).clamp(0, 1)\n",
"img_utils.show_images([examples, adv_examples, diffs])"
]
}
],
"metadata": {
"colab": {
"name": "getting_started.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3.7",
"language": "python",
"name": "python3.7"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
]
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit c030c00

Please sign in to comment.