feat: add training script
Browse files
train.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pytorch_lightning as ptl
|
3 |
+
from pytorch_lightning.loggers import TensorBoardLogger
|
4 |
+
|
5 |
+
from detector.data import FontDataModule
|
6 |
+
from detector.model import FontDetector, ResNet18Regressor
|
7 |
+
from utils import get_current_tag
|
8 |
+
|
9 |
+
|
10 |
+
devices = [6, 7]
|
11 |
+
|
12 |
+
final_batch_size = 128
|
13 |
+
single_device_num_workers = 24
|
14 |
+
|
15 |
+
|
16 |
+
lr = 0.0001
|
17 |
+
b1 = 0.9
|
18 |
+
b2 = 0.999
|
19 |
+
|
20 |
+
lambda_font = 2.0
|
21 |
+
lambda_direction = 0.5
|
22 |
+
lambda_regression = 1.0
|
23 |
+
|
24 |
+
num_warmup_epochs = 10
|
25 |
+
num_epochs = 100
|
26 |
+
|
27 |
+
log_every_n_steps = 100
|
28 |
+
|
29 |
+
num_device = len(devices)
|
30 |
+
|
31 |
+
data_module = FontDataModule(
|
32 |
+
batch_size=final_batch_size // num_device,
|
33 |
+
num_workers=single_device_num_workers,
|
34 |
+
pin_memory=True,
|
35 |
+
train_shuffle=True,
|
36 |
+
val_shuffle=False,
|
37 |
+
test_shuffle=False,
|
38 |
+
)
|
39 |
+
|
40 |
+
num_iters = data_module.get_train_num_iter(num_device) * num_epochs
|
41 |
+
num_warmup_iter = data_module.get_train_num_iter(num_device) * num_warmup_epochs
|
42 |
+
|
43 |
+
model_name = f"{get_current_tag()}"
|
44 |
+
|
45 |
+
logger_unconditioned = TensorBoardLogger(
|
46 |
+
save_dir=os.getcwd(), name="tensorboard", version=model_name
|
47 |
+
)
|
48 |
+
|
49 |
+
strategy = None if num_device == 1 else "ddp"
|
50 |
+
|
51 |
+
trainer = ptl.Trainer(
|
52 |
+
max_epochs=num_epochs,
|
53 |
+
logger=logger_unconditioned,
|
54 |
+
devices=devices,
|
55 |
+
accelerator="gpu",
|
56 |
+
enable_checkpointing=True,
|
57 |
+
log_every_n_steps=log_every_n_steps,
|
58 |
+
strategy=strategy,
|
59 |
+
)
|
60 |
+
|
61 |
+
model = ResNet18Regressor()
|
62 |
+
|
63 |
+
detector = FontDetector(
|
64 |
+
model=model,
|
65 |
+
lambda_font=lambda_font,
|
66 |
+
lambda_direction=lambda_direction,
|
67 |
+
lambda_regression=lambda_regression,
|
68 |
+
lr=lr,
|
69 |
+
betas=(b1, b2),
|
70 |
+
num_warmup_iters=num_warmup_iter,
|
71 |
+
num_iters=num_iters,
|
72 |
+
)
|
73 |
+
|
74 |
+
trainer.fit(detector, datamodule=data_module)
|