




import unittest

import numpy as np
import requests

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

							from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
							lowerCamelCase	:	Tuple				=   False

if is_vision_available():
							from PIL import Image

							from transformers import PixaStructImageProcessor




class 						__lowercase		(unittest.TestCase							):




						"""simple docstring"""





						def __init__(				self    ,				A    ,				A=7    ,				A=3    ,				A=1_8    ,				A=3_0    ,				A=4_0_0    ,				A=None    ,				A=True    ,				A=True    ,				A=None    ,				)		->    Any:
									snake_case				:    List[Any]           =	size if size is not None else {"""height""": 2_0, """width""": 2_0}
									snake_case				:    Optional[Any]           =	parent
									snake_case				:    Union[str, Any]           =	batch_size
									snake_case				:    Union[str, Any]           =	num_channels
									snake_case				:    Any           =	image_size
									snake_case				:    Optional[int]           =	min_resolution
									snake_case				:    Any           =	max_resolution
									snake_case				:    Tuple           =	size
									snake_case				:    Optional[int]           =	do_normalize
									snake_case				:    Optional[Any]           =	do_convert_rgb
									snake_case				:    List[Any]           =	[5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
									snake_case				:    Any           =	patch_size if patch_size is not None else {"""height""": 1_6, """width""": 1_6}

						def      UpperCAmelCase				(				self     )		->    Dict:
									return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}

						def      UpperCAmelCase				(				self     )		->    Any:
									snake_case				:    Dict           =	"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"""
									snake_case				:    Tuple           =	Image.open(requests.get(UpperCamelCase_    ,				stream=UpperCamelCase_     ).raw     ).convert("""RGB"""     )
									return raw_image




@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11					,		reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""					,		)
@require_torch
@require_vision
class 						__lowercase		(__lowerCamelCase					,		unittest.TestCase							):




						"""simple docstring"""





						_snake_case												=						PixaStructImageProcessor if is_vision_available() else None

						def      UpperCAmelCase				(				self     )		->    List[str]:
									snake_case				:    Optional[int]           =	PixaStructImageProcessingTester(self     )

						@property
						def      UpperCAmelCase				(				self     )		->    Any:
									return self.image_processor_tester.prepare_image_processor_dict()

						def      UpperCAmelCase				(				self     )		->    Optional[Any]:
									snake_case				:    str           =	self.image_processing_class(**self.image_processor_dict     )
									self.assertTrue(hasattr(UpperCamelCase_    ,				"""do_normalize"""     )     )
									self.assertTrue(hasattr(UpperCamelCase_    ,				"""do_convert_rgb"""     )     )

						def      UpperCAmelCase				(				self     )		->    List[Any]:
									snake_case				:    Dict           =	self.image_processor_tester.prepare_dummy_image()

									snake_case				:    List[Any]           =	self.image_processing_class(**self.image_processor_dict     )
									snake_case				:    List[str]           =	2_0_4_8

									snake_case				:    Any           =	image_processor(UpperCamelCase_    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     )
									self.assertTrue(torch.allclose(inputs.flattened_patches.mean()    ,				torch.tensor(0.06_06     )    ,				atol=1e-3    ,				rtol=1e-3     )     )

						def      UpperCAmelCase				(				self     )		->    str:
									# Initialize image_processor
									snake_case				:    List[Any]           =	self.image_processing_class(**self.image_processor_dict     )
									# create random PIL images
									snake_case				:    Optional[int]           =	prepare_image_inputs(self.image_processor_tester    ,				equal_resolution=UpperCamelCase_     )
									for image in image_inputs:
												self.assertIsInstance(UpperCamelCase_    ,				Image.Image     )

									# Test not batched input
									snake_case				:    Dict           =	(
									    (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
									    * self.image_processor_tester.num_channels
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												snake_case				:    Dict           =	image_processor(
												    image_inputs[0]    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(1, max_patch, expected_hidden_dim)    ,				)

												# Test batched
												snake_case				:    List[Any]           =	image_processor(
												    UpperCamelCase_    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)    ,				)

						def      UpperCAmelCase				(				self     )		->    Any:
									# Initialize image_processor
									snake_case				:    List[Any]           =	self.image_processing_class(**self.image_processor_dict     )
									# create random PIL images
									snake_case				:    List[Any]           =	prepare_image_inputs(self.image_processor_tester    ,				equal_resolution=UpperCamelCase_     )
									for image in image_inputs:
												self.assertIsInstance(UpperCamelCase_    ,				Image.Image     )

									# Test not batched input
									snake_case				:    Dict           =	(
									    (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
									    * self.image_processor_tester.num_channels
									) + 2

									snake_case				:    List[Any]           =	True

									for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												with self.assertRaises(UpperCamelCase_     ):
															snake_case				:    List[Any]           =	image_processor(
															    image_inputs[0]    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches

												snake_case				:    List[str]           =	"""Hello"""

												snake_case				:    Union[str, Any]           =	image_processor(
												    image_inputs[0]    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_    ,				header_text=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(1, max_patch, expected_hidden_dim)    ,				)

												# Test batched
												snake_case				:    Optional[int]           =	image_processor(
												    UpperCamelCase_    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_    ,				header_text=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)    ,				)

						def      UpperCAmelCase				(				self     )		->    Tuple:
									# Initialize image_processor
									snake_case				:    Any           =	self.image_processing_class(**self.image_processor_dict     )
									# create random numpy tensors
									snake_case				:    List[str]           =	prepare_image_inputs(self.image_processor_tester    ,				equal_resolution=UpperCamelCase_    ,				numpify=UpperCamelCase_     )
									for image in image_inputs:
												self.assertIsInstance(UpperCamelCase_    ,				np.ndarray     )

									snake_case				:    List[str]           =	(
									    (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
									    * self.image_processor_tester.num_channels
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												snake_case				:    Any           =	image_processor(
												    image_inputs[0]    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(1, max_patch, expected_hidden_dim)    ,				)

												# Test batched
												snake_case				:    Any           =	image_processor(
												    UpperCamelCase_    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)    ,				)

						def      UpperCAmelCase				(				self     )		->    Union[str, Any]:
									# Initialize image_processor
									snake_case				:    Union[str, Any]           =	self.image_processing_class(**self.image_processor_dict     )
									# create random PyTorch tensors
									snake_case				:    Tuple           =	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
									snake_case				:    Union[str, Any]           =	(
									    (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
									    * self.image_processor_tester.num_channels
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												snake_case				:    Optional[int]           =	image_processor(
												    image_inputs[0]    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(1, max_patch, expected_hidden_dim)    ,				)

												# Test batched
												snake_case				:    int           =	image_processor(
												    UpperCamelCase_    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)    ,				)




@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11					,		reason="""`Pix2StructImageProcessor` requires `torch>=1.11.0`."""					,		)
@require_torch
@require_vision
class 						__lowercase		(__lowerCamelCase					,		unittest.TestCase							):




						"""simple docstring"""





						_snake_case												=						PixaStructImageProcessor if is_vision_available() else None

						def      UpperCAmelCase				(				self     )		->    int:
									snake_case				:    int           =	PixaStructImageProcessingTester(self    ,				num_channels=4     )
									snake_case				:    Union[str, Any]           =	3

						@property
						def      UpperCAmelCase				(				self     )		->    Optional[int]:
									return self.image_processor_tester.prepare_image_processor_dict()

						def      UpperCAmelCase				(				self     )		->    List[str]:
									snake_case				:    int           =	self.image_processing_class(**self.image_processor_dict     )
									self.assertTrue(hasattr(UpperCamelCase_    ,				"""do_normalize"""     )     )
									self.assertTrue(hasattr(UpperCamelCase_    ,				"""do_convert_rgb"""     )     )

						def      UpperCAmelCase				(				self     )		->    List[str]:
									# Initialize image_processor
									snake_case				:    List[str]           =	self.image_processing_class(**self.image_processor_dict     )
									# create random PIL images
									snake_case				:    str           =	prepare_image_inputs(self.image_processor_tester    ,				equal_resolution=UpperCamelCase_     )
									for image in image_inputs:
												self.assertIsInstance(UpperCamelCase_    ,				Image.Image     )

									# Test not batched input
									snake_case				:    int           =	(
									    (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
									    * (self.image_processor_tester.num_channels - 1)
									) + 2

									for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												snake_case				:    str           =	image_processor(
												    image_inputs[0]    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(1, max_patch, expected_hidden_dim)    ,				)

												# Test batched
												snake_case				:    List[str]           =	image_processor(
												    UpperCamelCase_    ,				return_tensors="""pt"""    ,				max_patches=UpperCamelCase_     ).flattened_patches
												self.assertEqual(
												    encoded_images.shape    ,				(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)    ,				)











from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand




def 						SCREAMING_SNAKE_CASE__ (   lowercase					)  ->       List[str]:
   return ConvertCommand(
       args.model_type						,args.tf_checkpoint						,args.pytorch_dump_output						,args.config						,args.finetuning_task_name					)


lowerCamelCase	:	Tuple				=   '\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n'




class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      @staticmethod
      def      UpperCAmelCase				(				A     )		->    List[str]:
         snake_case				:    Union[str, Any]           =	parser.add_parser(
             """convert"""    ,				help="""CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints."""    ,				)
         train_parser.add_argument("""--model_type"""    ,				type=A    ,				required=A    ,				help="""Model's type."""     )
         train_parser.add_argument(
             """--tf_checkpoint"""    ,				type=A    ,				required=A    ,				help="""TensorFlow checkpoint path or folder."""     )
         train_parser.add_argument(
             """--pytorch_dump_output"""    ,				type=A    ,				required=A    ,				help="""Path to the PyTorch saved model output."""     )
         train_parser.add_argument("""--config"""    ,				type=A    ,				default=""""""    ,				help="""Configuration file path or folder."""     )
         train_parser.add_argument(
             """--finetuning_task_name"""    ,				type=A    ,				default=A    ,				help="""Optional fine-tuning task name if the TF model was a finetuned model."""    ,				)
         train_parser.set_defaults(func=A     )

      def __init__(				self    ,				A    ,				A    ,				A    ,				A    ,				A    ,				*A    ,				)		->    List[Any]:
         snake_case				:    Any           =	logging.get_logger("""transformers-cli/converting"""     )

         self._logger.info(f"""Loading model {model_type}"""     )
         snake_case				:    int           =	model_type
         snake_case				:    Any           =	tf_checkpoint
         snake_case				:    int           =	pytorch_dump_output
         snake_case				:    List[str]           =	config
         snake_case				:    Tuple           =	finetuning_task_name

      def      UpperCAmelCase				(				self     )		->    int:
         if self._model_type == "albert":
            try:
               from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
                   convert_tf_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "bert":
            try:
               from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
                   convert_tf_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "funnel":
            try:
               from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
                   convert_tf_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "t5":
            try:
               from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "gpt":
            from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
                convert_openai_checkpoint_to_pytorch,
            )

            convert_openai_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "transfo_xl":
            try:
               from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
                   convert_transfo_xl_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            if "ckpt" in self._tf_checkpoint.lower():
               snake_case				:    List[Any]           =	self._tf_checkpoint
               snake_case				:    Tuple           =	""""""
            else:
               snake_case				:    Tuple           =	self._tf_checkpoint
               snake_case				:    Tuple           =	""""""
            convert_transfo_xl_checkpoint_to_pytorch(
                A    ,				self._config    ,				self._pytorch_dump_output    ,				A     )
         elif self._model_type == "gpt2":
            try:
               from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
                   convert_gpta_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "xlnet":
            try:
               from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
                   convert_xlnet_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_xlnet_checkpoint_to_pytorch(
                self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output    ,				self._finetuning_task_name     )
         elif self._model_type == "xlm":
            from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
                convert_xlm_checkpoint_to_pytorch,
            )

            convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._pytorch_dump_output     )
         elif self._model_type == "lxmert":
            from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
                convert_lxmert_checkpoint_to_pytorch,
            )

            convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._pytorch_dump_output     )
         elif self._model_type == "rembert":
            from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
                convert_rembert_tf_checkpoint_to_pytorch,
            )

            convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         else:
            raise ValueError(
                """--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]"""     )





