utility.py 2.9 KB
Newer Older
H
Hui Zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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.
"""Contains common utility functions."""
import distutils.util
16 17
import math
import os
H
Hui Zhang 已提交
18
import random
19
from typing import List
H
Hui Zhang 已提交
20

H
Hui Zhang 已提交
21 22 23 24 25 26 27 28 29 30
import numpy as np
import paddle

__all__ = ["seed_all", 'print_arguments', 'add_arguments', "log_add"]


def seed_all(seed: int=210329):
    np.random.seed(seed)
    random.seed(seed)
    paddle.seed(seed)
H
Hui Zhang 已提交
31 32


33
def print_arguments(args, info=None):
H
Hui Zhang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
    """Print argparse's arguments.

    Usage:

    .. code-block:: python

        parser = argparse.ArgumentParser()
        parser.add_argument("name", default="Jonh", type=str, help="User name.")
        args = parser.parse_args()
        print_arguments(args)

    :param args: Input argparse.Namespace for printing.
    :type args: argparse.Namespace
    """
48 49 50 51 52
    filename = ""
    if info:
        filename = info["__file__"]
    filename = os.path.basename(filename)
    print(f"----------- {filename} Configuration Arguments -----------")
H
Hui Zhang 已提交
53 54
    for arg, value in sorted(vars(args).items()):
        print("%s: %s" % (arg, value))
55
    print("-----------------------------------------------------------")
H
Hui Zhang 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74


def add_arguments(argname, type, default, help, argparser, **kwargs):
    """Add argparse's argument.

    Usage:

    .. code-block:: python

        parser = argparse.ArgumentParser()
        add_argument("name", str, "Jonh", "User name.", parser)
        args = parser.parse_args()
    """
    type = distutils.util.strtobool if type == bool else type
    argparser.add_argument(
        "--" + argname,
        default=default,
        type=type,
        help=help + ' Default: %(default)s.',
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        **kwargs)


def log_add(args: List[int]) -> float:
    """Stable log add

    Args:
        args (List[int]): log scores

    Returns:
        float: sum of log scores
    """
    if all(a == -float('inf') for a in args):
        return -float('inf')
    a_max = max(args)
    lsp = math.log(sum(math.exp(a - a_max) for a in args))
    return a_max + lsp
H
Hui Zhang 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110


def get_subsample(config):
    """Subsample rate from config.

    Args:
        config (yacs.config.CfgNode): yaml config

    Returns:
        int: subsample rate.
    """
    input_layer = config["model"]["encoder_conf"]["input_layer"]
    assert input_layer in ["conv2d", "conv2d6", "conv2d8"]
    if input_layer == "conv2d":
        return 4
    elif input_layer == "conv2d6":
        return 6
    elif input_layer == "conv2d8":
        return 8