-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataLoader.py
263 lines (209 loc) · 12.3 KB
/
dataLoader.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
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
import tensorflow_datasets as tfds
import os
from PIL import Image
from tqdm import tqdm
def load_CH_MNIST(model_mode):
"""
Loads CH_MNIST dataset and maps it to Target Model and Shadow Model.
:param model_mode: one of "TargetModel" and "ShadowModel".
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `model_mode`.
"""
if model_mode not in ['TargetModel', 'ShadowModel']:
raise ValueError('model_mode must be one of TargetModel, ShadowModel.')
# Initialize Data
images, labels = tfds.load('colorectal_histology', split='train', batch_size=-1, as_supervised=True)
x_train, x_test, y_train, y_test = train_test_split(images.numpy(), labels.numpy(), train_size=0.5,
random_state=1 if model_mode == 'TargetModel' else 3,
stratify=labels.numpy())
x_train = tf.image.resize(x_train, (64, 64))
y_train = tf.keras.utils.to_categorical(y_train-1, num_classes=8)
m_train = np.ones(y_train.shape[0])
x_test = tf.image.resize(x_test, (64, 64))
y_test = tf.keras.utils.to_categorical(y_test-1, num_classes=8)
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member
def load_CIFAR(model_mode):
"""
Loads CIFAR-100 or CIFAR-10 dataset and maps it to Target Model and Shadow Model.
:param model_mode: one of "TargetModel" and "ShadowModel".
:param num_classes: one of 10 and 100 and the default value is 100
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `model_mode`.
"""
if model_mode not in ['TargetModel', 'ShadowModel']:
raise ValueError('model_mode must be one of TargetModel, ShadowModel.')
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar100.load_data(label_mode='fine')
if model_mode == "TargetModel":
(x_train, y_train), (x_test, y_test) = (x_train[40000:50000], y_train[40000:50000]), \
(x_test, y_test)
elif model_mode == "ShadowModel":
(x_train, y_train), (x_test, y_test) = (x_train[:10000], y_train[:10000]), \
(x_train[10000:20000], y_train[10000:20000])
y_train = tf.keras.utils.to_categorical(y_train, num_classes=100)
m_train = np.ones(y_train.shape[0])
y_test = tf.keras.utils.to_categorical(y_test, num_classes=100)
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member
def load_CIFAR10(model_mode):
"""
Loads CIFAR-100 or CIFAR-10 dataset and maps it to Target Model and Shadow Model.
:param model_mode: one of "TargetModel" and "ShadowModel".
:param num_classes: one of 10 and 100 and the default value is 100
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `model_mode`.
"""
if model_mode not in ['TargetModel', 'ShadowModel']:
raise ValueError('model_mode must be one of TargetModel, ShadowModel.')
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
if model_mode == "TargetModel":
(x_train, y_train), (x_test, y_test) = (x_train[40000:50000], y_train[40000:50000]), \
(x_test, y_test)
elif model_mode == "ShadowModel":
(x_train, y_train), (x_test, y_test) = (x_train[:10000], y_train[:10000]), \
(x_train[10000:20000], y_train[10000:20000])
y_train = tf.keras.utils.to_categorical(y_train, num_classes=100)
m_train = np.ones(y_train.shape[0])
y_test = tf.keras.utils.to_categorical(y_test, num_classes=100)
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member
def load_CUB(model_mode):
"""
Loads CALTECH_BIRDS2011 (CUB_200) dataset and maps it to Target Model and Shadow Model.
:param model_mode: one of "TargetModel" and "ShadowModel".
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `model_mode`.
"""
if model_mode not in ['TargetModel', 'ShadowModel']:
raise ValueError('model_mode must be one of TargetModel, ShadowModel.')
x_train, y_train = tfds.load('caltech_birds2011', split='train' if model_mode == 'TargetModel' else 'test',
batch_size=-1, as_supervised=True)
x_test, y_test = tfds.load('caltech_birds2011', split='test' if model_mode == 'TargetModel' else 'train',
batch_size=-1, as_supervised=True)
x_train = tf.image.resize(x_train, (150, 150))
y_train = tf.keras.utils.to_categorical(y_train, num_classes=200)
m_train = np.ones(y_train.shape[0])
x_test = tf.image.resize(x_test, (150, 150))
y_test = tf.keras.utils.to_categorical(y_test, num_classes=200)
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member
def load_EYE_PACS(model_mode):
"""
Loads EyePACs dataset and maps it to Target Model and Shadow Model.
If you would like to use this dataset, you could refer to the preprocess method mentioned in Kaggle.
:param model_mode: one of "TargetModel" and "ShadowModel".
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `model_mode`.
"""
if model_mode not in ['TargetModel', 'ShadowModel']:
raise ValueError('model_mode must be one of TargetModel, ShadowModel.')
mode = "target" if model_mode == "TargetModel" else "shadow"
img_folder = "data/Eye_PACs/{}_images/".format(mode)
label_df = pd.read_csv("data/Eye_PACs/{}_label.csv".format(mode), index_col=0)
def set_data(img_path, label, desired_size=150):
N = len(os.listdir(img_path))
x_ = np.empty((N, 150, 150, 3), dtype=np.uint8)
y_ = np.empty(N)
for i, img_name in enumerate(tqdm(os.listdir(img_path))):
x_[i, :, :, :] = Image.open(img_path + img_name).resize((desired_size,) * 2, resample=Image.LANCZOS)
y_[i] = label.loc[img_name, 'level']
y_ = tf.keras.utils.to_categorical(y_, num_classes=5)
return x_, y_
x_, y_ = set_data(img_folder, label_df)
x_train, x_test, y_train, y_test = train_test_split(x_, y_, train_size=0.5,
random_state=1 if model_mode == "TargetModel" else 3,
stratify=y_)
m_train = np.ones(y_train.shape[0])
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member
def load_Purchase_50(model_mode):
"""
Loads Purchase dataset and maps it to Target Model and Shadow Model.
This data comes from privacytrustlab/ml_privacy_meter, a simplified version.
:param model_mode: one of "TargetModel" and "ShadowModel".
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `label_mode`.
"""
if model_mode not in ['TargetModel', 'ShadowModel']:
raise ValueError('model_mode must be one of TargetModel, ShadowModel.')
# Initialize Data
dataframe = pd.read_csv('data/purchase_50_{}.csv'.
format("target"if model_mode == 'TargetModel' else "shadow"))
trainDF, testDF = train_test_split(dataframe, train_size=0.5, random_state=1,
stratify=dataframe['label'].values)
x_train = trainDF.iloc[:, range(600)].values
y_train = tf.keras.utils.to_categorical([i-1 for i in trainDF.loc[:, 'label']])
m_train = np.ones(y_train.shape[0])
x_test = testDF.iloc[:, range(600)].values
y_test = tf.keras.utils.to_categorical([i-1 for i in testDF.loc[:, 'label']])
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member
def load_CIFAR_Ratio(model_mode, ratio=1):
"""
Loads CIFAR-100 or CIFAR-10 dataset and maps it to Target Model and Shadow Model.
:param model_mode: one of "TargetModel" and "ShadowModel".
:param num_classes: one of 10 and 100 and the default value is 100
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `model_mode`.
"""
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar100.load_data(label_mode='fine')
(x_train, y_train), (x_test, y_test) = (x_train[40000:50000], y_train[40000:50000]), \
(x_test, y_test)
x_train, y_train = x_train[:int(20000/(ratio+1))], y_train[:int(20000/(ratio+1))]
y_train = tf.keras.utils.to_categorical(y_train, num_classes=100)
m_train = np.ones(y_train.shape[0])
y_test = tf.keras.utils.to_categorical(y_test, num_classes=100)
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member
def load_CIFAR_Class(model_mode, num_classes=100):
"""
Loads CIFAR-100 dataset and selects the concrete number of classes from it according to superclasses.
:param model_mode: one of "TargetModel" and "ShadowModel".
:param num_classes: Recommend to use one of [20, 40 ,60, 80, 100] and the default value is 100
:return: Tuple of numpy arrays:'(x_train, y_train), (x_test, y_test), member'.
:raise: ValueError: in case of invalid `model_mode`.
"""
if model_mode not in ['TargetModel', 'ShadowModel']:
raise ValueError('model_mode must be one of TargetModel, ShadowModel.')
data = tfds.load("cifar100", split=["train", "test"], batch_size=-1)
# train_index = [i in np.arange(int(num_classes/5)) for i in data[0]["coarse_label"]]
# x_train = tf.boolean_mask(data[0]['image'], train_index)
# train_label = tf.boolean_mask(data[0]['label'], train_index)
# label_sorted = np.sort(np.unique(train_label))
# y_train = tf.keras.utils.to_categorical([np.argwhere(label_sorted==i)[0] for i in train_label])
#
# test_index = [i in np.arange(int(num_classes/5)) for i in data[1]["coarse_label"]]
# x_test = tf.boolean_mask(data[1]['image'], test_index)
# test_label = tf.boolean_mask(data[1]['label'], test_index)
# y_test = tf.keras.utils.to_categorical([np.argwhere(label_sorted==i)[0] for i in test_label])
train_index = [i in np.arange(num_classes) for i in data[0]["label"]]
x_train = tf.boolean_mask(data[0]['image'], train_index)
train_label = tf.boolean_mask(data[0]['label'], train_index)
label_sorted = np.sort(np.unique(train_label))
y_train = tf.keras.utils.to_categorical([np.argwhere(label_sorted == i)[0] for i in train_label])
test_index = [i in np.arange(num_classes) for i in data[1]["label"]]
x_test = tf.boolean_mask(data[1]['image'], test_index)
test_label = tf.boolean_mask(data[1]['label'], test_index)
y_test = tf.keras.utils.to_categorical([np.argwhere(label_sorted == i)[0] for i in test_label])
if model_mode == "TargetModel":
(x_train, y_train), (x_test, y_test) = (x_train[400*num_classes:500*num_classes],
y_train[400*num_classes:500*num_classes]), (x_test, y_test)
elif model_mode == "ShadowModel":
(x_train, y_train), (x_test, y_test) = (x_train[:100*num_classes], y_train[:100*num_classes]), \
(x_train[100*num_classes:200*num_classes],
y_train[100*num_classes:200*num_classes])
m_train = np.ones(y_train.shape[0])
m_test = np.zeros(y_test.shape[0])
member = np.r_[m_train, m_test]
return (x_train, y_train), (x_test, y_test), member