utility.py 3.2 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
H
Hui Zhang 已提交
19
from contextlib import contextmanager
20
from typing import List
H
Hui Zhang 已提交
21

H
Hui Zhang 已提交
22 23 24
import numpy as np
import paddle

H
Hui Zhang 已提交
25 26 27 28 29 30 31 32 33 34 35
__all__ = [
    "UpdateConfig", "seed_all", 'print_arguments', 'add_arguments', "log_add"
]


@contextmanager
def UpdateConfig(config):
    """Update yacs config"""
    config.defrost()
    yield
    config.freeze()
H
Hui Zhang 已提交
36 37 38


def seed_all(seed: int=210329):
H
Hui Zhang 已提交
39
    """freeze random generator seed."""
H
Hui Zhang 已提交
40 41 42
    np.random.seed(seed)
    random.seed(seed)
    paddle.seed(seed)
H
Hui Zhang 已提交
43 44


45
def print_arguments(args, info=None):
H
Hui Zhang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    """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
    """
60 61 62 63 64
    filename = ""
    if info:
        filename = info["__file__"]
    filename = os.path.basename(filename)
    print(f"----------- {filename} Configuration Arguments -----------")
H
Hui Zhang 已提交
65 66
    for arg, value in sorted(vars(args).items()):
        print("%s: %s" % (arg, value))
67
    print("-----------------------------------------------------------")
H
Hui Zhang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86


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.',
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        **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 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122


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