-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
153 lines (119 loc) · 4.36 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
import logging
import json
import math
import os
import sys
import time
from datetime import datetime
import torch
import torchvision
import hydra
import wandb
from hydra.utils import get_original_cwd
from omegaconf import DictConfig
from omegaconf.omegaconf import OmegaConf
from data import get_transforms
from model import BarlowTwins
from optim import get_optimizer
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(sys.stdout))
def train(config, model, loader):
if config.wandb_entity:
wandb.watch(model, log_freq=100)
model.train()
optimizer = get_optimizer(config, model)
# automatically resume from checkpoint if it exists
if os.path.exists(f"{config.checkpoint_dir}/checkpoint.pth"):
ckpt = torch.load(f"{config.checkpoint_dir}/checkpoint.pth", map_location="cpu")
start_epoch = ckpt["epoch"]
model.load_state_dict(ckpt["model"])
optimizer.load_state_dict(ckpt["optimizer"])
else:
start_epoch = 0
start_time = time.time()
scaler = torch.cuda.amp.GradScaler()
for epoch in range(start_epoch, config.max_epochs):
for step, ((y1, y2), _) in enumerate(loader, start=epoch * len(loader)):
y1 = y1.to(config.device)
y2 = y2.to(config.device)
adjust_learning_rate(config, optimizer, loader, step)
optimizer.zero_grad()
with torch.cuda.amp.autocast():
loss = model.forward(y1, y2)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
if config.wandb_entity:
wandb.log(
{"loss": loss.item(), "lr": optimizer.param_groups[0]["lr"]},
)
if step % config.print_freq == 0:
stats = dict(
epoch=epoch,
step=step,
lr_weights=optimizer.param_groups[0]["lr"],
lr_biases=optimizer.param_groups[1]["lr"],
loss=loss.item(),
time=int(time.time() - start_time),
)
print(json.dumps(stats))
state = dict(
epoch=epoch + 1,
model=model.state_dict(),
optimizer=optimizer.state_dict(),
)
torch.save(state, config.checkpoint_dir / "checkpoint.pth")
torch.save(
model.module.encoder.state_dict(), config.checkpoint_dir / f"{config.model}.pth"
)
def adjust_learning_rate(config, optimizer, loader, step):
max_steps = config.max_epochs * len(loader)
warmup_steps = 10 * len(loader)
base_lr = config.batch_size / 256
if step < warmup_steps:
lr = base_lr * step / warmup_steps
else:
step -= warmup_steps
max_steps -= warmup_steps
q = 0.5 * (1 + math.cos(math.pi * step / max_steps))
end_lr = base_lr * 0.001
lr = base_lr * q + end_lr * (1 - q)
optimizer.param_groups[0]["lr"] = lr * config.lr_weights
optimizer.param_groups[1]["lr"] = lr * config.lr_biases
@hydra.main(config_path="./conf", config_name="config")
def main(config: DictConfig) -> None:
if config.seed:
torch.manual_seed(config.seed)
logger.info(OmegaConf.to_yaml(config, resolve=True))
logger.info(f"Using the model: {config.model}")
cwd = get_original_cwd()
# dataset = torchvision.datasets.CIFAR10(
# f"{cwd}/cifar10/", train=True, download=True, transform=get_transforms(config)
# )
dataset = torchvision.datasets.ImageFolder(
config.data.path / "train", get_transforms(config)
)
loader = torch.utils.data.DataLoader(
dataset,
batch_size=config.batch_size,
num_workers=config.workers,
pin_memory=True,
)
if not config.debug:
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
run_name = f"{config.run_name}_{config.data.path}_{timestamp}"
if config.wandb_entity:
wandb.init(
entity=config.wandb_entity,
project=config.wandb_project,
config=dict(config),
name=run_name,
)
if not config.train.pt:
config.train.pt = f"{config.train.pt}/{run_name}"
model = BarlowTwins(config)
model.to(config.device)
train(config, model, loader)
if __name__ == "__main__":
main()