-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadv_attack_test.py
More file actions
87 lines (75 loc) · 3.55 KB
/
adv_attack_test.py
File metadata and controls
87 lines (75 loc) · 3.55 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
import torch
import numpy as np
import matplotlib.pyplot as plt
import argparse
from env.env import Env
from agents.agents import Agent
parser = argparse.ArgumentParser(description='Adversarial attacks on the CarRacing-v0 environment')
parser.add_argument('--action-repeat', type=int, default=8, metavar='N', help='repeat action in N frames (default: 12)')
parser.add_argument('--img-stack', type=int, default=4, metavar='N', help='stack N image in a state (default: 4)')
parser.add_argument('--seed', type=int, default=0, metavar='N', help='random seed (default: 0)')
parser.add_argument('--render', action='store_true', help='render the environment')
parser.add_argument('--attack_type', type=str, default='general', metavar='N', help='type of the attack')
parser.add_argument('--adversarial_bound', type=float, default=0.1, metavar='N', help='epsilon value for perturbation')
# only use if attack_type is not general
parser.add_argument('--patch_type', type=str, default='box', metavar='N', help='type of patch in patch attack type')
parser.add_argument('--patch_size', type=int, default=24, metavar='N', help='size of patch in patch attack type')
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
torch.manual_seed(args.seed)
if use_cuda:
torch.cuda.manual_seed(args.seed)
def test_attack():
agent = Agent(args.img_stack, device)
agent.load_param()
env = Env(args.seed, args.img_stack, args.action_repeat)
# load adv input, by default general attack perturbation
delta_s = np.load('param/adv_general.npy')
if args.attack_type != 'general':
file_path = 'param/adv_' + args.attack_type
if args.attack_type == 'patch':
file_path += '_' + args.patch_type
file_path += '.npy'
delta_s = np.load(file_path)
# show adv
fig = plt.figure(figsize=(8, 8))
plt.title('Stack of ' + str(args.img_stack) + ' adversarial signals seen by Agent')
plt.axis('off')
columns, rows = args.img_stack // 2, args.img_stack // 2
for i in range(1, columns * rows + 1):
# denormalize while showing the image
img = (delta_s[i - 1] + 1) * 128
fig.add_subplot(rows, columns, i)
plt.imshow(img, cmap='gray')
plt.show()
for i_ep in range(10):
score = 0
state = env.reset()
for t in range(1000):
# steps range to render attack in 1000
attack_render = [30, 40]
if t in np.arange(attack_render[0], attack_render[1] + 1):
if t in attack_render:
s_with_ds = (state + delta_s)
# clip the image limits and denormalize for displaying
s_with_ds = np.clip(s_with_ds, -1, 0.9921875)
s_with_ds = (s_with_ds + 1) * 128
title = 'Attack Started' if t == attack_render[0] else 'Attack ended'
title += ' (showing first frame of 4 frames visible to policy)'
plt.imshow(s_with_ds[0], cmap='gray')
plt.axis('off')
plt.title(title)
plt.show()
state += delta_s
action = agent.select_action(state)
state_, reward, done, die = env.step(action * np.array([2., 1., 1.]) + np.array([-1., 0., 0.]))
if args.render:
env.render()
score += reward
state = state_
if done:
break
print('Ep {}\tScore: {:.2f}\t'.format(i_ep, score))
if __name__ == "__main__":
test_attack()