-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_img.py
323 lines (272 loc) · 11.3 KB
/
train_img.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import argparse
import os
import os.path as osp
import time
from os.path import join as osj
from typing import Any, Callable, Dict, List, Optional, Tuple
import pytorch_lightning as pl
from pytorch_lightning.strategies import DDPStrategy
import timm
import torch
import torchvision.transforms as T
import torchvision.transforms.functional as TF
import yaml
from fvcore.common.config import CfgNode
from pytorch_lightning import LightningDataModule, LightningModule, Trainer
from pytorch_lightning.callbacks import (LearningRateMonitor, ModelCheckpoint,
early_stopping)
from pytorch_lightning.loggers import TensorBoardLogger, WandbLogger
from pytorch_lightning.strategies import DDPStrategy
from torch import Tensor, nn, optim
from torch.utils.data import DataLoader
from workoutdetector.datasets import build_dataset
from workoutdetector.settings import PROJ_ROOT
class LitModel(LightningModule):
def __init__(self, cfg: CfgNode):
super().__init__()
self.example_input_array = torch.randn(1, 3, 224, 224)
self.save_hyperparameters()
backbone = timm.create_model(cfg.model.backbone_model,
pretrained=True,
num_classes=cfg.model.num_class)
self.classifier = backbone
self.loss_module = nn.CrossEntropyLoss()
self.cfg = cfg
self.best_val_acc = 0
def forward(self, x):
return self.classifier(x)
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self.forward(x)
loss = self.loss_module(y_hat, y)
acc = (y_hat.argmax(dim=1) == y).float().mean()
self.log("train/acc", acc, prog_bar=True, on_step=False, on_epoch=True)
self.log('train/loss', loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self.forward(x)
loss = self.loss_module(y_hat, y)
acc = (y_hat.argmax(dim=1) == y).float().mean()
self.log("val/acc", acc, prog_bar=True, on_step=False, on_epoch=True)
self.log('val/loss', loss)
return (y_hat.argmax(dim=1) == y).flatten()
def validation_epoch_end(self, outputs):
total = sum(len(o) for o in outputs)
correct = sum(o.sum().item() for o in outputs)
acc = correct / total
self.best_val_acc = max(self.best_val_acc, acc)
if self.trainer.is_global_zero:
self.log('val/best_acc', acc, rank_zero_only=True)
def test_step(self, batch, batch_idx):
x, y = batch
y_hat = self.forward(x)
loss = self.loss_module(y_hat, y)
acc = (y_hat.argmax(dim=1) == y).float().mean()
self.log("test/acc", acc, on_step=False, on_epoch=True)
self.log('test/loss', loss, prog_bar=True)
def predict_step(self, batch, batch_idx, dataloader_idx=0):
return self(batch)
def configure_optimizers(self):
cfg = self.cfg
OPTIMIZER = cfg.optimizer.method.lower()
SCHEDULER = cfg.lr_scheduler.policy.lower()
if OPTIMIZER == 'sgd':
optimizer = optim.SGD(self.parameters(),
lr=cfg.optimizer.lr,
momentum=cfg.optimizer.momentum,
weight_decay=cfg.optimizer.weight_decay)
elif OPTIMIZER == 'adamw':
optimizer = optim.AdamW(self.parameters(),
lr=cfg.optimizer.lr,
eps=cfg.optimizer.eps,
weight_decay=cfg.optimizer.weight_decay)
else:
raise NotImplementedError(
f'Not implemented optimizer: {cfg.optimizer.method}')
if SCHEDULER == 'steplr':
scheduler = optim.lr_scheduler.StepLR(optimizer,
step_size=cfg.lr_scheduler.step,
gamma=cfg.lr_scheduler.gamma)
else:
raise NotImplementedError(f'Not implemented lr schedular: {cfg.lr_schedular}')
return {
"optimizer": optimizer,
"lr_scheduler": scheduler,
"monitor": "val/loss",
}
class DataModule(LightningDataModule):
"""General image dataset
label text files of [image.png class] are required
Args:
cfg (CfgNode): configs of cfg.data
is_train: bool, train or test. Default True
"""
def __init__(self, cfg: CfgNode, is_train: bool = True, num_class: int = 0) -> None:
super().__init__()
self.cfg = cfg
self.num_class = num_class
# self._check_data()
def _check_data(self):
"""Check data exists and annotation files are correct."""
for split in ['train', 'val', 'test']:
ds = build_dataset(self.cfg, split)
for i, (x, y) in enumerate(ds):
assert type(x) == torch.Tensor, f"{type(x) is not Tensor}"
assert 0 <= y < self.num_class, f"{y} is not in [0, {self.num_class})"
def train_dataloader(self):
train_set = build_dataset(self.cfg, 'train')
loader = DataLoader(train_set,
num_workers=self.cfg.num_workers,
batch_size=self.cfg.batch_size,
shuffle=True)
return loader
def val_dataloader(self):
val_set = build_dataset(self.cfg, 'val')
loader = DataLoader(val_set,
num_workers=self.cfg.num_workers,
batch_size=self.cfg.batch_size,
shuffle=False)
return loader
def test_dataloader(self):
if self.cfg.test.anno:
test_set = build_dataset(self.cfg.dataset_type, self.cfg, 'test')
loader = DataLoader(test_set,
num_workers=self.cfg.num_workers,
batch_size=self.cfg.batch_size,
shuffle=False)
return loader
else:
return self.val_dataloader()
def test(cfg: CfgNode) -> None:
data_module = DataModule(cfg.data, is_train=False)
model = LitModel.load_from_checkpoint(cfg.checkpoint)
trainer = Trainer(
default_root_dir=cfg.trainer.default_root_dir,
accelerator=cfg.trainer.accelerator,
devices=cfg.trainer.devices,
)
trainer.test(model, data_module)
def train(cfg: CfgNode) -> None:
data_module = DataModule(cfg.data, num_class=cfg.model.num_class)
model = LitModel(cfg)
timenow = time.strftime('%Y%m%d-%H%M%S', time.localtime())
# callbacks
CALLBACKS: List[Any] = []
lr_monitor = LearningRateMonitor(logging_interval='step')
CALLBACKS.append(lr_monitor)
# ModelCheckpoint callback
if cfg.callbacks.modelcheckpoint.dirpath:
DIRPATH = cfg.callbacks.modelcheckpoint.dirpath
else:
DIRPATH = osj(cfg.trainer.default_root_dir, 'checkpoints')
if not os.path.isdir(DIRPATH):
print(f'Create checkpoint directory: {DIRPATH}')
os.makedirs(DIRPATH)
checkpoint_callback = ModelCheckpoint(
save_top_k=cfg.callbacks.modelcheckpoint.save_top_k,
save_weights_only=cfg.callbacks.modelcheckpoint.save_weights_only,
monitor=cfg.callbacks.modelcheckpoint.monitor,
mode=cfg.callbacks.modelcheckpoint.mode,
dirpath=DIRPATH,
filename="best-val-acc={val/acc:.2f}-epoch={epoch:02d}" + f"-{timenow}",
auto_insert_metric_name=False)
CALLBACKS.append(checkpoint_callback)
if cfg.trainer.early_stopping:
early_stop = early_stopping.EarlyStopping(monitor='train/loss',
mode='min',
patience=cfg.trainer.patience)
CALLBACKS.append(early_stop)
# loggers
cfg_dict = cfg_to_dict(cfg)
LOGGER: List[Any] = []
if cfg.log.wandb.enable:
wandb_logger = WandbLogger(
save_dir=osj(cfg.log.output_dir),
project=cfg.log.wandb.project,
name=cfg.log.name,
offline=cfg.log.wandb.offline,
)
wandb_logger.log_hyperparams(cfg_dict)
wandb_logger.watch(model, log="all")
LOGGER.append(wandb_logger)
if cfg.log.tensorboard.enable:
tensorboard_logger = TensorBoardLogger(save_dir=cfg.log.output_dir,
name=cfg.log.name,
default_hp_metric=False)
tensorboard_logger.log_hyperparams(cfg_dict,
metrics={
'train/acc': 0,
'val/acc': 0,
'test/acc': 0,
'train/loss': -1,
'val/loss': -1,
'test/loss': -1
})
LOGGER.append(tensorboard_logger)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
trainer = Trainer(
default_root_dir=cfg.trainer.default_root_dir,
max_epochs=cfg.trainer.max_epochs,
accelerator=cfg.trainer.accelerator,
devices=cfg.trainer.devices,
logger=LOGGER,
callbacks=CALLBACKS,
auto_lr_find=cfg.trainer.auto_lr_find,
log_every_n_steps=cfg.log.log_every_n_steps,
fast_dev_run=cfg.trainer.fast_dev_run,
strategy=DDPStrategy(find_unused_parameters=False,
process_group_backend='gloo'),
)
trainer.fit(model, data_module)
model.load_from_checkpoint(checkpoint_callback.best_model_path)
print(f"===>Best model saved at:\n{checkpoint_callback.best_model_path}")
trainer.test(model, data_module)
def export_model(ckpt: str, onnx_path: Optional[str] = None) -> None:
model = LitModel.load_from_checkpoint(ckpt)
model.eval()
if onnx_path is None:
onnx_path = ckpt.replace('.ckpt', '.onnx')
model.to_onnx(onnx_path, input_sample=model.example_input_array, export_params=True)
def cfg_to_dict(cfg: CfgNode) -> dict:
x = cfg.dump()
y = yaml.safe_load(x)
return y
def parse_args(argv=None) -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Train image model')
parser.add_argument(
"--cfg",
dest="cfg_file",
help="Path to the config file",
default=osj(PROJ_ROOT, "workoutdetector/configs/lit_img.yaml"),
type=str,
)
parser.add_argument(
"opts",
help="See workoutdetector/configs/lit_img.yaml for all options",
default=None,
nargs=argparse.REMAINDER,
)
return parser.parse_args(argv)
def load_config(args) -> CfgNode:
"""
Given the arguemnts, load and initialize the configs.
Args:
args (argument): arguments includes `cfg_file`.
"""
cfg = CfgNode(new_allowed=True)
cfg.merge_from_file(args.cfg_file)
if args.opts is not None:
cfg.merge_from_list(args.opts)
return cfg
def main(cfg: CfgNode) -> None:
pl.seed_everything(cfg.seed)
if cfg.train:
train(cfg)
else:
test(cfg)
if __name__ == '__main__':
args = parse_args()
cfg = load_config(args)
main(cfg)