-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·249 lines (201 loc) · 7.49 KB
/
train.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
#!/usr/bin/env python3
import os
import numpy as np
import torch
import torchvision
import utils.accumulators
config = dict(
dataset="Cifar10",
model="resnet18",
optimizer="SGD",
optimizer_decay_at_epochs=[150, 250],
optimizer_decay_with_factor=10.0,
optimizer_learning_rate=0.1,
optimizer_momentum=0.9,
optimizer_weight_decay=0.0001,
batch_size=256,
num_epochs=300,
seed=42,
)
output_dir = "./output.tmp" # Can be overwritten by a script calling this
def main():
"""
Train a model
You can either call this script directly (using the default parameters),
or import it as a module, override config and run main()
:return: scalar of the best accuracy
"""
# Set the seed
torch.manual_seed(config["seed"])
np.random.seed(config["seed"])
# We will run on CUDA if there is a GPU available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Configure the dataset, model and the optimizer based on the global
# `config` dictionary.
training_loader, test_loader = get_dataset()
model = get_model(device)
optimizer, scheduler = get_optimizer(model.parameters())
criterion = torch.nn.CrossEntropyLoss()
# We keep track of the best accuracy so far to store checkpoints
best_accuracy_so_far = utils.accumulators.Max()
for epoch in range(config["num_epochs"]):
print("Epoch {:03d}".format(epoch))
# Enable training mode (automatic differentiation + batch norm)
model.train()
# Keep track of statistics during training
mean_train_accuracy = utils.accumulators.Mean()
mean_train_loss = utils.accumulators.Mean()
for batch_x, batch_y in training_loader:
batch_x, batch_y = batch_x.to(device), batch_y.to(device)
# Compute gradients for the batch
optimizer.zero_grad()
prediction = model(batch_x)
loss = criterion(prediction, batch_y)
acc = accuracy(prediction, batch_y)
loss.backward()
# Do an optimizer step
optimizer.step()
# Store the statistics
mean_train_loss.add(loss.item(), weight=len(batch_x))
mean_train_accuracy.add(acc.item(), weight=len(batch_x))
# Update the optimizer's learning rate
scheduler.step()
# Log training stats
log_metric(
"accuracy",
{"epoch": epoch, "value": mean_train_accuracy.value()},
{"split": "train"},
)
log_metric(
"cross_entropy",
{"epoch": epoch, "value": mean_train_loss.value()},
{"split": "train"},
)
# Evaluation
model.eval()
mean_test_accuracy = utils.accumulators.Mean()
mean_test_loss = utils.accumulators.Mean()
for batch_x, batch_y in test_loader:
batch_x, batch_y = batch_x.to(device), batch_y.to(device)
prediction = model(batch_x)
loss = criterion(prediction, batch_y)
acc = accuracy(prediction, batch_y)
mean_test_loss.add(loss.item(), weight=len(batch_x))
mean_test_accuracy.add(acc.item(), weight=len(batch_x))
# Log test stats
log_metric(
"accuracy",
{"epoch": epoch, "value": mean_test_accuracy.value()},
{"split": "test"},
)
log_metric(
"cross_entropy",
{"epoch": epoch, "value": mean_test_loss.value()},
{"split": "test"},
)
best_accuracy_so_far.add(mean_test_accuracy.value())
# Return the optimal accuracy, could be used for learning rate tuning
return best_accuracy_so_far.value()
def accuracy(predicted_logits, reference):
"""Compute the ratio of correctly predicted labels"""
labels = torch.argmax(predicted_logits, 1)
correct_predictions = labels.eq(reference)
return correct_predictions.sum().float() / correct_predictions.nelement()
def log_metric(name, values, tags):
"""
Log timeseries data.
Placeholder implementation.
This function should be overwritten by any script that runs this as a module.
"""
print("{name}: {values} ({tags})".format(name=name, values=values, tags=tags))
def get_dataset(
test_batch_size=1000,
shuffle_train=True,
num_workers=2,
data_root=os.getenv("DATA_DIR", "./data"),
):
"""
Create dataset loaders for the chosen dataset
:return: Tuple (training_loader, test_loader)
"""
if config["dataset"] == "Cifar10":
dataset = torchvision.datasets.CIFAR10
elif config["dataset"] == "Cifar100":
dataset = torchvision.datasets.CIFAR100
else:
raise ValueError(
"Unexpected value for config[dataset] {}".format(config["dataset"])
)
data_mean = (0.4914, 0.4822, 0.4465)
data_stddev = (0.2023, 0.1994, 0.2010)
transform_train = torchvision.transforms.Compose(
[
torchvision.transforms.RandomCrop(32, padding=4),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(data_mean, data_stddev),
]
)
transform_test = torchvision.transforms.Compose(
[
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(data_mean, data_stddev),
]
)
training_set = dataset(
root=data_root, train=True, download=True, transform=transform_train
)
test_set = dataset(
root=data_root, train=False, download=True, transform=transform_test
)
training_loader = torch.utils.data.DataLoader(
training_set,
batch_size=config["batch_size"],
shuffle=shuffle_train,
num_workers=num_workers,
)
test_loader = torch.utils.data.DataLoader(
test_set, batch_size=test_batch_size, shuffle=False, num_workers=num_workers
)
return training_loader, test_loader
def get_optimizer(model_parameters):
"""
Create an optimizer for a given model
:param model_parameters: a list of parameters to be trained
:return: Tuple (optimizer, scheduler)
"""
if config["optimizer"] == "SGD":
optimizer = torch.optim.SGD(
model_parameters,
lr=config["optimizer_learning_rate"],
momentum=config["optimizer_momentum"],
weight_decay=config["optimizer_weight_decay"],
)
else:
raise ValueError("Unexpected value for optimizer")
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer,
milestones=config["optimizer_decay_at_epochs"],
gamma=1.0 / config["optimizer_decay_with_factor"],
)
return optimizer, scheduler
def get_model(device):
"""
:param device: instance of torch.device
:return: An instance of torch.nn.Module
"""
num_classes = 100 if config["dataset"] == "Cifar100" else 10
model = {
"vgg11": lambda: torchvision.models.vgg11(num_classes=num_classes),
"vgg11_bn": lambda: torchvision.models.vgg11_bn(num_classes=num_classes),
"resnet18": lambda: torchvision.models.resnet18(num_classes=num_classes),
"resnet50": lambda: torchvision.models.resnet50(num_classes=num_classes),
"resnet101": lambda: torchvision.models.resnet101(num_classes=num_classes),
}[config["model"]]()
model.to(device)
if device == "cuda":
model = torch.nn.DataParallel(model)
torch.backends.cudnn.benchmark = True
return model
if __name__ == "__main__":
main()