Raven-1/tests/test_knowledge_deepdoc_recognizer.py
Kevin Hu 82c76a82a9
feat(knowledge): read every format, show what the index holds, and let an agent search it (#489)
<img width="2318" height="1448" alt="image"
src="https://github.com/user-attachments/assets/bca71d69-4972-420c-b14e-c8cf74a17de9"
/>

## Summary

Everything a knowledge base could not read, and the pieces it cut them
into
saying what they are.

**Reading a PDF.** A PDF records glyphs at coordinates; whether they are
a
heading, a table cell or a running footer is inferred from how they
look. There
are now two readers. The geometric one infers structure from type size
and
position and needs nothing but the file. The other asks deepdoc's
models, ported
into `raven/knowledge/parser/deepdoc`: a layout model that says what
each region
is, a detector and recognizer that read a page carrying no text at all,
and a
table-structure model that rebuilds a grid from a picture of one. 
 Detecting boxes around text the file already spells
out exactly costs an inference pass per page to learn less, and
recognising it
costs accuracy.

The layout graph answers with a shape its own postprocessing
raises on, and its classes are DocLayout-YOLO's order rather than the
list
Both were established by running the model, and the label order is
pinned by a test.

**Tables go in as tables.** `<table>` is the only shape that keeps what
a grid
states -- which column a cell stood under, which cells are merged, which
row is
the header -- and it is the shape a model reads a table in, which is
what these
chunks are for. PDF, Word and PowerPoint all state their merges exactly,
and
they now survive as `colspan` and `rowspan`. The flat form this replaces
had to
repeat a header standing over two columns across both of them, because
there was
nowhere to record a span; it had no callers left afterwards and is gone.
Two
deviations from upstream, both about what the string is for: every
cell's text
is escaped, where upstream interpolates it raw and a cell holding a `<`
produces
markup that is no longer a table, and the span attributes are written
only where
there is a span, where upstream emits `<td  >` for every ordinary cell.

**A picture of where each chunk came from.** A chunk is a few hundred
words with
a page number on it, which is enough to cite and not enough to answer
whether
the parser read the page correctly. So every PDF chunk that knows where
it sits
carries one image: the regions it covers, cropped out of the rendered
pages and
stacked. here they are already structured in the parser's element spans,
so
the markers would only put text nobody wrote into the thing being
embedded.
The panel draws a chunk's tables as tables, parsed and
rebuilt as elements rather than injected: a person can rewrite any piece
from
that same panel, so what comes back from the store is user input however
it got
there.

**The page that reads them.** Upstream retired the knowledge page while
this
branch was out; it is rebuilt against the new architecture, and the half
that
never existed is the one a reader needs -- what a document actually
became once
it was cut and embedded. Documents sit in one-level folders and arrive
by four
routes: a file, a folder, a URL, a written note. Adding and indexing are
two
calls on the contract and nothing drains that queue on its own, so a
page that
stopped after the first left every upload saying Queued for good.

Opening a file takes the whole page: the document on the left, the
pieces the
index holds of it on the right. The left half is the file drawn as its
own
pages rather than framed by the browser's PDF viewer, which reads where
to open
once and answers to nothing afterwards -- and which nothing can be drawn
on.
Pages are rendered once and kept, and the region a piece was cut from is
washed
over the page it came from, placed in fractions of the page so the
picture's
own scale never enters the arithmetic. Clicking a piece takes the file
there;
double-clicking opens it for rewriting.

The pieces can be searched, filtered by state, turned on and off,
deleted,
rewritten and added to, one at a time or in a batch. Every one of those
was
already on the contract with no reader for it. A base's settings are
back and
fuller: how many pieces a search returns, how documents are cut, and how
much
prose comes with a table or a figure. The embedding model is a picker
while the
base holds no pieces and a stated fact once it does -- moving it then is
the
collection made again at the new width and every document re-cut, which
is not
a thing a panel does by being saved.

**An agent can read them.** The retrieval engine was there and nothing
could
reach it: no agent had a tool that opened a library. A reader now points
a
conversation at bases from the composer, the way they pin a working
directory,
and the selection rides in the same place -- the session's metadata,
bound
around the turn.

The tool takes a query and never a base. The scope is the reader's
choice, so a
model cannot reach material nobody offered it, and it is withheld where
nothing
was picked rather than offered and refused. A hit says which document
and page
it came from, because a hit a reader cannot place is a hit they cannot
check --
and the passages are pressable in the transcript, walking into the base,
the
document, the folder it is filed under and then the piece. A citation a
reader
cannot follow is a claim they have to take on trust, and the whole point
of
searching their own material is that they can go and look.

**The word segmenter and the resources.** `raven.core.Tokenizer` is
Infinity's
`rag_tokenizer`, ported: a forward and backward maximum match over a
dictionary
trie, with stemming and lemmatisation on the latin path. XGBoost is not
carried
over. None of the data lives in git -- the dictionary, the NLTK corpora
and the
ONNX graphs run to about a hundred megabytes, so `make fetch-resources`
on an
install and an optional Docker layer behind `RAVEN_RESOURCES` both point
at one
script. A checkout that could not reach the network still works, and
every
feature that reads this data says so when asked.

## Type

- [ ] Fix
- [x] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Run on macOS against Python 3.12, with the deepdoc weights fetched (the
model
tests skip without them):

```
uv run pytest                        27450 passed, 111 skipped, 3 failed
npm run test --prefix ui-web          3276 passed
npm run test --prefix ui-tui          2106 passed
npm run type-check --prefix ui-web    clean (tsc)
npm run lint --prefix ui-web          clean (4 pre-existing hook warnings)
make lint-python                      clean (ruff check + format)
make lint-types                       clean (ty)
make lint-imports                     10 contracts kept, 0 broken
make check-source-language            clean
make check-large-files                clean
make build-ui                         built, both boot snapshots match
node ui-web/scripts/check-css.mjs     clean
node ui-web/scripts/check-class-namespace.mjs  clean
```

The three Python failures are not this branch's and were each confirmed
on the
base alone: one asks a host without LibreOffice to refuse a render and
this host
has LibreOffice installed, and two simulation cases fail on an
unmodified
checkout of the base commit.

The RPC contract was regenerated for both consumers and both match it
(`npm run gen --prefix ui-web`, `npm run gen:rpc --prefix ui-tui`, then
`gen:check`). The page's boot snapshots and region goldens were
refreshed where
the composer bar gained a button, and each records that button and
nothing else.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

A PDF's tables, and a Word or PowerPoint file's, are now indexed as HTML
rather
than as header-prefixed lines. Chunk text changes shape for those
documents, so
a base indexed before this and searched after it holds both forms until
its
documents are reindexed; retrieval keeps working either way, because
both forms
are text. The same applies to how much prose a table or a figure carries
with
it: the setting now reaches as far as it says, where before it stopped
at the
nearest paragraph, so a chunk cut before this keeps the narrower context
until
its document is rebuilt. Rolling back is reverting the branch and
reindexing.

The deepdoc models are optional. An install that never ran `make
fetch-resources` reads PDFs geometrically, exactly as before, and says
so when
the models are asked for by name. Nothing here reaches the network at
request
time.

Chunk crops and rendered pages are written at index time and on first
view, both
under the state directory rather than in the repository, and both are
deleted
with their document. A page is immutable for the life of its document
id, so
the route serving them caches; the document route does not. All three
routes
take ids and no path, and the two image routes answer under the
strictest
sandbox in the tree.

What an agent may search is the reader's choice and nothing else. The
tool takes
no base argument, so a model cannot name one, and it is absent from the
schema
entirely for a conversation that named none -- a lane nobody pointed at
a base
is unchanged by this branch. The bases a session carries are stored
beside its
working directory and travel no further.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

N/A

---------

Co-authored-by: Claude (claude-opus-5) <noreply@anthropic.com>
2026-10-12 19:05:16 +08:00

624 lines
24 KiB
Python

"""The geometry every deepdoc pass rests on: sorting, overlap, and nearness.
`Recognizer` is the base of the layout and table models, and all of this is
static: given boxes it answers which sit on a line together, which overlap and
by how much, and which of a set is nearest. The models above it only supply
the boxes.
None of it needs the weights, and that is the point of testing it here -- it
was reachable only from the cases that skip without them, which is every
machine but a developer's. It is also ported code, where a wrong comparison
reorders a page rather than raising: the text still comes out, in the wrong
order, which no exception ever reports.
"""
from __future__ import annotations
import pytest
from raven.knowledge.parser.deepdoc._recognizer import Recognizer
def box(x0: float, top: float, x1: float, bottom: float, **extra: object) -> dict:
cell = {"x0": x0, "top": top, "x1": x1, "bottom": bottom}
cell.update(extra) # type: ignore[arg-type]
return cell
class TestSortYFirstly:
"""Reading order down the page, then across it."""
def test_boxes_on_separate_lines_sort_by_line(self) -> None:
lower = box(0, 100, 10, 110, text="second")
upper = box(50, 0, 60, 10, text="first")
out = Recognizer.sort_Y_firstly([lower, upper], threshold=5)
assert [b["text"] for b in out] == ["first", "second"]
def test_boxes_within_the_threshold_count_as_one_line_and_sort_across(self) -> None:
"""Two glyphs on the same printed line never share a top to the pixel.
The threshold is what makes them one line rather than two."""
right = box(50, 2, 60, 12, text="b")
left = box(0, 0, 10, 10, text="a")
out = Recognizer.sort_Y_firstly([right, left], threshold=5)
assert [b["text"] for b in out] == ["a", "b"]
def test_a_gap_wider_than_the_threshold_is_two_lines(self) -> None:
"""The same pair, with the threshold too small to join them: now the
lower-but-leftmost box sorts second."""
right = box(50, 2, 60, 12, text="b")
left = box(0, 0, 10, 10, text="a")
out = Recognizer.sort_Y_firstly([right, left], threshold=1)
assert [b["text"] for b in out] == ["a", "b"]
def test_an_empty_list_sorts_to_an_empty_list(self) -> None:
assert Recognizer.sort_Y_firstly([], threshold=5) == []
class TestSortXFirstly:
def test_columns_sort_left_to_right(self) -> None:
right = box(100, 0, 110, 10, text="b")
left = box(0, 50, 10, 60, text="a")
out = Recognizer.sort_X_firstly([right, left], threshold=5)
assert [b["text"] for b in out] == ["a", "b"]
def test_within_the_threshold_the_higher_box_comes_first(self) -> None:
lower = box(2, 100, 12, 110, text="b")
upper = box(0, 0, 10, 10, text="a")
out = Recognizer.sort_X_firstly([lower, upper], threshold=5)
assert [b["text"] for b in out] == ["a", "b"]
class TestSortByRegion:
"""The same two orders, but with the model's own row and column regions
overriding the geometry where it supplied them."""
def test_a_row_region_outranks_the_coordinates(self) -> None:
"""Two boxes the geometry would separate, which the model says are one
row. `R` wins, which is what keeps a row together when one cell sits
lower than the rest of its line."""
late = box(0, 100, 10, 110, R=0, text="first")
early = box(0, 0, 10, 10, R=1, text="second")
out = Recognizer.sort_R_firstly([late, early], thr=5)
assert [b["text"] for b in out] == ["first", "second"]
def test_within_one_row_region_the_order_is_left_to_right(self) -> None:
right = box(100, 0, 110, 10, R=0, text="b")
left = box(0, 0, 10, 10, R=0, text="a")
out = Recognizer.sort_R_firstly([right, left], thr=5)
assert [b["text"] for b in out] == ["a", "b"]
def test_boxes_without_a_region_keep_the_geometric_order(self) -> None:
"""A page the model found no rows on still has to come out in order."""
lower = box(0, 100, 10, 110, text="second")
upper = box(0, 0, 10, 10, text="first")
out = Recognizer.sort_R_firstly([lower, upper], thr=5)
assert [b["text"] for b in out] == ["first", "second"]
def test_a_column_region_outranks_the_coordinates(self) -> None:
late = box(100, 0, 110, 10, C=0, text="first")
early = box(0, 0, 10, 10, C=1, text="second")
out = Recognizer.sort_C_firstly([late, early], thr=5)
assert [b["text"] for b in out] == ["first", "second"]
def test_within_one_column_region_the_order_is_top_down(self) -> None:
lower = box(0, 100, 10, 110, C=0, text="b")
upper = box(0, 0, 10, 10, C=0, text="a")
out = Recognizer.sort_C_firstly([lower, upper], thr=5)
assert [b["text"] for b in out] == ["a", "b"]
class TestOverlappedArea:
"""How much of `a` lies under `b`, as a fraction of `a` unless told not to."""
def test_boxes_that_miss_horizontally_do_not_overlap(self) -> None:
assert Recognizer.overlapped_area(box(0, 0, 10, 10), box(20, 0, 30, 10)) == 0
def test_boxes_that_miss_vertically_do_not_overlap(self) -> None:
assert Recognizer.overlapped_area(box(0, 0, 10, 10), box(0, 20, 10, 30)) == 0
def test_a_box_inside_another_is_wholly_overlapped(self) -> None:
assert Recognizer.overlapped_area(box(2, 2, 8, 8), box(0, 0, 10, 10)) == pytest.approx(1.0)
def test_a_quarter_overlap_reads_as_a_quarter(self) -> None:
assert Recognizer.overlapped_area(box(0, 0, 2, 2), box(1, 1, 3, 3)) == pytest.approx(0.25)
def test_without_the_ratio_it_answers_in_square_points(self) -> None:
"""The absolute form is what `layouts_cleanup` compares between two
candidate regions, where the fractions are of different denominators."""
assert Recognizer.overlapped_area(box(0, 0, 2, 2), box(1, 1, 3, 3), ratio=False) == pytest.approx(1.0)
def test_a_box_of_no_area_overlaps_nothing(self) -> None:
"""A zero-width box divides by zero in the ratio. It arrives from a
detection that collapsed, and the answer is no overlap rather than a
raise in the middle of a page."""
assert Recognizer.overlapped_area(box(5, 0, 5, 10), box(0, 0, 10, 10)) == 0
def test_the_fraction_is_of_the_first_box(self) -> None:
"""Asymmetric on purpose: a small box inside a large one is wholly
covered, while the large one is barely covered by the small."""
small, large = box(0, 0, 2, 2), box(0, 0, 10, 10)
assert Recognizer.overlapped_area(small, large) == pytest.approx(1.0)
assert Recognizer.overlapped_area(large, small) == pytest.approx(0.04)
class TestLayoutsCleanup:
"""Two detections of the same thing, reduced to one."""
def test_the_higher_scoring_of_two_overlapping_regions_wins(self) -> None:
layouts = [
box(0, 0, 10, 10, type="table", score=0.6),
box(0, 0, 10, 10, type="table", score=0.9),
]
out = Recognizer.layouts_cleanup([], layouts)
assert len(out) == 1
assert out[0]["score"] == 0.9
def test_regions_of_different_kinds_both_survive(self) -> None:
"""A caption drawn over a table is not a duplicate of it."""
layouts = [
box(0, 0, 10, 10, type="table", score=0.9),
box(0, 0, 10, 10, type="figure", score=0.8),
]
assert len(Recognizer.layouts_cleanup([], layouts)) == 2
def test_regions_that_barely_touch_both_survive(self) -> None:
"""Below the threshold they are two things that happen to abut, not one
thing found twice."""
layouts = [
box(0, 0, 10, 10, type="table", score=0.9),
box(9, 9, 20, 20, type="table", score=0.8),
]
assert len(Recognizer.layouts_cleanup([], layouts)) == 2
def test_without_scores_the_region_covering_more_text_wins(self) -> None:
"""An unscored duplicate is settled by which one the page's own text
boxes actually sit inside."""
layouts = [
box(0, 0, 10, 10, type="table"),
box(0, 0, 100, 100, type="table"),
]
text = [box(20, 20, 80, 80)]
out = Recognizer.layouts_cleanup(text, layouts)
assert len(out) == 1
assert out[0]["x1"] == 100, "the region the text is in"
def test_a_single_region_is_left_alone(self) -> None:
layouts = [box(0, 0, 10, 10, type="table", score=0.9)]
assert Recognizer.layouts_cleanup([], layouts) == layouts
def test_an_empty_page_cleans_up_to_nothing(self) -> None:
assert Recognizer.layouts_cleanup([], []) == []
class TestFindOverlapped:
"""Which of a y-sorted list a box sits in, found by halving rather than
walking -- the list is every text box on the page."""
@staticmethod
def _sorted_boxes() -> list[dict]:
return [box(0, n * 20, 100, n * 20 + 10) for n in range(10)]
def test_it_finds_the_box_the_target_sits_in(self) -> None:
boxes = self._sorted_boxes()
assert Recognizer.find_overlapped(box(0, 60, 100, 70), boxes) == 3
def test_the_naive_walk_agrees_with_the_halving(self) -> None:
"""Same answer by the other route, which is what says the bisection
above did not simply land somewhere plausible."""
boxes = self._sorted_boxes()
target = box(0, 60, 100, 70)
assert Recognizer.find_overlapped(target, boxes) == Recognizer.find_overlapped(target, boxes, naive=True)
def test_a_box_above_everything_overlaps_nothing(self) -> None:
assert Recognizer.find_overlapped(box(0, -100, 100, -90), self._sorted_boxes()) is None
def test_a_box_below_everything_overlaps_nothing(self) -> None:
assert Recognizer.find_overlapped(box(0, 1000, 100, 1010), self._sorted_boxes()) is None
def test_an_empty_list_has_nothing_to_find(self) -> None:
assert Recognizer.find_overlapped(box(0, 0, 10, 10), []) is None
def test_the_most_overlapped_wins_when_a_box_straddles_two(self) -> None:
boxes = [box(0, 0, 100, 10), box(0, 10, 100, 60)]
assert Recognizer.find_overlapped(box(0, 8, 100, 60), boxes) == 1
class TestFindHorizontallyTightestFit:
"""Which column a cell belongs to, among the columns of its own table."""
def test_the_nearest_by_edge_wins(self) -> None:
boxes = [box(0, 0, 10, 10, layoutno="table-0"), box(90, 0, 100, 10, layoutno="table-0")]
assert Recognizer.find_horizontally_tightest_fit(box(88, 2, 98, 8, layoutno="table-0"), boxes) == 1
def test_a_candidate_from_another_table_is_refused(self) -> None:
boxes = [box(0, 0, 10, 10, layoutno="table-1")]
assert Recognizer.find_horizontally_tightest_fit(box(0, 0, 10, 10, layoutno="table-0"), boxes) is None
def test_a_candidate_sharing_no_line_is_refused(self) -> None:
"""`layoutno` resets per page, so the same string names a different
table further down. Vertical extent is what tells them apart -- without
this a column from another page wins on x alone.
"""
boxes = [box(0, 500, 10, 510, layoutno="table-0")]
assert Recognizer.find_horizontally_tightest_fit(box(0, 0, 10, 10, layoutno="table-0"), boxes) is None
def test_an_empty_candidate_list_fits_nothing(self) -> None:
assert Recognizer.find_horizontally_tightest_fit(box(0, 0, 10, 10), []) is None
class TestFindOverlappedWithThreshold:
def test_an_overlap_under_the_threshold_is_no_match(self) -> None:
boxes = [box(9, 9, 20, 20)]
assert Recognizer.find_overlapped_with_threshold(box(0, 0, 10, 10), boxes, thr=0.5) is None
def test_an_overlap_over_the_threshold_matches(self) -> None:
boxes = [box(0, 0, 10, 10)]
assert Recognizer.find_overlapped_with_threshold(box(1, 1, 9, 9), boxes, thr=0.5) == 0
def test_the_best_of_several_wins(self) -> None:
boxes = [box(0, 0, 20, 20), box(0, 0, 11, 11)]
assert Recognizer.find_overlapped_with_threshold(box(0, 0, 10, 10), boxes, thr=0.1) == 1
def test_an_empty_candidate_list_matches_nothing(self) -> None:
assert Recognizer.find_overlapped_with_threshold(box(0, 0, 10, 10), []) is None
LABELS = ["Table", "Figure", "Text"]
def _stub(*, input_names: list[str], input_shape: tuple[int, int] = (64, 64)) -> Recognizer:
"""A recognizer with its graph's shape but no graph.
`__init__` opens an onnx session, and none of the pre/post-processing below
reaches it: what those read is the input names the graph declared and the
label list the caller passed.
"""
recogniser = object.__new__(Recognizer)
recogniser.input_names = input_names # type: ignore[attr-defined]
recogniser.input_shape = input_shape # type: ignore[attr-defined]
recogniser.label_list = LABELS # type: ignore[attr-defined]
return recogniser
class TestPreprocess:
"""A rendered page, turned into the tensor the graph declared."""
def test_the_direct_path_scales_to_the_graphs_input_shape(self) -> None:
import numpy as np
page = np.zeros((200, 300, 3), dtype=np.uint8)
inputs = _stub(input_names=["images"], input_shape=(64, 96)).preprocess([page])
assert len(inputs) == 1
assert inputs[0]["images"].shape == (1, 3, 64, 96), "batch, channels, height, width"
def test_pixels_arrive_scaled_to_the_unit_range(self) -> None:
import numpy as np
page = np.full((10, 10, 3), 255, dtype=np.uint8)
inputs = _stub(input_names=["images"], input_shape=(8, 8)).preprocess([page])
assert inputs[0]["images"].max() == pytest.approx(1.0)
def test_the_scale_factor_records_what_the_resize_cost(self) -> None:
"""The boxes come back in the resized frame and are multiplied by this
to land on the page again."""
import numpy as np
page = np.zeros((200, 300, 3), dtype=np.uint8)
inputs = _stub(input_names=["images"], input_shape=(100, 150)).preprocess([page])
assert inputs[0]["scale_factor"] == [pytest.approx(2.0), pytest.approx(2.0)]
def test_a_page_is_not_swapped_to_bgr(self) -> None:
"""Upstream reads pages through cv2, whose arrays are BGR, and swaps
here. This tree renders through PyMuPDF, whose arrays are RGB, so the
same swap would feed the model blue text on a yellow ground -- which it
answers, a little worse, saying nothing."""
import numpy as np
page = np.zeros((8, 8, 3), dtype=np.uint8)
page[:, :, 0] = 255 # red in RGB
inputs = _stub(input_names=["images"], input_shape=(8, 8)).preprocess([page])
channels = inputs[0]["images"][0]
assert channels[0].max() == pytest.approx(1.0), "the red channel stays first"
assert channels[2].max() == pytest.approx(0.0)
def test_every_page_in_the_batch_comes_back(self) -> None:
import numpy as np
pages = [np.zeros((20, 20, 3), dtype=np.uint8) for _ in range(3)]
assert len(_stub(input_names=["images"], input_shape=(8, 8)).preprocess(pages)) == 3
class TestPostprocessDocLayoutYolo:
"""The shape the published weights actually answer in: one row per query,
already in corner form. Upstream has only the other two, so its YOLO branch
transposes this one into nonsense and indexes the labels with a query
number -- detected here by shape, because the graph states neither."""
@staticmethod
def _answer(rows: list[list[float]]):
import numpy as np
return np.array([rows], dtype=np.float32)
def test_a_detection_comes_back_scaled_onto_the_page(self) -> None:
boxes = self._answer([[0.0, 0.0, 10.0, 20.0, 0.9, 0.0]])
found = _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [2.0, 3.0]}, thr=0.5)
assert len(found) == 1
assert found[0]["type"] == "table"
assert found[0]["bbox"] == [0.0, 0.0, 20.0, 60.0]
def test_a_detection_under_the_threshold_is_dropped(self) -> None:
boxes = self._answer([[0.0, 0.0, 10.0, 20.0, 0.1, 0.0]])
assert _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5) == []
def test_a_class_the_label_list_does_not_have_is_dropped(self) -> None:
"""The graph is free to answer a class this build has no name for;
indexing the list with it would raise mid-page."""
boxes = self._answer([[0.0, 0.0, 10.0, 20.0, 0.9, 99.0]])
assert _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5) == []
def test_the_label_is_lowercased(self) -> None:
boxes = self._answer([[0.0, 0.0, 1.0, 1.0, 0.9, 1.0]])
found = _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5)
assert found[0]["type"] == "figure"
class TestPostprocessPaddleDetection:
"""The graph that takes a `scale_factor` input: one row per detection as
[class, score, x0, y0, x1, y1]."""
@staticmethod
def _answer(rows: list[list[float]]):
import numpy as np
return np.array(rows, dtype=np.float32)
def test_a_detection_comes_back_named(self) -> None:
boxes = self._answer([[0.0, 0.9, 1.0, 2.0, 3.0, 4.0]])
found = _stub(input_names=["image", "scale_factor"]).postprocess(boxes, {}, thr=0.5)
assert found == [{"type": "table", "bbox": [1.0, 2.0, 3.0, 4.0], "score": pytest.approx(0.9)}]
def test_a_detection_under_the_threshold_is_dropped(self) -> None:
boxes = self._answer([[0.0, 0.2, 1.0, 2.0, 3.0, 4.0]])
assert _stub(input_names=["image", "scale_factor"]).postprocess(boxes, {}, thr=0.5) == []
def test_a_class_past_the_label_list_is_dropped(self) -> None:
boxes = self._answer([[9.0, 0.9, 1.0, 2.0, 3.0, 4.0]])
assert _stub(input_names=["image", "scale_factor"]).postprocess(boxes, {}, thr=0.5) == []
#: A second query, scored below every threshold these cases use, so it is
#: filtered out and changes no assertion. Present because `np.squeeze` flattens
#: a one-query block into a single row, which this branch then cannot index --
#: a shape the graph never answers, since it emits one query per anchor.
_IGNORED = [1000.0, 1000.0, 1.0, 1.0, 0.0, 0.0, 0.0]
class TestPostprocessTransposedYolo:
"""The third shape: a transposed block of boxes and per-class scores, in
centre-and-size form, with overlapping detections still to be thinned."""
@staticmethod
def _answer(queries: list[list[float]]):
"""``queries`` are rows of [cx, cy, w, h, *class_scores]; the graph
answers them transposed, with a leading batch axis."""
import numpy as np
return np.array([np.array(queries, dtype=np.float32).T])
def test_a_detection_comes_back_in_corner_form(self) -> None:
boxes = self._answer([[10.0, 20.0, 4.0, 6.0, 0.9, 0.0, 0.0], _IGNORED])
found = _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5)
assert len(found) == 1
assert found[0]["type"] == "table"
assert found[0]["bbox"] == [8.0, 17.0, 12.0, 23.0], "centre 10,20 of size 4x6"
def test_everything_under_the_threshold_leaves_nothing(self) -> None:
boxes = self._answer([[10.0, 20.0, 4.0, 6.0, 0.1, 0.0, 0.0], _IGNORED])
assert _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5) == []
def test_the_class_with_the_highest_score_names_the_box(self) -> None:
boxes = self._answer([[10.0, 20.0, 4.0, 6.0, 0.1, 0.2, 0.95], _IGNORED])
found = _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5)
assert found[0]["type"] == "text"
def test_two_detections_of_one_thing_are_thinned_to_the_better(self) -> None:
"""The graph answers a query per anchor, so a single table arrives many
times over. Without the overlap filter the page carries each copy."""
boxes = self._answer(
[
[10.0, 20.0, 4.0, 6.0, 0.9, 0.0, 0.0],
[10.1, 20.1, 4.0, 6.0, 0.6, 0.0, 0.0],
]
)
found = _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5)
assert len(found) == 1
assert found[0]["score"] == pytest.approx(0.9), "the better of the two"
def test_two_different_things_that_overlap_both_survive(self) -> None:
"""The filter runs per class: a caption drawn over a table must not
thin the table away."""
boxes = self._answer(
[
[10.0, 20.0, 4.0, 6.0, 0.9, 0.0, 0.0],
[10.0, 20.0, 4.0, 6.0, 0.0, 0.9, 0.0],
]
)
found = _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [1.0, 1.0]}, thr=0.5)
assert {b["type"] for b in found} == {"table", "figure"}
def test_the_scale_factor_puts_the_boxes_back_on_the_page(self) -> None:
boxes = self._answer([[10.0, 20.0, 4.0, 6.0, 0.9, 0.0, 0.0], _IGNORED])
found = _stub(input_names=["images"]).postprocess(boxes, {"scale_factor": [2.0, 3.0]}, thr=0.5)
assert found[0]["bbox"] == [16.0, 51.0, 24.0, 69.0]
class TestCreateInputs:
"""The PaddleDetection shape, where a batch is one padded tensor."""
@staticmethod
def _info(height: float, width: float) -> dict:
import numpy as np
return {
"im_shape": np.array([height, width], dtype="float32"),
"scale_factor": np.array([1.0, 1.0], dtype="float32"),
}
def test_a_single_page_is_wrapped_without_padding(self) -> None:
import numpy as np
page = np.zeros((3, 10, 20), dtype=np.float32)
inputs = _stub(input_names=["image", "scale_factor"]).create_inputs([page], [self._info(10, 20)])
assert inputs["image"].shape == (1, 3, 10, 20)
assert set(inputs) == {"image", "im_shape", "scale_factor"}
def test_a_batch_is_padded_up_to_its_largest_page(self) -> None:
"""One tensor per batch, so the shorter pages are padded rather than
sent separately."""
import numpy as np
pages = [np.zeros((3, 10, 20), dtype=np.float32), np.zeros((3, 30, 40), dtype=np.float32)]
info = [self._info(10, 20), self._info(30, 40)]
inputs = _stub(input_names=["image", "scale_factor"]).create_inputs(pages, info)
assert inputs["image"].shape == (2, 3, 30, 40)
def test_the_padding_goes_after_the_page(self) -> None:
"""So the boxes keep the coordinates they were detected at."""
import numpy as np
small = np.ones((3, 10, 20), dtype=np.float32)
pages = [small, np.zeros((3, 30, 40), dtype=np.float32)]
info = [self._info(10, 20), self._info(30, 40)]
inputs = _stub(input_names=["image", "scale_factor"]).create_inputs(pages, info)
assert inputs["image"][0, :, :10, :20].min() == pytest.approx(1.0)
assert inputs["image"][0, :, 10:, :].max() == pytest.approx(0.0)
class TestRunningABatch:
"""``__call__``: pages in, one list of regions per page out."""
@staticmethod
def _running(answer, *, input_shape=(64, 64)):
recogniser = _stub(input_names=["images"], input_shape=input_shape)
recogniser.batches = [] # type: ignore[attr-defined]
def run(_names, feed, _options=None):
recogniser.batches.append(feed) # type: ignore[attr-defined]
return [answer]
recogniser.ort_sess = type("S", (), {"run": staticmethod(run)})() # type: ignore[attr-defined]
return recogniser
@staticmethod
def _answer():
import numpy as np
return np.array([[[0.0, 0.0, 10.0, 20.0, 0.9, 0.0]]], dtype=np.float32)
def test_each_page_answers_its_own_regions(self) -> None:
import numpy as np
recogniser = self._running(self._answer())
out = recogniser([np.zeros((50, 50, 3), dtype=np.uint8) for _ in range(2)])
assert len(out) == 2
assert out[0][0]["type"] == "table"
def test_a_long_document_is_run_in_batches(self) -> None:
import numpy as np
recogniser = self._running(self._answer())
recogniser([np.zeros((50, 50, 3), dtype=np.uint8) for _ in range(5)], batch_size=2)
assert len(recogniser.batches) == 5, "one run per page, three batches of pages"
def test_a_page_given_as_an_image_object_is_converted(self) -> None:
"""The caller hands pages straight from the renderer, which are not
always arrays."""
from PIL import Image
recogniser = self._running(self._answer())
out = recogniser([Image.new("RGB", (50, 50))])
assert len(out) == 1
def test_no_pages_answer_nothing(self) -> None:
assert self._running(self._answer())([]) == []