-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmembership_attackload_mnist22.py
274 lines (223 loc) · 8.19 KB
/
membership_attackload_mnist22.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import numpy as np
from keras.datasets import mnist, cifar10
from keras.utils import to_categorical
from keras import backend as K
def data_partition(datax, datay, fraction=1/2, random=False):
"""
data: len* \n
return x1,x2,y1,y2
@TODO part2 no random
"""
if fraction<0 or fraction>1:
return 0
allnum = int(datax.shape[0])
part1num = int(allnum*fraction)
part2num = allnum - part1num
# print(part1num,part2num,allnum)
# assert part1num + part2num == allnum
print(part1num,part2num)
if random == False:
partx1 = datax[:part1num]
partx2 = datax[part1num:]
party1 = datay[:part1num]
party2 = datay[part1num:]
else:
part1index = np.random.choice(allnum, part1num, replace=False)
part2index = np.delete(np.arange(allnum),part1index)
part2index = np.random.choice(part2index, part2num, replace=False)
#print(part1index[:100])
# print(part2inedx[:100])
assert len(part1index)+len(part2index) == allnum
partx1 = datax[part1index]
partx2 = datax[part2index]
party1 = datay[part1index]
party2 = datay[part2index]
return partx1, partx2, party1, party2
def load_part_mnist(part, partnum):
"""
divide mnist (include train and test) into 'partnum' part
return 'part'-th part of mnist
"""
num_classes = 10
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_data, y_data = np.append(x_train,x_test,axis=0), np.append(y_train,y_test,axis=0)
assert x_data.shape[0] == 70000
i = part-1
partsize = int(70000/partnum)
s = partsize
data_x = x_data[i*s:(i+1)*s]
data_y = y_data[i*s:(i+1)*s]
x_data = data_x
y_data = data_y
if K.image_data_format() == 'channels_first':
x_data = x_data.reshape(x_data.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_data = x_data.reshape(x_data.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_data = x_data.astype('float32')
x_data /= 255
print('x_data shape:', x_data.shape)
# convert class vectors to binary class matrices
y_data = to_categorical(y_data, num_classes)
return x_data, y_data, input_shape, num_classes
def add_noise(datax, k=0.1):
shape = datax.shape
# print(datax[0])
datax = datax.reshape(-1)
allnum = datax.shape[0]
partnum = int(allnum*k)
randomindex = np.random.choice(allnum, partnum, replace=False)
randoms = np.random.random((partnum))
# print(shape,randoms.shape)
datax[randomindex] = randoms
datax = datax.reshape(shape)
# print(datax.shape,datax[0])
return datax
def shadowload(fraction = 1/2, database='mnist', trainfraction=1/4):
"""
input fraction
load data for shadow models(50 or more)
return size: 17500 from fraction of the mnist
"""
if database == 'mnist':
x, y, _, _ = load_part_mnist(1,1)
total = 70000
elif database == 'cifar10':
x, y, _, _ = load_part_cifar10(1,1)
total=60000
else:
raise Exception('no such db')
# assert False
totalnum = int(x.shape[0]*fraction)
x, y = x[:totalnum], y[:totalnum]
assert x.shape[0]==int(total*fraction)
x,_, y,_ = data_partition(x,y,2*trainfraction,random=True)
x_train, x_test, y_train, y_test = data_partition(x,y,1/2,random=True)
# x_train = add_noise(x_train, 0.1)
# x_test = add_noise(x_test,0.1)
# tr_index = np.random.choice(x.shape[0], int(x.shape[0]/2), replace=False)
# x_train = x[tr_index]
# y_train = y[tr_index]
# index_all = np.arange(x.shape[0])
# te_index = np.delete(index_all, tr_index)
# x_test = x[te_index]
# y_test = y[te_index]
# assert x_test.shape[0] == 17500
return x_train, x_test, y_train, y_test
def targetload(fraction=1/2,database='mnist', trainfraction=1/12):
"""
load mnist for target
"""
# print ('aaaa')
if database == 'mnist':
x, y, _, _ = load_part_mnist(1,1)
total = 70000
elif database == 'cifar10':
x, y, _, _ = load_part_cifar10(1,1)
total=60000
else:
raise Exception('no such db')
totalnum = int(x.shape[0]*fraction)
x, y = x[totalnum:], y[totalnum:]
assert x.shape[0]==int(total*fraction)
# x,_, y,_ = data_partition(x,y,2*trainfraction,random=True)
x_train, x_test, y_train, y_test = data_partition(x,y,1/2,random=False)
print("target train shape2:",x_train.shape)
print("target test shape:",x_test.shape)
return x_train, x_test, y_train, y_test
def load_part_cifar10(part, partnum):
"""
divide - (include train and test) into 'partnum' part
return 'part'-th part of cifar10
"""
num_classes = 10
img_rows, img_cols = 32, 32
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
x_data, y_data = np.append(x_train,x_test,axis=0), np.append(y_train,y_test,axis=0)
assert x_data.shape[0] == 60000
i = part-1
partsize = int(60000/partnum)
s = partsize
data_x = x_data[i*s:(i+1)*s]
data_y = y_data[i*s:(i+1)*s]
x_data = data_x
y_data = data_y
# Convert class vectors to binary class matrices.
y_data = to_categorical(y_data, num_classes)
print('x_data shape:', x_data.shape,y_data.shape)
if K.image_data_format() == 'channels_first':
x_data = x_data.reshape(x_data.shape[0], 3, img_rows, img_cols)
input_shape = (3, img_rows, img_cols)
else:
x_data = x_data.reshape(x_data.shape[0], img_rows, img_cols, 3)
input_shape = (img_rows, img_cols, 3)
return x_data, y_data, input_shape, num_classes
def load_cifar10_class(classs=0):
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# print(x_test.shape,y_test.shape)
# print(x_test[-1],y_test[-1])
# deltest = np.where(y_test==classs)
#y_test = np.delete(y_test,deltest[0],axis=0)
# x_test = np.delete(x_test,deltest[0],axis=0)
#print(x_test.shape,y_test.shape)
deltrain = np.where(y_train==classs)
tmpx = np.take(x_train,deltrain[0],axis=0)
tmpy = np.take(y_train,deltrain[0],axis=0)
x_train = np.delete(x_train, deltrain[0], axis=0)
y_train = np.delete(y_train, deltrain[0], axis=0)
x_test = np.append(x_test,tmpx,axis=0)
y_test = np.append(y_test,tmpy,axis=0)
# print(x_test[-1],y_test[-1])
# print(y_test)
# assert False
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
return x_train, x_test, y_train, y_test
def load_cifar10_batch(batch_num=1):
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# print(x_test.shape,y_test.shape)
# print(x_test[-1],y_test[-1])
# deltest = np.where(y_test==classs)
#y_test = np.delete(y_test,deltest[0],axis=0)
# x_test = np.delete(x_test,deltest[0],axis=0)
#print(x_test.shape,y_test.shape)
allnum = x_train.shape[0]
randomindex = np.random.choice(allnum, batch_num, replace=False)
# print(randomindex)
deltrain = randomindex,1
tmpx = np.take(x_train,deltrain[0],axis=0)
tmpy = np.take(y_train,deltrain[0],axis=0)
x_train = np.delete(x_train, deltrain[0], axis=0)
y_train = np.delete(y_train, deltrain[0], axis=0)
x_test = np.append(x_test,tmpx,axis=0)
y_test = np.append(y_test,tmpy,axis=0)
# print(x_test[-1],y_test[-1])
# print(y_test).
# assert False
print('x_train shape:', x_train.shape)
# assert False
# Convert class vectors to binary class matrices.
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
tmpy = to_categorical(tmpy,10)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
tmpx = tmpx.astype('float32')
tmpx /= 255
x_train /= 255
x_test /= 255
return x_train, x_test, y_train, y_test,tmpx,tmpy