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

Add GOT-OCR 2.0 to Transformers #34721

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

yonigozlan
Copy link
Member

@yonigozlan yonigozlan commented Nov 13, 2024

What does this PR do?

Add GOT-OCR 2.0 to Transformers.

Left TODOs:

  • Tests
  • Docs
  • Post-processing

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@yonigozlan yonigozlan requested review from qubvel and molbap November 25, 2024 21:24
Copy link
Contributor

@molbap molbap left a comment

Choose a reason for hiding this comment

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

Seems very clean, congrats! The modular file is a bit verbose still, I left some comments for possible leads to reduce it. Let's make sure slow tests run, otherwise LGTM, and left a couple comments :)

Copy link
Contributor

Choose a reason for hiding this comment

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

interesting this formatting was missing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I guess my format-on-save did that 😅

write_tokenizer(
tokenizer_path="qwen.tiktoken",
save_dir=args.output_dir,
instruct=args.instruct,
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this model have an instruct version needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I forgot to remove that, thanks. I'd say the model is indeed instruct, but users are not really expected to have conversation with it, and the possible prompt are explicitly defined in the processor so I'm not sure in which categories this model falls and if we should add support for a chat template here.
I'd say it might make sense to add a chat template when and if we end up adding support for fine-tuning, but to me it doesn't make much sense in this state, I might be wrong though.
What do you think @molbap @Ucas-HaoranWei ?

Copy link
Contributor

Choose a reason for hiding this comment

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

From afar, maybe @Ucas-HaoranWei has a different opinion, I'd just remove the instruct - as you say users are not expected to converse with it. If it's instruct-tuned in a later version, it might make sense, but it's an OCR model as it stands, it sounds confusing to label it as instruct.

Choose a reason for hiding this comment

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

Yes, I agree that there should not be a chat template, as they are fixed prompts in this state. @molbap @yonigozlan. If the later versions can be fine-tuned, then the template can be maintained to ensure that users use their new prompts.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good thank you!

src/transformers/models/got_ocr2/modular_got_ocr2.py Outdated Show resolved Hide resolved
resized_image = image.resize((target_width, target_height))

# split the image into patches
processed_images = []
Copy link
Contributor

Choose a reason for hiding this comment

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

these are processed_patches, rather, yes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes indeed, but the reason I did not rename it that is because we add to it a "thumbnail" image which is the whole image resized

Comment on lines 429 to 434
format = output_kwargs["text_kwargs"].pop("format", False)
num_image_tokens = output_kwargs["images_kwargs"].pop("num_image_tokens", 256)
box = output_kwargs["images_kwargs"].pop("box", [None])
color = output_kwargs["images_kwargs"].pop("color", None)
multi_page = output_kwargs["images_kwargs"].pop("multi_page", False)
crop_to_patches = output_kwargs["images_kwargs"].pop("crop_to_patches", False)
Copy link
Contributor

Choose a reason for hiding this comment

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

here we could use the default values that are preset above

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean something like GotOcr2ProcessorKwargs._defaults["images_kwargs"].get("crop_to_patches")?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, for instance! what I mean is the default values in the pop method should not be specified in two places, because if they change, it's more likely to cause errors/mismatches

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense. I might be missing something, but if we have default kwargs (for multi_page, crop_to_patches, min_patches, max_patches etc.), it seems to me that there shouldn't be any issues with using "pop" without default? if the kwarg is not specified by the user, pop will return the default, and it will return the user-specified value otherwise?

Copy link
Member Author

Choose a reason for hiding this comment

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

@molbap just pinging you on this last question

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, you're right - I guess it's just weird to me to see the default values written in two distinct places, does not seem necessary 🤔

src/transformers/models/got_ocr2/modular_got_ocr2.py Outdated Show resolved Hide resolved
class GotOcr2VisionAdapter(nn.Module):
def __init__(self, language_hidden_size: int, vision_output_channels: int):
super().__init__()
self.conv_up1 = nn.Conv2d(
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume up stands for upsampler like in swin2sr, but a more explicit name would be better

src/transformers/models/got_ocr2/modular_got_ocr2.py Outdated Show resolved Hide resolved
@yonigozlan
Copy link
Member Author

yonigozlan commented Nov 29, 2024

Hey @ArthurZucker
This should be ready for you to review :).
There's one question left:

I'd say the model is indeed instruct, but users are not really expected to have conversation with it, and the possible prompt are explicitly defined in the processor so I'm not sure in which categories this model falls and if we should add support for a chat template here.
I'd say it might make sense to add a chat template when and if we end up adding support for fine-tuning, but to me it doesn't make much sense in this state, I might be wrong though.

@piercelamb
Copy link

HI all -- eager to try this model in transformers in the new year

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants