forked from aks2203/poisoning-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
executable file
·244 lines (221 loc) · 7.91 KB
/
train_model.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
#######################################################
#
# train_model.py
# Train and save models
# Developed as part of Poison Attack Benchmarking project
# June 2019
#
############################################################
import argparse
import os
import sys
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import torchvision
from learning_module import (
train,
test,
adjust_learning_rate,
to_log_file,
now,
get_model,
PoisonedDataset,
get_transform,
)
def main(args):
"""Main function to train and test a model
input:
args: Argparse object that contains all the parsed values
return:
void
"""
print(now(), "train_model.py main() running.")
np.random.seed(args.seed)
torch.manual_seed(args.seed)
train_log = "train_log.txt"
to_log_file(args, args.output, train_log)
# Set device
device = "cuda" if torch.cuda.is_available() else "cpu"
####################################################
# Load the Dataset
if args.dataset.lower() == "cifar10":
transform_train = get_transform(args.normalize, args.train_augment)
transform_test = get_transform(args.normalize, False)
trainset = torchvision.datasets.CIFAR10(
root="./data", train=True, download=True, transform=transform_train
)
trainset = PoisonedDataset(
trainset, (), args.trainset_size, transform=transform_train
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=args.batch_size, shuffle=True
)
testset = torchvision.datasets.CIFAR10(
root="./data", train=False, download=True, transform=transform_test
)
testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False)
elif args.dataset.lower() == "cifar100":
transform_train = get_transform(args.normalize, args.train_augment)
transform_test = get_transform(args.normalize, False)
trainset = torchvision.datasets.CIFAR100(
root="./data", train=True, download=True, transform=transform_train
)
trainset = PoisonedDataset(
trainset, (), args.trainset_size, transform=transform_train
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=args.batch_size, shuffle=True
)
testset = torchvision.datasets.CIFAR100(
root="./data", train=False, download=True, transform=transform_test
)
testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False)
else:
print("Dataset not yet implemented. Ending run from train_model.py.")
sys.exit()
####################################################
####################################################
# Network and Optimizer
net = get_model(args.model, args.dataset)
net = net.to(device)
start_epoch = 0
if args.optimizer == "SGD":
optimizer = optim.SGD(
net.parameters(), lr=args.lr, weight_decay=2e-4, momentum=0.9
)
elif args.optimizer == "adam":
optimizer = optim.Adam(net.parameters(), lr=args.lr, weight_decay=2e-4)
criterion = nn.CrossEntropyLoss()
if args.model_path is not None:
state_dict = torch.load(args.model_path, map_location=device)
net.load_state_dict(state_dict["net"])
optimizer.load_state_dict(state_dict["optimizer"])
start_epoch = state_dict["epoch"]
####################################################
####################################################
# Train and Test
print("==> Training network...")
loss = 0
all_losses = []
epoch = start_epoch
for epoch in range(start_epoch, args.epochs):
adjust_learning_rate(optimizer, epoch, args.lr_schedule, args.lr_factor)
loss, acc = train(net, trainloader, optimizer, criterion, device)
all_losses.append(loss)
if (epoch + 1) % args.val_period == 0:
natural_acc = test(net, testloader, device)
print(
now(),
" Epoch: ",
epoch,
", Loss: ",
loss,
", Training acc: ",
acc,
", Natural accuracy: ",
natural_acc,
)
to_log_file(
{
"epoch": epoch,
"loss": loss,
"training_acc": acc,
"natural_acc": natural_acc,
},
args.output,
train_log,
)
# test
natural_acc = test(net, testloader, device)
print(
now(), " Training ended at epoch ", epoch, ", Natural accuracy: ", natural_acc
)
to_log_file(
{"epoch": epoch, "loss": loss, "natural_acc": natural_acc},
args.output,
train_log,
)
####################################################
####################################################
# Save
if args.save_net:
state = {
"net": net.state_dict(),
"epoch": epoch,
"optimizer": optimizer.state_dict(),
}
out_str = os.path.join(
args.checkpoint,
args.model
+ "_seed_"
+ str(args.seed)
+ "_normalize="
+ str(args.normalize)
+ "_augment="
+ str(args.train_augment)
+ "_optimizer="
+ str(args.optimizer)
+ "_epoch="
+ str(epoch)
+ ".pth",
)
print("Saving model to: ", args.checkpoint, " out_str: ", out_str)
if not os.path.isdir(args.checkpoint):
os.makedirs(args.checkpoint)
torch.save(state, out_str)
####################################################
return
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Poisoning Benchmark")
parser.add_argument("--lr", default=0.01, type=float, help="learning rate")
parser.add_argument(
"--lr_schedule",
nargs="+",
default=[100, 150],
type=int,
help="when to decrease lr",
)
parser.add_argument(
"--lr_factor", default=0.1, type=float, help="factor by which to decrease lr"
)
parser.add_argument(
"--epochs", default=200, type=int, help="number of epochs for training"
)
parser.add_argument("--optimizer", default="SGD", type=str, help="optimizer")
parser.add_argument(
"--model", default="ResNet18", type=str, help="model for training"
)
parser.add_argument("--dataset", default="CIFAR10", type=str, help="dataset")
parser.add_argument("--trainset_size", default=None, type=int, help="Trainset size")
parser.add_argument(
"--val_period", default=20, type=int, help="print every __ epoch"
)
parser.add_argument(
"--output", default="output_default", type=str, help="output subdirectory"
)
parser.add_argument(
"--checkpoint",
default="check_default",
type=str,
help="where to save the network",
)
parser.add_argument(
"--model_path", default=None, type=str, help="where is the model saved?"
)
parser.add_argument("--save_net", action="store_true", help="save net?")
parser.add_argument(
"--seed", default=0, type=int, help="seed for seeding random processes."
)
parser.add_argument("--normalize", dest="normalize", action="store_true")
parser.add_argument("--no-normalize", dest="normalize", action="store_false")
parser.set_defaults(normalize=True)
parser.add_argument("--train_augment", dest="train_augment", action="store_true")
parser.add_argument(
"--no-train_augment", dest="train_augment", action="store_false"
)
parser.set_defaults(train_augment=False)
args = parser.parse_args()
main(args)