diff options
Diffstat (limited to 'text_recognizer/models/util.py')
-rw-r--r-- | text_recognizer/models/util.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/text_recognizer/models/util.py b/text_recognizer/models/util.py new file mode 100644 index 0000000..cc0df3c --- /dev/null +++ b/text_recognizer/models/util.py @@ -0,0 +1,10 @@ +from typing import Union + +from torch import Tensor + + +def first_element(x: Tensor, element: Union[int, float], dim: int = 1) -> Tensor: + nonz = x == element + ind = ((nonz.cumsum(dim) == 1) & nonz).max(dim).indices + ind[ind == 0] = x.shape[dim] + return ind |