Exercise 1
"Evasion" with random noise
Model
Exercise
For fun, let's try to get a German Shepard randomly. Write a loop that,
Generates a random tensor and send it to the model for inference -- try modifying the rescaling of the random noise below (the
*1
, and+0
bits)Store the output index, output label, and image in a tuple
Add that tuple to a list
Stop when you get output index of
235
OR have run1000
queriesLook at a few of the images
Success criteria for this exercise do not require you to actually create a sample of random noise that gets classified as a German Shepherd: that's extremely unlikely (but let us know if it happens!) -- just to try to do it. Please stop at 1000 attempts.
What we expect you to get from this exercise:
Even though the images you have generated at random look like noise, the model still classifies them confidently (why?).
Random search is not an efficient way of generating adversarial samples.
Solution
Initial Parameters:
queries = 1000
: Sets a limit of 1000 queries to the model.target_output = 235
: The target output index the model is expected to predict.output_index = 1000
: Initializes the output index, which will update with each iteration.i = 0
: Counter to track the number of attempts.
Tensor Value Range:
max_val
andmin_val
: Calculate the maximum and minimum values of the image tensor (img_tensor
).modifier
: Sets the range of values for the tensor (max_val - min_val
) to scale the random images generated.
Main Loop:
The
while
loop generates random tensors (tensor = torch.randn(3, 224, 224)
) that are scaled and shifted to match the range ofimg_tensor
.The tensor is resized and passed to
model(tensor)
for prediction.output_index = output[0].argmax()
: Gets the index of the highest-probability class from the model’s output.output_label = labels[output_index]
: Maps the output index to a corresponding label.Every 100 attempts, the code prints the current
output_index
andoutput_label
.
Termination:
The loop stops if
output_index
matchestarget_output
or if the maximum of 1000 iterations is reached.Finally, the last
output_index
andoutput_label
are printed.
Purpose: The code is randomly generating images to attempt to find one that the model classifies as a specific target class (target_output
). This is a common approach in adversarial attacks or model testing where an image is generated to trigger a specific prediction.
Last updated