Directory structure overhaul, poster almost done
@ -1 +0,0 @@
|
||||
,sharpe,dhcp-150-250-208-169,30.04.2024 21:44,file:///home/sharpe/.var/app/org.libreoffice.LibreOffice/config/libreoffice/4;
|
BIN
examples/bilateral_filter_eps_0.2.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
examples/bit_depth_eps_0.2.png
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
examples/gaussian_blur_eps_0.2.png
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
examples/gaussian_kuwahara_eps_0.2.png
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
examples/mean_kuwahara_eps_0.2.png
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
examples/random_noise_eps_0.2.png
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
examples/threshold_eps_0.2.png
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
examples/unfiltered_eps_0.2.png
Normal file
After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 221 KiB After Width: | Height: | Size: 221 KiB |
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 115 KiB After Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 117 KiB After Width: | Height: | Size: 117 KiB |
Before Width: | Height: | Size: 168 KiB After Width: | Height: | Size: 168 KiB |
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
Before Width: | Height: | Size: 115 KiB After Width: | Height: | Size: 115 KiB |
Before Width: | Height: | Size: 137 KiB After Width: | Height: | Size: 137 KiB |
Before Width: | Height: | Size: 164 KiB After Width: | Height: | Size: 164 KiB |
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB |
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 106 KiB After Width: | Height: | Size: 106 KiB |
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 133 KiB |
Before Width: | Height: | Size: 93 KiB After Width: | Height: | Size: 93 KiB |
Before Width: | Height: | Size: 202 B After Width: | Height: | Size: 202 B |
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 125 KiB |
Before Width: | Height: | Size: 132 KiB After Width: | Height: | Size: 132 KiB |
Before Width: | Height: | Size: 130 KiB After Width: | Height: | Size: 130 KiB |
Before Width: | Height: | Size: 163 KiB After Width: | Height: | Size: 163 KiB |
Before Width: | Height: | Size: 158 KiB After Width: | Height: | Size: 158 KiB |
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
BIN
src/__pycache__/cifar10.cpython-311.pyc
Normal file
BIN
src/__pycache__/defense_filters.cpython-311.pyc
Normal file
@ -1,3 +1,4 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import cv2
|
||||
from pykuwahara import kuwahara
|
||||
import numpy as np
|
||||
@ -26,6 +27,10 @@ def gaussian_kuwahara(data, dimensions, radius=5):
|
||||
|
||||
for i in range(dimensions[0]):
|
||||
filtered_images[i] = kuwahara(images[i], method='gaussian', radius=radius, image_2d=images[i])
|
||||
if i == 0 and radius == 5:
|
||||
plt.title("Gaussian Kuwahara", fontsize=40)
|
||||
plt.imshow(filtered_images[i], cmap='gray')
|
||||
plt.show()
|
||||
|
||||
filtered_images = filtered_images.transpose(0,3,1,2)
|
||||
return torch.tensor(filtered_images).float()
|
||||
@ -37,6 +42,10 @@ def mean_kuwahara(data, dimensions, radius=5):
|
||||
|
||||
for i in range(dimensions[0]):
|
||||
filtered_images[i] = kuwahara(images[i], method='mean', radius=radius, image_2d=images[i])
|
||||
if i == 0 and radius == 5:
|
||||
plt.title("Mean Kuwahara", fontsize=40)
|
||||
plt.imshow(filtered_images[i], cmap='gray')
|
||||
plt.show()
|
||||
|
||||
filtered_images = filtered_images.transpose(0,3,1,2)
|
||||
return torch.tensor(filtered_images).float()
|
||||
@ -52,6 +61,11 @@ def random_noise(data, dimensions, intensity=0.001):
|
||||
noise = np.zeros(images[i].shape, images[i].dtype)
|
||||
cv2.randn(noise, mean, stddev)
|
||||
filtered_images[i] = cv2.addWeighted(images[i], 1.0, noise, intensity, 0.0).reshape(filtered_images[i].shape)
|
||||
if i == 0 and intensity == 0.0015:
|
||||
plt.title("Random Noise", fontsize=40)
|
||||
plt.imshow(filtered_images[i], cmap='gray')
|
||||
plt.show()
|
||||
|
||||
|
||||
filtered_images = filtered_images.transpose(0,3,1,2)
|
||||
return torch.tensor(filtered_images).float()
|
||||
@ -63,6 +77,13 @@ def gaussian_blur(data, dimensions, ksize=(5,5)):
|
||||
|
||||
for i in range(dimensions[0]):
|
||||
filtered_images[i] = cv2.GaussianBlur(images[i], ksize=ksize, sigmaX=0).reshape(filtered_images[i].shape)
|
||||
if i == 0 and ksize[0] == 5:
|
||||
plt.title("Unfiltered", fontsize=40)
|
||||
plt.imshow(images[i], cmap='gray')
|
||||
plt.show()
|
||||
plt.title("Gaussian Blur", fontsize=40)
|
||||
plt.imshow(filtered_images[i], cmap='gray')
|
||||
plt.show()
|
||||
|
||||
filtered_images = filtered_images.transpose(0,3,1,2)
|
||||
return torch.tensor(filtered_images).float()
|
||||
@ -74,6 +95,10 @@ def bilateral_filter(data, dimensions, d=5, sigma=50):
|
||||
|
||||
for i in range(dimensions[0]):
|
||||
filtered_images[i] = cv2.bilateralFilter(images[i], d, sigma, sigma).reshape(filtered_images[i].shape)
|
||||
if i == 0 and d == 5:
|
||||
plt.title("Bilateral Filter", fontsize=40)
|
||||
plt.imshow(filtered_images[i], cmap='gray')
|
||||
plt.show()
|
||||
|
||||
filtered_images = filtered_images.transpose(0,3,1,2)
|
||||
return torch.tensor(filtered_images).float()
|
||||
@ -100,6 +125,11 @@ def threshold_filter(data, dimensions, threshold=0.5):
|
||||
if min_value < 0:
|
||||
filtered_images[i] -= min_value
|
||||
|
||||
if i == 0 and threshold == 0.5:
|
||||
plt.title("Threshold Filter", fontsize=40)
|
||||
plt.imshow(filtered_images[i], cmap='gray')
|
||||
plt.show()
|
||||
|
||||
filtered_images = filtered_images.transpose(0,3,1,2)
|
||||
return torch.tensor(filtered_images).float()
|
||||
|
||||
@ -111,6 +141,12 @@ def bit_depth(data, dimensions, bits=16):
|
||||
for i in range(dimensions[0]):
|
||||
filtered_images[i] = (images[i]*(2**bits)).astype(int).astype(float)/(2**bits)
|
||||
|
||||
|
||||
if i == 0 and bits == 4:
|
||||
plt.title("Bit-Depth", fontsize=40)
|
||||
plt.imshow(filtered_images[i], cmap='gray')
|
||||
plt.show()
|
||||
|
||||
filtered_images = filtered_images.transpose(0,3,1,2)
|
||||
return torch.tensor(filtered_images).float()
|
||||
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 66 KiB |
@ -20,39 +20,40 @@ import defense_filters
|
||||
|
||||
|
||||
ATTACK = "FGSM"
|
||||
DATASET = "CIFAR-10"
|
||||
DATASET = "MNIST"
|
||||
|
||||
RES_X = 32
|
||||
RES_Y = 32
|
||||
CHANNELS = 3
|
||||
RES_X = 28
|
||||
RES_Y = 28
|
||||
CHANNELS = 1
|
||||
|
||||
MIN_EPSILON = 0.2
|
||||
MAX_EPSILON = 0.3
|
||||
EPSILON_STEP = 0.025
|
||||
|
||||
TESTED_STRENGTH_COUNT = 5
|
||||
epsilons = np.arange(0.0, MAX_EPSILON+EPSILON_STEP, EPSILON_STEP)
|
||||
pretrained_model = "cifar_vgg.pth"
|
||||
epsilons = np.arange(MIN_EPSILON, MAX_EPSILON+EPSILON_STEP, EPSILON_STEP)
|
||||
pretrained_model = "mnist_cnn_unfiltered.pt"
|
||||
use_cuda=False
|
||||
|
||||
torch.manual_seed(69)
|
||||
|
||||
|
||||
#test_loader = torch.utils.data.DataLoader(
|
||||
# datasets.MNIST('data/', train=False, download=True, transform=transforms.Compose([
|
||||
# transforms.ToTensor(),
|
||||
# transforms.Normalize((0.1307,), (0.3081,)),
|
||||
# ])),
|
||||
# batch_size=1, shuffle=True)
|
||||
test_loader = torch.utils.data.DataLoader(
|
||||
datasets.MNIST('data/', train=False, download=True, transform=transforms.Compose([
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.1307,), (0.3081,)),
|
||||
])),
|
||||
batch_size=1, shuffle=False)
|
||||
|
||||
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
|
||||
batch_size = 1
|
||||
testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
|
||||
test_loader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=True, num_workers=2)
|
||||
#transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
|
||||
#batch_size = 1
|
||||
#testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
|
||||
#test_loader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=True, num_workers=2)
|
||||
|
||||
print("CUDA Available: ", torch.cuda.is_available())
|
||||
device = torch.device("cuda" if use_cuda and torch.cuda.is_available() else "cpu")
|
||||
|
||||
model = vgg.VGG("VGG16").to(device)
|
||||
model = mnist.Net().to(device)
|
||||
|
||||
model.load_state_dict(torch.load(pretrained_model, map_location=device))
|
||||
|
||||
@ -150,6 +151,7 @@ def test(model, device, test_loader, epsilon, filter):
|
||||
while i >= len(filtered_correct_counts):
|
||||
filtered_correct_counts.append(0)
|
||||
filtered_correct_counts[i] += 1
|
||||
return
|
||||
|
||||
# Get the predicted classification
|
||||
unfiltered_pred = output_unfiltered.max(1, keepdim=True)[1]
|
||||
@ -199,8 +201,9 @@ for filter in filters:
|
||||
results["filters"][filter].append(accuracies)
|
||||
|
||||
results_json = json.dumps(results, indent=4)
|
||||
with open("results/cifar10_fgsm.json", "w") as outfile:
|
||||
outfile.write(results_json)
|
||||
break
|
||||
#with open("results/cifar10_fgsm.json", "w") as outfile:
|
||||
# outfile.write(results_json)
|
||||
|
||||
# Plot the results
|
||||
#plt.figure(figsize=(16,9))
|
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 69 KiB |
Before Width: | Height: | Size: 266 KiB After Width: | Height: | Size: 266 KiB |