Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
87 lines
3.9 KiB
Python
87 lines
3.9 KiB
Python
# Copyright 2024 The HuggingFace Team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import unittest
|
|
|
|
from transformers import GotOcr2Processor
|
|
from transformers.testing_utils import require_vision
|
|
|
|
from ...test_processing_common import ProcessorTesterMixin
|
|
|
|
|
|
@require_vision
|
|
class GotOcr2ProcessorTest(ProcessorTesterMixin, unittest.TestCase):
|
|
processor_class = GotOcr2Processor
|
|
|
|
@classmethod
|
|
def _setup_tokenizer(cls):
|
|
tokenizer_class = cls._get_component_class_from_processor("tokenizer")
|
|
tokenizer = tokenizer_class.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf")
|
|
return tokenizer
|
|
|
|
@unittest.skip("GotOcr2Processor pop the image processor output 'num_patches'")
|
|
def test_image_processor_defaults(self):
|
|
pass
|
|
|
|
def test_ocr_queries(self):
|
|
processor = self.get_processor()
|
|
image_input = self.prepare_image_inputs()
|
|
inputs = processor(image_input, return_tensors="pt")
|
|
self.assertEqual(inputs["input_ids"].shape, (1, 286))
|
|
self.assertEqual(inputs["pixel_values"].shape, (1, 3, 384, 384))
|
|
|
|
inputs = processor(image_input, return_tensors="pt", format=True)
|
|
self.assertEqual(inputs["input_ids"].shape, (1, 288))
|
|
self.assertEqual(inputs["pixel_values"].shape, (1, 3, 384, 384))
|
|
|
|
inputs = processor(image_input, return_tensors="pt", color="red")
|
|
self.assertEqual(inputs["input_ids"].shape, (1, 290))
|
|
self.assertEqual(inputs["pixel_values"].shape, (1, 3, 384, 384))
|
|
|
|
inputs = processor(image_input, return_tensors="pt", box=[0, 0, 100, 100])
|
|
self.assertEqual(inputs["input_ids"].shape, (1, 303))
|
|
self.assertEqual(inputs["pixel_values"].shape, (1, 3, 384, 384))
|
|
|
|
inputs = processor([image_input, image_input], return_tensors="pt", multi_page=True, format=True)
|
|
self.assertEqual(inputs["input_ids"].shape, (1, 547))
|
|
self.assertEqual(inputs["pixel_values"].shape, (2, 3, 384, 384))
|
|
|
|
inputs = processor(image_input, return_tensors="pt", crop_to_patches=True, max_patches=6)
|
|
self.assertEqual(inputs["input_ids"].shape, (1, 1826))
|
|
self.assertEqual(inputs["pixel_values"].shape, (7, 3, 384, 384))
|
|
|
|
def test_processor_text_has_no_visual(self):
|
|
# Overwritten: requires `multi_page` kwarg to process nested vision inputs
|
|
processor = self.get_processor()
|
|
|
|
text = self.prepare_text_inputs(batch_size=3, modalities="image")
|
|
image_inputs = self.prepare_image_inputs(batch_size=3)
|
|
processing_kwargs = {"return_tensors": "pt", "padding": True, "multi_page": True}
|
|
|
|
# Call with nested list of vision inputs
|
|
image_inputs_nested = [[image] if not isinstance(image, list) else image for image in image_inputs]
|
|
inputs_dict_nested = {"text": text, "images": image_inputs_nested}
|
|
inputs = processor(**inputs_dict_nested, **processing_kwargs)
|
|
self.assertTrue(self.text_input_name in inputs)
|
|
|
|
# Call with one of the samples with no associated vision input
|
|
plain_text = "lower newer"
|
|
image_inputs_nested[0] = []
|
|
text[0] = plain_text
|
|
inputs_dict_no_vision = {"text": text, "images": image_inputs_nested}
|
|
inputs_nested = processor(**inputs_dict_no_vision, **processing_kwargs)
|
|
self.assertListEqual(
|
|
inputs[self.text_input_name][1:].tolist(), inputs_nested[self.text_input_name][1:].tolist()
|
|
)
|