Spaces:
Sleeping
Sleeping
File size: 5,094 Bytes
9bf4bd7 |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Dict
import torch
from mmocr.registry import MODELS
from mmocr.utils.typing_utils import (ConfigType, InitConfigType,
OptConfigType, OptRecSampleList,
RecForwardResults, RecSampleList)
from .base import BaseRecognizer
@MODELS.register_module()
class EncoderDecoderRecognizer(BaseRecognizer):
"""Base class for encode-decode recognizer.
Args:
preprocessor (dict, optional): Config dict for preprocessor. Defaults
to None.
backbone (dict, optional): Backbone config. Defaults to None.
encoder (dict, optional): Encoder config. If None, the output from
backbone will be directly fed into ``decoder``. Defaults to None.
decoder (dict, optional): Decoder config. Defaults to None.
data_preprocessor (dict, optional): Model preprocessing config
for processing the input image data. Keys allowed are
``to_rgb``(bool), ``pad_size_divisor``(int), ``pad_value``(int or
float), ``mean``(int or float) and ``std``(int or float).
Preprcessing order: 1. to rgb; 2. normalization 3. pad.
Defaults to None.
init_cfg (dict or list[dict], optional): Initialization configs.
Defaults to None.
"""
def __init__(self,
preprocessor: OptConfigType = None,
backbone: OptConfigType = None,
encoder: OptConfigType = None,
decoder: OptConfigType = None,
data_preprocessor: ConfigType = None,
init_cfg: InitConfigType = None) -> None:
super().__init__(
init_cfg=init_cfg, data_preprocessor=data_preprocessor)
# Preprocessor module, e.g., TPS
if preprocessor is not None:
self.preprocessor = MODELS.build(preprocessor)
# Backbone
if backbone is not None:
self.backbone = MODELS.build(backbone)
# Encoder module
if encoder is not None:
self.encoder = MODELS.build(encoder)
# Decoder module
assert decoder is not None
self.decoder = MODELS.build(decoder)
def extract_feat(self, inputs: torch.Tensor) -> torch.Tensor:
"""Directly extract features from the backbone."""
if self.with_preprocessor:
inputs = self.preprocessor(inputs)
if self.with_backbone:
inputs = self.backbone(inputs)
return inputs
def loss(self, inputs: torch.Tensor, data_samples: RecSampleList,
**kwargs) -> Dict:
"""Calculate losses from a batch of inputs and data samples.
Args:
inputs (tensor): Input images of shape (N, C, H, W).
Typically these should be mean centered and std scaled.
data_samples (list[TextRecogDataSample]): A list of N
datasamples, containing meta information and gold
annotations for each of the images.
Returns:
dict[str, tensor]: A dictionary of loss components.
"""
feat = self.extract_feat(inputs)
out_enc = None
if self.with_encoder:
out_enc = self.encoder(feat, data_samples)
return self.decoder.loss(feat, out_enc, data_samples)
def predict(self, inputs: torch.Tensor, data_samples: RecSampleList,
**kwargs) -> RecSampleList:
"""Predict results from a batch of inputs and data samples with post-
processing.
Args:
inputs (torch.Tensor): Image input tensor.
data_samples (list[TextRecogDataSample]): A list of N datasamples,
containing meta information and gold annotations for each of
the images.
Returns:
list[TextRecogDataSample]: A list of N datasamples of prediction
results. Results are stored in ``pred_text``.
"""
feat = self.extract_feat(inputs)
out_enc = None
if self.with_encoder:
out_enc = self.encoder(feat, data_samples)
return self.decoder.predict(feat, out_enc, data_samples)
def _forward(self,
inputs: torch.Tensor,
data_samples: OptRecSampleList = None,
**kwargs) -> RecForwardResults:
"""Network forward process. Usually includes backbone, encoder and
decoder forward without any post-processing.
Args:
inputs (Tensor): Inputs with shape (N, C, H, W).
data_samples (list[TextRecogDataSample]): A list of N
datasamples, containing meta information and gold
annotations for each of the images.
Returns:
Tensor: A tuple of features from ``decoder`` forward.
"""
feat = self.extract_feat(inputs)
out_enc = None
if self.with_encoder:
out_enc = self.encoder(feat, data_samples)
return self.decoder(feat, out_enc, data_samples)
|