





'''simple docstring'''

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						(a__					):







  def 				UpperCAmelCase_				(		self		)		->						List[Any]:






        """simple docstring"""





        _snake_case		:	Tuple					     =     SMALL_MODEL_IDENTIFIER
        _snake_case		:	Optional[int]					     =     '''pt'''
        _snake_case		:	Optional[Any]					     =     '''tf'''







  def 				UpperCAmelCase_				(		self     ,     lowercase__		)		->						Optional[Any]:






        """simple docstring"""





        _snake_case		:	List[str]					     =     AutoModel.from_pretrained(self.test_model		)
        model_pt.save_pretrained(lowercase__		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__		)		->						Optional[int]:






        """simple docstring"""





        _snake_case		:	Dict					     =     TFAutoModel.from_pretrained(self.test_model     ,     from_pt=lowercase__		)
        model_tf.save_pretrained(lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						List[Any]:






        """simple docstring"""





        _snake_case		:	List[str]					     =     '''mock_framework'''

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

        # Local checkpoint and framework provided - return provided framework
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
              self._setup_pt_ckpt(lowercase__		)
              _snake_case		:	Optional[Any]					     =     FeaturesManager.determine_framework(lowercase__     ,     lowercase__		)
              self.assertEqual(lowercase__     ,     lowercase__		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
              self._setup_tf_ckpt(lowercase__		)
              _snake_case		:	Tuple					     =     FeaturesManager.determine_framework(lowercase__     ,     lowercase__		)
              self.assertEqual(lowercase__     ,     lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						Union[str, Any]:






        """simple docstring"""





        with TemporaryDirectory() as local_pt_ckpt:
              self._setup_pt_ckpt(lowercase__		)
              _snake_case		:	Dict					     =     FeaturesManager.determine_framework(lowercase__		)
              self.assertEqual(lowercase__     ,     self.framework_pt		)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
              self._setup_tf_ckpt(lowercase__		)
              _snake_case		:	Optional[int]					     =     FeaturesManager.determine_framework(lowercase__		)
              self.assertEqual(lowercase__     ,     self.framework_tf		)

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
              with self.assertRaises(lowercase__		):
                    _snake_case		:	Union[str, Any]					     =     FeaturesManager.determine_framework(lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						List[str]:






        """simple docstring"""





        _snake_case		:	Optional[int]					     =     MagicMock(return_value=lowercase__		)
        with patch('''transformers.onnx.features.is_tf_available'''     ,     lowercase__		):
              _snake_case		:	Optional[Any]					     =     FeaturesManager.determine_framework(self.test_model		)
              self.assertEqual(lowercase__     ,     self.framework_pt		)

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

        # Both in environment -> use PyTorch
        _snake_case		:	Optional[int]					     =     MagicMock(return_value=lowercase__		)
        _snake_case		:	str					     =     MagicMock(return_value=lowercase__		)
        with patch('''transformers.onnx.features.is_tf_available'''     ,     lowercase__		), patch(
            '''transformers.onnx.features.is_torch_available'''     ,     lowercase__		):
              _snake_case		:	Tuple					     =     FeaturesManager.determine_framework(self.test_model		)
              self.assertEqual(lowercase__     ,     self.framework_pt		)

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









'''simple docstring'''

from typing import List, Optional, Union

import numpy as np
import PIL.Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCAmelCase					:		Dict		=							logging.get_logger(__name__)





class lowerCamelCase						(a__					):
  _lowercase  :	int       					=				["""pixel_values"""]







  def __init__(		self     ,     lowercase__ = True     ,     lowercase__ = 32     ,     lowercase__=PILImageResampling.BILINEAR     ,     lowercase__ = True     ,     **lowercase__     ,     )		->						None:






        """simple docstring"""





        _snake_case		:	Any					     =     do_resize
        _snake_case		:	List[str]					     =     do_rescale
        _snake_case		:	Any					     =     size_divisor
        _snake_case		:	Optional[Any]					     =     resample
        super().__init__(**lowercase__		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__ = None     ,     **lowercase__		)		->						np.ndarray:






        """simple docstring"""





        _snake_case     ,							_snake_case		:	Dict					     =     get_image_size(lowercase__		)
        # Rounds the height and width down to the closest multiple of size_divisor
        _snake_case		:	Optional[int]					     =     height // size_divisor * size_divisor
        _snake_case		:	Dict					     =     width // size_divisor * size_divisor
        _snake_case		:	str					     =     resize(lowercase__     ,     (new_h, new_w)     ,     resample=lowercase__     ,     data_format=lowercase__     ,     **lowercase__		)
        return image







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__ = None     ,     **lowercase__		)		->						np.ndarray:






        """simple docstring"""





        return rescale(image=lowercase__     ,     scale=lowercase__     ,     data_format=lowercase__     ,     **lowercase__		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__=None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = ChannelDimension.FIRST     ,     **lowercase__     ,     )		->						BatchFeature:






        """simple docstring"""





        _snake_case		:	Any					     =     do_resize if do_resize is not None else self.do_resize
        _snake_case		:	List[Any]					     =     do_rescale if do_rescale is not None else self.do_rescale
        _snake_case		:	List[str]					     =     size_divisor if size_divisor is not None else self.size_divisor
        _snake_case		:	int					     =     resample if resample is not None else self.resample

        if do_resize and size_divisor is None:
              raise ValueError('''size_divisor is required for resizing'''		)

        _snake_case		:	Tuple					     =     make_list_of_images(lowercase__		)

        if not valid_images(lowercase__		):
              raise ValueError('''Invalid image(s)'''		)

        # All transformations expect numpy arrays.
        _snake_case		:	Tuple					     =     [to_numpy_array(lowercase__		) for img in images]

        if do_resize:
              _snake_case		:	Optional[int]					     =     [self.resize(lowercase__     ,     size_divisor=lowercase__     ,     resample=lowercase__		) for image in images]

        if do_rescale:
              _snake_case		:	Union[str, Any]					     =     [self.rescale(lowercase__     ,     scale=1 / 255		) for image in images]

        _snake_case		:	Union[str, Any]					     =     [to_channel_dimension_format(lowercase__     ,     lowercase__		) for image in images]

        _snake_case		:	List[str]					     =     {'''pixel_values''': images}
        return BatchFeature(data=lowercase__     ,     tensor_type=lowercase__		)


