-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_info.py
173 lines (143 loc) · 5.11 KB
/
extract_info.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
173
<<<<<<< HEAD
import os
import sys
import argparse
import keras
import numpy as np
from tqdm import tqdm
import pandas as pd
from keras.optimizers import SGD, adadelta
import tools.data_utils as DataUtils
import tools.model_utils as ModelUtils
if __name__ == "__main__":
# os.environ["CUDA_VISIBLE_DEVICES"] = ""
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", "-d", help="Dataset", type=str, default="mnist")
parser.add_argument("--network", "-n", help="Network", type=str, default="lenet5")
args = parser.parse_args()
dataset = args.dataset
network = args.network
for target in ['nature', 'cw', 'bim', 'fgsm', 'jsma']:
x_test, y_test = DataUtils.get_candidate_general(target=target, name=dataset, network=network)
all_sample_size = x_test.shape[0]
model = ModelUtils.load_model(network=network, dataset=dataset)
y_predict = np.argmax(model.predict(x_test, verbose=0), axis=1)
x_true = []
x_false = []
for i in range(all_sample_size):
if y_test[i] == y_predict[i]:
x_true.append(i)
else:
x_false.append(i)
trueOrFalse = []
for i in range(all_sample_size):
if y_test[i] == y_predict[i]:
trueOrFalse.append(0)
else:
trueOrFalse.append(1)
res_dir = f"finetuned_prediction/{dataset}_{network}"
for iteration in range(40):
combined_prediction = []
for iter in range(iteration):
if target == 'nature':
prediction = np.load(f"{res_dir}/iter_{iter}_models/prediction.npy")
else:
prediction = np.load(f"{res_dir}/iter_{iter}_models/prediction_target_{target}.npy")
# print(prediction.shape)
if np.any(combined_prediction):
combined_prediction = np.vstack((combined_prediction, prediction))
else:
combined_prediction = prediction
mutate_matrix = []
for i, item in tqdm(enumerate(combined_prediction)):
row = np.zeros(all_sample_size, np.int)
for j in range(all_sample_size):
if y_predict[j] != item[j]:
row[j] = 1
mutate_matrix.append(row)
mutate_matrix = np.array(mutate_matrix)
print(mutate_matrix.shape)
save_dir = f"finetuned_prediction/{dataset}_{network}"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
np.save(f"{save_dir}/prediction_{target}.npy", mutate_matrix)
prediction_trans = []
for item in tqdm(combined_prediction.T):
label_count = np.zeros(10)
for label in item:
label_count[label] += 1
label_count /= np.sum(label_count)
prediction_trans.append(label_count)
np.save(f'finetuned_prediction/{dataset}_{network}/prediction_trans_{target}.npy', prediction_trans)
=======
import os
import sys
import argparse
import keras
import numpy as np
from tqdm import tqdm
import pandas as pd
from keras.optimizers import SGD, adadelta
import tools.data_utils as DataUtils
import tools.model_utils as ModelUtils
if __name__ == "__main__":
# os.environ["CUDA_VISIBLE_DEVICES"] = ""
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", "-d", help="Dataset", type=str, default="mnist")
parser.add_argument("--network", "-n", help="Network", type=str, default="lenet5")
args = parser.parse_args()
dataset = args.dataset
network = args.network
for target in ['nature', 'cw', 'bim', 'fgsm', 'jsma']:
x_test, y_test = DataUtils.get_candidate_general(target=target, name=dataset, network=network)
all_sample_size = x_test.shape[0]
model = ModelUtils.load_model(network=network, dataset=dataset)
y_predict = np.argmax(model.predict(x_test, verbose=0), axis=1)
x_true = []
x_false = []
for i in range(all_sample_size):
if y_test[i] == y_predict[i]:
x_true.append(i)
else:
x_false.append(i)
trueOrFalse = []
for i in range(all_sample_size):
if y_test[i] == y_predict[i]:
trueOrFalse.append(0)
else:
trueOrFalse.append(1)
res_dir = f"finetuned_prediction/{dataset}_{network}"
for iteration in range(40):
combined_prediction = []
for iter in range(iteration):
if target == 'nature':
prediction = np.load(f"{res_dir}/iter_{iter}_models/prediction.npy")
else:
prediction = np.load(f"{res_dir}/iter_{iter}_models/prediction_target_{target}.npy")
# print(prediction.shape)
if np.any(combined_prediction):
combined_prediction = np.vstack((combined_prediction, prediction))
else:
combined_prediction = prediction
mutate_matrix = []
for i, item in tqdm(enumerate(combined_prediction)):
row = np.zeros(all_sample_size, np.int)
for j in range(all_sample_size):
if y_predict[j] != item[j]:
row[j] = 1
mutate_matrix.append(row)
mutate_matrix = np.array(mutate_matrix)
print(mutate_matrix.shape)
save_dir = f"finetuned_prediction/{dataset}_{network}"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
np.save(f"{save_dir}/prediction_{target}.npy", mutate_matrix)
prediction_trans = []
for item in tqdm(combined_prediction.T):
label_count = np.zeros(10)
for label in item:
label_count[label] += 1
label_count /= np.sum(label_count)
prediction_trans.append(label_count)
np.save(f'finetuned_prediction/{dataset}_{network}/prediction_trans_{target}.npy', prediction_trans)
>>>>>>> 1dffd00419da92d924b616008c876798ac08764e