
from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import TFCvtForImageClassification, TFCvtModel
    from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor




class      __A(    __lowerCamelCase			):

  """simple docstring"""







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.config_class(**self.inputs_dict						)
       self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,      """embed_dim"""						)						)
       self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,      """num_heads"""						)						)






class      __A:

  """simple docstring"""







  def __init__(self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=13	,      SCREAMING_SNAKE_CASE_=64	,      SCREAMING_SNAKE_CASE_=3	,      SCREAMING_SNAKE_CASE_=[16, 48, 96]	,      SCREAMING_SNAKE_CASE_=[1, 3, 6]	,      SCREAMING_SNAKE_CASE_=[1, 2, 10]	,      SCREAMING_SNAKE_CASE_=[7, 3, 3]	,      SCREAMING_SNAKE_CASE_=[4, 2, 2]	,      SCREAMING_SNAKE_CASE_=[2, 1, 1]	,      SCREAMING_SNAKE_CASE_=[2, 2, 2]	,      SCREAMING_SNAKE_CASE_=[False, False, True]	,      SCREAMING_SNAKE_CASE_=[0.0, 0.0, 0.0]	,      SCREAMING_SNAKE_CASE_=0.02	,      SCREAMING_SNAKE_CASE_=1E-12	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=2	,      ):
       UpperCamelCase__				=    parent
       UpperCamelCase__				=    batch_size
       UpperCamelCase__				=    image_size
       UpperCamelCase__				=    patch_sizes
       UpperCamelCase__				=    patch_stride
       UpperCamelCase__				=    patch_padding
       UpperCamelCase__				=    is_training
       UpperCamelCase__				=    use_labels
       UpperCamelCase__				=    num_labels
       UpperCamelCase__				=    num_channels
       UpperCamelCase__				=    embed_dim
       UpperCamelCase__				=    num_heads
       UpperCamelCase__				=    stride_kv
       UpperCamelCase__				=    depth
       UpperCamelCase__				=    cls_token
       UpperCamelCase__				=    attention_drop_rate
       UpperCamelCase__				=    initializer_range
       UpperCamelCase__				=    layer_norm_eps







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       UpperCamelCase__				=    None
       if self.use_labels:
            # create a random int32 tensor of given shape
            UpperCamelCase__				=    ids_tensor([self.batch_size]	,      self.num_labels						)

       UpperCamelCase__				=    self.get_config()
       return config, pixel_values, labels







  def 	UpperCAmelCase_ (self						):
       return CvtConfig(
           image_size=self.image_size	,      num_labels=self.num_labels	,      num_channels=self.num_channels	,      embed_dim=self.embed_dim	,      num_heads=self.num_heads	,      patch_sizes=self.patch_sizes	,      patch_padding=self.patch_padding	,      patch_stride=self.patch_stride	,      stride_kv=self.stride_kv	,      depth=self.depth	,      cls_token=self.cls_token	,      attention_drop_rate=self.attention_drop_rate	,      initializer_range=self.initializer_range	,      )







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    TFCvtModel(config=SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    model(SCREAMING_SNAKE_CASE_	,      training=SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    (self.image_size, self.image_size)
       UpperCamelCase__		,	UpperCamelCase__				=    image_size[0], image_size[1]
       for i in range(len(self.depth						)						):
            UpperCamelCase__				=    floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
            UpperCamelCase__				=    floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
       self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, self.embed_dim[-1], height, width)						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.num_labels
       UpperCamelCase__				=    TFCvtForImageClassification(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    model(SCREAMING_SNAKE_CASE_	,      labels=SCREAMING_SNAKE_CASE_	,      training=SCREAMING_SNAKE_CASE_						)
       self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.num_labels)						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.prepare_config_and_inputs()
       UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    config_and_inputs
       UpperCamelCase__				=    {"""pixel_values""": pixel_values}
       return config, inputs_dict






@require_tf
class      __A(    __lowerCamelCase    ,				__lowerCamelCase    ,				unittest.TestCase			):

  """simple docstring"""
  SCREAMING_SNAKE_CASE__         =      (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
  SCREAMING_SNAKE_CASE__         =      (
      {"""feature-extraction""": TFCvtModel, """image-classification""": TFCvtForImageClassification}
      if is_tf_available()
      else {}
  )
  SCREAMING_SNAKE_CASE__         =      False
  SCREAMING_SNAKE_CASE__         =      False
  SCREAMING_SNAKE_CASE__         =      False
  SCREAMING_SNAKE_CASE__         =      False
  SCREAMING_SNAKE_CASE__         =      False







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    TFCvtModelTester(self						)
       UpperCamelCase__				=    TFCvtConfigTester(self	,      config_class=SCREAMING_SNAKE_CASE_	,      has_text_modality=SCREAMING_SNAKE_CASE_	,      hidden_size=37						)







  def 	UpperCAmelCase_ (self						):
       self.config_tester.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()







  @unittest.skip(reason="""Cvt does not output attentions"""						)
  def 	UpperCAmelCase_ (self						):
       pass







  @unittest.skip(reason="""Cvt does not use inputs_embeds"""						)
  def 	UpperCAmelCase_ (self						):
       pass







  @unittest.skip(reason="""Cvt does not support input and output embeddings"""						)
  def 	UpperCAmelCase_ (self						):
       pass







  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""						)						) == 0	,      reason="""TF does not support backprop for grouped convolutions on CPU."""	,      )
  def 	UpperCAmelCase_ (self						):
       super().test_dataset_conversion()







  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""						)						) == 0	,      reason="""TF does not support backprop for grouped convolutions on CPU."""	,      )
  @slow
  def 	UpperCAmelCase_ (self						):
       super().test_keras_fit()







  @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8"""						)
  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    tf.keras.mixed_precision.Policy("""mixed_float16"""						)
       tf.keras.mixed_precision.set_global_policy(SCREAMING_SNAKE_CASE_						)
       super().test_keras_fit()
       tf.keras.mixed_precision.set_global_policy("""float32"""						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__		,	UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            UpperCamelCase__				=    model_class(SCREAMING_SNAKE_CASE_						)
            UpperCamelCase__				=    inspect.signature(model.call						)
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            UpperCamelCase__				=    [*signature.parameters.keys()]

            UpperCamelCase__				=    ["""pixel_values"""]
            self.assertListEqual(arg_names[:1]	,      SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       def check_hidden_states_output(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
            UpperCamelCase__				=    model_class(SCREAMING_SNAKE_CASE_						)

            UpperCamelCase__				=    model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)						)
            UpperCamelCase__				=    outputs.hidden_states

            UpperCamelCase__				=    len(self.model_tester.depth						)
            self.assertEqual(len(SCREAMING_SNAKE_CASE_						)	,      SCREAMING_SNAKE_CASE_						)

            # verify the first hidden states (first block)
            self.assertListEqual(
                list(hidden_states[0].shape[-3:]						)	,      [
                    self.model_tester.embed_dim[0],
                    self.model_tester.image_size // 4,
                    self.model_tester.image_size // 4,
                ]	,      )

       UpperCamelCase__		,	UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            UpperCamelCase__				=    True
            check_hidden_states_output(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            UpperCamelCase__				=    True

            check_hidden_states_output(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_						)







  @slow
  def 	UpperCAmelCase_ (self						):
       for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCamelCase__				=    TFCvtModel.from_pretrained(SCREAMING_SNAKE_CASE_						)
            self.assertIsNotNone(SCREAMING_SNAKE_CASE_						)






def 						__magic_name__			(   ):



     '''simple docstring'''




     UpperCamelCase__				=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
     return image




@require_tf
@require_vision
class      __A(    unittest.TestCase			):

  """simple docstring"""







  @cached_property
  def 	UpperCAmelCase_ (self						):
       return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)







  @slow
  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)

       UpperCamelCase__				=    self.default_image_processor
       UpperCamelCase__				=    prepare_img()
       UpperCamelCase__				=    image_processor(images=SCREAMING_SNAKE_CASE_	,      return_tensors="""tf"""						)

       # forward pass
       UpperCamelCase__				=    model(**SCREAMING_SNAKE_CASE_						)

       # verify the logits
       UpperCamelCase__				=    tf.TensorShape((1, 10_00)						)
       self.assertEqual(outputs.logits.shape	,      SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    tf.constant([0.9285, 0.9015, -0.3150]						)
       self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()	,      SCREAMING_SNAKE_CASE_	,      atol=1E-4						)						)







from collections import defaultdict
from typing import Optional

from ..image_utils import load_image
from ..utils import (
    add_end_docstrings,
    is_torch_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_torch_available():
    import torch

    from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING

lowerCamelCase_						     =					logging.get_logger(__name__)




@add_end_docstrings(__lowerCamelCase			)
class      __A(    __lowerCamelCase			):

  """simple docstring"""







  def __init__(self	,      **SCREAMING_SNAKE_CASE_						):
       super().__init__(**SCREAMING_SNAKE_CASE_						)
       requires_backends(self	,      """vision"""						)
       requires_backends(self	,      """torch"""						)

       if self.framework != "pt":
            raise ValueError(F"The {self.__class__} is only available in PyTorch."						)

       self.check_model_type(SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self	,      **SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    {}
       UpperCamelCase__				=    {}
       UpperCamelCase__				=    {}
       # preprocess args
       if "points_per_batch" in kwargs:
            UpperCamelCase__				=    kwargs["""points_per_batch"""]
       if "points_per_crop" in kwargs:
            UpperCamelCase__				=    kwargs["""points_per_crop"""]
       if "crops_n_layers" in kwargs:
            UpperCamelCase__				=    kwargs["""crops_n_layers"""]
       if "crop_overlap_ratio" in kwargs:
            UpperCamelCase__				=    kwargs["""crop_overlap_ratio"""]
       if "crop_n_points_downscale_factor" in kwargs:
            UpperCamelCase__				=    kwargs["""crop_n_points_downscale_factor"""]
       # postprocess args
       if "pred_iou_thresh" in kwargs:
            UpperCamelCase__				=    kwargs["""pred_iou_thresh"""]
       if "stability_score_offset" in kwargs:
            UpperCamelCase__				=    kwargs["""stability_score_offset"""]
       if "mask_threshold" in kwargs:
            UpperCamelCase__				=    kwargs["""mask_threshold"""]
       if "stability_score_thresh" in kwargs:
            UpperCamelCase__				=    kwargs["""stability_score_thresh"""]
       if "crops_nms_thresh" in kwargs:
            UpperCamelCase__				=    kwargs["""crops_nms_thresh"""]
       if "output_rle_mask" in kwargs:
            UpperCamelCase__				=    kwargs["""output_rle_mask"""]
       if "output_bboxes_mask" in kwargs:
            UpperCamelCase__				=    kwargs["""output_bboxes_mask"""]
       return preprocess_kwargs, forward_params, postprocess_kwargs







  def __call__(self	,      SCREAMING_SNAKE_CASE_	,      *SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=None	,      SCREAMING_SNAKE_CASE_=None	,      **SCREAMING_SNAKE_CASE_						):
       return super().__call__(SCREAMING_SNAKE_CASE_	,      *SCREAMING_SNAKE_CASE_	,      num_workers=SCREAMING_SNAKE_CASE_	,      batch_size=SCREAMING_SNAKE_CASE_	,      **SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=64	,      SCREAMING_SNAKE_CASE_ = 0	,      SCREAMING_SNAKE_CASE_ = 5_12 / 15_00	,      SCREAMING_SNAKE_CASE_ = 32	,      SCREAMING_SNAKE_CASE_ = 1	,      ):
       UpperCamelCase__				=    load_image(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    self.image_processor.size["""longest_edge"""]
       UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    self.image_processor.generate_crop_boxes(
           SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    self.image_processor(images=SCREAMING_SNAKE_CASE_	,      return_tensors="""pt"""						)

       with self.device_placement():
            if self.framework == "pt":
                 UpperCamelCase__				=    self.get_inference_context()
                 with inference_context():
                      UpperCamelCase__				=    self._ensure_tensor_on_device(SCREAMING_SNAKE_CASE_	,      device=self.device						)
                      UpperCamelCase__				=    self.model.get_image_embeddings(model_inputs.pop("""pixel_values"""						)						)
                      UpperCamelCase__				=    image_embeddings

       UpperCamelCase__				=    grid_points.shape[1]
       UpperCamelCase__				=    points_per_batch if points_per_batch is not None else n_points

       if points_per_batch <= 0:
            raise ValueError(
                """Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. """
                """To return all points at once, set points_per_batch to None"""						)

       for i in range(0	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
            UpperCamelCase__				=    grid_points[:, i : i + points_per_batch, :, :]
            UpperCamelCase__				=    input_labels[:, i : i + points_per_batch]
            UpperCamelCase__				=    i == n_points - points_per_batch
            yield {
                "input_points": batched_points,
                "input_labels": labels,
                "input_boxes": crop_boxes,
                "is_last": is_last,
                **model_inputs,
            }







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=0.88	,      SCREAMING_SNAKE_CASE_=0.95	,      SCREAMING_SNAKE_CASE_=0	,      SCREAMING_SNAKE_CASE_=1	,      ):
       UpperCamelCase__				=    model_inputs.pop("""input_boxes"""						)
       UpperCamelCase__				=    model_inputs.pop("""is_last"""						)
       UpperCamelCase__				=    model_inputs.pop("""original_sizes"""						).tolist()
       UpperCamelCase__				=    model_inputs.pop("""reshaped_input_sizes"""						).tolist()

       UpperCamelCase__				=    self.model(**SCREAMING_SNAKE_CASE_						)

       # post processing happens here in order to avoid CPU GPU copies of ALL the masks
       UpperCamelCase__				=    model_outputs["""pred_masks"""]
       UpperCamelCase__				=    self.image_processor.post_process_masks(
           SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      binarize=SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    model_outputs["""iou_scores"""]
       UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    self.image_processor.filter_masks(
           masks[0]	,      iou_scores[0]	,      original_sizes[0]	,      input_boxes[0]	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      )
       return {
           "masks": masks,
           "is_last": is_last,
           "boxes": boxes,
           "iou_scores": iou_scores,
       }







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=False	,      SCREAMING_SNAKE_CASE_=False	,      SCREAMING_SNAKE_CASE_=0.7	,      ):
       UpperCamelCase__				=    []
       UpperCamelCase__				=    []
       UpperCamelCase__				=    []
       for model_output in model_outputs:
            all_scores.append(model_output.pop("""iou_scores"""						)						)
            all_masks.extend(model_output.pop("""masks"""						)						)
            all_boxes.append(model_output.pop("""boxes"""						)						)

       UpperCamelCase__				=    torch.cat(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    torch.cat(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    self.image_processor.post_process_for_mask_generation(
           SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    defaultdict(SCREAMING_SNAKE_CASE_						)
       for output in model_outputs:
            for k, v in output.items():
                 extra[k].append(SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    {}
       if output_rle_mask:
            UpperCamelCase__				=    rle_mask

       if output_bboxes_mask:
            UpperCamelCase__				=    bounding_boxes

       return {"masks": output_masks, "scores": iou_scores, **optional, **extra}





