utils.py 3.0 KB
Newer Older
D
dongshuilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2021 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, division, print_function

import datetime
G
gaotingquan 已提交
17
from ppcls.utils import logger, type_name
D
dongshuilong 已提交
18 19 20 21 22 23 24 25 26 27
from ppcls.utils.misc import AverageMeter


def update_metric(trainer, out, batch, batch_size):
    # calc metric
    if trainer.train_metric_func is not None:
        metric_dict = trainer.train_metric_func(out, batch[-1])
        for key in metric_dict:
            if key not in trainer.output_info:
                trainer.output_info[key] = AverageMeter(key, '7.5f')
28 29
            trainer.output_info[key].update(
                float(metric_dict[key]), batch_size)
D
dongshuilong 已提交
30 31 32 33 34 35 36


def update_loss(trainer, loss_dict, batch_size):
    # update_output_info
    for key in loss_dict:
        if key not in trainer.output_info:
            trainer.output_info[key] = AverageMeter(key, '7.5f')
37
        trainer.output_info[key].update(float(loss_dict[key]), batch_size)
D
dongshuilong 已提交
38 39 40


def log_info(trainer, batch_size, epoch_id, iter_id):
41
    lr_msg = ", ".join([
42
        "lr({}): {:.8f}".format(type_name(lr), lr.get_lr())
43 44
        for i, lr in enumerate(trainer.lr_sch)
    ])
D
dongshuilong 已提交
45 46 47 48 49 50 51 52 53
    metric_msg = ", ".join([
        "{}: {:.5f}".format(key, trainer.output_info[key].avg)
        for key in trainer.output_info
    ])
    time_msg = "s, ".join([
        "{}: {:.5f}".format(key, trainer.time_info[key].avg)
        for key in trainer.time_info
    ])

D
dongshuilong 已提交
54
    ips_msg = "ips: {:.5f} samples/s".format(
D
dongshuilong 已提交
55
        batch_size / trainer.time_info["batch_cost"].avg)
D
dongshuilong 已提交
56

57
    eta_sec = ((trainer.config["Global"]["epochs"] - epoch_id + 1
58
                ) * trainer.dataloader_dict["Train"].max_iter - iter_id
59
               ) * trainer.time_info["batch_cost"].avg
D
dongshuilong 已提交
60 61
    eta_msg = "eta: {:s}".format(str(datetime.timedelta(seconds=int(eta_sec))))
    logger.info("[Train][Epoch {}/{}][Iter: {}/{}]{}, {}, {}, {}, {}".format(
62 63
        epoch_id, trainer.config["Global"][
            "epochs"], iter_id, trainer.dataloader_dict["Train"]
64
        .max_iter, lr_msg, metric_msg, time_msg, ips_msg, eta_msg))
D
dongshuilong 已提交
65

66 67
    for i, lr in enumerate(trainer.lr_sch):
        logger.scaler(
68
            name="lr({})".format(type_name(lr)),
69 70 71
            value=lr.get_lr(),
            step=trainer.global_step,
            writer=trainer.vdl_writer)
D
dongshuilong 已提交
72 73 74 75 76 77
    for key in trainer.output_info:
        logger.scaler(
            name="train_{}".format(key),
            value=trainer.output_info[key].avg,
            step=trainer.global_step,
            writer=trainer.vdl_writer)