File size: 5,565 Bytes
ff82fe6
3163344
10f217b
3163344
 
 
 
8e068be
3163344
 
 
ff82fe6
416c7bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edd29d3
416c7bb
 
 
 
 
 
 
 
855e240
 
 
 
 
 
01d9a57
 
 
 
 
a7a9b11
01d9a57
 
0693434
 
 
 
 
 
 
bc0f7fc
 
 
 
 
 
 
 
ac3ee6a
 
 
 
705feb9
ac3ee6a
 
afbe904
 
 
 
 
 
705feb9
 
 
 
 
 
 
9ea0468
 
 
 
 
 
 
 
86f4fb9
 
a7a9b11
86f4fb9
 
 
ff82fe6
 
 
9ea0468
 
ff82fe6
fd9442f
3163344
fd9442f
 
3163344
705feb9
 
65e514f
 
3163344
0693434
3163344
 
 
8364103
3163344
 
 
8d2e833
68dd12a
8364103
3163344
 
 
 
 
 
 
bc0f7fc
 
 
fd9442f
3163344
 
 
 
 
68dd12a
01d9a57
855e240
a7a9b11
3163344
 
 
 
 
705feb9
3163344
 
 
 
 
98da805
3163344
 
 
 
 
 
 
 
 
c681b80
3163344
 
416c7bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edd29d3
 
 
 
 
416c7bb
 
3163344
98da805
1750035
 
3163344
 
 
 
 
afbe904
3163344
 
 
 
912d566
3163344
 
8d9c0ef
5c43f60
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
import argparse
import os
import torch
import pytorch_lightning as ptl
from pytorch_lightning.loggers import TensorBoardLogger

from detector.data import FontDataModule
from detector.model import *
from utils import get_current_tag


parser = argparse.ArgumentParser()
parser.add_argument(
    "-d",
    "--devices",
    nargs="*",
    type=int,
    default=[0],
    help="GPU devices to use (default: [0])",
)
parser.add_argument(
    "-b",
    "--single-batch-size",
    type=int,
    default=64,
    help="Batch size of single device (default: 64)",
)
parser.add_argument(
    "-c",
    "--checkpoint",
    type=str,
    default=None,
    help="Trainer checkpoint path (default: None)",
)
parser.add_argument(
    "-m",
    "--model",
    type=str,
    default="resnet18",
    choices=["resnet18", "resnet34", "resnet50", "resnet101", "deepfont"],
    help="Model to use (default: resnet18)",
)
parser.add_argument(
    "-p",
    "--pretrained",
    action="store_true",
    help="Use pretrained model for ResNet (default: False)",
)
parser.add_argument(
    "-i",
    "--crop-roi-bbox",
    action="store_true",
    help="Crop ROI bounding box (default: False)",
)
parser.add_argument(
    "-a",
    "--augmentation",
    type=str,
    default=None,
    choices=["v1", "v2", "v3"],
    help="Augmentation strategy to use (default: None)",
)
parser.add_argument(
    "-l",
    "--lr",
    type=float,
    default=0.0001,
    help="Learning rate (default: 0.0001)",
)
parser.add_argument(
    "-s",
    "--datasets",
    nargs="*",
    type=str,
    default=["./dataset/font_img"],
    help="Datasets paths, seperated by space (default: ['./dataset/font_img'])",
)
parser.add_argument(
    "-n",
    "--model-name",
    type=str,
    default=None,
    help="Model name (default: current tag)",
)
parser.add_argument(
    "-f",
    "--font-classification-only",
    action="store_true",
    help="Font classification only (default: False)",
)
parser.add_argument(
    "-z",
    "--size",
    type=int,
    default=512,
    help="Model feature image input size (default: 512)",
)
parser.add_argument(
    "-t",
    "--tensor-core",
    type=str,
    choices=["medium", "high", "heighest"],
    default="high",
    help="Tensor core precision (default: high)",
)
parser.add_argument(
    "-r",
    "--preserve-aspect-ratio-by-random-crop",
    action="store_true",
    help="Preserve aspect ratio (default: False)",
)

args = parser.parse_args()

torch.set_float32_matmul_precision(args.tensor_core)

devices = args.devices
single_batch_size = args.single_batch_size

total_num_workers = os.cpu_count()
single_device_num_workers = total_num_workers // len(devices)

config.INPUT_SIZE = args.size

if os.name == "nt":
    single_device_num_workers = 0

lr = args.lr
b1 = 0.9
b2 = 0.999

lambda_font = 2.0
lambda_direction = 0.5
lambda_regression = 1.0

regression_use_tanh = False

num_warmup_epochs = 5
num_epochs = 100

log_every_n_steps = 100

num_device = len(devices)

data_module = FontDataModule(
    train_paths=[os.path.join(path, "train") for path in args.datasets],
    val_paths=[os.path.join(path, "val") for path in args.datasets],
    test_paths=[os.path.join(path, "test") for path in args.datasets],
    batch_size=single_batch_size,
    num_workers=single_device_num_workers,
    pin_memory=True,
    train_shuffle=True,
    val_shuffle=False,
    test_shuffle=False,
    regression_use_tanh=regression_use_tanh,
    train_transforms=args.augmentation,
    crop_roi_bbox=args.crop_roi_bbox,
    preserve_aspect_ratio_by_random_crop=args.preserve_aspect_ratio_by_random_crop,
)

num_iters = data_module.get_train_num_iter(num_device) * num_epochs
num_warmup_iter = data_module.get_train_num_iter(num_device) * num_warmup_epochs

model_name = get_current_tag() if args.model_name is None else args.model_name

logger_unconditioned = TensorBoardLogger(
    save_dir=os.getcwd(), name="tensorboard", version=model_name
)

strategy = "auto" if num_device == 1 else "ddp"

trainer = ptl.Trainer(
    max_epochs=num_epochs,
    logger=logger_unconditioned,
    devices=devices,
    accelerator="gpu",
    enable_checkpointing=True,
    log_every_n_steps=log_every_n_steps,
    strategy=strategy,
    deterministic=True,
)

if args.model == "resnet18":
    model = ResNet18Regressor(
        pretrained=args.pretrained, regression_use_tanh=regression_use_tanh
    )
elif args.model == "resnet34":
    model = ResNet34Regressor(
        pretrained=args.pretrained, regression_use_tanh=regression_use_tanh
    )
elif args.model == "resnet50":
    model = ResNet50Regressor(
        pretrained=args.pretrained, regression_use_tanh=regression_use_tanh
    )
elif args.model == "resnet101":
    model = ResNet101Regressor(
        pretrained=args.pretrained, regression_use_tanh=regression_use_tanh
    )
elif args.model == "deepfont":
    assert args.pretrained is False
    assert args.size == 105
    assert args.font_classification_only is True
    model = DeepFontBaseline()
else:
    raise NotImplementedError()

if torch.__version__ >= "2.0" and os.name == "posix":
    model = torch.compile(model)

detector = FontDetector(
    model=model,
    lambda_font=lambda_font,
    lambda_direction=lambda_direction,
    lambda_regression=lambda_regression,
    font_classification_only=args.font_classification_only,
    lr=lr,
    betas=(b1, b2),
    num_warmup_iters=num_warmup_iter,
    num_iters=num_iters,
    num_epochs=num_epochs,
)

trainer.fit(detector, datamodule=data_module, ckpt_path=args.checkpoint)
trainer.test(detector, datamodule=data_module)