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.3.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.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: leap_labs_task-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 ed314d66cd12609d63d441916885e6861b6851637c5d8dcf9b535a4585447018
MD5 a9e1e16c3fbb4b94bc0d1e58665dfe2d
BLAKE2b-256 3fc83941aa7c713216c68451eccc03978e85c33e4a0dddb42ec039e60d3e9bd6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: leap_labs_task-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 babc1aa3084b57e47e4b46508f7c76e710cffe931cb51fa1530c054304bea19b
MD5 337cdb6ea476df0826f3321916e1d25d
BLAKE2b-256 bfbb2236fdfd93503d8d700b62a99a088b424bd546ef4a2c8705f5c7de9c8abb

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