From 5a53efd3570238f20be3fd644ad4ddce9606b903 Mon Sep 17 00:00:00 2001 From: Vladislav Ershov <75930878+ErshovVE@users.noreply.github.com> Date: Mon, 5 Oct 2026 13:03:09 +0400 Subject: [PATCH 1/3] perf(aug): vectorise WarpMLS (TIA distort/stretch/perspective) calc_delta looped over grid nodes and control points with scalar numpy ops, and gen_img looped over grid cells: in RecAug + RecConAug the TIA warp was ~73 % of the data-loading time (11 samples/s per CPU core on PP-OCRv6 small RU/EN data). Now the node displacements are solved for all nodes at once, interpolated to pixels separably and sampled with cv2.remap: 6-11x faster per call (48x150..48x1200), the whole training data pipeline 3x faster. The output matches the loop version (kept in the test as reference): node displacements within 0.05 px (the loop accumulated in float32), images within a level or two on smooth images and up to ~5 levels on sharp text edges (remap quantises sub-pixel positions to 1/32 px and rounds instead of truncating). --- ppocr/data/imaug/text_image_aug/warp_mls.py | 236 ++++++++------------ tests/ppocr/test_warp_mls.py | 197 ++++++++++++++++ 2 files changed, 290 insertions(+), 143 deletions(-) create mode 100644 tests/ppocr/test_warp_mls.py diff --git a/ppocr/data/imaug/text_image_aug/warp_mls.py b/ppocr/data/imaug/text_image_aug/warp_mls.py index 2d349a628fb..5a00209c27a 100644 --- a/ppocr/data/imaug/text_image_aug/warp_mls.py +++ b/ppocr/data/imaug/text_image_aug/warp_mls.py @@ -16,10 +16,42 @@ https://github.com/RubanSeven/Text-Image-Augmentation-python/blob/master/warp_mls.py """ +import cv2 import numpy as np +def _grid_nodes(size, step): + """Grid coordinates visited by the original loop: 0, step, 2*step, ... and the last pixel.""" + nodes, t = [], 0 + while True: + if size <= t < size + step - 1: + t = size - 1 + elif t >= size: + break + nodes.append(t) + t += step + return np.array(nodes) + + +def _cells(size, step): + """Per pixel: start and end node of its grid cell and the position inside it (as in gen_img).""" + t = np.arange(size) + start = (t // step) * step + end = start + step + last = end >= size + length = np.where(last, size - start, step) + end = np.where(last, size - 1, end) + return start, end, (t - start) / length + + class WarpMLS: + """Moving-least-squares warp of text images (TIA distort / stretch / perspective). + + Vectorised over the grid nodes and the pixels (the original looped over both in + Python and took most of the data-loading time); results match the loop version + within a level or two (tests/ppocr/test_warp_mls.py). + """ + def __init__(self, src, src_pts, dst_pts, dst_w, dst_h, trans_ratio=1.0): self.src = src self.src_pts = src_pts @@ -32,156 +64,74 @@ def __init__(self, src, src_pts, dst_pts, dst_w, dst_h, trans_ratio=1.0): self.rdx = np.zeros((self.dst_h, self.dst_w)) self.rdy = np.zeros((self.dst_h, self.dst_w)) - @staticmethod - def __bilinear_interp(x, y, v11, v12, v21, v22): - return (v11 * (1 - y) + v12 * y) * (1 - x) + (v21 * (1 - y) + v22 * y) * x - def generate(self): self.calc_delta() return self.gen_img() def calc_delta(self): - w = np.zeros(self.pt_count, dtype=np.float32) - + """Displacement (rdx, rdy) at the grid nodes by affine-free similarity MLS.""" if self.pt_count < 2: return - - i = 0 - while 1: - if self.dst_w <= i < self.dst_w + self.grid_size - 1: - i = self.dst_w - 1 - elif i >= self.dst_w: - break - - j = 0 - while 1: - if self.dst_h <= j < self.dst_h + self.grid_size - 1: - j = self.dst_h - 1 - elif j >= self.dst_h: - break - - sw = 0 - swp = np.zeros(2, dtype=np.float32) - swq = np.zeros(2, dtype=np.float32) - new_pt = np.zeros(2, dtype=np.float32) - cur_pt = np.array([i, j], dtype=np.float32) - - k = 0 - for k in range(self.pt_count): - if i == self.dst_pts[k][0] and j == self.dst_pts[k][1]: - break - - w[k] = 1.0 / ( - (i - self.dst_pts[k][0]) * (i - self.dst_pts[k][0]) - + (j - self.dst_pts[k][1]) * (j - self.dst_pts[k][1]) - ) - - sw += w[k] - swp = swp + w[k] * np.array(self.dst_pts[k]) - swq = swq + w[k] * np.array(self.src_pts[k]) - - if k == self.pt_count - 1: - pstar = 1 / sw * swp - qstar = 1 / sw * swq - - miu_s = 0 - for k in range(self.pt_count): - if i == self.dst_pts[k][0] and j == self.dst_pts[k][1]: - continue - pt_i = self.dst_pts[k] - pstar - miu_s += w[k] * np.sum(pt_i * pt_i) - - cur_pt -= pstar - cur_pt_j = np.array([-cur_pt[1], cur_pt[0]]) - - for k in range(self.pt_count): - if i == self.dst_pts[k][0] and j == self.dst_pts[k][1]: - continue - - pt_i = self.dst_pts[k] - pstar - pt_j = np.array([-pt_i[1], pt_i[0]]) - - tmp_pt = np.zeros(2, dtype=np.float32) - tmp_pt[0] = ( - np.sum(pt_i * cur_pt) * self.src_pts[k][0] - - np.sum(pt_j * cur_pt) * self.src_pts[k][1] - ) - tmp_pt[1] = ( - -np.sum(pt_i * cur_pt_j) * self.src_pts[k][0] - + np.sum(pt_j * cur_pt_j) * self.src_pts[k][1] - ) - tmp_pt *= w[k] / miu_s - new_pt += tmp_pt - - new_pt += qstar - else: - new_pt = self.src_pts[k] - - self.rdx[j, i] = new_pt[0] - i - self.rdy[j, i] = new_pt[1] - j - - j += self.grid_size - i += self.grid_size + p = np.asarray(self.dst_pts, dtype=np.float64) + q = np.asarray(self.src_pts, dtype=np.float64) + ys, xs = np.meshgrid( + _grid_nodes(self.dst_h, self.grid_size), + _grid_nodes(self.dst_w, self.grid_size), + indexing="ij", + ) + node = np.stack([xs.ravel(), ys.ravel()], axis=1).astype(np.float64) + + d2 = ((node[:, None, :] - p[None, :, :]) ** 2).sum(axis=2) + same = d2 == 0 + # the original loop stops at the first control point equal to the node: before the + # last point the node takes that point's source position; equal to the last point only, + # the node is solved from the other points + first = np.where(same.any(axis=1), same.argmax(axis=1), self.pt_count) + snap = first < self.pt_count - 1 + + with np.errstate(divide="ignore", invalid="ignore"): + w = np.where(same, 0.0, 1.0 / np.where(same, 1.0, d2)) + sw = w.sum(axis=1, keepdims=True) + pstar = (w @ p) / sw + qstar = (w @ q) / sw + pt_i = p[None, :, :] - pstar[:, None, :] + miu = (w * (pt_i**2).sum(axis=2)).sum(axis=1, keepdims=True) + cur = node - pstar + cur_j = np.stack([-cur[:, 1], cur[:, 0]], axis=1) + pt_j = np.stack([-pt_i[..., 1], pt_i[..., 0]], axis=2) + a = (pt_i * cur[:, None, :]).sum(axis=2) + b = (pt_j * cur[:, None, :]).sum(axis=2) + c = (pt_i * cur_j[:, None, :]).sum(axis=2) + e = (pt_j * cur_j[:, None, :]).sum(axis=2) + scale = w / miu + new_x = (scale * (a * q[None, :, 0] - b * q[None, :, 1])).sum(axis=1) + qstar[:, 0] + new_y = (scale * (-c * q[None, :, 0] + e * q[None, :, 1])).sum(axis=1) + qstar[:, 1] + + if snap.any(): + new_x[snap] = q[first[snap], 0] + new_y[snap] = q[first[snap], 1] + rows, cols = ys.ravel(), xs.ravel() + self.rdx[rows, cols] = new_x - cols + self.rdy[rows, cols] = new_y - rows def gen_img(self): + """Bilinear interpolation of the node displacements to every pixel, then bilinear + sampling of the source (cv2.remap; positions clipped to the image as before).""" src_h, src_w = self.src.shape[:2] - dst = np.zeros_like(self.src, dtype=np.float32) - - for i in np.arange(0, self.dst_h, self.grid_size): - for j in np.arange(0, self.dst_w, self.grid_size): - ni = i + self.grid_size - nj = j + self.grid_size - w = h = self.grid_size - if ni >= self.dst_h: - ni = self.dst_h - 1 - h = ni - i + 1 - if nj >= self.dst_w: - nj = self.dst_w - 1 - w = nj - j + 1 - - di = np.reshape(np.arange(h), (-1, 1)) - dj = np.reshape(np.arange(w), (1, -1)) - delta_x = self.__bilinear_interp( - di / h, - dj / w, - self.rdx[i, j], - self.rdx[i, nj], - self.rdx[ni, j], - self.rdx[ni, nj], - ) - delta_y = self.__bilinear_interp( - di / h, - dj / w, - self.rdy[i, j], - self.rdy[i, nj], - self.rdy[ni, j], - self.rdy[ni, nj], - ) - nx = j + dj + delta_x * self.trans_ratio - ny = i + di + delta_y * self.trans_ratio - nx = np.clip(nx, 0, src_w - 1) - ny = np.clip(ny, 0, src_h - 1) - nxi = np.array(np.floor(nx), dtype=np.int32) - nyi = np.array(np.floor(ny), dtype=np.int32) - nxi1 = np.array(np.ceil(nx), dtype=np.int32) - nyi1 = np.array(np.ceil(ny), dtype=np.int32) - - if len(self.src.shape) == 3: - x = np.tile(np.expand_dims(ny - nyi, axis=-1), (1, 1, 3)) - y = np.tile(np.expand_dims(nx - nxi, axis=-1), (1, 1, 3)) - else: - x = ny - nyi - y = nx - nxi - dst[i : i + h, j : j + w] = self.__bilinear_interp( - x, - y, - self.src[nyi, nxi], - self.src[nyi, nxi1], - self.src[nyi1, nxi], - self.src[nyi1, nxi1], - ) - - dst = np.clip(dst, 0, 255) - dst = np.array(dst, dtype=np.uint8) - - return dst + y0, y1, fy = _cells(self.dst_h, self.grid_size) + x0, x1, fx = _cells(self.dst_w, self.grid_size) + fy = fy[:, None] + nx = np.arange(self.dst_w)[None, :] + self._interp(self.rdx, y0, y1, fy, x0, x1, fx) * self.trans_ratio + ny = np.arange(self.dst_h)[:, None] + self._interp(self.rdy, y0, y1, fy, x0, x1, fx) * self.trans_ratio + map_x = np.clip(nx, 0, src_w - 1).astype(np.float32) + map_y = np.clip(ny, 0, src_h - 1).astype(np.float32) + dst = cv2.remap(self.src, map_x, map_y, cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) + return np.array(dst, dtype=np.uint8) + + @classmethod + def _interp(cls, field, y0, y1, fy, x0, x1, fx): + """field known at the grid nodes -> every pixel: along x on the node rows, then along y.""" + rows = np.unique(np.concatenate([y0, y1])) + along_x = field[rows][:, x0] * (1 - fx) + field[rows][:, x1] * fx + index = np.searchsorted(rows, np.arange(rows.max() + 1)) + return along_x[index[y0]] * (1 - fy) + along_x[index[y1]] * fy diff --git a/tests/ppocr/test_warp_mls.py b/tests/ppocr/test_warp_mls.py new file mode 100644 index 00000000000..7fa47ab92c9 --- /dev/null +++ b/tests/ppocr/test_warp_mls.py @@ -0,0 +1,197 @@ +"""The vectorised WarpMLS (TIA distort / stretch / perspective) must match the original loop version.""" + +import cv2 +import numpy as np +import pytest + +from ppocr.data.imaug.text_image_aug import augment +from ppocr.data.imaug.text_image_aug.warp_mls import WarpMLS + + +class LoopWarpMLS: + """The original implementation (PaddleOCR 3.7, from RubanSeven/Text-Image-Augmentation-python), kept as reference.""" + + def __init__(self, src, src_pts, dst_pts, dst_w, dst_h, trans_ratio=1.0): + self.src = src + self.src_pts = src_pts + self.dst_pts = dst_pts + self.pt_count = len(self.dst_pts) + self.dst_w = dst_w + self.dst_h = dst_h + self.trans_ratio = trans_ratio + self.grid_size = 100 + self.rdx = np.zeros((self.dst_h, self.dst_w)) + self.rdy = np.zeros((self.dst_h, self.dst_w)) + + @staticmethod + def _bilinear_interp(x, y, v11, v12, v21, v22): + return (v11 * (1 - y) + v12 * y) * (1 - x) + (v21 * (1 - y) + v22 * y) * x + + def generate(self): + self.calc_delta() + return self.gen_img() + + def calc_delta(self): + w = np.zeros(self.pt_count, dtype=np.float32) + if self.pt_count < 2: + return + i = 0 + while 1: + if self.dst_w <= i < self.dst_w + self.grid_size - 1: + i = self.dst_w - 1 + elif i >= self.dst_w: + break + j = 0 + while 1: + if self.dst_h <= j < self.dst_h + self.grid_size - 1: + j = self.dst_h - 1 + elif j >= self.dst_h: + break + sw = 0 + swp = np.zeros(2, dtype=np.float32) + swq = np.zeros(2, dtype=np.float32) + new_pt = np.zeros(2, dtype=np.float32) + cur_pt = np.array([i, j], dtype=np.float32) + k = 0 + for k in range(self.pt_count): + if i == self.dst_pts[k][0] and j == self.dst_pts[k][1]: + break + w[k] = 1.0 / ( + (i - self.dst_pts[k][0]) * (i - self.dst_pts[k][0]) + + (j - self.dst_pts[k][1]) * (j - self.dst_pts[k][1]) + ) + sw += w[k] + swp = swp + w[k] * np.array(self.dst_pts[k]) + swq = swq + w[k] * np.array(self.src_pts[k]) + if k == self.pt_count - 1: + pstar = 1 / sw * swp + qstar = 1 / sw * swq + miu_s = 0 + for k in range(self.pt_count): + if i == self.dst_pts[k][0] and j == self.dst_pts[k][1]: + continue + pt_i = self.dst_pts[k] - pstar + miu_s += w[k] * np.sum(pt_i * pt_i) + cur_pt -= pstar + cur_pt_j = np.array([-cur_pt[1], cur_pt[0]]) + for k in range(self.pt_count): + if i == self.dst_pts[k][0] and j == self.dst_pts[k][1]: + continue + pt_i = self.dst_pts[k] - pstar + pt_j = np.array([-pt_i[1], pt_i[0]]) + tmp_pt = np.zeros(2, dtype=np.float32) + tmp_pt[0] = ( + np.sum(pt_i * cur_pt) * self.src_pts[k][0] + - np.sum(pt_j * cur_pt) * self.src_pts[k][1] + ) + tmp_pt[1] = ( + -np.sum(pt_i * cur_pt_j) * self.src_pts[k][0] + + np.sum(pt_j * cur_pt_j) * self.src_pts[k][1] + ) + tmp_pt *= w[k] / miu_s + new_pt += tmp_pt + new_pt += qstar + else: + new_pt = self.src_pts[k] + self.rdx[j, i] = new_pt[0] - i + self.rdy[j, i] = new_pt[1] - j + j += self.grid_size + i += self.grid_size + + def gen_img(self): + src_h, src_w = self.src.shape[:2] + dst = np.zeros_like(self.src, dtype=np.float32) + for i in np.arange(0, self.dst_h, self.grid_size): + for j in np.arange(0, self.dst_w, self.grid_size): + ni = i + self.grid_size + nj = j + self.grid_size + w = h = self.grid_size + if ni >= self.dst_h: + ni = self.dst_h - 1 + h = ni - i + 1 + if nj >= self.dst_w: + nj = self.dst_w - 1 + w = nj - j + 1 + di = np.reshape(np.arange(h), (-1, 1)) + dj = np.reshape(np.arange(w), (1, -1)) + delta_x = self._bilinear_interp( + di / h, dj / w, self.rdx[i, j], self.rdx[i, nj], self.rdx[ni, j], self.rdx[ni, nj] + ) + delta_y = self._bilinear_interp( + di / h, dj / w, self.rdy[i, j], self.rdy[i, nj], self.rdy[ni, j], self.rdy[ni, nj] + ) + nx = np.clip(j + dj + delta_x * self.trans_ratio, 0, src_w - 1) + ny = np.clip(i + di + delta_y * self.trans_ratio, 0, src_h - 1) + nxi = np.array(np.floor(nx), dtype=np.int32) + nyi = np.array(np.floor(ny), dtype=np.int32) + nxi1 = np.array(np.ceil(nx), dtype=np.int32) + nyi1 = np.array(np.ceil(ny), dtype=np.int32) + if len(self.src.shape) == 3: + x = np.tile(np.expand_dims(ny - nyi, axis=-1), (1, 1, 3)) + y = np.tile(np.expand_dims(nx - nxi, axis=-1), (1, 1, 3)) + else: + x = ny - nyi + y = nx - nxi + dst[i : i + h, j : j + w] = self._bilinear_interp( + x, y, self.src[nyi, nxi], self.src[nyi, nxi1], self.src[nyi1, nxi], self.src[nyi1, nxi1] + ) + dst = np.clip(dst, 0, 255) + return np.array(dst, dtype=np.uint8) + + +# The loop version keeps weights and partial sums in float32; with coordinates up to ~1600 its +# node displacements are off by up to a few hundredths of a pixel (the float64 vectorised +# version is the accurate one). Hence the tolerance on the field and smooth test images: +# on text-like images such a shift moves a pixel by a level or two at most. +FIELD_ATOL_PX = 0.05 + + +def _image(h, w, channels, seed): + """Blurred noise: text crops change smoothly from pixel to pixel, unlike raw noise.""" + rng = np.random.RandomState(seed) + shape = (h, w, channels) if channels == 3 else (h, w) + noise = rng.randint(0, 256, size=shape).astype(np.uint8) + return cv2.GaussianBlur(noise, (5, 5), 1.5) + + +def _assert_same_image(new, old): + """Sampling is cv2.remap now: it rounds to the nearest level where the loop version + truncated (which darkened images by half a level on average) and quantises the sub-pixel + position to 1/32 px -- about half of the values move by one level, none by more than two.""" + assert new.shape == old.shape and new.dtype == old.dtype + diff = np.abs(new.astype(np.int16) - old.astype(np.int16)) + assert diff.max() <= 2 + assert diff.mean() < 0.75 + + +@pytest.mark.parametrize("h,w", [(16, 30), (48, 100), (48, 101), (32, 250), (64, 1600), (150, 333)]) +def test_displacement_field_matches_loop_version(h, w): + rng = np.random.RandomState(h * 7 + w) + # corner points coincide with grid nodes -- the special case of the original loop + dst = [[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]] + rng.randint(0, max(w, h), size=(6, 2)).tolist() + src = (np.array(dst) + rng.randint(-5, 6, size=(len(dst), 2))).tolist() + img = _image(h, w, 3, 0) + old, new = LoopWarpMLS(img, src, dst, w, h), WarpMLS(img, src, dst, w, h) + old.calc_delta() + new.calc_delta() + np.testing.assert_allclose(new.rdx, old.rdx, atol=FIELD_ATOL_PX) + np.testing.assert_allclose(new.rdy, old.rdy, atol=FIELD_ATOL_PX) + + +@pytest.mark.parametrize("func", [augment.tia_distort, augment.tia_stretch, augment.tia_perspective]) +@pytest.mark.parametrize("h,w,channels", [(32, 120, 3), (48, 960, 3), (48, 97, 1), (64, 1600, 3), (20, 40, 3)]) +def test_tia_output_matches_loop_version(monkeypatch, func, h, w, channels): + img = _image(h, w, channels, h + w) + args = (img, 4) if func is not augment.tia_perspective else (img,) + outputs = [] + for impl in (LoopWarpMLS, WarpMLS): + monkeypatch.setattr(augment, "WarpMLS", impl) + np.random.seed(123) + outputs.append(func(*args)) + _assert_same_image(outputs[1], outputs[0]) + + +def test_fewer_than_two_points_leaves_image_unwarped(): + img = _image(20, 40, 3, 1) + out = WarpMLS(img, [[3, 3]], [[3, 3]], 40, 20).generate() + _assert_same_image(out, LoopWarpMLS(img, [[3, 3]], [[3, 3]], 40, 20).generate()) From e5045d927be5dc9101aa80c95dcb9c650c3f0c52 Mon Sep 17 00:00:00 2001 From: Vladislav Ershov <75930878+ErshovVE@users.noreply.github.com> Date: Mon, 5 Oct 2026 13:11:55 +0400 Subject: [PATCH 2/3] test(aug): check WarpMLS field against the exact float64 loop, up to 4000 px On wide crops (~3000 px, synthetic lines up to 250 chars) the original float32 loop drifts by up to ~0.5 px near the right edge, so a few pixels of sharp glyph edges differed by up to 33 levels from the vectorised version. The vectorised field equals the same loop run in float64 to 1e-10 px; the test now compares against that reference strictly. --- tests/ppocr/test_warp_mls.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/tests/ppocr/test_warp_mls.py b/tests/ppocr/test_warp_mls.py index 7fa47ab92c9..479e878acbb 100644 --- a/tests/ppocr/test_warp_mls.py +++ b/tests/ppocr/test_warp_mls.py @@ -9,7 +9,13 @@ class LoopWarpMLS: - """The original implementation (PaddleOCR 3.7, from RubanSeven/Text-Image-Augmentation-python), kept as reference.""" + """The original implementation (PaddleOCR 3.7, from RubanSeven/Text-Image-Augmentation-python), kept as reference. + + FLOAT is float32 as in the original; Loop64 below is the same loop in float64 -- the exact + reference for the displacement field (the float32 loop is off by up to ~0.5 px at 4000 px). + """ + + FLOAT = np.float32 def __init__(self, src, src_pts, dst_pts, dst_w, dst_h, trans_ratio=1.0): self.src = src @@ -32,7 +38,7 @@ def generate(self): return self.gen_img() def calc_delta(self): - w = np.zeros(self.pt_count, dtype=np.float32) + w = np.zeros(self.pt_count, dtype=self.FLOAT) if self.pt_count < 2: return i = 0 @@ -48,10 +54,10 @@ def calc_delta(self): elif j >= self.dst_h: break sw = 0 - swp = np.zeros(2, dtype=np.float32) - swq = np.zeros(2, dtype=np.float32) - new_pt = np.zeros(2, dtype=np.float32) - cur_pt = np.array([i, j], dtype=np.float32) + swp = np.zeros(2, dtype=self.FLOAT) + swq = np.zeros(2, dtype=self.FLOAT) + new_pt = np.zeros(2, dtype=self.FLOAT) + cur_pt = np.array([i, j], dtype=self.FLOAT) k = 0 for k in range(self.pt_count): if i == self.dst_pts[k][0] and j == self.dst_pts[k][1]: @@ -79,7 +85,7 @@ def calc_delta(self): continue pt_i = self.dst_pts[k] - pstar pt_j = np.array([-pt_i[1], pt_i[0]]) - tmp_pt = np.zeros(2, dtype=np.float32) + tmp_pt = np.zeros(2, dtype=self.FLOAT) tmp_pt[0] = ( np.sum(pt_i * cur_pt) * self.src_pts[k][0] - np.sum(pt_j * cur_pt) * self.src_pts[k][1] @@ -146,6 +152,10 @@ def gen_img(self): FIELD_ATOL_PX = 0.05 +class Loop64(LoopWarpMLS): + FLOAT = np.float64 + + def _image(h, w, channels, seed): """Blurred noise: text crops change smoothly from pixel to pixel, unlike raw noise.""" rng = np.random.RandomState(seed) @@ -164,7 +174,7 @@ def _assert_same_image(new, old): assert diff.mean() < 0.75 -@pytest.mark.parametrize("h,w", [(16, 30), (48, 100), (48, 101), (32, 250), (64, 1600), (150, 333)]) +@pytest.mark.parametrize("h,w", [(16, 30), (48, 100), (48, 101), (32, 250), (64, 1600), (150, 333), (44, 4000)]) def test_displacement_field_matches_loop_version(h, w): rng = np.random.RandomState(h * 7 + w) # corner points coincide with grid nodes -- the special case of the original loop @@ -174,8 +184,14 @@ def test_displacement_field_matches_loop_version(h, w): old, new = LoopWarpMLS(img, src, dst, w, h), WarpMLS(img, src, dst, w, h) old.calc_delta() new.calc_delta() - np.testing.assert_allclose(new.rdx, old.rdx, atol=FIELD_ATOL_PX) - np.testing.assert_allclose(new.rdy, old.rdy, atol=FIELD_ATOL_PX) + exact = Loop64(img, src, dst, w, h) + exact.calc_delta() + np.testing.assert_allclose(new.rdx, exact.rdx, atol=1e-6) + np.testing.assert_allclose(new.rdy, exact.rdy, atol=1e-6) + # the float32 loop drifts with the coordinate size; keep its error bounded where it is used + if w <= 1600: + np.testing.assert_allclose(new.rdx, old.rdx, atol=FIELD_ATOL_PX) + np.testing.assert_allclose(new.rdy, old.rdy, atol=FIELD_ATOL_PX) @pytest.mark.parametrize("func", [augment.tia_distort, augment.tia_stretch, augment.tia_perspective]) From d2fa563a60807e76eb98b598d9886c4ff341f8ab Mon Sep 17 00:00:00 2001 From: Vladislav Ershov <75930878+ErshovVE@users.noreply.github.com> Date: Mon, 26 Oct 2026 20:14:11 +0100 Subject: [PATCH 3/3] style(aug): black for WarpMLS and its test --- ppocr/data/imaug/text_image_aug/warp_mls.py | 22 ++++++++--- tests/ppocr/test_warp_mls.py | 42 +++++++++++++++++---- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/ppocr/data/imaug/text_image_aug/warp_mls.py b/ppocr/data/imaug/text_image_aug/warp_mls.py index 5a00209c27a..e786e23d2c2 100644 --- a/ppocr/data/imaug/text_image_aug/warp_mls.py +++ b/ppocr/data/imaug/text_image_aug/warp_mls.py @@ -104,8 +104,12 @@ def calc_delta(self): c = (pt_i * cur_j[:, None, :]).sum(axis=2) e = (pt_j * cur_j[:, None, :]).sum(axis=2) scale = w / miu - new_x = (scale * (a * q[None, :, 0] - b * q[None, :, 1])).sum(axis=1) + qstar[:, 0] - new_y = (scale * (-c * q[None, :, 0] + e * q[None, :, 1])).sum(axis=1) + qstar[:, 1] + new_x = (scale * (a * q[None, :, 0] - b * q[None, :, 1])).sum( + axis=1 + ) + qstar[:, 0] + new_y = (scale * (-c * q[None, :, 0] + e * q[None, :, 1])).sum( + axis=1 + ) + qstar[:, 1] if snap.any(): new_x[snap] = q[first[snap], 0] @@ -121,11 +125,19 @@ def gen_img(self): y0, y1, fy = _cells(self.dst_h, self.grid_size) x0, x1, fx = _cells(self.dst_w, self.grid_size) fy = fy[:, None] - nx = np.arange(self.dst_w)[None, :] + self._interp(self.rdx, y0, y1, fy, x0, x1, fx) * self.trans_ratio - ny = np.arange(self.dst_h)[:, None] + self._interp(self.rdy, y0, y1, fy, x0, x1, fx) * self.trans_ratio + nx = ( + np.arange(self.dst_w)[None, :] + + self._interp(self.rdx, y0, y1, fy, x0, x1, fx) * self.trans_ratio + ) + ny = ( + np.arange(self.dst_h)[:, None] + + self._interp(self.rdy, y0, y1, fy, x0, x1, fx) * self.trans_ratio + ) map_x = np.clip(nx, 0, src_w - 1).astype(np.float32) map_y = np.clip(ny, 0, src_h - 1).astype(np.float32) - dst = cv2.remap(self.src, map_x, map_y, cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) + dst = cv2.remap( + self.src, map_x, map_y, cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE + ) return np.array(dst, dtype=np.uint8) @classmethod diff --git a/tests/ppocr/test_warp_mls.py b/tests/ppocr/test_warp_mls.py index 479e878acbb..d1ae91de10c 100644 --- a/tests/ppocr/test_warp_mls.py +++ b/tests/ppocr/test_warp_mls.py @@ -121,10 +121,20 @@ def gen_img(self): di = np.reshape(np.arange(h), (-1, 1)) dj = np.reshape(np.arange(w), (1, -1)) delta_x = self._bilinear_interp( - di / h, dj / w, self.rdx[i, j], self.rdx[i, nj], self.rdx[ni, j], self.rdx[ni, nj] + di / h, + dj / w, + self.rdx[i, j], + self.rdx[i, nj], + self.rdx[ni, j], + self.rdx[ni, nj], ) delta_y = self._bilinear_interp( - di / h, dj / w, self.rdy[i, j], self.rdy[i, nj], self.rdy[ni, j], self.rdy[ni, nj] + di / h, + dj / w, + self.rdy[i, j], + self.rdy[i, nj], + self.rdy[ni, j], + self.rdy[ni, nj], ) nx = np.clip(j + dj + delta_x * self.trans_ratio, 0, src_w - 1) ny = np.clip(i + di + delta_y * self.trans_ratio, 0, src_h - 1) @@ -139,7 +149,12 @@ def gen_img(self): x = ny - nyi y = nx - nxi dst[i : i + h, j : j + w] = self._bilinear_interp( - x, y, self.src[nyi, nxi], self.src[nyi, nxi1], self.src[nyi1, nxi], self.src[nyi1, nxi1] + x, + y, + self.src[nyi, nxi], + self.src[nyi, nxi1], + self.src[nyi1, nxi], + self.src[nyi1, nxi1], ) dst = np.clip(dst, 0, 255) return np.array(dst, dtype=np.uint8) @@ -167,18 +182,24 @@ def _image(h, w, channels, seed): def _assert_same_image(new, old): """Sampling is cv2.remap now: it rounds to the nearest level where the loop version truncated (which darkened images by half a level on average) and quantises the sub-pixel - position to 1/32 px -- about half of the values move by one level, none by more than two.""" + position to 1/32 px -- about half of the values move by one level, none by more than two. + """ assert new.shape == old.shape and new.dtype == old.dtype diff = np.abs(new.astype(np.int16) - old.astype(np.int16)) assert diff.max() <= 2 assert diff.mean() < 0.75 -@pytest.mark.parametrize("h,w", [(16, 30), (48, 100), (48, 101), (32, 250), (64, 1600), (150, 333), (44, 4000)]) +@pytest.mark.parametrize( + "h,w", + [(16, 30), (48, 100), (48, 101), (32, 250), (64, 1600), (150, 333), (44, 4000)], +) def test_displacement_field_matches_loop_version(h, w): rng = np.random.RandomState(h * 7 + w) # corner points coincide with grid nodes -- the special case of the original loop - dst = [[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]] + rng.randint(0, max(w, h), size=(6, 2)).tolist() + dst = [[0, 0], [w - 1, 0], [w - 1, h - 1], [0, h - 1]] + rng.randint( + 0, max(w, h), size=(6, 2) + ).tolist() src = (np.array(dst) + rng.randint(-5, 6, size=(len(dst), 2))).tolist() img = _image(h, w, 3, 0) old, new = LoopWarpMLS(img, src, dst, w, h), WarpMLS(img, src, dst, w, h) @@ -194,8 +215,13 @@ def test_displacement_field_matches_loop_version(h, w): np.testing.assert_allclose(new.rdy, old.rdy, atol=FIELD_ATOL_PX) -@pytest.mark.parametrize("func", [augment.tia_distort, augment.tia_stretch, augment.tia_perspective]) -@pytest.mark.parametrize("h,w,channels", [(32, 120, 3), (48, 960, 3), (48, 97, 1), (64, 1600, 3), (20, 40, 3)]) +@pytest.mark.parametrize( + "func", [augment.tia_distort, augment.tia_stretch, augment.tia_perspective] +) +@pytest.mark.parametrize( + "h,w,channels", + [(32, 120, 3), (48, 960, 3), (48, 97, 1), (64, 1600, 3), (20, 40, 3)], +) def test_tia_output_matches_loop_version(monkeypatch, func, h, w, channels): img = _image(h, w, channels, h + w) args = (img, 4) if func is not augment.tia_perspective else (img,)