




'''simple docstring'''



import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402







class 		lowercase_   (			unittest.TestCase       ):


   """simple docstring"""

   def 							lowerCAmelCase_      (   self :     Any   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				0



   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"   )
      self.assertIsInstance(__lowerCamelCase  ,				__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      with tempfile.TemporaryDirectory() as tmpdirname:
         _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "preprocessor_config.json"
         _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "config.json"
         json.dump(
             {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}  ,				open(__lowerCamelCase  ,				"w"   )  ,				)
         json.dump({"model_type": "clip"}  ,				open(__lowerCamelCase  ,				"w"   )   )

         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase   )
         self.assertIsInstance(__lowerCamelCase  ,				__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Any   ):

      """simple docstring"""
      # Ensure we can load the image processor from the feature extractor config
      with tempfile.TemporaryDirectory() as tmpdirname:
         _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "preprocessor_config.json"
         _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "config.json"
         json.dump(
             {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}  ,				open(__lowerCamelCase  ,				"w"   )  ,				)
         json.dump({"model_type": "clip"}  ,				open(__lowerCamelCase  ,				"w"   )   )

         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase   )
         self.assertIsInstance(__lowerCamelCase  ,				__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Optional[int]   ):

      """simple docstring"""
      with tempfile.TemporaryDirectory() as tmpdirname:
         _SCREAMING_SNAKE_CASE	=				CLIPConfig()

         # Create a dummy config file with image_proceesor_type
         _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "preprocessor_config.json"
         _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "config.json"
         json.dump(
             {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}  ,				open(__lowerCamelCase  ,				"w"   )  ,				)
         json.dump({"model_type": "clip"}  ,				open(__lowerCamelCase  ,				"w"   )   )

         # remove image_processor_type to make sure config.json alone is enough to load image processor locally
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase   ).to_dict()

         config_dict.pop("image_processor_type"   )
         _SCREAMING_SNAKE_CASE	=				CLIPImageProcessor(**__lowerCamelCase   )

         # save in new folder
         model_config.save_pretrained(__lowerCamelCase   )
         config.save_pretrained(__lowerCamelCase   )

         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase   )

         # make sure private variable is not incorrectly saved
         _SCREAMING_SNAKE_CASE	=				json.loads(config.to_json_string()   )
         self.assertTrue("_processor_class" not in dict_as_saved   )

      self.assertIsInstance(__lowerCamelCase  ,				__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      with tempfile.TemporaryDirectory() as tmpdirname:
         _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "preprocessor_config.json"
         json.dump(
             {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}  ,				open(__lowerCamelCase  ,				"w"   )  ,				)

         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase   )
         self.assertIsInstance(__lowerCamelCase  ,				__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      with self.assertRaisesRegex(
          __lowerCamelCase  ,				"clip-base is not a local folder and is not a valid model identifier"   ):
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained("clip-base"   )



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      with self.assertRaisesRegex(
          __lowerCamelCase  ,				r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"   ):
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase  ,				revision="aaaaaa"   )



   def 							lowerCAmelCase_      (   self :     List[str]   ):

      """simple docstring"""
      with self.assertRaisesRegex(
          __lowerCamelCase  ,				"hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."  ,				):
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model"   )



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      # If remote code is not set, we will time out when asking whether to load the model.
      with self.assertRaises(__lowerCamelCase   ):
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"   )
      # If remote code is disabled, we can't load this config.
      with self.assertRaises(__lowerCamelCase   ):
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(
             "hf-internal-testing/test_dynamic_image_processor"  ,				trust_remote_code=__lowerCamelCase   )

      _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(
          "hf-internal-testing/test_dynamic_image_processor"  ,				trust_remote_code=__lowerCamelCase   )
      self.assertEqual(image_processor.__class__.__name__  ,				"NewImageProcessor"   )

      # Test image processor can be reloaded.
      with tempfile.TemporaryDirectory() as tmp_dir:
         image_processor.save_pretrained(__lowerCamelCase   )
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase  ,				trust_remote_code=__lowerCamelCase   )
      self.assertEqual(reloaded_image_processor.__class__.__name__  ,				"NewImageProcessor"   )



   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      try:
         AutoConfig.register("custom"  ,				__lowerCamelCase   )
         AutoImageProcessor.register(__lowerCamelCase  ,				__lowerCamelCase   )
         # Trying to register something existing in the Transformers library will raise an error
         with self.assertRaises(__lowerCamelCase   ):
            AutoImageProcessor.register(__lowerCamelCase  ,				__lowerCamelCase   )

         with tempfile.TemporaryDirectory() as tmpdirname:
            _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "preprocessor_config.json"
            _SCREAMING_SNAKE_CASE	=				Path(__lowerCamelCase   ) / "config.json"
            json.dump(
                {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}  ,				open(__lowerCamelCase  ,				"w"   )  ,				)
            json.dump({"model_type": "clip"}  ,				open(__lowerCamelCase  ,				"w"   )   )

            _SCREAMING_SNAKE_CASE	=				CustomImageProcessor.from_pretrained(__lowerCamelCase   )

         # Now that the config is registered, it can be used as any other config with the auto-API
         with tempfile.TemporaryDirectory() as tmp_dir:
            image_processor.save_pretrained(__lowerCamelCase   )
            _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(__lowerCamelCase   )
            self.assertIsInstance(__lowerCamelCase  ,				__lowerCamelCase   )

      finally:
         if "custom" in CONFIG_MAPPING._extra_content:
            del CONFIG_MAPPING._extra_content["custom"]
         if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
            del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]



   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""







      class 		lowercase_   (			A       ):


         """simple docstring"""

         lowerCamelCase_      =						True

      try:
         AutoConfig.register("custom"  ,				__lowerCamelCase   )
         AutoImageProcessor.register(__lowerCamelCase  ,				__lowerCamelCase   )
         # If remote code is not set, the default is to use local
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"   )
         self.assertEqual(image_processor.__class__.__name__  ,				"NewImageProcessor"   )
         self.assertTrue(image_processor.is_local   )

         # If remote code is disabled, we load the local one.
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(
             "hf-internal-testing/test_dynamic_image_processor"  ,				trust_remote_code=__lowerCamelCase   )
         self.assertEqual(image_processor.__class__.__name__  ,				"NewImageProcessor"   )
         self.assertTrue(image_processor.is_local   )

         # If remote is enabled, we load from the Hub
         _SCREAMING_SNAKE_CASE	=				AutoImageProcessor.from_pretrained(
             "hf-internal-testing/test_dynamic_image_processor"  ,				trust_remote_code=__lowerCamelCase   )
         self.assertEqual(image_processor.__class__.__name__  ,				"NewImageProcessor"   )
         self.assertTrue(not hasattr(__lowerCamelCase  ,				"is_local"   )   )

      finally:
         if "custom" in CONFIG_MAPPING._extra_content:
            del CONFIG_MAPPING._extra_content["custom"]
         if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
            del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]







'''simple docstring'''



from collections import OrderedDict
from typing import List, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_			=						logging.get_logger(__name__)

lowerCamelCase_			=						{
    'google/efficientnet-b7': 'https://huggingface.co/google/efficientnet-b7/resolve/main/config.json',
}







class 		lowercase_   (			A       ):


   """simple docstring"""

   lowerCamelCase_      =						'''efficientnet'''



   def __init__(   self :     Optional[Any]  ,				__lowerCamelCase :     int = 3  ,				__lowerCamelCase :     int = 6_0_0  ,				__lowerCamelCase :     float = 2.0  ,				__lowerCamelCase :     float = 3.1  ,				__lowerCamelCase :     int = 8  ,				__lowerCamelCase :     List[int] = [3, 3, 5, 3, 5, 5, 3]  ,				__lowerCamelCase :     List[int] = [3_2, 1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2]  ,				__lowerCamelCase :     List[int] = [1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2, 3_2_0]  ,				__lowerCamelCase :     List[int] = []  ,				__lowerCamelCase :     List[int] = [1, 2, 2, 2, 1, 2, 1]  ,				__lowerCamelCase :     List[int] = [1, 2, 2, 3, 3, 4, 1]  ,				__lowerCamelCase :     List[int] = [1, 6, 6, 6, 6, 6, 6]  ,				__lowerCamelCase :     float = 0.2_5  ,				__lowerCamelCase :     str = "swish"  ,				__lowerCamelCase :     int = 2_5_6_0  ,				__lowerCamelCase :     str = "mean"  ,				__lowerCamelCase :     float = 0.0_2  ,				__lowerCamelCase :     float = 0.0_0_1  ,				__lowerCamelCase :     float = 0.9_9  ,				__lowerCamelCase :     float = 0.5  ,				__lowerCamelCase :     float = 0.2  ,				**__lowerCamelCase :     Tuple  ,				):

      """simple docstring"""
      super().__init__(**__lowerCamelCase   )

      _SCREAMING_SNAKE_CASE	=				num_channels
      _SCREAMING_SNAKE_CASE	=				image_size
      _SCREAMING_SNAKE_CASE	=				width_coefficient
      _SCREAMING_SNAKE_CASE	=				depth_coefficient
      _SCREAMING_SNAKE_CASE	=				depth_divisor
      _SCREAMING_SNAKE_CASE	=				kernel_sizes
      _SCREAMING_SNAKE_CASE	=				in_channels
      _SCREAMING_SNAKE_CASE	=				out_channels
      _SCREAMING_SNAKE_CASE	=				depthwise_padding
      _SCREAMING_SNAKE_CASE	=				strides
      _SCREAMING_SNAKE_CASE	=				num_block_repeats
      _SCREAMING_SNAKE_CASE	=				expand_ratios
      _SCREAMING_SNAKE_CASE	=				squeeze_expansion_ratio
      _SCREAMING_SNAKE_CASE	=				hidden_act
      _SCREAMING_SNAKE_CASE	=				hidden_dim
      _SCREAMING_SNAKE_CASE	=				pooling_type
      _SCREAMING_SNAKE_CASE	=				initializer_range
      _SCREAMING_SNAKE_CASE	=				batch_norm_eps
      _SCREAMING_SNAKE_CASE	=				batch_norm_momentum
      _SCREAMING_SNAKE_CASE	=				dropout_rate
      _SCREAMING_SNAKE_CASE	=				drop_connect_rate
      _SCREAMING_SNAKE_CASE	=				sum(__lowerCamelCase   ) * 4







class 		lowercase_   (			A       ):


   """simple docstring"""

   lowerCamelCase_      =						version.parse('''1.11'''       )



   @property
   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      return OrderedDict(
          [
              ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
          ]   )



   @property
   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      return 1e-5

