Skip to main content

Ahmed Shahin submission for leap labs task -- adversarial noise

Project description

Adversarial Generation Task

Problem: Adversarial Noise

The task involves developing a program that manipulates images by adding adversarial noise. This noise is designed to trick an image classification model into misclassifying the altered image as a specified target class, regardless of the original content.

You may select any pre-trained image classification model for this task. A model from the torchvision library is recommended, but not mandatory. The core challenge is to effectively introduce noise into the image in such a way that the model misclassifies it as the desired target class, without making the noise perceptible to a casual human viewer.

Input: The user will provide an image and specify a target class.

Output: The program should output an image that has been altered with adversarial noise. The altered image should be classified by the model as the target class, irrespective of the original image's content. The altered image should not be obviously different to the original.

My Approach

My approach depends on using gradient ascent to maximize the probability of the target class. The gradient ascent is applied to the input image, and the gradient is calculated with respect to the target class. The gradient ascent is applied iteratively to the input image until the model classifies the image as the target class. The update formula is as follows: $$ \hat{x} = x + \epsilon * \frac{d}{dx} \log p(y_{target}|x) $$ where:

  • $\hat{x}$ is the updated image
  • $x$ is the original image
  • $\epsilon$ is the step size
  • $p(y_{target}|x)$ is the probability of the target class given the image (softmax output of the model)
  • $\frac{d}{dx} \log p(y_{target}|x)$ is the gradient of the log probability of the target class with respect to the image

The user will provide an image and specify a target class and optionally specify the number of iterations, the step size, and the threshold for the probability of the target class. The program will output an image that has been altered with adversarial noise. The altered image should be classified by the model as the target class, irrespective of the original image's content. The altered image should not be obviously different from the original.

Sample output

The example below shows the original image, the target class, and the altered image. The altered image is classified as the target class by the model with high confidence. Example

Usage

The package can be installed using pip:

pip install leap-labs-task

From python, the user can then run the program by providing the path to the image, the target class, and optionally the number of iterations, the step size, and the threshold for the probability of the target class. The program will output the altered image and the probability of the target class.

Example:

from leap_labs_task import AdversarialGenerator, show_images

image_path = '/path/to/image.jpg'
target_class = 'goldfish'
iterations = 100
epsilon = 0.01
desired_confidence = 0.9
pretrained_model = 'resnet50'

generator = AdversarialGenerator(pretrained_model)
output = generator(image_path, target_class, epsilon, desired_confidence, iterations)
show_images(output)

The values of iterations, epsilon, and desired_confidence might need to be adjusted based on the image and the target class to achieve the desired result.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leap_labs_task-0.1.4.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

leap_labs_task-0.1.4-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file leap_labs_task-0.1.4.tar.gz.

File metadata

  • Download URL: leap_labs_task-0.1.4.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.8.0-49-generic

File hashes

Hashes for leap_labs_task-0.1.4.tar.gz
Algorithm Hash digest
SHA256 405e68cdca533ec711a9991e395db3001acd0dc1451482c1be5d8138011268e6
MD5 310caf7c587b0f5a3a05a59b2070bd9f
BLAKE2b-256 d9415fb477c7d051327bc20cf8253115dd8a1d2a492420d225b47246300dac50

See more details on using hashes here.

File details

Details for the file leap_labs_task-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: leap_labs_task-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.8.0-49-generic

File hashes

Hashes for leap_labs_task-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9b3e63e455a0d666fc3fbf95b69a5d07225034769801d3b34b7e3c5c7fae2350
MD5 37bdf1b2087a23cd30ec474218c8f8c2
BLAKE2b-256 86529b5dbf11d1586b8732c1ef8d48324aa94e98c3338e51cd18373e41ea13da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page