train.py 7.41 KB
Newer Older
LDOUBLEV's avatar
LDOUBLEV committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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
WenmuZhou's avatar
WenmuZhou committed
21

22
__dir__ = os.path.dirname(os.path.abspath(__file__))
LDOUBLEV's avatar
LDOUBLEV committed
23
sys.path.append(__dir__)
littletomatodonkey's avatar
littletomatodonkey committed
24
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, '..')))
LDOUBLEV's avatar
LDOUBLEV committed
25

WenmuZhou's avatar
WenmuZhou committed
26
27
28
import yaml
import paddle
import paddle.distributed as dist
LDOUBLEV's avatar
LDOUBLEV committed
29

WenmuZhou's avatar
WenmuZhou committed
30
from ppocr.data import build_dataloader
dyning's avatar
dyning committed
31
32
from ppocr.modeling.architectures import build_model
from ppocr.losses import build_loss
WenmuZhou's avatar
WenmuZhou committed
33
34
35
from ppocr.optimizer import build_optimizer
from ppocr.postprocess import build_post_process
from ppocr.metrics import build_metric
36
from ppocr.utils.save_load import load_model
WenmuZhou's avatar
WenmuZhou committed
37
from ppocr.utils.utility import set_seed
38
from ppocr.modeling.architectures import apply_to_static
WenmuZhou's avatar
WenmuZhou committed
39
import tools.program as program
LDOUBLEV's avatar
LDOUBLEV committed
40

WenmuZhou's avatar
WenmuZhou committed
41
dist.get_world_size()
LDOUBLEV's avatar
LDOUBLEV committed
42
43


WenmuZhou's avatar
WenmuZhou committed
44
45
46
47
def main(config, device, logger, vdl_writer):
    # init dist environment
    if config['Global']['distributed']:
        dist.init_parallel_env()
LDOUBLEV's avatar
LDOUBLEV committed
48

WenmuZhou's avatar
WenmuZhou committed
49
    global_config = config['Global']
dyning's avatar
dyning committed
50

WenmuZhou's avatar
WenmuZhou committed
51
    # build dataloader
dyning's avatar
dyning committed
52
    train_dataloader = build_dataloader(config, 'Train', device, logger)
WenmuZhou's avatar
WenmuZhou committed
53
54
    if len(train_dataloader) == 0:
        logger.error(
55
56
57
58
            "No Images in train dataset, please ensure\n" +
            "\t1. The images num in the train label_file_list should be larger than or equal with batch size.\n"
            +
            "\t2. The annotation file and path in the configuration file are provided normally."
WenmuZhou's avatar
WenmuZhou committed
59
        )
WenmuZhou's avatar
WenmuZhou committed
60
        return
WenmuZhou's avatar
WenmuZhou committed
61

dyning's avatar
dyning committed
62
    if config['Eval']:
dyning's avatar
dyning committed
63
        valid_dataloader = build_dataloader(config, 'Eval', device, logger)
WenmuZhou's avatar
WenmuZhou committed
64
    else:
dyning's avatar
dyning committed
65
66
        valid_dataloader = None

WenmuZhou's avatar
WenmuZhou committed
67
    # build post process
dyning's avatar
dyning committed
68
69
70
    post_process_class = build_post_process(config['PostProcess'],
                                            global_config)

WenmuZhou's avatar
WenmuZhou committed
71
    # build model
WenmuZhou's avatar
WenmuZhou committed
72
    # for rec algorithm
WenmuZhou's avatar
WenmuZhou committed
73
    if hasattr(post_process_class, 'character'):
dyning's avatar
dyning committed
74
        char_num = len(getattr(post_process_class, 'character'))
littletomatodonkey's avatar
littletomatodonkey committed
75
76
77
        if config['Architecture']["algorithm"] in ["Distillation",
                                                   ]:  # distillation model
            for key in config['Architecture']["Models"]:
andyjpaddle's avatar
andyjpaddle committed
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
                if config['Architecture']['Models'][key]['Head'][
                        'name'] == 'MultiHead':  # for multi head
                    if config['PostProcess'][
                            'name'] == 'DistillationSARLabelDecode':
                        char_num = char_num - 2
                    # update SARLoss params
                    assert list(config['Loss']['loss_config_list'][-1].keys())[
                        0] == 'DistillationSARLoss'
                    config['Loss']['loss_config_list'][-1][
                        'DistillationSARLoss']['ignore_index'] = char_num + 1
                    out_channels_list = {}
                    out_channels_list['CTCLabelDecode'] = char_num
                    out_channels_list['SARLabelDecode'] = char_num + 2
                    config['Architecture']['Models'][key]['Head'][
                        'out_channels_list'] = out_channels_list
                else:
                    config['Architecture']["Models"][key]["Head"][
                        'out_channels'] = char_num
        elif config['Architecture']['Head'][
                'name'] == 'MultiHead':  # for multi head
            if config['PostProcess']['name'] == 'SARLabelDecode':
                char_num = char_num - 2
            # update SARLoss params
            assert list(config['Loss']['loss_config_list'][1].keys())[
                0] == 'SARLoss'
            if config['Loss']['loss_config_list'][1]['SARLoss'] is None:
                config['Loss']['loss_config_list'][1]['SARLoss'] = {
                    'ignore_index': char_num + 1
                }
            else:
                config['Loss']['loss_config_list'][1]['SARLoss'][
                    'ignore_index'] = char_num + 1
            out_channels_list = {}
            out_channels_list['CTCLabelDecode'] = char_num
            out_channels_list['SARLabelDecode'] = char_num + 2
            config['Architecture']['Head'][
                'out_channels_list'] = out_channels_list
littletomatodonkey's avatar
littletomatodonkey committed
115
116
117
        else:  # base rec model
            config['Architecture']["Head"]['out_channels'] = char_num

andyjpaddle's avatar
andyjpaddle committed
118
119
120
        if config['PostProcess']['name'] == 'SARLabelDecode':  # for SAR model
            config['Loss']['ignore_index'] = char_num - 1

WenmuZhou's avatar
WenmuZhou committed
121
122
123
124
    model = build_model(config['Architecture'])
    if config['Global']['distributed']:
        model = paddle.DataParallel(model)

125
126
    model = apply_to_static(model, config, logger)

dyning's avatar
dyning committed
127
128
    # build loss
    loss_class = build_loss(config['Loss'])
dyning's avatar
dyning committed
129

WenmuZhou's avatar
WenmuZhou committed
130
    # build optim
dyning's avatar
dyning committed
131
132
    optimizer, lr_scheduler = build_optimizer(
        config['Optimizer'],
WenmuZhou's avatar
WenmuZhou committed
133
        epochs=config['Global']['epoch_num'],
dyning's avatar
dyning committed
134
        step_each_epoch=len(train_dataloader),
Topdu's avatar
Topdu committed
135
        model=model)
WenmuZhou's avatar
WenmuZhou committed
136
137
138

    # build metric
    eval_class = build_metric(config['Metric'])
dyning's avatar
dyning committed
139
    # load pretrain model
140
141
    pre_best_model_dict = load_model(config, model, optimizer,
                                     config['Architecture']["model_type"])
142
143
144
145
    logger.info('train dataloader has {} iters'.format(len(train_dataloader)))
    if valid_dataloader is not None:
        logger.info('valid dataloader has {} iters'.format(
            len(valid_dataloader)))
stephon's avatar
stephon committed
146

147
    use_amp = config["Global"].get("use_amp", False)
stephon's avatar
stephon committed
148
149
150
151
152
153
    if use_amp:
        AMP_RELATED_FLAGS_SETTING = {
            'FLAGS_cudnn_batchnorm_spatial_persistent': 1,
            'FLAGS_max_inplace_grad_add': 8,
        }
        paddle.fluid.set_flags(AMP_RELATED_FLAGS_SETTING)
154
155
156
        scale_loss = config["Global"].get("scale_loss", 1.0)
        use_dynamic_loss_scaling = config["Global"].get(
            "use_dynamic_loss_scaling", False)
stephon's avatar
stephon committed
157
158
159
160
161
162
        scaler = paddle.amp.GradScaler(
            init_loss_scaling=scale_loss,
            use_dynamic_loss_scaling=use_dynamic_loss_scaling)
    else:
        scaler = None

WenmuZhou's avatar
WenmuZhou committed
163
    # start train
dyning's avatar
dyning committed
164
165
    program.train(config, train_dataloader, valid_dataloader, device, model,
                  loss_class, optimizer, lr_scheduler, post_process_class,
stephon's avatar
stephon committed
166
                  eval_class, pre_best_model_dict, logger, vdl_writer, scaler)
dyning's avatar
dyning committed
167
168
169


def test_reader(config, device, logger):
WenmuZhou's avatar
WenmuZhou committed
170
    loader = build_dataloader(config, 'Train', device, logger)
171
172
173
174
    import time
    starttime = time.time()
    count = 0
    try:
dyning's avatar
dyning committed
175
        for data in loader():
176
177
178
179
            count += 1
            if count % 1 == 0:
                batch_time = time.time() - starttime
                starttime = time.time()
WenmuZhou's avatar
WenmuZhou committed
180
181
                logger.info("reader: {}, {}, {}".format(
                    count, len(data[0]), batch_time))
182
    except Exception as e:
LDOUBLEV's avatar
LDOUBLEV committed
183
184
        logger.info(e)
    logger.info("finish reader: {}, Success!".format(count))
185

dyning's avatar
dyning committed
186

LDOUBLEV's avatar
LDOUBLEV committed
187
if __name__ == '__main__':
188
    config, device, logger, vdl_writer = program.preprocess(is_train=True)
WenmuZhou's avatar
WenmuZhou committed
189
190
    seed = config['Global']['seed'] if 'seed' in config['Global'] else 1024
    set_seed(seed)
dyning's avatar
dyning committed
191
    main(config, device, logger, vdl_writer)
WenmuZhou's avatar
WenmuZhou committed
192
    # test_reader(config, device, logger)