"vscode:/vscode.git/clone" did not exist on "268ed03b3ec01af8ff2de9f9329659022c13bc5e"
__init__.py 3.31 KB
Newer Older
LDOUBLEV's avatar
LDOUBLEV committed
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.
WenmuZhou's avatar
WenmuZhou committed
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 committed
23
import skimage
WenmuZhou's avatar
WenmuZhou committed
24
import paddle
dyning's avatar
dyning committed
25
26
import signal
import random
WenmuZhou's avatar
WenmuZhou committed
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
dyning's avatar
dyning committed
32
from paddle.io import Dataset, DataLoader, BatchSampler, DistributedBatchSampler
WenmuZhou's avatar
WenmuZhou committed
33
34
35
import paddle.distributed as dist

from ppocr.data.imaug import transform, create_operators
dyning's avatar
dyning committed
36
from ppocr.data.simple_dataset import SimpleDataSet
tink2123's avatar
tink2123 committed
37
from ppocr.data.lmdb_dataset import LMDBDataSet
38
from ppocr.data.pgnet_dataset import PGDataSet
MissPenguin's avatar
MissPenguin committed
39
from ppocr.data.pubtab_dataset import PubTabDataSet
WenmuZhou's avatar
WenmuZhou committed
40
41
42

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

dyning's avatar
dyning committed
43

dyning's avatar
dyning committed
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)
WenmuZhou's avatar
WenmuZhou committed
51

dyning's avatar
dyning committed
52

53
def build_dataloader(config, mode, device, logger, seed=None):
dyning's avatar
dyning committed
54
    config = copy.deepcopy(config)
dyning's avatar
dyning committed
55

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

65
    dataset = eval(module_name)(config, mode, logger, seed)
dyning's avatar
dyning committed
66
67
68
    loader_config = config[mode]['loader']
    batch_size = loader_config['batch_size_per_card']
    drop_last = loader_config['drop_last']
LDOUBLEV's avatar
LDOUBLEV committed
69
    shuffle = loader_config['shuffle']
dyning's avatar
dyning committed
70
    num_workers = loader_config['num_workers']
71
72
73
74
    if 'use_shared_memory' in loader_config.keys():
        use_shared_memory = loader_config['use_shared_memory']
    else:
        use_shared_memory = True
dyning's avatar
dyning committed
75
    if mode == "Train":
Jethong's avatar
Jethong committed
76
        # Distribute data to multiple cards
dyning's avatar
dyning committed
77
78
79
        batch_sampler = DistributedBatchSampler(
            dataset=dataset,
            batch_size=batch_size,
LDOUBLEV's avatar
LDOUBLEV committed
80
            shuffle=shuffle,
dyning's avatar
dyning committed
81
            drop_last=drop_last)
WenmuZhou's avatar
WenmuZhou committed
82
    else:
Jethong's avatar
Jethong committed
83
        # Distribute data to single card
dyning's avatar
dyning committed
84
85
86
        batch_sampler = BatchSampler(
            dataset=dataset,
            batch_size=batch_size,
LDOUBLEV's avatar
LDOUBLEV committed
87
            shuffle=shuffle,
dyning's avatar
dyning committed
88
89
            drop_last=drop_last)

dyning's avatar
dyning committed
90
91
92
93
94
    data_loader = DataLoader(
        dataset=dataset,
        batch_sampler=batch_sampler,
        places=device,
        num_workers=num_workers,
95
96
        return_list=True,
        use_shared_memory=use_shared_memory)
dyning's avatar
dyning committed
97

littletomatodonkey's avatar
littletomatodonkey committed
98
99
100
101
    # support exit using ctrl+c
    signal.signal(signal.SIGINT, term_mp)
    signal.signal(signal.SIGTERM, term_mp)

dyning's avatar
dyning committed
102
    return data_loader