-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFebruus.py
More file actions
executable file
·226 lines (193 loc) · 8.03 KB
/
Februus.py
File metadata and controls
executable file
·226 lines (193 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# coding: utf-8
from __future__ import print_function
import time
import torch
from torchvision import datasets, transforms, models
import numpy as np
from PIL import Image
import torchvision
from cifar.model import cifar10
from tqdm import tqdm
import os
from PIL import ImageOps
from models import CompletionNetwork
from utils import poisson_blend_old
from grad_cam import GradCam
##################################################
# PARAMETER SETTING
##################################################
MODEL = 'cifar10_net.pth'
MASK_COND = 0.7
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
BATCH_SIZE = 32
##################################################
# Load the backdoored model
net = cifar10(128)
net = net.to(device)
net.load_state_dict(torch.load(MODEL))
net.eval()
gcam = GradCam(net, True, device)
print("Loading model successfully\n")
# Data Loader
# --------------
data_transforms = {
'train': transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]),
'val': transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]),
}
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=data_transforms['train'])
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=data_transforms['val'])
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE,
shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=BATCH_SIZE,
shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# This function is to stamp the trigger to the input
# img is the normalized tensor after input transformation
def poison_one(img):
img = img /2 + 0.5 # unnormalize to [0 1]
npimg = img.cpu().numpy() # convert to numpy array
npimg = np.transpose(npimg, (1,2,0)) # fix the dimension of image
src_im = Image.fromarray(np.uint8(255.0*npimg)) # convert to PIL image
# Stamping the trigger
logo = Image.open('flower_nobg.png').convert("RGBA")
logo = ImageOps.fit(logo, (8,8), Image.ANTIALIAS)
position = (24, 24)
src_im.paste(logo, position, logo) # stamp the trigger
newimg = np.array(src_im) # convert PIL image to array
newimg = newimg / 255.0
newimg = np.transpose(newimg, (2,0,1)) # fix the dimension of tensor
newimg = (newimg -0.5)*2 # normalize img
return torch.from_numpy(np.asarray(newimg)) # convert to tensor
def unnormalize(input):
output = input / 2. + 0.5
return output
def normalize_tensor_batch(input):
output = (input - 0.5) / 0.5
return output
# This funciton is GAN restoration module
def GAN_patching_inputs(images, predicted): # images and its predicted tensors
global N
model = CompletionNetwork()
model.load_state_dict(torch.load("cifar10_inpainting", map_location='cuda'))
model.eval()
model = model.to(device)
cleanimgs = list(range(len(images))) # GAN inpainted
# This is to apply Grad CAM to the load images
# --------------------------------------------
for j in range(len(images)):
N += 1
image = images[j]
image = unnormalize(image) # unnormalize to [0 1] to feed into GAN
image = torch.unsqueeze(image, 0) # unsqueeze meaning adding 1D to the tensor
start_time = time.time()
mask = gcam(image) # get the mask through GradCAM
cond_mask = mask >= MASK_COND
mask = cond_mask.astype(int)
# ---------------------------------------
mask = np.expand_dims(mask,axis=0) # add 1D to mask
mask = np.expand_dims(mask,axis=0)
mask = torch.tensor(mask) # convert mask to tensor 1,1,32,32
mask = mask.type(torch.FloatTensor)
mask = mask.to(device)
x = image # original test image
mpv = [0.4914655575466156, 0.4821903321331739, 0.4465675537097454]
mpv = torch.tensor(mpv).view(1,3,1,1)
mpv = mpv.to(device)
# inpaint
with torch.no_grad():
x_mask = x - x * mask + mpv * mask # generate the occluded input [0 1]
inputx = torch.cat((x_mask, mask), dim=1)
output = model(inputx) # generate the output for the occluded input [0 1]
end_time = time.time()
GAN_process_time = 1000.0*(end_time - start_time) # convert to ms
GAN_process_time = round(GAN_process_time, 3)
np.savetxt('runtime.csv', (N,GAN_process_time), delimiter=',')
# image restoration
inpainted = poisson_blend_old(x_mask, output, mask) # this is GAN output [0 1]
inpainted = inpainted.to(device)
# store GAN output
clean_input = inpainted
clean_input = normalize_tensor_batch(clean_input) # normalize to [-1 1]
clean_input = torch.squeeze(clean_input) # remove the 1st dimension
cleanimgs[j] = clean_input.cpu().numpy() # store to a list
# this is tensor for GAN output
cleanimgs_tensor = torch.from_numpy(np.asarray(cleanimgs))
cleanimgs_tensor = cleanimgs_tensor.type(torch.FloatTensor)
cleanimgs_tensor = cleanimgs_tensor.to(device)
return cleanimgs_tensor
##################################################
# MAIN SECTION
##################################################
# Initilization
##################################################
correct_GAN = 0
attack_success = 0
target = 7
total = 0
pbar = tqdm(total=round(len(testset)/BATCH_SIZE))
N = 0
ASR_beforeGAN = 0
correct_beforeGAN = 0
##################################################
pbar.set_description('Februus: Input Sanitizing')
for i, data in enumerate(testloader):
images, labels = data
images = images.to(device)
labels = labels.to(device)
true_labels = labels.clone().detach()
target_labels = torch.ones_like(labels)*target
target_labels = target_labels.to(device)
# get the predicted before Februus
outputs_ori = net(images)
_, predicted_ori = torch.max(outputs_ori, 1)
correct_beforeGAN += (predicted_ori == labels).sum().item()
# --------------------------------------
# stamp the trigger
for j in range(len(images)):
images[j] = poison_one(images[j])
images = images.type(torch.FloatTensor)
images = images.to(device)
labels = labels.to(device)
outputs = net(images)
_, predicted = torch.max(outputs, 1)
# get the ASR before Februus
ASR_beforeGAN += (predicted == target_labels).sum().item()
# sanitize the inputs
clean_GAN_inputs = GAN_patching_inputs(images, predicted)
GAN_outputs = net(clean_GAN_inputs)
_, GAN_predicted = torch.max(GAN_outputs.data, 1)
total += labels.size(0)
correct_GAN += (GAN_predicted == labels).sum().item()
pbar.update()
for j in range(len(true_labels)):
label = true_labels[j]
label = label.to(device)
GAN_predict = GAN_predicted[j]
classification_result = predicted[j]
if(GAN_predict != label and predicted_ori[j] == label): # to avoid counting normal misclassification
if label.cpu().numpy() != target and GAN_predict.cpu().numpy() == target: # avoid counting the examples in the target label but only other source labels
attack_success += 1
pbar.close()
print('##################################################')
print('# Before Februus:\n')
print('Accuracy of inputs before Februus: %.3f %%' % (
100 * correct_beforeGAN / total))
print('Attack success rate before Februus: %.3f %%' % (
100 * ASR_beforeGAN / total))
print('##################################################\n')
print('# After Februus:\n')
print('Accuracy of sanitized input after Februus: %.3f %%' % (
100 * correct_GAN / total))
print('Atack Success rate after Februus: %.3f %%' % (
100 * attack_success / total))