-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2_training.py
163 lines (140 loc) · 5.5 KB
/
P2_training.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
import argparse
import pathlib
import shutil
from heapq import heappop, heappush
import torch
from torch.utils.data import DataLoader, random_split
from torch.utils.tensorboard.writer import SummaryWriter
from torchvision import models, transforms
from tqdm import tqdm
from byol_pytorch import BYOL
from P2_dataloader import ImageFolderDataset
from warmup_scheduler import GradualWarmupScheduler
def main(args):
train_transform = transforms.Compose([
transforms.Resize(128),
transforms.ToTensor(),
# already normalized in BYOL pipeline
# transforms.Normalize(
# mean=[0.485, 0.456, 0.406],
# std=[0.229, 0.224, 0.225]
# )
])
dataset = ImageFolderDataset(
args.train_image_dir,
transform=train_transform,
)
train_set, valid_set = random_split(dataset, [0.9, 0.1])
train_loader = DataLoader(
train_set, batch_size=args.batch_size, shuffle=True, num_workers=4, pin_memory=True
)
valid_loader = DataLoader(
valid_set, batch_size=2 * args.batch_size, shuffle=False, num_workers=4, pin_memory=True
)
resnet = models.resnet50(weights=None)
learner = BYOL(
resnet,
image_size=128,
hidden_layer='avgpool',
use_momentum=False,
).to(args.device)
# Training
amp_enable = args.fp16
amp_device_type = 'cpu' if args.device == torch.device('cpu') else 'cuda'
if amp_enable:
print(
f"Enable AMP training on {args.device}:{amp_device_type}")
optim = torch.optim.Adam(learner.parameters(), lr=args.lr)
scaler = torch.cuda.amp.GradScaler(enabled=amp_enable)
scheduler = GradualWarmupScheduler(
optim,
multiplier=1,
total_epoch=args.warmup_steps,
after_scheduler=torch.optim.lr_scheduler.CosineAnnealingLR(
optim,
T_max=args.epochs * len(train_loader) - args.warmup_steps
)
)
# Logging
if args.tensorboard_path.exists():
shutil.rmtree(args.tensorboard_path)
writer = SummaryWriter(args.tensorboard_path)
optim.zero_grad(set_to_none=True)
optim.step()
log_global_step = 0
saved_files = [] # max_heap
for epoch in range(args.epochs):
writer.add_scalar('training/epoch', epoch, global_step=log_global_step)
# Training
for data in tqdm(train_loader):
# Forward & Backpropagation
img = data['img'].to(args.device)
with torch.autocast(device_type=amp_device_type, enabled=amp_enable):
loss = learner(img)
# Update
scheduler.step()
optim.zero_grad(set_to_none=True)
scaler.scale(loss).backward()
scaler.step(optim)
scaler.update()
# learner.update_moving_average() # update moving average of target encoder
# Log
writer.add_scalar("training/lr",
optim.param_groups[0]['lr'], global_step=log_global_step)
writer.add_scalar("training/loss", loss.item(),
global_step=log_global_step)
log_global_step += 1
# Validation
va_loss = []
for data in valid_loader:
# Forward & Backpropagation
img = data['img'].to(args.device)
with torch.no_grad():
with torch.autocast(device_type=amp_device_type, enabled=amp_enable):
loss = learner(img)
# Update
loss = loss.item()
va_loss.append(loss)
# Log
writer.add_scalar("validation/loss", loss,
global_step=log_global_step)
# Save
va_loss = sum(va_loss) / len(va_loss)
print(f"Epoch {epoch}, validation loss: {va_loss}")
if not saved_files or va_loss < -saved_files[0][0]:
save_path = args.ckpt_dir / f"{epoch}_backbone.pth"
torch.save(resnet.state_dict(), save_path)
print("Saved model")
while len(saved_files) > args.save_best_k - 1:
_, popped_state_dict = heappop(saved_files)
popped_state_dict.unlink()
heappush(saved_files, (-va_loss, save_path))
torch.save(resnet.state_dict(), args.ckpt_dir / f"last_backbone.pth")
print(
f'Done, best validation loss: {min(saved_files, key=lambda x: -x[0])}')
def parse_args():
parser = argparse.ArgumentParser()
# Environment
parser.add_argument('--train_image_dir', type=pathlib.Path,
default='hw4_data/mini/train')
parser.add_argument('--device', type=torch.device,
default='cuda' if torch.cuda.is_available() else 'cpu')
# Output Path
parser.add_argument("--tensorboard_path",
type=pathlib.Path, default="./P2_tb/backbone/")
parser.add_argument("--ckpt_dir",
type=pathlib.Path, default="./P2_ckpt")
parser.add_argument('--save_best_k', type=int, default=4)
# Training args
parser.add_argument("--fp16", action="store_true")
parser.add_argument("--epochs", type=int, default=1000)
parser.add_argument("--lr", type=float, default=1e-4)
parser.add_argument("--batch_size", type=int, default=512)
parser.add_argument("--warmup_steps", type=int, default=1000)
args = parser.parse_args()
assert args.save_best_k > 0, "--save_best_k must > 0"
args.ckpt_dir.mkdir(exist_ok=True, parents=True)
return args
if __name__ == '__main__':
args = parse_args()
main(args)