PaddleOCR/tools/train.py

121 lines
3.9 KiB
Python
Raw Normal View History

2020-05-10 16:26:57 +08:00
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import sys
2020-10-13 17:13:33 +08:00
__dir__ = os.path.dirname(os.path.abspath(__file__))
2020-06-12 13:49:24 +08:00
sys.path.append(__dir__)
sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
2020-05-10 16:26:57 +08:00
2020-10-13 17:13:33 +08:00
import yaml
import paddle
import paddle.distributed as dist
2020-05-10 16:26:57 +08:00
2020-11-05 15:13:36 +08:00
paddle.seed(2)
2020-05-10 16:26:57 +08:00
2020-10-13 17:13:33 +08:00
from ppocr.data import build_dataloader
2020-11-04 20:43:27 +08:00
from ppocr.modeling.architectures import build_model
from ppocr.losses import build_loss
2020-10-13 17:13:33 +08:00
from ppocr.optimizer import build_optimizer
from ppocr.postprocess import build_post_process
from ppocr.metrics import build_metric
from ppocr.utils.save_load import init_model
import tools.program as program
2020-05-10 16:26:57 +08:00
2020-10-13 17:13:33 +08:00
dist.get_world_size()
2020-05-10 16:26:57 +08:00
2020-10-13 17:13:33 +08:00
def main(config, device, logger, vdl_writer):
# init dist environment
if config['Global']['distributed']:
dist.init_parallel_env()
2020-05-10 16:26:57 +08:00
2020-10-13 17:13:33 +08:00
global_config = config['Global']
2020-11-05 15:13:36 +08:00
2020-10-13 17:13:33 +08:00
# build dataloader
2020-11-05 15:13:36 +08:00
train_dataloader = build_dataloader(config, 'Train', device, logger)
2021-02-04 11:33:48 +08:00
if len(train_dataloader) == 0:
logger.error(
'No Images in train dataset, please check annotation file and path in the configuration file'
)
2020-11-04 20:43:27 +08:00
if config['Eval']:
2020-11-05 15:13:36 +08:00
valid_dataloader = build_dataloader(config, 'Eval', device, logger)
2020-10-13 17:13:33 +08:00
else:
2020-11-04 20:43:27 +08:00
valid_dataloader = None
2020-10-13 17:13:33 +08:00
# build post process
2020-11-05 15:13:36 +08:00
post_process_class = build_post_process(config['PostProcess'],
global_config)
2020-10-13 17:13:33 +08:00
# build model
2020-11-06 19:11:35 +08:00
# for rec algorithm
2020-10-13 17:13:33 +08:00
if hasattr(post_process_class, 'character'):
2020-11-04 20:43:27 +08:00
char_num = len(getattr(post_process_class, 'character'))
config['Architecture']["Head"]['out_channels'] = char_num
2020-10-13 17:13:33 +08:00
model = build_model(config['Architecture'])
if config['Global']['distributed']:
model = paddle.DataParallel(model)
2020-11-04 20:43:27 +08:00
# build loss
loss_class = build_loss(config['Loss'])
2020-11-05 15:13:36 +08:00
2020-10-13 17:13:33 +08:00
# build optim
2020-11-05 15:13:36 +08:00
optimizer, lr_scheduler = build_optimizer(
config['Optimizer'],
2020-10-13 17:13:33 +08:00
epochs=config['Global']['epoch_num'],
2020-11-04 20:43:27 +08:00
step_each_epoch=len(train_dataloader),
2020-10-13 17:13:33 +08:00
parameters=model.parameters())
# build metric
eval_class = build_metric(config['Metric'])
2020-11-04 20:43:27 +08:00
# load pretrain model
pre_best_model_dict = init_model(config, model, logger, optimizer)
2020-10-13 17:13:33 +08:00
2020-11-06 19:15:21 +08:00
logger.info('train dataloader has {} iters, valid dataloader has {} iters'.
2020-11-06 19:11:35 +08:00
format(len(train_dataloader), len(valid_dataloader)))
2020-10-13 17:13:33 +08:00
# start train
2020-11-05 15:13:36 +08:00
program.train(config, train_dataloader, valid_dataloader, device, model,
loss_class, optimizer, lr_scheduler, post_process_class,
eval_class, pre_best_model_dict, logger, vdl_writer)
2020-11-04 20:43:27 +08:00
def test_reader(config, device, logger):
2020-11-06 19:11:35 +08:00
loader = build_dataloader(config, 'Train', device, logger)
import time
starttime = time.time()
count = 0
try:
2020-11-04 20:43:27 +08:00
for data in loader():
count += 1
if count % 1 == 0:
batch_time = time.time() - starttime
starttime = time.time()
2020-11-12 12:06:46 +08:00
logger.info("reader: {}, {}, {}".format(
count, len(data[0]), batch_time))
except Exception as e:
2020-07-01 13:09:44 +08:00
logger.info(e)
logger.info("finish reader: {}, Success!".format(count))
2020-11-05 15:13:36 +08:00
2020-05-10 16:26:57 +08:00
if __name__ == '__main__':
config, device, logger, vdl_writer = program.preprocess(is_train=True)
2020-11-04 20:43:27 +08:00
main(config, device, logger, vdl_writer)
2020-11-12 12:06:46 +08:00
# test_reader(config, device, logger)