<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muhammad Mubeen on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Mubeen on Medium]]></description>
        <link>https://medium.com/@muhammadmubeen?source=rss-166434437228------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*q5DWxjhcVh4bI5Y4.</url>
            <title>Stories by Muhammad Mubeen on Medium</title>
            <link>https://medium.com/@muhammadmubeen?source=rss-166434437228------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:15:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@muhammadmubeen/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Generating Adversarial Images Using AlexNet]]></title>
            <link>https://medium.com/@muhammadmubeen/generating-adversarial-images-using-alexnet-924ecb78ba4f?source=rss-166434437228------2</link>
            <guid isPermaLink="false">https://medium.com/p/924ecb78ba4f</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[adversarial-network]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Muhammad Mubeen]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 18:59:40 GMT</pubDate>
            <atom:updated>2018-01-16T19:00:24.168Z</atom:updated>
            <content:encoded><![CDATA[<p>So, here’s is my first blog in which I will show you how to generate an Adversarial Images using AlexNet. This tutorial is a part of my semester project, which was to fool a CNN into predicting another class label for some image by minimally updating the image. It is largely inspired from this <a href="http://www.anishathalye.com/2017/07/25/synthesizing-adversarial-examples/">blog</a>.</p><p><strong>Note: </strong>This tutorial has been written using PyTorch and uses AlexNet as the classifier. This network can be easily replaced by any other network like VGG-Net or GoogleNet with minimal effort.</p><p>So, as it turns out that it is not very difficult to fool our state of the art classifier to mis-classify the input image. All we have to do is to update our input image slightly based on our required class.</p><h3>Procedure</h3><p>The basic steps in order to fool our classifier are: -</p><ol><li>First use classifier (in our case AlexNet) to classify the input image. The final output of the classifier would be a probability vector for different classes of ImageNet.</li><li>Create another vector (1000 numbers) of all zeros except at the specific index whose class probability we want to maximize there should be 1. Calculate the cost/loss between the two vectors. You can use any cost metric like Cross-Entropy, KL-Divergence .etc to do so.</li><li>Back-propagate this cost/loss but only update the input not the weights of the Network.</li><li>Repeat this process but this time with the updated input image. This should be done until sufficiently good results are obtained i.e. in our case, the loss decreases.</li></ol><h3>Working</h3><h4>Setup</h4><p>So, let’s try to practically fool our Network. To do so, we first need to import our necessary modules.</p><pre>import numpy<br>import matplotlib.pyplot as plt<br>import cv2<br>from caffe_classes import class_names<br>import torch<br>import torch.nn as nn<br>from torch.autograd import Variable<br>import torch.nn.functional as F<br>from torch.optim import Adam</pre><p>The caffe_classes.py file can be downloaded from this <a href="https://github.com/guerzh/tf_weights/blob/master/caffe_classes.py">link</a>. It contains a list of ImageNet class names.</p><h4>Loading AlexNet</h4><p>After successful importing of required modules, we now move towards our next step of creating a class for AlexNet. Thankfully, PyTorch not only provides the network structure for AlexNet but it also provides the trained weights (unlike Tensorflow). You can download the weights of AlexNet from this <a href="https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth">link</a>. The AlexNet class is defined as: -</p><pre>class AlexNet(nn.Module):<br>    def __init__(self, num_classes=1000):<br>        super(AlexNet, self).__init__()<br>        self.features = nn.Sequential(<br>            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),<br>            nn.ReLU(inplace=True),<br>            nn.MaxPool2d(kernel_size=3, stride=2),<br>            nn.Conv2d(64, 192, kernel_size=5, padding=2),<br>            nn.ReLU(inplace=True),<br>            nn.MaxPool2d(kernel_size=3, stride=2),<br>            nn.Conv2d(192, 384, kernel_size=3, padding=1),<br>            nn.ReLU(inplace=True),<br>            nn.Conv2d(384, 256, kernel_size=3, padding=1),<br>            nn.ReLU(inplace=True),<br>            nn.Conv2d(256, 256, kernel_size=3, padding=1),<br>            nn.ReLU(inplace=True),<br>            nn.MaxPool2d(kernel_size=3, stride=2),<br>        )<br>        self.classifier = nn.Sequential(<br>            nn.Dropout(),<br>            nn.Linear(256 * 6 * 6, 4096),<br>            nn.ReLU(inplace=True),<br>            nn.Dropout(),<br>            nn.Linear(4096, 4096),<br>            nn.ReLU(inplace=True),<br>            nn.Linear(4096, num_classes),<br>        )</pre><pre>    def forward(self, x):<br>        x = self.features(x)<br>        x = x.view(x.size(0), 256 * 6 * 6)<br>        x = self.classifier(x)<br>        return x</pre><p>After creating a class for AlexNet, we now create its object and then load its predefined weights. Since, we do not want to update the model weights, we set the model.requires_grad to False.</p><pre>pretrained = True<br>alexnet_path = &#39;models/alexnet-owt-4df8aa71.pth&#39;</pre><pre>print(&quot;Creating Network&quot;)<br>model = AlexNet()<br>model.cuda()<br>model.requires_grad = False</pre><pre>print(&quot;Loading Network&quot;)<br>if pretrained == True:<br>    model.load_state_dict(torch.load(alexnet_path))<br>    print(&quot;Network Loaded Successfully&quot;)</pre><p>Make sure you set the path for AlexNet weights alexnet_path correctly.</p><h4>Helper Functions</h4><p>After successful loading of model weights, we create a function to preprocess our image so that it can be used for classification by AlexNet. The function simply resizes the image, converts it into RGB format, makes the mean zero and standard deviation 1 and in the end it converts the input into <strong>Channels x Height x Width</strong> format which is required by Pytorch.</p><pre>def preprocess(image):<br>    ALEX_MEAN = [0.485, 0.456, 0.406]<br>    ALEX_STD=[0.229, 0.224, 0.225]<br>    <br>    out = cv2.resize(image, (227, 227))<br>    out = out.astype(numpy.float32) / 256.<br>    image = numpy.clip(image, 0, 1)<br>    out = out[:, :, [2, 1, 0]]  # swap channel from BGR to RGB<br>    out[:, :, 0] = (out[:, :, 0] - ALEX_MEAN[0]) / ALEX_STD[0]<br>    out[:, :, 1] = (out[:, :, 1] - ALEX_MEAN[1]) / ALEX_STD[1]<br>    out[:, :, 2] = (out[:, :, 2] - ALEX_MEAN[2]) / ALEX_STD[2]<br>    out = numpy.transpose(out, (2, 0, 1))<br>    return out</pre><p>Similarly, in order to deprocess our final output i.e. extract it from the Torch Autograd Variable, another function is created. It simply extracts the image from Variable and then does opposite of all what was done in the preprocess function.</p><pre>def deprocess(tensor):<br>    ALEX_MEAN = [0.485, 0.456, 0.406]<br>    ALEX_STD=[0.229, 0.224, 0.225]<br>    <br>    out = tensor.data.cpu().numpy()<br>    out = numpy.reshape(out,[3, 227, 227])<br>    out = numpy.transpose(out, (1, 2, 0))<br>    <br>    out[:, :, 0] = (out[:, :, 0] * ALEX_STD[0]) + ALEX_MEAN[0]<br>    out[:, :, 1] = (out[:, :, 1] * ALEX_STD[1]) + ALEX_MEAN[1]<br>    out[:, :, 2] = (out[:, :, 2] * ALEX_STD[2]) + ALEX_MEAN[2]<br>    out = out[:, :, [2, 1, 0]]  # swap channel from RGB to BGR<br>    out = numpy.clip(out, 0, 1)<br>    out = out * 256<br>    <br>    out = out.astype(numpy.uint8)<br>    return out</pre><h4>Example Image</h4><p>Now we load an input image and show it to the user.</p><pre>image_path = &quot;cat.png&quot;<br>image = cv2.imread(image_path)<br>original_image = numpy.copy(image)<br>plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*clsHLzT3S8edx5FSMd_THA.png" /><figcaption>An Image of a Tabby, Tabby Cat</figcaption></figure><p>The next step is to run preprocessing on the input image and then predict its class. For this, we first apply the preprocess function and then convert it into a torch tensor and in the end into Torch Autograd Variable. Importantly, we want to update our input, so we set the requires_grad to True for the img_variable.</p><pre>img = preprocess(image=original_image)<br>img_tensor = torch.from_numpy(img)<br>img_tensor = img_tensor.unsqueeze_(0)<br>img_variable = Variable(img_tensor.cuda(), requires_grad=True)</pre><p>After this, we run the prediction on this input and print the results. This is done using: -</p><pre>fc_out = model(img_variable)<br>fc_out = F.softmax(fc_out, dim=1)<br>class_name = class_names[fc_out.data.cpu().numpy().argmax()]<br>print(&quot;\nPredicted Class No.: {}&quot;. format(fc_out.data.cpu().numpy().argmax()))<br>print(&quot;Predicted Class Label: {}&quot;.format(class_name))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*HI-xVS0Jt8DKQL0YdNjFdA.png" /><figcaption>Prediction Results on Input Image</figcaption></figure><p>We can see that the classifier predicts that it is a tabby, tabby cat and it sure looks like one. We can also print the top-10 probabilities of various classes of the image.</p><pre>max_indices = numpy.argsort(numpy.reshape(fc_out.data.cpu().numpy(), [1000,]))[990:1000]<br>max_vals = numpy.reshape(fc_out.data.cpu().numpy(), [1000,])[max_indices]<br>fig, ax = plt.subplots(1, 1, figsize=(10, 8))<br>rects = plt.bar(numpy.arange(10), max_vals, 0.8, color=&#39;g&#39;)<br>rects[9].set_color(&#39;r&#39;)<br>plt.xticks(range(10),<br>               [class_names[i][:15] for i in max_indices],<br>               rotation=&#39;vertical&#39;)<br>plt.tight_layout<br>plt.xlabel(&#39;Labels&#39;)<br>plt.ylabel(&#39;Probabability&#39;)<br>plt.title(&#39;Top 10 Probabilities of Input Image&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*bGn_yi0soUO7qP2v2hpK8g.png" /></figure><p>And we can see that the maximum probability is of the tabby, tabby cat class which is around 0.8.</p><h4>Target Class</h4><p>Now, in the next step, we select a target class i.e. the class which we want to classifier to predict for the image as a fooling. In our case we choose target_class to be 150. This class corresponds to sea loin in the ImageNet database.</p><pre>target_class = 150<br>target_vector = numpy.array([target_class], dtype=numpy.int64)<br>target_tensor = torch.from_numpy(target_vector)<br>target_variable = Variable(target_tensor.cuda())</pre><h4>Optimizer Step and Loss Function</h4><p>Since, for our proposed methodology for adversarial example generation we have to update in input image, so we create an optimizer to update the input. In our case, we created an Adam optimizer.</p><pre>optimizer = Adam([img_variable], lr=0.001)</pre><p>Moreover, we require a loss metric to check the cost of our actual output and the desired output, on the basis of which our input will be updated. In our case, the cross-entropy loss was used.</p><pre>criteron = nn.CrossEntropyLoss()</pre><h4>Execution</h4><p>Now, we simply run a loop in which we check the loss value (between desired output and output of classifier) and then update our input accordingly. For the next iteration the updated input would be used.</p><pre>iterations = 501<br>print(&quot;\nStarting Optimization...&quot;)<br>for iteration in range(1,iterations):<br>    fc_out = model(img_variable)<br>    fc_out = F.softmax(fc_out, dim=1)<br>    <br>    optimizer.zero_grad()<br>    loss = criteron(fc_out, target_variable)<br>    <br>    loss.backward()<br>    optimizer.step()<br>    if iteration == 1 or iteration%10 == 0:<br>        print(&quot;Current Iteration: {}\tCurrent Cost: {}&quot;.format(iteration, numpy.round(loss.data[0], 6)))</pre><p>The output of which is: -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/409/1*nF9zDwiHvGqO_aRzuQhDAQ.png" /><figcaption>Optimization Iterations Output (uptil 290 iterations only)</figcaption></figure><p>We can clearly see that around iteration 200, there is a drop in the cost, which may be an indication that our input has been altered in such a way that the classifier is getting fooled now.</p><h4>Final Output</h4><p>After optimization, we extract our final updated image and store it. Moreover, show the output to the user on the screen.</p><pre>final_image = deprocess(img_variable)<br>cv2.imwrite(&quot;output.jpg&quot;, final_image)<br>plt.imshow(cv2.cvtColor(cv2.imread(&quot;output.jpg&quot;),cv2.COLOR_BGR2RGB))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*_GfZfJJCF9sz8BRn26lKYA.png" /><figcaption>Final Updated Image</figcaption></figure><p>And here we can clearly see there is not much difference between the input and the updated image. They look the same image.</p><p>Now, lets run the prediction on this updated image.</p><pre>fc_out = model(img_variable)<br>fc_out = F.softmax(fc_out, dim=1)<br>class_name = class_names[fc_out.data.cpu().numpy().argmax()]<br>print(&quot;Predicted Class No.: {}&quot;. format(fc_out.data.cpu().numpy().argmax()))<br>print(&quot;Predicted Class: {}&quot;.format(class_name))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*91rW2Z3O3HolDdhhDvtoiQ.png" /><figcaption>Prediction on Output Image</figcaption></figure><p>And Ka-Boom!!! It has been predicted as a sea-loin. xD</p><p>We can also print the top-10 probabilities for the output image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*ez_ZuyTwqO7F_l0KnobpuQ.png" /></figure><p>And we can see that how much we have fooled our classifier that it is predicting the image of tabby cat to a sea-loin and that too with a <strong>100%</strong> probability.</p><h3>End Notes</h3><p>I have provided the code for this project as a <strong>Jupyter Notebook</strong>. You can download the code from my <a href="https://github.com/mmubeen-6/Adversarial-Images-Generation">github repository</a>. Feel free to play with it and try some new things :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=924ecb78ba4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>