Spaces:
Sleeping
Sleeping
File size: 4,455 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 |
# Copyright (c) OpenMMLab. All rights reserved.
from abc import ABCMeta, abstractmethod
from typing import Union
import torch
from mmengine.model.base_model import BaseModel
from mmocr.utils import (OptConfigType, OptMultiConfig, OptRecSampleList,
RecForwardResults, RecSampleList)
class BaseRecognizer(BaseModel, metaclass=ABCMeta):
"""Base class for recognizer.
Args:
data_preprocessor (dict or ConfigDict, optional): The pre-process
config of :class:`BaseDataPreprocessor`. it usually includes,
``pad_size_divisor``, ``pad_value``, ``mean`` and ``std``.
init_cfg (dict or ConfigDict or List[dict], optional): the config
to control the initialization. Defaults to None.
"""
def __init__(self,
data_preprocessor: OptConfigType = None,
init_cfg: OptMultiConfig = None):
super().__init__(
data_preprocessor=data_preprocessor, init_cfg=init_cfg)
@property
def with_backbone(self):
"""bool: whether the recognizer has a backbone"""
return hasattr(self, 'backbone')
@property
def with_encoder(self):
"""bool: whether the recognizer has an encoder"""
return hasattr(self, 'encoder')
@property
def with_preprocessor(self):
"""bool: whether the recognizer has a preprocessor"""
return hasattr(self, 'preprocessor')
@property
def with_decoder(self):
"""bool: whether the recognizer has a decoder"""
return hasattr(self, 'decoder')
@abstractmethod
def extract_feat(self, inputs: torch.Tensor) -> torch.Tensor:
"""Extract features from images."""
pass
def forward(self,
inputs: torch.Tensor,
data_samples: OptRecSampleList = None,
mode: str = 'tensor',
**kwargs) -> RecForwardResults:
"""The unified entry for a forward process in both training and test.
The method should accept three modes: "tensor", "predict" and "loss":
- "tensor": Forward the whole network and return tensor or tuple of
tensor without any post-processing, same as a common nn.Module.
- "predict": Forward and return the predictions, which are fully
processed to a list of :obj:`DetDataSample`.
- "loss": Forward and return a dict of losses according to the given
inputs and data samples.
Note that this method doesn't handle neither back propagation nor
optimizer updating, which are done in the :meth:`train_step`.
Args:
inputs (torch.Tensor): The input tensor with shape
(N, C, ...) in general.
data_samples (list[:obj:`DetDataSample`], optional): The
annotation data of every samples. Defaults to None.
mode (str): Return what kind of value. Defaults to 'tensor'.
Returns:
The return type depends on ``mode``.
- If ``mode="tensor"``, return a tensor or a tuple of tensor.
- If ``mode="predict"``, return a list of :obj:`DetDataSample`.
- If ``mode="loss"``, return a dict of tensor.
"""
if mode == 'loss':
return self.loss(inputs, data_samples, **kwargs)
elif mode == 'predict':
return self.predict(inputs, data_samples, **kwargs)
elif mode == 'tensor':
return self._forward(inputs, data_samples, **kwargs)
else:
raise RuntimeError(f'Invalid mode "{mode}". '
'Only supports loss, predict and tensor mode')
@abstractmethod
def loss(self, inputs: torch.Tensor, data_samples: RecSampleList,
**kwargs) -> Union[dict, tuple]:
"""Calculate losses from a batch of inputs and data samples."""
pass
@abstractmethod
def predict(self, inputs: torch.Tensor, data_samples: RecSampleList,
**kwargs) -> RecSampleList:
"""Predict results from a batch of inputs and data samples with post-
processing."""
pass
@abstractmethod
def _forward(self,
inputs: torch.Tensor,
data_samples: OptRecSampleList = None,
**kwargs):
"""Network forward process.
Usually includes backbone, neck and head forward without any post-
processing.
"""
pass
|