Finished testing random noise and threshold filter
This commit is contained in:
parent
35fc6dd2e5
commit
3c35a2bc02
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 115 KiB |
BIN
Filter_Analysis/Images/RandomNoisePerformance.png
Normal file
BIN
Filter_Analysis/Images/RandomNoisePerformance.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 133 KiB |
BIN
Filter_Analysis/Images/ThresholdFilterPerformance.png
Normal file
BIN
Filter_Analysis/Images/ThresholdFilterPerformance.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 93 KiB |
Binary file not shown.
@ -4,7 +4,6 @@ import numpy as np
|
||||
import torch
|
||||
|
||||
|
||||
|
||||
# Turn a pytorch tensor into an image
|
||||
def pttensor_to_images(data):
|
||||
images = None
|
||||
|
@ -118,10 +118,10 @@ def test(model, device, test_loader, epsilon):
|
||||
|
||||
# Evaluate performance for
|
||||
for i in range(TESTED_STRENGTH_COUNT):
|
||||
strength = 2*i + 1
|
||||
strength = (2*i + 1)/10
|
||||
|
||||
# Apply the filter with the specified strength
|
||||
filtered_input = defense_filters.gaussian_blur(perturbed_data_normalized, batch_size=len(perturbed_data_normalized), ksize=(strength, strength))
|
||||
filtered_input = defense_filters.threshold_filter(perturbed_data_normalized, batch_size=len(perturbed_data_normalized), threshold=strength)
|
||||
# Evaluate the model on the filtered images
|
||||
filtered_output = model(filtered_input)
|
||||
# Get the predicted classification
|
||||
@ -157,8 +157,8 @@ def test(model, device, test_loader, epsilon):
|
||||
print(f"Clean (No Filter) Accuracy = {orig_correct} / {len(test_loader)} = {orig_acc}")
|
||||
print(f"Unfiltered Accuracy = {unfiltered_correct} / {len(test_loader)} = {unfiltered_acc}")
|
||||
for i in range(TESTED_STRENGTH_COUNT):
|
||||
strength = 2*i + 1
|
||||
print(f"Gaussian Blur (strength = {strength}) = {filtered_correct_counts[i]} / {len(test_loader)} = {filtered_accuracies[i]}")
|
||||
strength = (2*i + 1)/10
|
||||
print(f"Threshold Filter (threshold = {strength}) = {filtered_correct_counts[i]} / {len(test_loader)} = {filtered_accuracies[i]}")
|
||||
|
||||
return unfiltered_acc, filtered_accuracies
|
||||
|
||||
@ -176,10 +176,10 @@ plt.figure(figsize=(16,9))
|
||||
plt.plot(epsilons, unfiltered_accuracies, label="Attacked Accuracy")
|
||||
for i in range(TESTED_STRENGTH_COUNT):
|
||||
filtered_accuracy = [filter_eps[i] for filter_eps in filtered_accuracies]
|
||||
plt.plot(epsilons, filtered_accuracy, label=f"Gaussian Blur (strength = {2*i + 1})")
|
||||
plt.plot(epsilons, filtered_accuracy, label=f"Threshold Filter (threshold = {(2*i + 1)/10})")
|
||||
|
||||
plt.legend(loc="upper right")
|
||||
plt.title("Gaussian Blur Performance")
|
||||
plt.title("Threshold Filter Performance")
|
||||
plt.xlabel("Attack Strength ($\\epsilon$)")
|
||||
plt.ylabel("Accuracy")
|
||||
plt.savefig("Images/GaussianBlurPerformance.png", )
|
||||
plt.savefig("Images/ThresholdFilterPerformance.png", )
|
||||
|
Loading…
Reference in New Issue
Block a user