__init__.py 3.6 KB
Newer Older
L
LDOUBLEV 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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.
W
WenmuZhou 已提交
14 15 16 17 18 19 20 21 22

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import sys
import numpy as np
littletomatodonkey's avatar
littletomatodonkey 已提交
23
import skimage
W
WenmuZhou 已提交
24
import paddle
D
dyning 已提交
25 26
import signal
import random
W
WenmuZhou 已提交
27 28 29 30 31

__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.abspath(os.path.join(__dir__, '../..')))

import copy
D
dyning 已提交
32
from paddle.io import Dataset, DataLoader, BatchSampler, DistributedBatchSampler
W
WenmuZhou 已提交
33 34 35
import paddle.distributed as dist

from ppocr.data.imaug import transform, create_operators
D
dyning 已提交
36
from ppocr.data.simple_dataset import SimpleDataSet
X
xiaoting 已提交
37
from ppocr.data.lmdb_dataset import LMDBDataSet, LMDBDataSetSR
38
from ppocr.data.pgnet_dataset import PGDataSet
M
MissPenguin 已提交
39
from ppocr.data.pubtab_dataset import PubTabDataSet
W
WenmuZhou 已提交
40 41 42

__all__ = ['build_dataloader', 'transform', 'create_operators']

D
dyning 已提交
43

D
dyning 已提交
44 45 46 47 48 49 50
def term_mp(sig_num, frame):
    """ kill all child processes
    """
    pid = os.getpid()
    pgid = os.getpgid(os.getpid())
    print("main proc {} exit, kill process group " "{}".format(pid, pgid))
    os.killpg(pgid, signal.SIGKILL)
W
WenmuZhou 已提交
51

D
dyning 已提交
52

53
def build_dataloader(config, mode, device, logger, seed=None):
D
dyning 已提交
54
    config = copy.deepcopy(config)
D
dyning 已提交
55

littletomatodonkey's avatar
littletomatodonkey 已提交
56
    support_dict = [
X
xiaoting 已提交
57
        'SimpleDataSet', 'LMDBDataSet', 'PGDataSet', 'PubTabDataSet',
58
        'LMDBDataSetSR'
littletomatodonkey's avatar
littletomatodonkey 已提交
59
    ]
D
dyning 已提交
60
    module_name = config[mode]['dataset']['name']
W
WenmuZhou 已提交
61 62
    assert module_name in support_dict, Exception(
        'DataSet only support {}'.format(support_dict))
D
dyning 已提交
63 64 65
    assert mode in ['Train', 'Eval', 'Test'
                    ], "Mode should be Train, Eval or Test."

66
    dataset = eval(module_name)(config, mode, logger, seed)
D
dyning 已提交
67 68 69
    loader_config = config[mode]['loader']
    batch_size = loader_config['batch_size_per_card']
    drop_last = loader_config['drop_last']
L
LDOUBLEV 已提交
70
    shuffle = loader_config['shuffle']
D
dyning 已提交
71
    num_workers = loader_config['num_workers']
72 73 74 75
    if 'use_shared_memory' in loader_config.keys():
        use_shared_memory = loader_config['use_shared_memory']
    else:
        use_shared_memory = True
littletomatodonkey's avatar
littletomatodonkey 已提交
76

D
dyning 已提交
77
    if mode == "Train":
J
Jethong 已提交
78
        # Distribute data to multiple cards
D
dyning 已提交
79 80 81
        batch_sampler = DistributedBatchSampler(
            dataset=dataset,
            batch_size=batch_size,
L
LDOUBLEV 已提交
82
            shuffle=shuffle,
D
dyning 已提交
83
            drop_last=drop_last)
W
WenmuZhou 已提交
84
    else:
J
Jethong 已提交
85
        # Distribute data to single card
D
dyning 已提交
86 87 88
        batch_sampler = BatchSampler(
            dataset=dataset,
            batch_size=batch_size,
L
LDOUBLEV 已提交
89
            shuffle=shuffle,
D
dyning 已提交
90 91
            drop_last=drop_last)

92 93 94 95 96
    if 'collate_fn' in loader_config:
        from . import collate_fn
        collate_fn = getattr(collate_fn, loader_config['collate_fn'])()
    else:
        collate_fn = None
D
dyning 已提交
97 98 99 100 101
    data_loader = DataLoader(
        dataset=dataset,
        batch_sampler=batch_sampler,
        places=device,
        num_workers=num_workers,
102
        return_list=True,
103 104
        use_shared_memory=use_shared_memory,
        collate_fn=collate_fn)
D
dyning 已提交
105

littletomatodonkey's avatar
littletomatodonkey 已提交
106 107 108 109
    # support exit using ctrl+c
    signal.signal(signal.SIGINT, term_mp)
    signal.signal(signal.SIGTERM, term_mp)

D
dyning 已提交
110
    return data_loader