-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
308 lines (265 loc) · 9.4 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
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
import argparse
import os
from pprint import pprint
import timm
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from PIL import Image
from pytorch_lightning import LightningDataModule, LightningModule, Trainer
from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint
from pytorch_lightning.utilities.seed import seed_everything
from torch.utils.data import DataLoader
from torchmetrics import Accuracy
from torchvision.datasets import ImageFolder
# solver settings
OPT = 'adam' # adam, sgd
WEIGHT_DECAY = 0.0001
MOMENTUM = 0.9 # only when OPT is sgd
BASE_LR = 0.001
LR_SCHEDULER = 'step' # step, multistep, reduce_on_plateau
LR_DECAY_RATE = 0.1
LR_STEP_SIZE = 5 # only when LR_SCHEDULER is step
LR_STEP_MILESTONES = [10, 15] # only when LR_SCHEDULER is multistep
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Train classifier.')
parser.add_argument(
'--dataset', '-d', type=str, required=True, help='Root directory of dataset'
)
parser.add_argument(
'--outdir', '-o', type=str, default='results', help='Output directory'
)
parser.add_argument(
'--model-name', '-m', type=str, default='resnet18', help='Model name (timm)'
)
parser.add_argument(
'--img-size', '-i', type=int, default=112, help='Input size of image'
)
parser.add_argument(
'--epochs', '-e', type=int, default=100, help='Number of training epochs'
)
parser.add_argument(
'--save-interval', '-s', type=int, default=10, help='Save interval (epoch)'
)
parser.add_argument('--batch-size', '-b', type=int, default=8, help='Batch size')
parser.add_argument(
'--num-workers', '-w', type=int, default=12, help='Number of workers'
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--gpu-ids', type=int, default=None, nargs='+', help='GPU IDs to use'
)
group.add_argument('--n-gpu', type=int, default=None, help='Number of GPUs')
parser.add_argument('--seed', type=int, default=42, help='Seed')
args = parser.parse_args()
return args
def get_optimizer(parameters) -> torch.optim.Optimizer:
if OPT == 'adam':
optimizer = torch.optim.Adam(parameters, lr=BASE_LR, weight_decay=WEIGHT_DECAY)
elif OPT == 'sgd':
optimizer = torch.optim.SGD(
parameters, lr=BASE_LR, weight_decay=WEIGHT_DECAY, momentum=MOMENTUM
)
else:
raise NotImplementedError()
return optimizer
def get_lr_scheduler_config(optimizer: torch.optim.Optimizer) -> dict:
if LR_SCHEDULER == 'step':
scheduler = torch.optim.lr_scheduler.StepLR(
optimizer, step_size=LR_STEP_SIZE, gamma=LR_DECAY_RATE
)
lr_scheduler_config = {
'scheduler': scheduler,
'interval': 'epoch',
'frequency': 1,
}
elif LR_SCHEDULER == 'multistep':
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=LR_STEP_MILESTONES, gamma=LR_DECAY_RATE
)
lr_scheduler_config = {
'scheduler': scheduler,
'interval': 'epoch',
'frequency': 1,
}
elif LR_SCHEDULER == 'reduce_on_plateau':
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, mode='max', factor=0.1, patience=10, threshold=0.0001
)
lr_scheduler_config = {
'scheduler': scheduler,
'monitor': 'val/loss',
'interval': 'epoch',
'frequency': 1,
}
else:
raise NotImplementedError
return lr_scheduler_config
class ImageTransform:
def __init__(self, is_train: bool, img_size: int | tuple = 112):
if is_train:
self.transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(p=0.5),
transforms.Resize(img_size),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
else:
self.transform = transforms.Compose(
[
transforms.Resize(img_size),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
def __call__(self, img: Image.Image) -> torch.Tensor:
return self.transform(img)
class SimpleData(LightningDataModule):
def __init__(
self,
root_dir: str,
img_size: int = 112,
batch_size: int = 8,
num_workers: int = 16,
):
super().__init__()
self.root_dir = root_dir
self.img_size = img_size
self.batch_size = batch_size
self.num_workers = num_workers
self.train_dataset = ImageFolder(
root=os.path.join(root_dir, 'train'),
transform=ImageTransform(is_train=True, img_size=self.img_size),
)
self.val_dataset = ImageFolder(
root=os.path.join(root_dir, 'val'),
transform=ImageTransform(is_train=False, img_size=self.img_size),
)
self.classes = self.train_dataset.classes
self.class_to_idx = self.train_dataset.class_to_idx
def train_dataloader(self) -> DataLoader:
dataloader = DataLoader(
self.train_dataset,
batch_size=self.batch_size,
shuffle=True,
drop_last=True,
num_workers=self.num_workers,
)
return dataloader
def val_dataloader(self) -> DataLoader:
dataloader = DataLoader(
self.val_dataset,
batch_size=self.batch_size,
shuffle=False,
drop_last=False,
num_workers=self.num_workers,
)
return dataloader
class SimpleModel(LightningModule):
def __init__(
self,
model_name: str = 'resnet18',
pretrained: bool = False,
num_classes: int | None = None,
):
super().__init__()
self.save_hyperparameters()
self.model = timm.create_model(
model_name=model_name, pretrained=pretrained, num_classes=num_classes
)
self.train_loss = nn.CrossEntropyLoss()
self.train_acc = Accuracy()
self.val_loss = nn.CrossEntropyLoss()
self.val_acc = Accuracy()
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, target = batch
out = self(x)
_, pred = out.max(1)
loss = self.train_loss(out, target)
acc = self.train_acc(pred, target)
self.log_dict({'train/loss': loss, 'train/acc': acc}, prog_bar=True)
return loss
def validation_step(self, batch, batch_idx):
x, target = batch
out = self(x)
_, pred = out.max(1)
loss = self.val_loss(out, target)
acc = self.val_acc(pred, target)
self.log_dict({'val/loss': loss, 'val/acc': acc})
def configure_optimizers(self):
optimizer = get_optimizer(self.parameters())
lr_scheduler_config = get_lr_scheduler_config(optimizer)
return {"optimizer": optimizer, "lr_scheduler": lr_scheduler_config}
def get_basic_callbacks(checkpoint_interval: int = 1) -> list:
lr_callback = LearningRateMonitor(logging_interval='epoch')
ckpt_callback = ModelCheckpoint(
filename='epoch{epoch:03d}',
auto_insert_metric_name=False,
save_top_k=-1,
every_n_epochs=checkpoint_interval,
)
return [ckpt_callback, lr_callback]
def get_gpu_settings(
gpu_ids: list[int], n_gpu: int
) -> tuple[str, int | list[int] | None, str | None]:
"""Get gpu settings for pytorch-lightning trainer:
https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#trainer-flags
Args:
gpu_ids (list[int])
n_gpu (int)
Returns:
tuple[str, int, str]: accelerator, devices, strategy
"""
if not torch.cuda.is_available():
return "cpu", None, None
if gpu_ids is not None:
devices = gpu_ids
strategy = "ddp" if len(gpu_ids) > 1 else None
elif n_gpu is not None:
# int
devices = n_gpu
strategy = "ddp" if n_gpu > 1 else None
else:
devices = 1
strategy = None
return "gpu", devices, strategy
def get_trainer(args: argparse.Namespace) -> Trainer:
callbacks = get_basic_callbacks(checkpoint_interval=args.save_interval)
accelerator, devices, strategy = get_gpu_settings(args.gpu_ids, args.n_gpu)
trainer = Trainer(
max_epochs=args.epochs,
callbacks=callbacks,
default_root_dir=args.outdir,
accelerator=accelerator,
devices=devices,
strategy=strategy,
logger=True,
deterministic=True,
)
return trainer
if __name__ == '__main__':
args = get_args()
seed_everything(args.seed, workers=True)
data = SimpleData(
root_dir=args.dataset,
img_size=args.img_size,
batch_size=args.batch_size,
num_workers=args.num_workers,
)
model = SimpleModel(
model_name=args.model_name, pretrained=True, num_classes=len(data.classes)
)
trainer = get_trainer(args)
print('Args:')
pprint(args.__dict__)
print('Training classes:')
pprint(data.class_to_idx)
trainer.fit(model, data)