|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +from typing import Dict, List, Optional, Sequence, Union |
| 3 | + |
| 4 | +import torch |
| 5 | +import torch.nn as nn |
| 6 | + |
| 7 | +from mmocr.models.common.dictionary import Dictionary |
| 8 | +from mmocr.registry import MODELS |
| 9 | +from mmocr.structures import TextRecogDataSample |
| 10 | +from .base import BaseDecoder |
| 11 | + |
| 12 | + |
| 13 | +@MODELS.register_module() |
| 14 | +class SVTRDecoder(BaseDecoder): |
| 15 | + """Decoder module in `SVTR <https://arxiv.org/abs/2205.00159>`_. |
| 16 | +
|
| 17 | + Args: |
| 18 | + in_channels (int): The num of input channels. |
| 19 | + dictionary (Union[Dict, Dictionary]): The config for `Dictionary` or |
| 20 | + the instance of `Dictionary`. Defaults to None. |
| 21 | + module_loss (Optional[Dict], optional): Cfg to build module_loss. |
| 22 | + Defaults to None. |
| 23 | + postprocessor (Optional[Dict], optional): Cfg to build postprocessor. |
| 24 | + Defaults to None. |
| 25 | + max_seq_len (int, optional): Maximum output sequence length :math:`T`. |
| 26 | + Defaults to 25. |
| 27 | + init_cfg (dict or list[dict], optional): Initialization configs. |
| 28 | + Defaults to None. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, |
| 32 | + in_channels: int, |
| 33 | + dictionary: Union[Dict, Dictionary] = None, |
| 34 | + module_loss: Optional[Dict] = None, |
| 35 | + postprocessor: Optional[Dict] = None, |
| 36 | + max_seq_len: int = 25, |
| 37 | + init_cfg: Optional[Union[Dict, List[Dict]]] = None) -> None: |
| 38 | + |
| 39 | + super().__init__( |
| 40 | + dictionary=dictionary, |
| 41 | + module_loss=module_loss, |
| 42 | + postprocessor=postprocessor, |
| 43 | + max_seq_len=max_seq_len, |
| 44 | + init_cfg=init_cfg) |
| 45 | + |
| 46 | + self.decoder = nn.Linear( |
| 47 | + in_features=in_channels, out_features=self.dictionary.num_classes) |
| 48 | + self.softmax = nn.Softmax(dim=-1) |
| 49 | + |
| 50 | + def forward_train( |
| 51 | + self, |
| 52 | + feat: Optional[torch.Tensor] = None, |
| 53 | + out_enc: Optional[torch.Tensor] = None, |
| 54 | + data_samples: Optional[Sequence[TextRecogDataSample]] = None |
| 55 | + ) -> torch.Tensor: |
| 56 | + """Forward for training. |
| 57 | +
|
| 58 | + Args: |
| 59 | + feat (torch.Tensor, optional): The feature map from backbone of |
| 60 | + shape :math:`(N, E, H, W)`. Defaults to None. |
| 61 | + out_enc (torch.Tensor, optional): Encoder output. Defaults to None. |
| 62 | + data_samples (Sequence[TextRecogDataSample]): Batch of |
| 63 | + TextRecogDataSample, containing gt_text information. Defaults |
| 64 | + to None. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + Tensor: The raw logit tensor. Shape :math:`(N, T, C)` where |
| 68 | + :math:`C` is ``num_classes``. |
| 69 | + """ |
| 70 | + assert feat.size(2) == 1, 'feature height must be 1' |
| 71 | + x = feat.squeeze(2) |
| 72 | + x = x.permute(0, 2, 1) |
| 73 | + predicts = self.decoder(x) |
| 74 | + return predicts |
| 75 | + |
| 76 | + def forward_test( |
| 77 | + self, |
| 78 | + feat: Optional[torch.Tensor] = None, |
| 79 | + out_enc: Optional[torch.Tensor] = None, |
| 80 | + data_samples: Optional[Sequence[TextRecogDataSample]] = None |
| 81 | + ) -> torch.Tensor: |
| 82 | + """Forward for testing. |
| 83 | +
|
| 84 | + Args: |
| 85 | + feat (torch.Tensor, optional): The feature map from backbone of |
| 86 | + shape :math:`(N, E, H, W)`. Defaults to None. |
| 87 | + out_enc (torch.Tensor, optional): Encoder output. Defaults to None. |
| 88 | + data_samples (Sequence[TextRecogDataSample]): Batch of |
| 89 | + TextRecogDataSample, containing gt_text information. Defaults |
| 90 | + to None. |
| 91 | + Returns: |
| 92 | + Tensor: Character probabilities. of shape |
| 93 | + :math:`(N, self.max_seq_len, C)` where :math:`C` is |
| 94 | + ``num_classes``. |
| 95 | + """ |
| 96 | + return self.softmax(self.forward_train(feat, out_enc, data_samples)) |
0 commit comments