-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_emnist.py
198 lines (159 loc) · 7.71 KB
/
train_emnist.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
# -*- coding: utf-8 -*-
"""
@date: 2023/10/9 下午3:08
@file: train.py
@author: zj
@description:
Usage - Single-GPU training:
$ python3 train_emnist.py ../datasets/emnist/ ./runs/crnn_tiny-emnist-b512/ --batch-size 512 --device 0
$ python3 train_emnist.py ../datasets/emnist/ ./runs/crnn-emnist-b512/ --batch-size 512 --device 0 --not-tiny
"""
import argparse
import os.path
import time
from tqdm import tqdm
import torch
import torch.optim as optim
import torch.distributed as dist
from torch.utils.data import DataLoader, distributed
from utils.model.crnn import CRNN
from utils.loss import CTCLoss
from utils.evaluator import Evaluator
from utils.torchutil import select_device
from utils.ddputil import smart_DDP
from utils.logger import LOGGER
from utils.general import init_seeds
from utils.dataset.emnist import EMNISTDataset, DIGITS_CHARS
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
RANK = int(os.getenv('RANK', -1))
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
def parse_opt():
parser = argparse.ArgumentParser(description='Training')
parser.add_argument('data', metavar='DIR', type=str, help='path to EMNIST dataset')
parser.add_argument('output', metavar='OUTPUT', type=str, help='path to output')
parser.add_argument('--batch-size', type=int, default=512, help='total batch size for all GPUs, -1 for autobatch')
parser.add_argument('--use-lstm', action='store_true', help='use nn.LSTM instead of nn.GRU')
parser.add_argument('--not-tiny', action='store_true', help='Use this flag to specify non-tiny mode')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--seed', type=int, default=0, help='Global training seed')
parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
args = parser.parse_args()
LOGGER.info(f"args: {args}")
return args
def adjust_learning_rate(lr, warmup_epoch, optimizer, epoch: int, step: int, len_epoch: int) -> None:
"""LR schedule that should yield 76% converged accuracy with batch size 256"""
# Warmup
lr = lr * float(1 + step + epoch * len_epoch) / (warmup_epoch * len_epoch)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def train(opt, device):
data_root, batch_size, not_tiny, use_lstm, output = opt.data, opt.batch_size, opt.not_tiny, opt.use_lstm, opt.output
if RANK in {-1, 0} and not os.path.exists(output):
os.makedirs(output)
img_h = 32
digits_per_sequence = 5
# (W, H)
input_shape = (digits_per_sequence * 5, img_h)
LOGGER.info("=> Create Model")
model = CRNN(in_channel=1, num_classes=len(DIGITS_CHARS), cnn_input_height=input_shape[1], is_tiny=not not_tiny,
use_gru=not use_lstm).to(device)
blank_label = len(DIGITS_CHARS) - 1
criterion = CTCLoss(blank_label=blank_label).to(device)
learn_rate = 0.001 * WORLD_SIZE
weight_decay = 1e-5
LOGGER.info(f"Final learning rate: {learn_rate}, weight decay: {weight_decay}")
optimizer = optim.Adam(model.parameters(), lr=learn_rate, weight_decay=weight_decay)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[40, 70, 90])
LOGGER.info("=> Load data")
train_dataset = EMNISTDataset(data_root, is_train=True, num_of_sequences=100000,
digits_per_sequence=digits_per_sequence, img_h=img_h)
sampler = None if LOCAL_RANK == -1 else distributed.DistributedSampler(train_dataset, shuffle=True)
train_dataloader = DataLoader(train_dataset,
batch_size=batch_size,
shuffle=True and sampler is None,
sampler=sampler,
num_workers=4,
drop_last=False,
pin_memory=True)
if RANK in {-1, 0}:
val_dataset = EMNISTDataset(data_root, is_train=False, num_of_sequences=5000,
digits_per_sequence=digits_per_sequence, img_h=img_h)
val_dataloader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=4, drop_last=False,
pin_memory=True)
LOGGER.info("=> Load evaluator")
emnist_evaluator = Evaluator(blank_label=blank_label)
LOGGER.info("=> Start training")
t0 = time.time()
amp = True
scaler = torch.cuda.amp.GradScaler(enabled=amp)
# DDP mode
cuda = device.type != 'cpu'
if cuda and RANK != -1:
model = smart_DDP(model)
epochs = 100
start_epoch = 1
warmup_epoch = 5
for epoch in range(start_epoch, epochs + start_epoch):
# epoch: start from 1
model.train()
if RANK != -1:
train_dataloader.sampler.set_epoch(epoch)
pbar = train_dataloader
if LOCAL_RANK in {-1, 0}:
pbar = tqdm(pbar)
optimizer.zero_grad()
for idx, (images, targets) in enumerate(pbar):
images = images.to(device)
targets = targets.to(device)
with torch.cuda.amp.autocast(amp):
outputs = model(images)
loss = criterion(outputs, targets)
scaler.scale(loss).backward()
if epoch <= warmup_epoch:
adjust_learning_rate(learn_rate, warmup_epoch, optimizer, epoch - 1, idx, len(train_dataloader))
scaler.step(optimizer) # optimizer.step
scaler.update()
optimizer.zero_grad()
if RANK in {-1, 0}:
lr = optimizer.param_groups[0]["lr"]
info = f"Epoch:{epoch} Batch:{idx} LR:{lr:.6f} Loss:{loss:.6f}"
pbar.set_description(info)
if RANK in {-1, 0} and epoch % 5 == 0 and epoch > 0:
model.eval()
if not_tiny:
save_path = os.path.join(output, f"crnn-emnist-b{batch_size}-e{epoch}.pth")
else:
save_path = os.path.join(output, f"crnn_tiny-emnist-b{batch_size}-e{epoch}.pth")
LOGGER.info(f"Save to {save_path}")
torch.save(model.state_dict(), save_path)
emnist_evaluator.reset()
pbar = tqdm(val_dataloader)
for idx, (images, targets) in enumerate(pbar):
images = images.to(device)
with torch.no_grad():
outputs = model(images).cpu()
acc = emnist_evaluator.update(outputs, targets)
info = f"Batch:{idx} ACC:{acc * 100:.3f}"
pbar.set_description(info)
acc = emnist_evaluator.result()
LOGGER.info(f"ACC: {acc * 100:.3f}")
scheduler.step()
torch.cuda.empty_cache()
LOGGER.info(f'\n{epochs} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
def main(opt):
# DDP mode
device = select_device(opt.device, batch_size=opt.batch_size)
if LOCAL_RANK != -1:
msg = 'is not compatible with LPDet Multi-GPU DDP training'
assert opt.batch_size != -1, f'AutoBatch with --batch-size -1 {msg}, please pass a valid --batch-size'
assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
torch.cuda.set_device(LOCAL_RANK)
device = torch.device('cuda', LOCAL_RANK)
dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
init_seeds(opt.seed + 1 + RANK, deterministic=False)
# LOGGER.info(f"LOCAL_RANK: {LOCAL_RANK} RANK: {RANK} WORLD_SIZE: {WORLD_SIZE}")
train(opt, device)
if __name__ == '__main__':
opt = parse_opt()
main(opt)