-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
174 lines (130 loc) · 5.15 KB
/
main.py
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from imp import new_module
import os
import re
# from cv2 import FileNode_NAMED
import pandas as pd
import sklearn
import tensorflow as tf
from tensorflow.python.platform import flags
import numpy as np
import keras
from keras import backend
from keras.layers import Layer
import pandas as pd
from tensorflow.python.ops import array_ops
from tensorflow.python.keras import backend as K
from cleverhans_copy.attacks import FastGradientMethod
from cleverhans_copy.attacks import BasicIterativeMethod
from cleverhans_copy.utils import AccuracyReport
from cleverhans_copy.utils_keras import KerasModelWrapper
from cleverhans_copy.utils_tf import model_eval
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from cleverhans_tutorials import augmentation
from collections import defaultdict
tf.compat.v1.disable_eager_execution()
FLAGS = flags.FLAGS
BATCH_SIZE = 256
class Warp_Layer(Layer):
### default sigma = 0.5 knot = 4
def __init__(self,sigma,knot):
super().__init__()
self.sigma = sigma
self.knot = knot
self.batch_size = BATCH_SIZE
def call(self,inputs):
# print('now warp_layer->call')
return time_warp_tf(inputs,self.sigma,self.knot)
def time_warp(x, sigma=0.5, knot=4,batch_size=BATCH_SIZE):
from scipy.interpolate import CubicSpline
orig_steps = np.arange(x.shape[1])
random_warps = np.random.normal(loc=1.0, scale=sigma, size=(batch_size, knot+2, x.shape[2]))
warp_steps = (np.ones((x.shape[2],1))*(np.linspace(0, x.shape[1]-1., num=knot+2))).T
ret = np.zeros_like(x)
# ret = tf.zeros_like(x)
for i, pat in enumerate(x):
for dim in range(x.shape[2]):
time_warp = CubicSpline(warp_steps[:,dim], warp_steps[:,dim] * random_warps[i,:,dim])(orig_steps)
scale = (x.shape[1]-1)/time_warp[-1]
ret[i,:,dim] = np.interp(orig_steps, np.clip(scale*time_warp, 0, x.shape[1]-1), pat[:,dim]).T
return ret
# @tf.function(input_signature=[tf.TensorSpec(None, tf.float64)])
def time_warp_tf(input,sigma,knot):
# print('now time')
ret = tf.numpy_function(time_warp,[input,sigma,knot],tf.float32)
return ret
def create_warp_model(model,sigma,knot,batch):
backbone = model
sigma = sigma
knot = knot
batch = batch
mapping = defaultdict()
for i, layer in enumerate(backbone.layers):
if i == 0:
inpt = layer.input
x = layer.input
out_name = layer.output.name
mapping[layer.output.name] = x
continue
if type(layer.input) is list:
input_tensors = list(map(lambda t: mapping[t.name], layer.input))
else:
input_tensors = mapping[layer.input.name]
out_name = layer.output.name
if isinstance(layer, tf.keras.layers.Flatten):
warp_layer = Warp_Layer(sigma,knot)
x = warp_layer(input_tensors)
x = layer(x)
else:
x = layer(input_tensors)
mapping[out_name] = x
return tf.keras.Model(inpt, x)
def SE(model,input,num):
out_list = []
for _ in range(num):
out = model.predict(input)
out_list.append(out)
return(sum(out_list))
def report_match(y_pred,y_true):
y_pred_argmax = K.argmax(y_pred)
y_true_argmax = K.argmax(y_true)
match = K.equal(y_pred_argmax,y_true_argmax)
with tf.compat.v1.Session():
match = match.eval()
return np.sum(match)
def main(attack_method = 'fgsm'):
file_path = 'home/yamashita/time_series/model/'
model = keras.models.load_model(file_path+'best_model.hdf5')
warp_model = create_warp_model(model,sigma,knot,batch)
warp_model.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.Adam(),
metrics=['accuracy'])
for i in range(0,len(X),batch_size):
curr_X = X[i:i+batch_size]
curr_Y = Y[i:i+batch_size]
# Define input TF placeholder
x = tf.compat.v1.placeholder(tf.float32, shape=(None, img_rows, nchannels))
y = tf.compat.v1.placeholder(tf.float32, shape=(None, nb_classes))
print("Defined TensorFlow model graph.")
wrap = KerasModelWrapper(model)
if attack_method == 'fgsm':
# Initialize the Fast Gradient Sign Method (FGSM) attack object and graph
fgsm = FastGradientMethod(wrap, sess=sess)
fgsm_params = {'eps': eps }
adv_x = fgsm.generate(x, **fgsm_params)
elif attack_method == 'bim':
BasicIterativeMethod
bim = BasicIterativeMethod(wrap,sess=sess)
bim_params = {'eps':eps, 'eps_iter':0.05, 'nb_iter':10}
adv_x = bim.generate(x,**bim_params)
else:
print('Either bim or fgsm are acceptable as attack methods')
adv_x = tf.stop_gradient(adv_x)
adv = adv_x.eval({x: curr_X}, session=sess)
preds_rwse_adv = SE(w_model,adv,num)
rwse_ata = report_match(preds_rwse_adv,curr_Y)
rwse_adv += rwse_ata
print(rwse_adv)