-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenc_train.py
More file actions
273 lines (204 loc) · 8.6 KB
/
enc_train.py
File metadata and controls
273 lines (204 loc) · 8.6 KB
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
import shutil
import crypten
import crypten.communicator as comm
import numpy as np
import torch
import torch.optim
import torch.optim as optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.transforms as transforms
from torch import nn
from torchvision.datasets import ImageFolder
from model import get_model
# def get_model(model_name, num_classes):
# return LeNet()
def train_epochs(args):
epochs = args.max_epochs
start_epoch = 0
batch_size = args.batch_size
lr = args.lr
print_freq = 10
best_prec1 = 0
crypten.init()
model = get_model(model_name=args.backbone, num_classes=args.num_classes)
print(f"\n{'#'*40}")
print(f"loaded {args.backbone} with {args.num_classes} classes")
print(model)
print(f"{'#'*40}\n")
# optimizer
optimizer = optim.SGD(model.parameters(), lr=args.lr)
# loss
criterion = nn.CrossEntropyLoss()
if args.backbone == 'LeNet':
input_dim = 32
elif args.backbone == 'BigLeNet':
input_dim = 64
else:
input_dim = 244
# define appropriate transforms:
transform = transforms.Compose(
[
transforms.Resize(input_dim),
transforms.CenterCrop(input_dim),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
train_set = ImageFolder(args.source_dir_train, transform=transform)
eval_set = ImageFolder(args.source_dir_eval, transform=transform)
train_loader = torch.utils.data.DataLoader(train_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=1)
val_loader = torch.utils.data.DataLoader(eval_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=1)
training_scores = []
# define loss function (criterion) and optimizer
for epoch in range(start_epoch, epochs):
adjust_learning_rate(optimizer, epoch, lr)
# train for one epoch
train(train_loader, model, criterion, optimizer, epoch, print_freq)
# evaluate on validation set
prec1 = validate(val_loader, model, criterion)
training_scores.append(prec1)
# remember best prec@1 and save checkpoint
best_prec1 = max(prec1, best_prec1)
is_best = prec1 > best_prec1
print(f"\n{'#'*40}")
print(f"EPOCH {epoch} with score {np.mean(training_scores)}")
print(f"{'#'*40}\n")
save_checkpoint(model=model, filename=args.training_run_out)
input_size = get_input_size(val_loader, batch_size)
private_model = construct_private_model(input_size, model, args.backbone, args.num_classes)
validate_side_by_side(val_loader, plaintext_model=model, private_model=private_model)
def train(train_loader, model, criterion, optimizer, epoch, print_freq=10):
# switch to train mode
model.train()
for i, (input, target) in enumerate(train_loader):
output = model(input)
loss = criterion(output, target)
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % print_freq == 0:
print(f"train: {epoch}.{i} loss {loss}")
def validate_side_by_side(val_loader, plaintext_model, private_model):
"""Validate the plaintext and private models side-by-side on each example"""
# switch to evaluate mode
plaintext_model.eval()
private_model.eval()
softmax = nn.Softmax(dim=1)
correct = 0
scores = []
total = 0
with torch.no_grad():
for i, (input, target) in enumerate(val_loader):
# compute output for plaintext
output_plaintext = plaintext_model(input)
# encrypt input and compute output for private
# assumes that private model is encrypted with src=0
input_encr = encrypt_data_tensor_with_src(input)
output_encr = private_model(input_encr)
p = softmax(output_plaintext)
p, predicted = p.data.max(1)
score = accuracy(predicted, target)
scores.append(score)
print(f"Example {i}")
print(f"target:\t\t {target}")
print(f"predicted:\t {predicted}")
print(f"confidence: {p}")
print(f"Plaintext:\n{output_plaintext}")
print(f"Encrypted:\n{output_encr.get_plain_text()}\n")
print(f'Accuracy of the network on the 10000 test images: {np.mean(scores)}')
# only use the first 1000 examples
if i > 3:
break
def accuracy(pred, target):
correct = (pred == target).sum().item()
correct /= target.size(0)
return correct
def get_input_size(val_loader, batch_size):
input, target = next(iter(val_loader))
return input.size()
def construct_private_model(input_size, model, model_name: str, num_classes: int):
"""Encrypt and validate trained model for multi-party setting."""
# get rank of current process
rank = comm.get().get_rank()
dummy_input = torch.empty(input_size)
# party 0 always gets the actual model; remaining parties get dummy model
if rank == 0:
model_upd = model
else:
model_upd = get_model(model_name=model_name, num_classes=num_classes)
private_model = crypten.nn.from_pytorch(model_upd, dummy_input).encrypt(src=0)
return private_model
def encrypt_data_tensor_with_src(input):
"""Encrypt data tensor for multi-party setting"""
# get rank of current process
rank = comm.get().get_rank()
# get world size
world_size = comm.get().get_world_size()
if world_size > 1:
# party 1 gets the actual tensor; remaining parties get dummy tensor
src_id = 1
else:
# party 0 gets the actual tensor since world size is 1
src_id = 0
if rank == src_id:
input_upd = input
else:
input_upd = torch.empty(input.size())
private_input = crypten.cryptensor(input_upd, src=src_id)
return private_input
def validate(val_loader, model, criterion):
# switch to evaluate mode
model.eval()
scores = []
softmax = nn.Softmax(dim=1)
with torch.no_grad():
for i, (input, target) in enumerate(val_loader):
if isinstance(model, crypten.nn.Module) and not crypten.is_encrypted_tensor(
input
):
input = encrypt_data_tensor_with_src(input)
# compute output
output = model(input)
if crypten.is_encrypted_tensor(output):
output = output.get_plain_text()
p = softmax(output)
p, predicted = p.data.max(1)
score = accuracy(predicted, target)
scores.append(score)
loss = criterion(output, target)
print(f"validate {i}, loss {loss.data}, score {np.mean(scores)}")
return np.mean(scores)
def save_checkpoint(model, filename="checkpoint.pth.tar"):
"""Saves checkpoint of plaintext model"""
# only save from rank 0 process to avoid race condition
print(f"\n{'#'*40}")
print(f"model saved to {filename}")
print(f"{'#'*40}\n")
torch.save(model, filename)
def adjust_learning_rate(optimizer, epoch, lr=0.01):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
new_lr = lr * (0.1 ** (epoch // 5))
for param_group in optimizer.param_groups:
param_group["lr"] = new_lr
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="CrypTen Predoc Training")
parser.add_argument('--source-dir-train', type=str, help='source dir to folder training set')
parser.add_argument('--source-dir-eval', type=str, help='source dir to folder training set')
parser.add_argument('--training-run-out', type=str, help='save model at ..')
parser.add_argument('--backbone', type=str, default='resnet18', help="backbone, network architecture")
parser.add_argument('--num-classes', type=int, default=2, help='number of classes to train on')
parser.add_argument('--validate-encrypted', type=bool, default=False, help='validate on encrypted model and data')
parser.add_argument('--max-epochs', type=int, default=5, help='maxi epochs to train')
parser.add_argument('--batch-size', type=int, default=5, help='batch size for training')
parser.add_argument('--lr', type=float, default=0.001, help='learning rate for optimizer')
args = parser.parse_args()
train_epochs(args)