Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: allow table model to accept optional OCR data #256

Merged
merged 11 commits into from
Oct 17, 2023

Conversation

yuming-long
Copy link
Contributor

@yuming-long yuming-long commented Oct 16, 2023

Summary

Change run_prediction in the table model to accept optional OCR data for table OCR refactor as an alternative to getting OCR tokens in get_tokens.

TODO

please see CORE-2259 to update ocr_token from dict to a data class.

@yuming-long yuming-long marked this pull request as ready for review October 16, 2023 16:16
@yuming-long yuming-long requested a review from badGarnet October 16, 2023 16:16
):
"""Predict table structure"""
outputs_structure = self.get_structure(x, pad_for_structure_detection)
tokens = self.get_tokens(x=x)
if ocr_tokens == []:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably should allow it to be empty if the OCR detects no text. And we can set the default "not OCR'ed" state as None.
Additionally we may want to specify a bit more of the requirement for ocr_tokens is a list of dictionaries. For now we can just add the docstring to layout what is required for each token. And leave a note for future FIXME to refactor token data into a dataclass so we have clear expectations on what it should "look" like.

Copy link
Collaborator

@badGarnet badGarnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you link the ticket associated with this PR in the PR note please?

@yuming-long yuming-long merged commit e8caa4e into main Oct 17, 2023
8 checks passed
@yuming-long yuming-long deleted the yuming/table_accept_ocr_data branch October 17, 2023 16:56
github-merge-queue bot pushed a commit to Unstructured-IO/unstructured that referenced this pull request Oct 21, 2023
…#1801)

### Summary

Table OCR refactor, move the OCR part for table model in inference repo
to unst repo.
* Before this PR, table model extracts OCR tokens with texts and
bounding box and fills the tokens to the table structure in inference
repo. This means we need to do an additional OCR for tables.
* After this PR, we use the OCR data from entire page OCR and pass the
OCR tokens to inference repo, which means we only do one OCR for the
entire document.

**Tech details:**
* Combined env `ENTIRE_PAGE_OCR` and `TABLE_OCR` to `OCR_AGENT`, this
means we use the same OCR agent for entire page and tables since we only
do one OCR.
* Bump inference repo to `0.7.9`, which allow table model in inference
to use pre-computed OCR data from unst repo. Please check in
[PR](Unstructured-IO/unstructured-inference#256).
* All notebooks lint are made by `make tidy`
* This PR also fixes
[issue](#1564),
I've added test for the issue in
`test_pdf.py::test_partition_pdf_hi_table_extraction_with_languages`
* Add same scaling logic to image [similar to previous Table
OCR](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/models/tables.py#L109C1-L113),
but now scaling is applied to entire image

### Test
* Not much to manually testing expect table extraction still works
* But due to change on scaling and use pre-computed OCR data from entire
page, there are some slight (better) changes on table output, here is an
comparison on test outputs i found from the same test
`test_partition_image_with_table_extraction`:

screen shot for table in `layout-parser-paper-with-table.jpg`:
<img width="343" alt="expected"
src="https://github.com/Unstructured-IO/unstructured/assets/63475068/278d7665-d212-433d-9a05-872c4502725c">
before refactor:
<img width="709" alt="before"
src="https://github.com/Unstructured-IO/unstructured/assets/63475068/347fbc3b-f52b-45b5-97e9-6f633eaa0d5e">
after refactor:
<img width="705" alt="after"
src="https://github.com/Unstructured-IO/unstructured/assets/63475068/b3cbd809-cf67-4e75-945a-5cbd06b33b2d">

### TODO
(added as a ticket) Still have some clean up to do in inference repo
since now unst repo have duplicate logic, but can keep them as a fall
back plan. If we want to remove anything OCR related in inference, here
are items that is deprecated and can be removed:
*
[`get_tokens`](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/models/tables.py#L77)
(already noted in code)
* parameter `extract_tables` in inference
*
[`interpret_table_block`](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/inference/layoutelement.py#L88)
*
[`load_agent`](https://github.com/Unstructured-IO/unstructured-inference/blob/main/unstructured_inference/models/tables.py#L197)
* env `TABLE_OCR` 

### Note
if we want to fallback for an additional table OCR (may need this for
using paddle for table), we need to:
* pass `infer_table_structure` to inference with `extract_tables`
parameter
* stop passing `infer_table_structure` to `ocr.py`

---------

Co-authored-by: Yao You <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants