-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.py
80 lines (56 loc) · 2.26 KB
/
main.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
import time
from collections import defaultdict
import hydra
import torch
from omegaconf import DictConfig
from src import utils
def train(opt, model, optimizer):
start_time = time.time()
train_loader = utils.get_data(opt, "train")
num_steps_per_epoch = len(train_loader)
for epoch in range(opt.training.epochs):
train_results = defaultdict(float)
optimizer = utils.update_learning_rate(optimizer, opt, epoch)
for inputs, labels in train_loader:
inputs, labels = utils.preprocess_inputs(opt, inputs, labels)
optimizer.zero_grad()
scalar_outputs = model(inputs, labels)
scalar_outputs["Loss"].backward()
optimizer.step()
train_results = utils.log_results(
train_results, scalar_outputs, num_steps_per_epoch
)
utils.print_results("train", time.time() - start_time, train_results, epoch)
start_time = time.time()
# Validate.
if epoch % opt.training.val_idx == 0 and opt.training.val_idx != -1:
validate_or_test(opt, model, "val", epoch=epoch)
return model
def validate_or_test(opt, model, partition, epoch=None):
test_time = time.time()
test_results = defaultdict(float)
data_loader = utils.get_data(opt, partition)
num_steps_per_epoch = len(data_loader)
model.eval()
print(partition)
with torch.no_grad():
for inputs, labels in data_loader:
inputs, labels = utils.preprocess_inputs(opt, inputs, labels)
scalar_outputs = model.forward_downstream_classification_model(
inputs, labels
)
test_results = utils.log_results(
test_results, scalar_outputs, num_steps_per_epoch
)
utils.print_results(partition, time.time() - test_time, test_results, epoch=epoch)
model.train()
@hydra.main(config_path=".", config_name="config", version_base=None)
def my_main(opt: DictConfig) -> None:
opt = utils.parse_args(opt)
model, optimizer = utils.get_model_and_optimizer(opt)
model = train(opt, model, optimizer)
validate_or_test(opt, model, "val")
if opt.training.final_test:
validate_or_test(opt, model, "test")
if __name__ == "__main__":
my_main()