first commit
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
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
This commit is contained in:
0
tests/models/pp_ocrv6_small_det/__init__.py
Normal file
0
tests/models/pp_ocrv6_small_det/__init__.py
Normal file
@@ -0,0 +1,291 @@
|
||||
# coding = utf-8
|
||||
# Copyright 2026 The HuggingFace Inc. 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.
|
||||
"""Testing suite for the PP-OCRV6SmallDet model."""
|
||||
|
||||
import inspect
|
||||
import unittest
|
||||
|
||||
from parameterized import parameterized
|
||||
|
||||
from transformers import (
|
||||
PPOCRV5ServerDetImageProcessor,
|
||||
PPOCRV6SmallDetConfig,
|
||||
PPOCRV6SmallDetForObjectDetection,
|
||||
is_torch_available,
|
||||
is_vision_available,
|
||||
)
|
||||
from transformers.image_utils import load_image
|
||||
from transformers.testing_utils import (
|
||||
require_cv2,
|
||||
require_torch,
|
||||
require_torch_accelerator,
|
||||
require_vision,
|
||||
slow,
|
||||
torch_device,
|
||||
)
|
||||
|
||||
from ...test_configuration_common import ConfigTester
|
||||
from ...test_modeling_common import ModelTesterMixin, floats_tensor
|
||||
from ...test_processing_common import url_to_local_path
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
|
||||
class PPOCRV6SmallDetModelTester:
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=3,
|
||||
image_size=128,
|
||||
num_channels=3,
|
||||
num_stages=4,
|
||||
is_training=False,
|
||||
reduction=4,
|
||||
hidden_act="hardswish",
|
||||
layer_list_out_channels=[16, 16, 16, 16],
|
||||
neck_out_channels=16,
|
||||
kernel_list=[3, 2, 2],
|
||||
interpolate_mode="nearest",
|
||||
):
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
self.image_size = image_size
|
||||
self.num_channels = num_channels
|
||||
self.num_stages = num_stages
|
||||
self.is_training = is_training
|
||||
self.reduction = reduction
|
||||
self.hidden_act = hidden_act
|
||||
self.layer_list_out_channels = layer_list_out_channels
|
||||
self.neck_out_channels = neck_out_channels
|
||||
self.kernel_list = kernel_list
|
||||
self.interpolate_mode = interpolate_mode
|
||||
|
||||
def prepare_config_and_inputs_for_common(self):
|
||||
config, pixel_values = self.prepare_config_and_inputs()
|
||||
inputs_dict = {"pixel_values": pixel_values}
|
||||
return config, inputs_dict
|
||||
|
||||
def prepare_config_and_inputs(self):
|
||||
pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
|
||||
config = self.get_config()
|
||||
|
||||
return config, pixel_values
|
||||
|
||||
def get_config(self) -> PPOCRV6SmallDetConfig:
|
||||
backbone_config = {
|
||||
"model_type": "pp_lcnet_v4",
|
||||
"out_features": ["stage1", "stage2", "stage3", "stage4"],
|
||||
"out_indices": [1, 2, 3, 4],
|
||||
"stem_channels": [3, 16, 16],
|
||||
"stem_type": "large",
|
||||
"block_configs": [
|
||||
[[3, 16, 16, 1, False]],
|
||||
[[3, 16, 16, 2, False], [3, 16, 16, 1, False]],
|
||||
[[3, 16, 16, 2, False], [3, 16, 16, 1, False]],
|
||||
[
|
||||
[3, 16, 16, 2, False],
|
||||
[5, 16, 16, 1, False],
|
||||
[5, 16, 16, 1, False],
|
||||
[5, 16, 16, 1, False],
|
||||
[5, 16, 16, 1, False],
|
||||
],
|
||||
],
|
||||
}
|
||||
config = PPOCRV6SmallDetConfig(
|
||||
backbone_config=backbone_config,
|
||||
reduction=self.reduction,
|
||||
hidden_act=self.hidden_act,
|
||||
layer_list_out_channels=self.layer_list_out_channels,
|
||||
neck_out_channels=self.neck_out_channels,
|
||||
kernel_list=self.kernel_list,
|
||||
interpolate_mode=self.interpolate_mode,
|
||||
)
|
||||
return config
|
||||
|
||||
def create_and_check_pp_ocrv6_small_det_object_detection(self, config, pixel_values):
|
||||
model = PPOCRV6SmallDetForObjectDetection(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
result = model(pixel_values)
|
||||
|
||||
self.parent.assertEqual(result.logits.shape, (self.batch_size, 1, self.image_size, self.image_size))
|
||||
|
||||
|
||||
@require_torch
|
||||
class PPOCRV6SmallDetModelTest(ModelTesterMixin, unittest.TestCase):
|
||||
all_model_classes = (PPOCRV6SmallDetForObjectDetection,) if is_torch_available() else ()
|
||||
pipeline_model_mapping = {"object-detection": PPOCRV6SmallDetForObjectDetection} if is_torch_available() else {}
|
||||
|
||||
has_attentions = False
|
||||
test_inputs_embeds = False
|
||||
test_resize_embeddings = False
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = PPOCRV6SmallDetModelTester(
|
||||
self,
|
||||
batch_size=3,
|
||||
is_training=False,
|
||||
image_size=128,
|
||||
)
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=PPOCRV6SmallDetConfig, has_text_modality=False, common_properties=[]
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="PPOCRV6SmallDet does not support input and output embeddings")
|
||||
def test_model_common_attributes(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(reason="Feed forward chunking is not implemented")
|
||||
def test_feed_forward_chunking(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(reason="PPOCRV6SmallDet does not support attention")
|
||||
def test_retain_grad_hidden_states_attentions(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(reason="PPOCRV6SmallDet does not support input and output embeddings")
|
||||
def test_model_get_set_embeddings(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(reason="PPOCRV6SmallDet does not support.")
|
||||
def test_multi_gpu_data_parallel_forward(self):
|
||||
pass
|
||||
|
||||
def test_forward_signature(self):
|
||||
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config)
|
||||
signature = inspect.signature(model.forward)
|
||||
arg_names = [*signature.parameters.keys()]
|
||||
expected_arg_names = ["pixel_values"]
|
||||
self.assertListEqual(arg_names[:1], expected_arg_names)
|
||||
|
||||
# PPOCRV6SmallDet have no seq_length
|
||||
def test_hidden_states_output(self):
|
||||
def check_hidden_states_output(inputs_dict, config, model_class):
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
|
||||
hidden_states = outputs.hidden_states
|
||||
expected_num_stages = self.model_tester.num_stages
|
||||
|
||||
self.assertEqual(len(hidden_states), expected_num_stages + 1)
|
||||
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
for model_class in self.all_model_classes:
|
||||
inputs_dict["output_hidden_states"] = True
|
||||
check_hidden_states_output(inputs_dict, config, model_class)
|
||||
|
||||
# check that output_hidden_states also work using config
|
||||
del inputs_dict["output_hidden_states"]
|
||||
config.output_hidden_states = True
|
||||
self._set_subconfig_attributes(config, "output_hidden_states", True)
|
||||
check_hidden_states_output(inputs_dict, config, model_class)
|
||||
|
||||
@parameterized.expand(["float32", "float16", "bfloat16"])
|
||||
@require_torch_accelerator
|
||||
@slow
|
||||
def test_inference_with_different_dtypes(self, dtype_str):
|
||||
dtype = {
|
||||
"float32": torch.float32,
|
||||
"float16": torch.float16,
|
||||
"bfloat16": torch.bfloat16,
|
||||
}[dtype_str]
|
||||
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config)
|
||||
model.to(torch_device).to(dtype)
|
||||
model.eval()
|
||||
for key, tensor in inputs_dict.items():
|
||||
if tensor.dtype == torch.float32:
|
||||
inputs_dict[key] = tensor.to(dtype)
|
||||
with torch.no_grad():
|
||||
_ = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
|
||||
|
||||
# TODO: vasqu
|
||||
@require_cv2
|
||||
@require_torch
|
||||
@require_vision
|
||||
@slow
|
||||
@unittest.skip(reason="PP-OCRv6_small_det_safetensors weights have not been uploaded yet.")
|
||||
class PPOCRV6SmallDetModelIntegrationTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
model_path = "PaddlePaddle/PP-OCRv6_small_det_safetensors"
|
||||
self.model = PPOCRV6SmallDetForObjectDetection.from_pretrained(model_path).to(torch_device)
|
||||
self.image_processor = (
|
||||
PPOCRV5ServerDetImageProcessor.from_pretrained(model_path) if is_vision_available() else None
|
||||
)
|
||||
img_url = url_to_local_path(
|
||||
"https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png"
|
||||
)
|
||||
self.image = load_image(img_url)
|
||||
|
||||
def test_inference_object_detection_head(self):
|
||||
inputs = self.image_processor(images=self.image, return_tensors="pt").to(torch_device)
|
||||
bs, c, h, w = inputs["pixel_values"].shape
|
||||
|
||||
with torch.no_grad():
|
||||
outputs = self.model(**inputs)
|
||||
|
||||
results = self.image_processor.post_process_object_detection(outputs, target_sizes=inputs["target_sizes"])
|
||||
|
||||
expected_shape_logits = torch.Size((bs, c // 3, h, w))
|
||||
|
||||
expected_logits = torch.tensor(
|
||||
[
|
||||
[4.7810e-07, 5.0727e-07, 4.7810e-07],
|
||||
[5.7200e-07, 6.0746e-07, 5.7200e-07],
|
||||
[4.7810e-07, 5.0727e-07, 4.7810e-07],
|
||||
],
|
||||
device=torch_device,
|
||||
)
|
||||
|
||||
self.assertEqual(outputs.last_hidden_state.shape, expected_shape_logits)
|
||||
torch.testing.assert_close(outputs.last_hidden_state[0, 0, :3, :3], expected_logits, rtol=2e-4, atol=2e-4)
|
||||
|
||||
expected_shape_boxes = torch.Size((4, 4, 2))
|
||||
expected_boxes = torch.tensor(
|
||||
[
|
||||
[[76, 550], [451, 539], [452, 576], [77, 587]],
|
||||
[[11, 504], [518, 483], [520, 534], [13, 555]],
|
||||
[[189, 452], [401, 445], [402, 482], [190, 490]],
|
||||
[[38, 408], [488, 387], [490, 433], [40, 454]],
|
||||
],
|
||||
dtype=torch.short,
|
||||
device=torch_device,
|
||||
)
|
||||
|
||||
self.assertEqual(results[0]["boxes"].shape, expected_shape_boxes)
|
||||
torch.testing.assert_close(results[0]["boxes"], expected_boxes, rtol=2e-2, atol=2e-2)
|
||||
|
||||
expected_scores = torch.tensor([0.8363, 0.8170, 0.8746, 0.8694]).to(torch_device)
|
||||
self.assertEqual(len(results[0]["scores"]), 4)
|
||||
torch.testing.assert_close(
|
||||
torch.tensor(results[0]["scores"]).to(device=torch_device), expected_scores, rtol=2e-2, atol=2e-2
|
||||
)
|
||||
Reference in New Issue
Block a user