




'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import GLPNImageProcessor






class  _lowerCAmelCase     (  unittest.TestCase	):

      def __init__(   self      ,				__UpperCAmelCase      ,				__UpperCAmelCase=7      ,				__UpperCAmelCase=3      ,				__UpperCAmelCase=18      ,				__UpperCAmelCase=30      ,				__UpperCAmelCase=400      ,				__UpperCAmelCase=True      ,				__UpperCAmelCase=32      ,				__UpperCAmelCase=True      ,				):
        lowerCAmelCase__				:  Optional[int]		       =     parent
        lowerCAmelCase__				:  str		       =     batch_size
        lowerCAmelCase__				:  Dict		       =     num_channels
        lowerCAmelCase__				:  Dict		       =     image_size
        lowerCAmelCase__				:  Optional[int]		       =     min_resolution
        lowerCAmelCase__				:  str		       =     max_resolution
        lowerCAmelCase__				:  Optional[int]		       =     do_resize
        lowerCAmelCase__				:  Union[str, Any]		       =     size_divisor
        lowerCAmelCase__				:  List[str]		       =     do_rescale







      def 	__magic_name__(   self		):
        return {
            "do_resize": self.do_resize,
            "size_divisor": self.size_divisor,
            "do_rescale": self.do_rescale,
        }






@require_torch
@require_vision
class  _lowerCAmelCase     (  _lowercase    ,			unittest.TestCase	):
      A__		     =		GLPNImageProcessor if is_vision_available() else None

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Optional[Any]		       =     GLPNImageProcessingTester(self		)

      @property
      def 	__magic_name__(   self		):
        return self.image_processor_tester.prepare_image_processor_dict()

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Optional[Any]		       =     self.image_processing_class(**self.image_processor_dict		)
        self.assertTrue(hasattr(__UpperCAmelCase      ,				'''do_resize'''		)		)
        self.assertTrue(hasattr(__UpperCAmelCase      ,				'''size_divisor'''		)		)
        self.assertTrue(hasattr(__UpperCAmelCase      ,				'''resample'''		)		)
        self.assertTrue(hasattr(__UpperCAmelCase      ,				'''do_rescale'''		)		)

      def 	__magic_name__(   self		):
        pass

      def 	__magic_name__(   self		):
        # Initialize image_processing
        lowerCAmelCase__				:  str		       =     self.image_processing_class(**self.image_processor_dict		)
        # create random PIL images
        lowerCAmelCase__				:  Dict		       =     prepare_image_inputs(self.image_processor_tester      ,				equal_resolution=__UpperCAmelCase		)
        for image in image_inputs:
          self.assertIsInstance(__UpperCAmelCase      ,				Image.Image		)

        # Test not batched input (GLPNImageProcessor doesn't support batching)
        lowerCAmelCase__				:  str		       =     image_processing(image_inputs[0]      ,				return_tensors='''pt'''		).pixel_values
        self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0		)
        self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0		)

      def 	__magic_name__(   self		):
        # Initialize image_processing
        lowerCAmelCase__				:  List[Any]		       =     self.image_processing_class(**self.image_processor_dict		)
        # create random numpy tensors
        lowerCAmelCase__				:  int		       =     prepare_image_inputs(self.image_processor_tester      ,				equal_resolution=__UpperCAmelCase      ,				numpify=__UpperCAmelCase		)
        for image in image_inputs:
          self.assertIsInstance(__UpperCAmelCase      ,				np.ndarray		)

        # Test not batched input (GLPNImageProcessor doesn't support batching)
        lowerCAmelCase__				:  Optional[Any]		       =     image_processing(image_inputs[0]      ,				return_tensors='''pt'''		).pixel_values
        self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0		)
        self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0		)







      def 	__magic_name__(   self		):
        # Initialize image_processing
        lowerCAmelCase__				:  Tuple		       =     self.image_processing_class(**self.image_processor_dict		)
        # create random PyTorch tensors
        lowerCAmelCase__				:  List[Any]		       =     prepare_image_inputs(self.image_processor_tester      ,				equal_resolution=__UpperCAmelCase      ,				torchify=__UpperCAmelCase		)
        for image in image_inputs:
          self.assertIsInstance(__UpperCAmelCase      ,				torch.Tensor		)

        # Test not batched input (GLPNImageProcessor doesn't support batching)
        lowerCAmelCase__				:  int		       =     image_processing(image_inputs[0]      ,				return_tensors='''pt'''		).pixel_values
        self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0		)
        self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0		)







from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch






@require_torch
@require_tf
class  _lowerCAmelCase     (  _lowercase	):

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Union[str, Any]		       =     SMALL_MODEL_IDENTIFIER
        lowerCAmelCase__				:  List[str]		       =     '''pt'''
        lowerCAmelCase__				:  int		       =     '''tf'''

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  Optional[Any]		       =     AutoModel.from_pretrained(self.test_model		)
        model_pt.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self      ,				__UpperCAmelCase		):
        lowerCAmelCase__				:  str		       =     TFAutoModel.from_pretrained(self.test_model      ,				from_pt=__UpperCAmelCase		)
        model_tf.save_pretrained(__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  int		       =     '''mock_framework'''

        # Framework provided - return whatever the user provides
        lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(self.test_model      ,				__UpperCAmelCase		)
        self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

        # Local checkpoint and framework provided - return provided framework
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  List[str]		       =     FeaturesManager.determine_framework(__UpperCAmelCase      ,				__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
          self._setup_pt_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
          self._setup_tf_ckpt(__UpperCAmelCase		)
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_tf		)

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
          with self.assertRaises(__UpperCAmelCase		):
            lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(__UpperCAmelCase		)







      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  int		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # PyTorch not in environment -> use TensorFlow
        lowerCAmelCase__				:  Union[str, Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_torch_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  Optional[int]		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_tf		)

        # Both in environment -> use PyTorch
        lowerCAmelCase__				:  Union[str, Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        lowerCAmelCase__				:  List[Any]		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		), patch(
            '''transformers.onnx.features.is_torch_available'''      ,				__UpperCAmelCase		):
          lowerCAmelCase__				:  List[Any]		       =     FeaturesManager.determine_framework(self.test_model		)
          self.assertEqual(__UpperCAmelCase      ,				self.framework_pt		)

        # Both not in environment -> raise error
        lowerCAmelCase__				:  Optional[int]		       =     MagicMock(return_value=__UpperCAmelCase		)
        lowerCAmelCase__				:  Dict		       =     MagicMock(return_value=__UpperCAmelCase		)
        with patch('''transformers.onnx.features.is_tf_available'''      ,				__UpperCAmelCase		), patch(
            '''transformers.onnx.features.is_torch_available'''      ,				__UpperCAmelCase		):
          with self.assertRaises(__UpperCAmelCase		):
            lowerCAmelCase__				:  Tuple		       =     FeaturesManager.determine_framework(self.test_model		)

