ssm-ocr-benchmark / core /utils.py
MarcoPerson's picture
Add the OCR demo (Mamba-CTC and Mamba-NAR, Antiqua and Fraktur)
39cbe48 verified
Raw History Blame Contribute Delete
7.5 kB
# Copyright Université de Rouen Normandie (1), INSA Rouen (2),
# tutelles du laboratoire LITIS (1 et 2)
# contributors :
# - Denis Coquenet
#
#
# This software is a computer program written in XXX whose purpose is XXX.
#
# This software is governed by the CeCILL-C license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL-C
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-C license and that you accept its terms.
import re
import numpy as np
import torch
from torch.distributions.uniform import Uniform
def randint(low, high):
"""
call torch.randint to preserve random among dataloader workers
"""
return int(torch.randint(low, high, (1, )))
def randint_with_bias(low, high, bias_value, bias_prob=0.4):
"""
Return a random integer in [low, high),
but pick `bias_value` with probability `bias_prob`.
"""
if torch.rand(1).item() < bias_prob:
return bias_value
else:
return int(torch.randint(low, high, (1, )))
def rand():
"""
call torch.rand to preserve random among dataloader workers
"""
return float(torch.rand((1, )))
def rand_uniform(low, high):
"""
call torch uniform to preserve random among dataloader workers
"""
return float(Uniform(low, high).sample())
def pad_sequences_1D(data, padding_value, max_length=None):
"""
Pad data with padding_value to get same length
"""
x_lengths = [len(x) for x in data]
if max_length is not None:
longest_x = max_length
else:
longest_x = max(x_lengths)
padded_data = np.ones((len(data), longest_x)).astype(np.int32) * padding_value
for i, x_len in enumerate(x_lengths):
# Truncate if sequence is longer than max_length
actual_len = min(x_len, longest_x)
padded_data[i, :actual_len] = data[i][:actual_len]
return padded_data
def pad_images(data, padding_value, padding_mode="br"):
"""
data: list of numpy array
mode: "br"/"tl"/"random"/"tlbr" (bottom-right, top-left, random, uniform)
"""
x_lengths = [x.shape[0] for x in data]
y_lengths = [x.shape[1] for x in data]
longest_x = max(x_lengths)
longest_y = max(y_lengths)
padded_data = np.ones((len(data), longest_x, longest_y, data[0].shape[2])) * padding_value
for i, xy_len in enumerate(zip(x_lengths, y_lengths)):
x_len, y_len = xy_len
if padding_mode == "br":
padded_data[i, :x_len, :y_len, ...] = data[i]
elif padding_mode == "tl":
padded_data[i, -x_len:, -y_len:, ...] = data[i]
elif padding_mode == "random":
xmax = longest_x - x_len
ymax = longest_y - y_len
xi = randint(0, xmax) if xmax >= 1 else 0
yi = randint(0, ymax) if ymax >= 1 else 0
padded_data[i, xi:xi+x_len, yi:yi+y_len, ...] = data[i]
elif padding_mode == "tlbr":
# Uniform padding - center the image
x_pad = longest_x - x_len
y_pad = longest_y - y_len
# Calculate padding for each side
x_start = x_pad // 2 # Top padding
y_start = y_pad // 2 # Left padding
# Place image in the center
padded_data[i, x_start:x_start+x_len, y_start:y_start+y_len, ...] = data[i]
else:
raise NotImplementedError("Undefined padding mode: {}".format(padding_mode))
return padded_data
def pad_image(image, padding_value, new_height=None, new_width=None, pad_width=None, pad_height=None, padding_mode="br", return_position=False):
"""
data: list of numpy array
mode: "br"/"tl"/"random" (bottom-right, top-left, random)
"""
if pad_width is not None and new_width is not None:
raise NotImplementedError("pad_with and new_width are not compatible")
if pad_height is not None and new_height is not None:
raise NotImplementedError("pad_height and new_height are not compatible")
h, w, c = image.shape
pad_width = pad_width if pad_width is not None else max(0, new_width - w) if new_width is not None else 0
pad_height = pad_height if pad_height is not None else max(0, new_height - h) if new_height is not None else 0
if not (pad_width == 0 and pad_height == 0):
padded_image = np.ones((h+pad_height, w+pad_width, c)) * padding_value
if padding_mode == "br":
hi, wi = 0, 0
elif padding_mode == "tl":
hi, wi = pad_height, pad_width
elif padding_mode == "random":
hi = randint(0, pad_height) if pad_height >= 1 else 0
wi = randint(0, pad_width) if pad_width >= 1 else 0
else:
raise NotImplementedError("Undefined padding mode: {}".format(padding_mode))
padded_image[hi:hi + h, wi:wi + w, ...] = image
output = padded_image
else:
hi, wi = 0, 0
output = image
if return_position:
return output, [[hi, hi+h], [wi, wi+w]]
return output
def pad_image_width_right(img, new_width, padding_value):
"""
Pad img to right side with padding value to reach new_width as width
"""
h, w, c = img.shape
pad_width = max((new_width - w), 0)
pad_right = np.ones((h, pad_width, c), dtype=img.dtype) * padding_value
img = np.concatenate([img, pad_right], axis=1)
return img
def pad_image_width_random(img, new_width, padding_value, max_pad_left_ratio=1):
"""
Randomly pad img to left and right sides with padding value to reach new_width as width
"""
h, w, c = img.shape
pad_width = max((new_width - w), 0)
max_pad_left = int(max_pad_left_ratio*pad_width)
pad_left = randint(0, min(pad_width, max_pad_left)) if pad_width != 0 and max_pad_left > 0 else 0
pad_right = pad_width - pad_left
pad_left = np.ones((h, pad_left, c), dtype=img.dtype) * padding_value
pad_right = np.ones((h, pad_right, c), dtype=img.dtype) * padding_value
img = np.concatenate([pad_left, img, pad_right], axis=1)
return img
# OCR METRICS
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)