import re


def rename_pretrained(name: str):
    """ Matches the name of a variable saved in the pre-trained MobileNet
    networks with the name of the corresponding variable in this network.

    https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.md

    Parameters
    ----------
    name: the original name of the variable.

    Returns
    -------
    name: the new name of the variable.
    """

    replace_list = {
        r"BatchNorm": r"batch_normalization",
        r"Conv2d_(\d+)_(\w+)/": r"Conv2d_\1/\2/",
        r"Conv2d_1c_1x1/biases": r"conv2d_final/bias",
        r"Conv2d_1c_1x1/weights": r"conv2d_final/kernel",
        r"depthwise_weights": r"weights"
    }

    for match, replace in replace_list.items():
        name = re.sub(match, replace, name)

    return name