-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
129 lines (102 loc) · 4.24 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
import os
import os.path as osp
import time
import math
from datetime import timedelta
from argparse import ArgumentParser
import torch
from torch import cuda
from torch.utils.data import DataLoader
from torch.optim import lr_scheduler
from tqdm import tqdm
from east_dataset import EASTDataset
from dataset import SceneTextDataset
from model import EAST
def parse_args():
parser = ArgumentParser()
# Conventional args
parser.add_argument('--data_dir', type=str,
default=os.environ.get('SM_CHANNEL_TRAIN', '../data/medical'))
parser.add_argument('--model_dir', type=str, default=os.environ.get('SM_MODEL_DIR',
'trained_models'))
parser.add_argument('--device', default='cuda' if cuda.is_available() else 'cpu')
parser.add_argument('--num_workers', type=int, default=8)
parser.add_argument('--image_size', type=int, default=2048)
parser.add_argument('--input_size', type=int, default=1024)
parser.add_argument('--batch_size', type=int, default=8)
parser.add_argument('--learning_rate', type=float, default=1e-3)
parser.add_argument('--max_epoch', type=int, default=150)
parser.add_argument('--save_interval', type=int, default=5)
parser.add_argument('--ignore_tags', type=list, default=['masked', 'excluded-region', 'maintable', 'stamp'])
args = parser.parse_args()
if args.input_size % 32 != 0:
raise ValueError('`input_size` must be a multiple of 32')
return args
def do_training(data_dir, model_dir, device, image_size, input_size, num_workers, batch_size,
learning_rate, max_epoch, save_interval, ignore_tags):
dataset = SceneTextDataset(
data_dir,
split='train',
image_size=image_size,
crop_size=input_size,
ignore_tags=ignore_tags
)
dataset = EASTDataset(dataset)
num_batches = math.ceil(len(dataset) / batch_size)
train_loader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers
)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = EAST()
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[max_epoch // 2], gamma=0.1)
min_epoch_loss = 999
model.train()
for epoch in range(max_epoch):
epoch_loss, epoch_start = 0, time.time()
with tqdm(total=num_batches) as pbar:
for img, gt_score_map, gt_geo_map, roi_mask in train_loader:
pbar.set_description('[Epoch {}]'.format(epoch + 1))
loss, extra_info = model.train_step(img, gt_score_map, gt_geo_map, roi_mask)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_val = loss.item()
epoch_loss += loss_val
pbar.update(1)
val_dict = {
'Cls loss': extra_info['cls_loss'], 'Angle loss': extra_info['angle_loss'],
'IoU loss': extra_info['iou_loss']
}
pbar.set_postfix(val_dict)
scheduler.step()
if (epoch_loss / num_batches <= min_epoch_loss):
min_epoch_loss = epoch_loss / num_batches
if not osp.exists(model_dir):
os.makedirs(model_dir)
best_fpath = osp.join(model_dir, 'best.pth')
torch.save(model.state_dict(), best_fpath)
print('Mean loss: {:.4f} | Elapsed time: {}'.format(
epoch_loss / num_batches, timedelta(seconds=time.time() - epoch_start)))
if (epoch + 1) % save_interval == 0:
if not osp.exists(model_dir):
os.makedirs(model_dir)
ckpt_fpath = osp.join(model_dir, 'latest.pth')
torch.save(model.state_dict(), ckpt_fpath)
def main(args):
# seed 고정
seed = 47
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
do_training(**args.__dict__)
if __name__ == '__main__':
args = parse_args()
main(args)