summaryrefslogtreecommitdiff
path: root/text_recognizer/networks/util.py
blob: 9c6b1514875620dd75abc798cfa7755ee711d89d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Miscellaneous neural network functionality."""
import importlib
from pathlib import Path
from typing import Dict, NamedTuple, Union, Type

from loguru import logger
import torch
from torch import nn


def activation_function(activation: str) -> Type[nn.Module]:
    """Returns the callable activation function."""
    activation_fns = nn.ModuleDict(
        [
            ["elu", nn.ELU(inplace=True)],
            ["gelu", nn.GELU()],
            ["glu", nn.GLU()],
            ["leaky_relu", nn.LeakyReLU(negative_slope=1.0e-2, inplace=True)],
            ["none", nn.Identity()],
            ["relu", nn.ReLU(inplace=True)],
            ["selu", nn.SELU(inplace=True)],
        ]
    )
    return activation_fns[activation.lower()]


# def configure_backbone(backbone: Union[OmegaConf, NamedTuple]) -> Type[nn.Module]:
#     """Loads a backbone network."""
#     network_module = importlib.import_module("text_recognizer.networks")
#     backbone_class = getattr(network_module, backbone.type)
#
#     if "pretrained" in backbone.args:
#         logger.info("Loading pretrained backbone.")
#         checkpoint_file = Path(__file__).resolve().parents[2] / backbone.args.pop(
#             "pretrained"
#         )
#
#         # Loading state directory.
#         state_dict = torch.load(checkpoint_file)
#         network_args = state_dict["network_args"]
#         weights = state_dict["model_state"]
#
#         freeze = False
#         if "freeze" in backbone.args and backbone.args["freeze"] is True:
#             backbone.args.pop("freeze")
#             freeze = True
#
#         # Initializes the network with trained weights.
#         backbone_ = backbone_(**backbone.args)
#         backbone_.load_state_dict(weights)
#         if freeze:
#             for params in backbone_.parameters():
#                 params.requires_grad = False
#     else:
#         backbone_ = getattr(network_module, backbone.type)
#         backbone_ = backbone_(**backbone.args)
#
#     if "remove_layers" in backbone_args and backbone_args["remove_layers"] is not None:
#         backbone = nn.Sequential(
#             *list(backbone.children())[:][: -backbone_args["remove_layers"]]
#         )
#
#     return backbone