-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipeline.py
256 lines (209 loc) · 8.79 KB
/
pipeline.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
import os
import random
import time
from argparse import ArgumentParser
import numpy as np
import torch
import yaml
from torch import nn
import wandb
from config import get_config
from utils.dataset import (
get_test_loader,
get_train_valid_loader_CIFAR10,
get_test_loader_CIFAR100,
get_train_valid_loader_CIFAR100,
get_CUB_data_loaders,
get_miniImageNet_dataloaders,
)
from utils.loss import LabelSmoothingLoss
from utils.misc import calc_step, count_params, get_model, log, seed_everything
from utils.optim import get_optimizer, get_optimizer_classifier
from utils.scheduler import WarmUpLR, get_scheduler
from utils.train import evaluate, train
def training_pipeline(config):
"""Initiates and executes all the steps involved with model training.
Args:
config (dict) - Dict containing various settings for the training run.
"""
config["exp"]["save_dir"] = os.path.join(
config["exp"]["exp_dir"], config["exp"]["exp_name"]
)
os.makedirs(config["exp"]["save_dir"], exist_ok=True)
######################################
# save hyperparameters for current run
######################################
config_str = yaml.dump(config)
print("Using settings:\n", config_str)
with open(os.path.join(config["exp"]["save_dir"], "settings.txt"), "w+") as f:
f.write(config_str)
#####################################
# initialize training items
#####################################
# data
if config["hparams"]["dataset"] == "CIFAR10":
trainloader, valloader = get_train_valid_loader_CIFAR10(
data_dir=config["data_dir"],
random_seed=config["hparams"]["seed"],
train_transform_config=config["hparams"]["train_transform"],
valid_transform_config=config["hparams"]["val_transform"],
batch_size=config["hparams"]["batch_size"],
num_workers=config["exp"]["n_workers"],
pin_memory=config["exp"]["pin_memory"],
augment=config["hparams"]["augment"],
)
testloader = get_test_loader(
data_dir=config["data_dir"],
batch_size=config["hparams"]["batch_size"],
num_workers=config["exp"]["n_workers"],
pin_memory=config["exp"]["pin_memory"],
)
elif config["hparams"]["dataset"] == "CIFAR100":
trainloader, valloader = get_train_valid_loader_CIFAR100(
data_dir=config["data_dir"],
random_seed=config["hparams"]["seed"],
train_transform_config=config["hparams"]["train_transform"],
valid_transform_config=config["hparams"]["val_transform"],
batch_size=config["hparams"]["batch_size"],
num_workers=config["exp"]["n_workers"],
pin_memory=config["exp"]["pin_memory"],
augment=config["hparams"]["augment"],
)
testloader = get_test_loader_CIFAR100(
data_dir=config["data_dir"],
batch_size=config["hparams"]["batch_size"],
num_workers=config["exp"]["n_workers"],
pin_memory=config["exp"]["pin_memory"],
)
elif config["hparams"]["dataset"] == "CUB":
trainloader = get_CUB_data_loaders(
config["data_dir"], config["hparams"]["batch_size"], train=True
)
(valloader, testloader) = get_CUB_data_loaders(
config["data_dir"], config["hparams"]["batch_size"], train=False
)
elif config["hparams"]["dataset"] == "miniImageNet":
trainloader, valloader, testloader = get_miniImageNet_dataloaders(
data_dir=config["data_dir"],
batch_size=config["hparams"]["batch_size"],
augment=config["hparams"]["augment"],
)
# model
model = get_model(config["hparams"]["model"])
model = model.to(config["hparams"]["device"])
print(f"Created model with {count_params(model)} parameters.")
# loss
if config["hparams"]["l_smooth"]:
criterion = LabelSmoothingLoss(
num_classes=config["hparams"]["num_classes"],
smoothing=config["hparams"]["l_smooth"],
)
else:
criterion = nn.CrossEntropyLoss()
# optimizer
optimizer = get_optimizer(model, config["hparams"]["optimizer"])
#####################################
# Freeze Weights
#####################################
if config["hparams"]["restore_ckpt"] and config["hparams"]["freeze_weights"]:
ckpt = torch.load(config["hparams"]["restore_ckpt"])
model.load_state_dict(ckpt["model_state_dict"], strict=False)
for name, module in model.named_modules():
if "lorentz_mlp_head" in name:
module.requires_grad_(requires_grad=True)
else:
module.requires_grad_(requires_grad=False)
optimizer = get_optimizer_classifier(model, config["hparams"]["optimizer"])
print(f'Restored state from {config["hparams"]["restore_ckpt"]} successfully.')
# scheduler
schedulers = {"warmup": None, "scheduler": None}
if config["hparams"]["scheduler"].get("n_warmup") is not None:
schedulers["warmup"] = WarmUpLR(
optimizer,
total_iters=len(trainloader) * config["hparams"]["scheduler"]["n_warmup"],
)
if config["hparams"]["scheduler"]["scheduler_type"] == "cosine_annealing":
total_iters = len(trainloader) * max(
1,
(
config["hparams"]["scheduler"]["max_epochs"]
- config["hparams"]["scheduler"]["cosine_annealing"]["start_epoch"]
),
)
schedulers["scheduler"] = get_scheduler(
optimizer,
config["hparams"]["scheduler"]["scheduler_type"],
T_max=total_iters,
)
elif config["hparams"]["scheduler"]["scheduler_type"] == "one_cycle_lr":
total_iters = len(trainloader) * config["hparams"]["scheduler"]["max_epochs"]
schedulers["scheduler"] = get_scheduler(
optimizer,
scheduler_type=config["hparams"]["scheduler"]["scheduler_type"],
max_lr=config["hparams"]["scheduler"]["one_cycle_lr"]["max_lr"],
T_max=total_iters,
)
#####################################
# Resume run
#####################################
if config["hparams"]["restore_ckpt"] and config["hparams"]["continue_train"]:
ckpt = torch.load(config["hparams"]["restore_ckpt"])
config["hparams"]["start_epoch"] = ckpt["epoch"] + 1
model.load_state_dict(ckpt["model_state_dict"])
optimizer.load_state_dict(ckpt["optimizer_state_dict"])
if schedulers["scheduler"]:
schedulers["scheduler"].load_state_dict(ckpt["scheduler_state_dict"])
print(f'Restored state from {config["hparams"]["restore_ckpt"]} successfully.')
#####################################
# Training
#####################################
print("Initiating training.")
train(model, optimizer, criterion, trainloader, valloader, schedulers, config)
#####################################
# Final Test
#####################################
final_step = calc_step(
config["hparams"]["n_epochs"] + 1, len(trainloader), len(trainloader) - 1
)
# evaluating the final state (last.pth)
test_acc, test_loss = evaluate(
model, criterion, testloader, config["hparams"]["device"]
)
log_dict = {"test_loss_last": test_loss, "test_acc_last": test_acc}
log(log_dict, final_step, config)
# evaluating the best validation state (best.pth)
ckpt = torch.load(os.path.join(config["exp"]["save_dir"], "best.pth"))
model.load_state_dict(ckpt["model_state_dict"])
print("Best ckpt loaded.")
test_acc, test_loss = evaluate(
model, criterion, testloader, config["hparams"]["device"]
)
log_dict = {"test_loss_best": test_loss, "test_acc_best": test_acc}
log(log_dict, final_step, config)
def main(args):
config = get_config(args.conf)
seed_everything(config["hparams"]["seed"])
if config["exp"]["wandb"]:
if config["exp"]["wandb_api_key"] is not None:
with open(config["exp"]["wandb_api_key"], "r") as f:
os.environ["WANDB_API_KEY"] = f.read()
print(os.environ["WANDB_API_KEY"])
elif os.environ.get("WANDB_API_KEY", False):
print(f"Found API key from env variable.")
else:
wandb.login()
with wandb.init(
project=config["exp"]["proj_name"],
name=config["exp"]["exp_name"],
config=config["hparams"],
):
training_pipeline(config)
else:
training_pipeline(config)
if __name__ == "__main__":
parser = ArgumentParser("Driver code.")
parser.add_argument(
"--conf", type=str, required=True, help="Path to config.yaml file."
)
args = parser.parse_args()
main(args)