-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
301 lines (268 loc) · 10.8 KB
/
models.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import torch
import torchvision
import numpy as np
import torch.onnx
import onnx
from torch import nn
import matplotlib.pyplot as plt
'''
Defining several common model for testing.
SingleNet: SLP with 1 hidden layer.
SimpleNet: MLP with 2 hidden layers.
ConvNet: Convolutional NN with 2 convolutional layers and one FC layer.
'''
class SingleNet(nn.Module):
'''an SLP'''
def __init__(self, n_hidden = 100, batch_size = 200) -> None:
super().__init__()
self.batch_size = batch_size
self.fc1 = nn.Linear(in_features=784, out_features=n_hidden)
self.fc2 = nn.Linear(in_features=n_hidden, out_features=10)
def forward(self, x):
x = x.view(-1, 784)
x = self.fc1(x)
x = torch.relu(x)
x = self.fc2(x)
return x
class SimpleNet(nn.Module):
'''Simple MLP'''
def __init__(self, n_hidden = 100, batch_size = 200) -> None:
super().__init__()
self.batch_size = batch_size
self.fc1 = nn.Linear(in_features=784, out_features=n_hidden)
self.fc2 = nn.Linear(in_features=n_hidden, out_features=n_hidden)
self.fc3 = nn.Linear(in_features=n_hidden, out_features=10)
def forward(self, x):
x = x.view(-1, 784)
x = self.fc1(x)
x = torch.relu(x)
x = self.fc2(x)
x = torch.relu(x)
x = self.fc3(x)
return x
class ConvNet(nn.Module):
'''Simple Convolutional NN'''
def __init__(self, dropout_rate=0.5):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 8, kernel_size=3, padding='same')
self.conv2 = nn.Conv2d(8, 16, kernel_size=3, padding='same')
img_size = 7
self.fc1 = torch.nn.Linear(16 * img_size * img_size, 10)
self.dropout_rate = dropout_rate
def forward(self, x):
x = nn.functional.relu(nn.functional.max_pool2d(self.conv1(x), 2))
x = nn.functional.relu(nn.functional.max_pool2d(self.conv2(x), 2))
x = nn.functional.dropout(x, training=self.training, p=self.dropout_rate)
img_size = 7
x = x.view(-1, 16 * img_size * img_size)
x = nn.functional.relu(self.fc1(x))
return nn.functional.log_softmax(x, dim=1)
'''
The dataset that we will be using across different frameworks.
we are using the simplest MNIST here.
'''
class MNIST:
def __init__(self, batch_size, splits=None, shuffle=True):
"""
Args:
batch_size : number of samples per batch
splits : [train_frac, valid_frac]
shuffle : (bool)
"""
self.transform = torchvision.transforms.ToTensor()
self.batch_size = batch_size
self.eval_batch_size = 200
self.splits = splits
self.shuffle = shuffle
self._build()
def _build(self):
train_split, valid_split = self.splits
trainset = torchvision.datasets.MNIST(
root="data", train=True, download=True, transform=self.transform)
num_samples = len(trainset)
self.num_train_samples = int(train_split * num_samples)
self.num_valid_samples = int(valid_split * num_samples)
# create training set
self.train_dataset = torch.utils.data.Subset(
trainset, range(0, self.num_train_samples))
self.train_loader = list(iter(torch.utils.data.DataLoader(
self.train_dataset,
batch_size=self.batch_size,
shuffle=self.shuffle,
)))
# create validation set
self.valid_dataset = torch.utils.data.Subset(
trainset, range(self.num_train_samples, num_samples))
self.valid_loader = list(iter(torch.utils.data.DataLoader(
self.valid_dataset,
batch_size=self.eval_batch_size,
shuffle=self.shuffle,
)))
# create test set
test_dataset = torchvision.datasets.MNIST(
root="data", train=False, download=True, transform=self.transform
)
self.test_loader = list(iter(torch.utils.data.DataLoader(
test_dataset,
batch_size=self.eval_batch_size,
shuffle=False,
)))
self.num_test_samples = len(test_dataset)
def get_num_samples(self, split="train"):
if split == "train":
return self.num_train_samples
elif split == "valid":
return self.num_valid_samples
elif split == "test":
return self.num_test_samples
def get_batch(self, idx, split="train"):
if split == "train":
return self.train_loader[idx]
elif split == "valid":
return self.valid_loader[idx]
elif split == "test":
return self.test_loader[idx]
dataset = MNIST(batch_size=200, splits=(0.9, 0.1))
'''
Training the model on plaintext data before exporting to onnx format and
run in different PPML frameworks.
'''
def train(model, num_epochs=10, lr=1e-3):
all_train_losses = []
all_val_losses = []
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
for epoch in range(1, num_epochs + 1):
train_losses = []
model.train()
for (data, target) in dataset.train_loader:
# # Put the data on the same device as the model
# data = data.to(device)
# target = target.to(device)
optimizer.zero_grad()
output = model(data)
loss = torch.nn.CrossEntropyLoss()(output, target)
loss.backward()
train_losses.append(loss.item())
train_losses = train_losses[-100:]
optimizer.step()
model.eval()
val_loss = 0
correct = 0
with torch.no_grad():
for data, target in dataset.valid_loader:
# # Put the data on the same device as the model
# data = data.to(device)
# target = target.to(device)
output = model(data)
val_loss += torch.nn.CrossEntropyLoss(reduction='sum')(output, target).item() # sum up batch loss
pred = output.data.max(1, keepdim=True)[1] # get the index of the max log-probability
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
val_loss /= dataset.get_num_samples("valid")
train_loss = np.mean(train_losses)
print('Train Epoch: {} of {} Train Loss: {:.3f}, Val Loss: {:3f}, Val Accuracy: {:3f}'.format(
epoch, num_epochs, train_loss, val_loss, 100. * correct / dataset.get_num_samples("valid")))
all_train_losses.append(train_loss)
all_val_losses.append(val_loss)
# plt.plot(all_train_losses)
# plt.plot(all_val_losses)
# plt.legend(['train', 'val'])
# plt.show()
return all_train_losses, all_val_losses
slp_model = SingleNet()
mlp_model = SimpleNet()
conv_model = ConvNet()
print("\nTraining SLP model: ")
train(model=slp_model, num_epochs=10)
print("\nTraining MLP model: ")
train(model=mlp_model, num_epochs=10)
print("\nTraining ConvNet model: ")
train(model=conv_model, num_epochs=10)
# Testing for performance
# test_loss = 0
# correct = 0
# for data, target in dataset.test_loader:
# output = model(data)
# test_loss += torch.nn.CrossEntropyLoss(reduction='sum')(output, target).item() # sum up batch loss
# pred = output.data.max(1, keepdim=True)[1] # get the index of the max log-probability
# correct += pred.eq(target.data.view_as(pred)).cpu().sum()
# test_loss /= dataset.get_num_samples("test")
# print('After training, Test Loss: {:3f}, Test Accuracy: {:3f}'.format(
# test_loss, 100. * correct / dataset.get_num_samples("test")))
'''
set model to inference mode to prevent dropout and other training time functionalities
from interfering with inferences.
Export the model to onnx format.
'''
slp_model.eval()
mlp_model.eval()
# Random input to trace the flow in the model
x = torch.randn(1, 1, 28, 28, requires_grad=True)
torch_out = slp_model(x)
torch_out = mlp_model(x)
# Export the model
torch.onnx.export(slp_model, # model
x, # example input for tracing
"slp.onnx", # output file
export_params=True, # store the trained parameter weights inside the model file
opset_version=14, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'], # the model's output names
dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes
'output' : {0 : 'batch_size'}})
# Export the model
torch.onnx.export(mlp_model, # model
x, # example input for tracing
"mlp.onnx", # output file
export_params=True, # store the trained parameter weights inside the model file
opset_version=14, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'], # the model's output names
dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes
'output' : {0 : 'batch_size'}})
'''
Visualize some examples to make sure that the model is running properly.
'''
# predictions = []
# x_batch, y_batch = dataset.get_batch(1, "test")
# print(x_batch.shape)
# for i in range(50,55):
# y_pred = model(x_batch[i]).max(1, keepdim=True)[1]
# predictions.append(y_pred)
# x = x_batch[i].view(28, 28)
# plt.imshow(X=x, cmap="Greys")
# plt.show()
# print(predictions)
'''
Checking the validity of the output models.
'''
onnx_model = onnx.load("slp.onnx")
onnx.checker.check_model(onnx_model)
onnx_model = onnx.load("mlp.onnx")
onnx.checker.check_model(onnx_model)
'''
Generate testing inputs and outputs for the PPML frameworks, using the first
200 testing data points. (NOT USED: 200 DATA POINT IS TOO SLOW FOR FHE EVALUATION)
'''
input_batch, output_batch = dataset.get_batch(0, 'test')
with open("input0.npy", 'xb') as f:
np.save(f, input_batch)
with open("label0.npy", 'xb') as f:
np.save(f, output_batch)
'''
Save a subset of the training data as calibration data
'''
input_batch, output_batch = dataset.get_batch(0, 'train')
with open("input_calib.npy", 'xb') as f:
np.save(f, input_batch)
input = None
output = None
with open("input.npy", 'rb') as f:
input = np.load(f, allow_pickle=True)
input = torch.from_numpy(input)
# print(input.shape)
output = slp_model(input).detach()
# print(output)
with open("output.npy", 'xb') as f:
np.save(f, output)