{"id":38124,"date":"2023-02-06T15:16:34","date_gmt":"2023-02-06T15:16:34","guid":{"rendered":"https:\/\/hypersense.subex.com\/?p=38124"},"modified":"2023-02-06T15:16:34","modified_gmt":"2023-02-06T15:16:34","slug":"adversarial-attacks-a-detailed-review-part-2","status":"publish","type":"post","link":"https:\/\/dev.enki.studio\/test\/?p=38124","title":{"rendered":"Adversarial attacks: A detailed review &#8211; Part 2"},"content":{"rendered":"<p>In previous part, we understood what is and adversarial attack and how it can be classified based on various attributes. (In case you missed it, you can read it\u00a0<a href=\"https:\/\/hypersense.subex.com\/blog\/adversarial-attacks-a-detailed-review\/\">here<\/a>). In this part, we will study some of the most common types of attacks in detail.<\/p>\n<p><strong>Outline<\/strong><\/p>\n<p>In this part we will overview some of the most common attacks on image classifiers and implement a very popular method called FGSM (Fast Gradient Sign Method) attack proposed in\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1412.6572\">Goodfellow et al.<\/a>\u00a0and understand how it works as well as how it challenges the notion of non-linearity of <a href=\"https:\/\/hypersense.subex.com\/aiglossary\/neural-network\/\">neural networks<\/a> being the reason behind success of <a href=\"https:\/\/hypersense.subex.com\/blog\/adversarial-attacks-a-detailed-review\/\">adversarial attacks<\/a>.<\/p>\n<p><strong>L-BFGS Attack<\/strong><\/p>\n<p>This was one of the earliest attacks where\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1312.6199\">Szegedy et al.<\/a>\u00a0first discovered the vulnerability of deep visual models to adversarial perturbations by solving for the following optimization problem:<\/p>\n<figure id=\"attachment_38128\" aria-describedby=\"caption-attachment-38128\" style=\"width: 752px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-38128 size-full\" src=\"https:\/\/hypersense.subex.com\/wp-content\/uploads\/2023\/02\/Adversarial-attacks-A-detailed-review-01.jpg\" alt=\"Adversarial Attacks A detailed review-02\" width=\"752\" height=\"93\" \/><figcaption id=\"caption-attachment-38128\" class=\"wp-caption-text\">equation for L-BFGS attack<\/figcaption><\/figure>\n<p><span style=\"background-color: var(--bs-body-bg); color: var(--bs-body-color); font-family: var(--bs-body-font-family); font-size: var(--bs-body-font-size); font-weight: var(--bs-body-font-weight); text-align: var(--bs-body-text-align);\">where we are trying to minimize \u03c1 (which is the adversary signal) with second-norm. If we look closely, this equation is similar to equation described in part \u2014 1, with norm-value p=2.<\/span><\/p>\n<p>For this problem, approximate solution was computed by\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1312.6199\">Szegedy et al<\/a>. with the Limited Memory Broyden\u2013Fletcher\u2013Goldfarb\u2013Shanno (L-BFGS) algorithm, upon which this method is named. However, solving this equation for large number of examples, is computationally prohibitive, which is addressed in next method. That is when the next method comes into picture.<\/p>\n<p><strong>FGSM Attack<\/strong><\/p>\n<p>The FGSM is among the most influential attacks in the existing literature, especially in the\u00a0<strong><em>white-box<\/em><\/strong>\u00a0setup. Its core concept of performing gradient ascend over the model\u2019s loss surface to fool it, is the basis for a plethora of adversarial attacks. Many follow-up attacks can be strongly related to the original idea of FGSM. The most common image showing adversarial attacks has been of FGSM attack:<\/p>\n<figure id=\"attachment_38129\" aria-describedby=\"caption-attachment-38129\" style=\"width: 752px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-38129\" src=\"https:\/\/hypersense.subex.com\/wp-content\/uploads\/2023\/02\/Adversarial-attacks-A-detailed-review-02.jpg\" alt=\"Adversarial Attacks A detailed review-02\" width=\"752\" height=\"307\" \/><figcaption id=\"caption-attachment-38129\" class=\"wp-caption-text\">A common example of FGSM attack<\/figcaption><\/figure>\n<p>The FGSM is a one-step gradient-based method that computes norm-bounded perturbations, focusing on the \u2018efficiency\u2019 of perturbation computation rather than achieving high fooling rates.\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1412.6572\">Goodfellow et al.<\/a>\u00a0also used this attack to corroborate their\u00a0<strong><em>linearity hypothesis<\/em><\/strong>, which considers the linear behavior of the modern neural networks in high dimension spaces (induced by ReLUs) as a sufficient reason for their vulnerability to adversarial perturbations. At the time, the linearity hypothesis was in sharp contrast to the developing idea that adversarial vulnerability was a result of high \u2018<em>non-linearity<\/em>\u2019 of the complex modern networks.<\/p>\n<p><a href=\"https:\/\/arxiv.org\/abs\/1412.6572\">Goodfellow et al.<\/a>\u00a0claimed that adversarial examples expose fundamental blind spots in our training algorithms. They also claimed that linear behavior in high-dimensional spaces is sufficient to cause adversarial examples. And using this, they designed a fast method of generating adversarial examples that makes adversarial training practical.<\/p>\n<p><strong>Linear explanation of adversarial examples<\/strong><\/p>\n<p>We start with explaining the existence of adversarial examples for linear models. Since the precision of the features is limited, we will see how the classifier can be forced to respond differently to an input x than to an adversarial input\u00a0<em>x\u0303<\/em>\u00a0= x + \u03b7 if every element of the perturbation \u03b7 is smaller than the precision of the features.<\/p>\n<p>Adversarial attacks for linear model<\/p>\n<p>Thus, for high dimensional problems, we can make many infinitesimal changes to the input that will add up to one large change to the output.<\/p>\n<p><strong>Linear Perturbation of Non-Linear Models<\/strong><\/p>\n<p>The linear view of adversarial examples suggests a fast way of generating them. We hypothesize that neural networks are too linear to resist linear adversarial perturbation. LSTMs, ReLUs, and maxout networks are all intentionally designed to behave in very linear ways, so that they are easier to optimize. More nonlinear models such as sigmoid networks are carefully tuned to spend most of their time in the non-saturating, more linear regime for the same reason. This linear behavior suggests that cheap, analytical perturbations of a linear model should also damage neural networks. Let us see how adversarial examples can be generated for the neural networks.<\/p>\n<figure id=\"attachment_38130\" aria-describedby=\"caption-attachment-38130\" style=\"width: 752px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-38130\" src=\"https:\/\/hypersense.subex.com\/wp-content\/uploads\/2023\/02\/Adversarial-attacks-A-detailed-review-03.jpg\" alt=\"Adversarial Attacks A detailed review-02\" width=\"752\" height=\"578\" \/><figcaption id=\"caption-attachment-38130\" class=\"wp-caption-text\">Adversarial perturbation for neural networks<\/figcaption><\/figure>\n<p>We refer to this as the \u201c<strong>fast gradient sign method<\/strong>\u201d of generating adversarial examples. Note that the required gradient can be computed efficiently using backpropagation.<\/p>\n<p><strong>Code Implementation for FGSM<\/strong><\/p>\n<p>Let us see how we can implement this in code. We will be using code from this\u00a0<a href=\"https:\/\/www.tensorflow.org\/tutorials\/generative\/adversarial_fgsm\">link<\/a>, which is part of tensorflow official documentation. We will analyze this function\u00a0<em>\u2018create_adversarial_pattern\u2019,\u00a0<\/em>as it implements the crux of the paper,\u00a0<em>i.e.\u00a0<\/em><strong>calculates the gradient sign<\/strong>.<\/p>\n<pre>def create_adversarial_pattern(input_image, input_label):\nwith tf.GradientTape() as tape:\ntape.watch(input_image)\nprediction = pretrained_model(input_image)\nloss = loss_object(input_label, prediction)\n\n# Get the gradients of the loss w.r.t to the input image.\ngradient = tape.gradient(loss, input_image)\n# Get the sign of the gradients to create the perturbation\nsigned_grad = tf.sign(gradient)\nreturn signed_grad\n\n# codel_url: https:\/\/www.tensorflow.org\/tutorials\/generative\/adversarial_fgsm<\/pre>\n<p>&nbsp;<\/p>\n<p>Using below lines, we are basically asking tensorflow to keep track of computations related to \u2018<em>input_image\u2019<\/em><\/p>\n<pre>with tf.GradientTape() as tape:\ntape.watch(input_image)<\/pre>\n<p>&nbsp;<\/p>\n<p>Using below two lines, we are making predictions for <em>\u2018input_image\u2019,<\/em>\u00a0and calculating the loss related to this prediction.<\/p>\n<pre>prediction = pretrained_model(input_image)\nloss = loss_object(input_label, prediction)<\/pre>\n<p>&nbsp;<\/p>\n<p>Using below lines, we are calculating the gradient of loss wrt input image, this part contributes to the \u2018<em>gradient<\/em>\u2019 term in the FGSM (Fast\u00a0<strong>Gradient\u00a0<\/strong>Sign Method)<\/p>\n<pre>gradient = tape.gradient(loss, input_image)<\/pre>\n<p>&nbsp;<\/p>\n<p>Once we have gradients, we need to calculate use \u2018sign\u2019 function of gradient, i.e, sign(gradient), and we have \u2018<em>gradient sign<\/em>\u2019 term in FGSM (Fast\u00a0<strong>Gradient Sign<\/strong>\u00a0Method). Below is the input image, for which we are calculation adversarial perturbation (\u03b7).<\/p>\n<figure id=\"attachment_38132\" aria-describedby=\"caption-attachment-38132\" style=\"width: 526px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-38132\" src=\"https:\/\/hypersense.subex.com\/wp-content\/uploads\/2023\/02\/Adversarial-attacks-A-detailed-review-05.png\" alt=\"Adversarial Attacks A detailed review-02\" width=\"526\" height=\"520\" \/><figcaption id=\"caption-attachment-38132\" class=\"wp-caption-text\">Prediction on input image<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Calculated adversarial perturbation (\u03b7) using FGSM method comes out as:<\/p>\n<figure id=\"attachment_38133\" aria-describedby=\"caption-attachment-38133\" style=\"width: 752px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-38133\" src=\"https:\/\/hypersense.subex.com\/wp-content\/uploads\/2023\/02\/Adversarial-attacks-A-detailed-review-06.jpg\" alt=\"Adversarial Attacks A detailed review-02\" width=\"752\" height=\"742\" \/><figcaption id=\"caption-attachment-38133\" class=\"wp-caption-text\">Calculated adversarial perturbation (\u03b7) using FGSM<\/figcaption><\/figure>\n<p>Adversarial image to fool the model is calculated using the below code:<\/p>\n<pre>adv_x = image + eps*perturbations<\/pre>\n<p>&nbsp;<\/p>\n<p>Here is what the generated result looks like for different values of \u03f5<\/p>\n<figure id=\"attachment_38134\" aria-describedby=\"caption-attachment-38134\" style=\"width: 752px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-38134\" src=\"https:\/\/hypersense.subex.com\/wp-content\/uploads\/2023\/02\/Adversarial-attacks-A-detailed-review-07.jpg\" alt=\"Adversarial Attacks A detailed review-02\" width=\"752\" height=\"262\" \/><figcaption id=\"caption-attachment-38134\" class=\"wp-caption-text\">Generated adversarial examples for different values of epsilon (0.01, 0.1, 0.15)<\/figcaption><\/figure>\n<p><a href=\"https:\/\/arxiv.org\/abs\/1412.6572\">Goodfellow et al.<\/a>\u00a0concluded the following things as a result of this experiment:<\/p>\n<ul>\n<li>Adversarial examples can be explained as a property of high-dimensional dot products. They are a result of models being too linear, rather than too nonlinear.<\/li>\n<li>The generalization of adversarial examples across different models can be explained as a result of adversarial perturbations being highly aligned with the weight vectors of a model, and different models learning similar functions when trained to perform the same task.<\/li>\n<li>The direction of perturbation, rather than the specific point in space, matters most. Space is not full of pockets of adversarial examples that finely tile the reals like the rational numbers.<\/li>\n<li>Because it is the direction that matters most, adversarial perturbations generalize across different clean examples.<\/li>\n<\/ul>\n<p><strong>Other attacks<\/strong><\/p>\n<ul>\n<li>Some other attacks that built on FGSM attack or had similar methodology are:\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1607.02533\">Basic Iterative Method (BIM)<\/a>,\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1706.06083\">The Projected Gradient Descent (PGD) attack,<\/a><a href=\"https:\/\/arxiv.org\/abs\/1511.04599\">The DeepFool Attack<\/a>,\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1608.04644\">The C&amp;W Attack<\/a>.<\/li>\n<li><strong>JSMA &amp; One-pixel Attack:\u00a0<\/strong>Whereas most of the early attacks focused on perturbing a clean image holistically by limiting perturbation by minimizing\u00a0<a href=\"https:\/\/en.wiktionary.org\/wiki\/two-norm#:~:text=two%2Dnorm%20(plural%20two%2D,two%2Dnorm%20of%20a%20vector\">second-norm<\/a>or\u00a0<a href=\"https:\/\/fncbook.github.io\/v1.0\/linsys\/norms.html#:~:text=The%20infinity%20norm%20of%20a,all%20after%20taking%20absolute%20values.&amp;text=In%20words%2C%20the%20infinity%20norm,is%20the%20maximum%20column%20sum.\">infinity-norm<\/a>\u00a0of the perturbations, the\u00a0<a href=\"https:\/\/arxiv.org\/pdf\/1511.07528.pdf&amp;xid=25657,15700023,15700124,15700149,15700186,15700191,15700201,15700237,15700242.pdf\">Jacobian-based Saliency Map Attack (JSMA)<\/a>\u00a0and\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1710.08864\">One-pixel attack<\/a>\u00a0deviate from this practice by restricting the perturbations to smaller regions of the image.<\/li>\n<li><strong>Universal Adversarial Perturbations:<\/strong>The above-mentioned methods compute adversarial perturbations that fool a target model on a specific image.\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1610.08401\">Moosavi-Dezfooli et al<\/a>. focused on computing image-agnostic perturbations that could fool the model on any image with a high probability as shown in below image:<\/li>\n<\/ul>\n<figure id=\"attachment_38135\" aria-describedby=\"caption-attachment-38135\" style=\"width: 752px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-38135\" src=\"https:\/\/hypersense.subex.com\/wp-content\/uploads\/2023\/02\/Adversarial-attacks-A-detailed-review-08.jpg\" alt=\"Adversarial Attacks A detailed review-02\" width=\"752\" height=\"605\" \/><figcaption id=\"caption-attachment-38135\" class=\"wp-caption-text\">A single Universal Adversarial Perturbation can fool a model on multiple images. Fooling of GoogLeNet is shown here. These perturbations often transfer well across different models <b>(<a href=\"https:\/\/arxiv.org\/abs\/2108.00401\" target=\"_blank\" rel=\"noopener\">Source<\/a>)<\/b><\/figcaption><\/figure>\n<p>Thus, we studied FGSM attack in details and understood its implementation in tensorflow. We also overviewed some other attacks on image classification. In further parts, we will be moving beyond classification and seeing how <a href=\"https:\/\/hypersense.subex.com\/blog\/adversarial-attacks-a-detailed-review\/\">adversarial attacks<\/a> can be performed on other tasks such as Face Recognition, Object detection, Object Tracking and how they can affect the real world in several ways.<\/p>\n<p style=\"text-align: center; font-size: 22px;\"><span style=\"color: #a020f0;\"><strong>Leapfrog your Enterprise AI adoption journey<\/strong><\/span><\/p>\n<p style=\"text-align: center;\"><a class=\"qodef-button qodef-layout--filled qodef-html--link\" href=\"https:\/\/hypersense.subex.com\/request-demo\/\" target=\"_blank\" rel=\"noopener noreferrer\">Request Demo!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous part, we understood what is and adversarial attack and how it can be classified based on various attributes. (In case you missed it, you can read it\u00a0here). In this part, we will study some of the most common types of attacks in detail. Outline In this part we will overview some of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":38141,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[270],"tags":[],"class_list":["post-38124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence"],"acf":[],"_links":{"self":[{"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=\/wp\/v2\/posts\/38124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38124"}],"version-history":[{"count":0,"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=\/wp\/v2\/posts\/38124\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.enki.studio\/test\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}