-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
148 lines (125 loc) · 4.68 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
from argparse import ArgumentParser
import os
import os.path as osp
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.utils.data import DataLoader
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.callbacks import LearningRateMonitor
from pytorch_lightning.loggers import TensorBoardLogger
import timm
from lib_datasets.dataset_synthetics import FaceDataset, DataLoaderX
os.environ["PL_TORCH_DISTRIBUTED_BACKEND"] = "gloo"
class FaceSynthetics(pl.LightningModule):
def __init__(self, backbone):
super().__init__()
self.save_hyperparameters()
backbone = timm.create_model(backbone, num_classes=68*2)
self.backbone = backbone
self.loss = nn.L1Loss(reduction='mean')
self.hard_mining = False
def forward(self, x):
# use forward for inference/predictions
y = self.backbone(x)
return y
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self.backbone(x)
if self.hard_mining:
loss = torch.abs(y_hat - y) #(B,K)
loss = torch.mean(loss, dim=1) #(B,)
B = len(loss)
S = int(B*0.5)
loss, _ = torch.sort(loss, descending=True)
loss = loss[:S]
loss = torch.mean(loss) * 5.0
else:
loss = self.loss(y_hat, y) * 5.0
self.log('train_loss', loss, on_epoch=True)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self.backbone(x)
loss = self.loss(y_hat, y)
self.log('val_loss', loss, on_step=True)
def test_step(self, batch, batch_idx):
x, y = batch
y_hat = self.backbone(x)
loss = self.loss(y_hat, y)
self.log('test_loss', loss)
def configure_optimizers(self):
#return torch.optim.Adam(self.parameters(), lr=0.0002)
opt = torch.optim.SGD(self.parameters(), lr = 0.1, momentum=0.9, weight_decay = 0.0005)
def lr_step_func(epoch):
return 0.1 ** len([m for m in [15, 25, 28] if m <= epoch])
scheduler = torch.optim.lr_scheduler.LambdaLR(
optimizer=opt, lr_lambda=lr_step_func)
lr_scheduler = {
'scheduler': scheduler,
'name': 'learning_rate',
'interval':'epoch',
'frequency': 1}
return [opt], [lr_scheduler]
def cli_main():
pl.seed_everything(727)
# ------------
# args
# ------------
parser = ArgumentParser()
parser.add_argument('--backbone', default='resnet50d', type=str)
parser.add_argument('--batch_size', default=16, type=int)
parser.add_argument('--num_epochs', default=100, type=int)
parser.add_argument('--root', default='D:/datasets/FaceSynthetics/process_100000/', type=str)
parser.add_argument('--num_gpus', default=1, type=int)
parser.add_argument('--tf32', default= True, action='store_true')
parser = pl.Trainer.add_argparse_args(parser)
args = parser.parse_args()
# print(args.tf32)
# input()
if not args.tf32:
torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cudnn.allow_tf32 = False
else:
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
torch.backends.cudnn.benchmark = True
# ------------
# data
# ------------
train_set = FaceDataset(root_dir=args.root, is_train=True)
val_set = FaceDataset(root_dir=args.root, is_train=False)
train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, num_workers=8, pin_memory=True)
val_loader = DataLoader(val_set, batch_size=args.batch_size, num_workers=8, shuffle=False)
# ------------
# model
# ------------
model = FaceSynthetics(backbone=args.backbone)
ckpt_path = './output/synthetics/'
if not os.path.exists(ckpt_path):
os.makedirs(ckpt_path)
# ------------
# training
# ------------
checkpoint_callback = ModelCheckpoint(
monitor='val_loss',
dirpath=ckpt_path,
filename='{epoch:02d}-{val_loss:.6f}',
save_top_k=10,
mode='min',
)
lr_monitor = LearningRateMonitor(logging_interval='step')
trainer = pl.Trainer(
gpus = args.num_gpus,
# accelerator="ddp",
benchmark=True,
logger=TensorBoardLogger(osp.join(ckpt_path, 'logs')),
callbacks=[checkpoint_callback, lr_monitor],
check_val_every_n_epoch=1,
progress_bar_refresh_rate=1,
max_epochs=args.num_epochs,
)
trainer.fit(model, train_loader, val_loader)
if __name__ == '__main__':
cli_main()