




'''simple docstring'''







import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import EfficientNetForImageClassification, EfficientNetModel
						from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor







class 				_a   :





					'''simple docstring'''



					def __init__(							self       ,__a       ,__a=13       ,__a=32       ,__a=3       ,__a=[3, 3, 5]       ,__a=[32, 16, 24]       ,__a=[16, 24, 40]       ,__a=[1, 1, 2]       ,__a=[1, 1, 2]       ,__a=[1, 6, 6]       ,__a=True       ,__a=True       ,__a=37       ,__a="gelu"       ,__a=10       ,)			->							Optional[int]:
										snake_case :			Union[str, Any]					   = parent
										snake_case :			Dict					   = batch_size
										snake_case :			str					   = image_size
										snake_case :			List[str]					   = num_channels
										snake_case :			Optional[Any]					   = kernel_sizes
										snake_case :			Union[str, Any]					   = in_channels
										snake_case :			List[Any]					   = out_channels
										snake_case :			Optional[int]					   = strides
										snake_case :			Dict					   = num_block_repeats
										snake_case :			str					   = expand_ratios
										snake_case :			Optional[Any]					   = is_training
										snake_case :			Any					   = hidden_act
										snake_case :			Dict					   = num_labels
										snake_case :			List[str]					   = use_labels







					def  snake_case_			(							self       )			->							Optional[Any]:
										snake_case :			Tuple					   = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

										snake_case :			Any					   = None
										if self.use_labels:
															snake_case :			List[Any]					   = ids_tensor([self.batch_size]       ,self.num_labels       )

										snake_case :			str					   = self.get_config()
										return config, pixel_values, labels







					def  snake_case_			(							self       )			->							int:
										return EfficientNetConfig(
										    num_channels=self.num_channels       ,kernel_sizes=self.kernel_sizes       ,in_channels=self.in_channels       ,out_channels=self.out_channels       ,strides=self.strides       ,num_block_repeats=self.num_block_repeats       ,expand_ratios=self.expand_ratios       ,hidden_act=self.hidden_act       ,num_labels=self.num_labels       ,)







					def  snake_case_			(							self       ,__a       ,__a       ,__a       )			->							str:
										snake_case :			int					   = EfficientNetModel(config=__a       )
										model.to(__a       )
										model.eval()
										snake_case :			Optional[int]					   = model(__a       )
										# expected last hidden states: B, C, H // 4, W // 4
										self.parent.assertEqual(
										    result.last_hidden_state.shape       ,(self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)       ,)







					def  snake_case_			(							self       ,__a       ,__a       ,__a       )			->							Optional[Any]:
										snake_case :			Tuple					   = EfficientNetForImageClassification(__a       )
										model.to(__a       )
										model.eval()
										snake_case :			Tuple					   = model(__a       ,labels=__a       )
										self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)       )







					def  snake_case_			(							self       )			->							Tuple:
										snake_case :			Optional[Any]					   = self.prepare_config_and_inputs()
										snake_case :			Union[str, Any]					   = config_and_inputs
										snake_case :			Union[str, Any]					   = {"""pixel_values""": pixel_values}
										return config, inputs_dict







@require_torch
class 				_a   (a__,		a__,		unittest.TestCase  ):





					'''simple docstring'''



					lowerCAmelCase_     :    List[str]									=				(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
					lowerCAmelCase_     :    Dict									=				(
					    {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
					    if is_torch_available()
					    else {}
					)

					lowerCAmelCase_     :    Optional[int]									=				False
					lowerCAmelCase_     :    Optional[Any]									=				False
					lowerCAmelCase_     :    Tuple									=				False
					lowerCAmelCase_     :    List[Any]									=				False
					lowerCAmelCase_     :    Tuple									=				False







					def  snake_case_			(							self       )			->							int:
										snake_case :			List[Any]					   = EfficientNetModelTester(self       )
										snake_case :			Union[str, Any]					   = ConfigTester(
										    self       ,config_class=__a       ,has_text_modality=__a       ,hidden_size=37       )







					def  snake_case_			(							self       )			->							Optional[int]:
										self.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()







					def  snake_case_			(							self       )			->							Tuple:
										return







					@unittest.skip(reason="""EfficientNet does not use inputs_embeds"""       )
					def  snake_case_			(							self       )			->							List[str]:
										pass







					@unittest.skip(reason="""EfficientNet does not support input and output embeddings"""       )
					def  snake_case_			(							self       )			->							Tuple:
										pass







					@unittest.skip(reason="""EfficientNet does not use feedforward chunking"""       )
					def  snake_case_			(							self       )			->							Dict:
										pass







					def  snake_case_			(							self       )			->							Optional[Any]:
										snake_case :			Optional[Any]					   = self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
															snake_case :			Tuple					   = model_class(__a       )
															snake_case :			List[str]					   = inspect.signature(model.forward       )
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															snake_case :			List[str]					   = [*signature.parameters.keys()]

															snake_case :			str					   = ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]       ,__a       )







					def  snake_case_			(							self       )			->							Tuple:
										snake_case :			List[str]					   = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__a       )







					def  snake_case_			(							self       )			->							Tuple:
										def check_hidden_states_output(__a       ,__a       ,__a       ):
															snake_case :			Tuple					   = model_class(__a       )
															model.to(__a       )
															model.eval()

															with torch.no_grad():
																				snake_case :			Optional[int]					   = model(**self._prepare_for_class(__a       ,__a       )       )

															snake_case :			int					   = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
															snake_case :			str					   = sum(config.num_block_repeats       ) * 4
															self.assertEqual(len(__a       )       ,__a       )

															# EfficientNet's feature maps are of shape (batch_size, num_channels, height, width)
															self.assertListEqual(
															    list(hidden_states[0].shape[-2:]       )       ,[self.model_tester.image_size // 2, self.model_tester.image_size // 2]       ,)

										snake_case :			List[str]					   = self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
															snake_case :			int					   = True
															check_hidden_states_output(__a       ,__a       ,__a       )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															snake_case :			Dict					   = True

															check_hidden_states_output(__a       ,__a       ,__a       )







					def  snake_case_			(							self       )			->							List[str]:
										snake_case :			str					   = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__a       )







					@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""       )
					def  snake_case_			(							self       )			->							Optional[int]:
										pass







					@slow
					def  snake_case_			(							self       )			->							List[Any]:
										for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case :			Optional[Any]					   = EfficientNetModel.from_pretrained(__a       )
															self.assertIsNotNone(__a       )







					@is_pipeline_test
					@require_vision
					@slow
					def  snake_case_			(							self       )			->							str:
										super().test_pipeline_image_classification()



def 					lowerCamelCase__							(			):
					snake_case :			Union[str, Any]					   = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
					return image







@require_torch
@require_vision
class 				_a   (unittest.TestCase  ):





					'''simple docstring'''



					@cached_property
					def  snake_case_			(							self       )			->							List[Any]:
										return AutoImageProcessor.from_pretrained("""google/efficientnet-b7"""       ) if is_vision_available() else None







					@slow
					def  snake_case_			(							self       )			->							List[Any]:
										snake_case :			Optional[Any]					   = EfficientNetForImageClassification.from_pretrained("""google/efficientnet-b7"""       ).to(__a       )

										snake_case :			int					   = self.default_image_processor
										snake_case :			Optional[Any]					   = prepare_img()
										snake_case :			str					   = image_processor(images=__a       ,return_tensors="""pt"""       ).to(__a       )

										# forward pass
										with torch.no_grad():
															snake_case :			Optional[Any]					   = model(**__a       )

										# verify the logits
										snake_case :			Any					   = torch.Size((1, 1_000)       )
										self.assertEqual(outputs.logits.shape       ,__a       )

										snake_case :			int					   = torch.tensor([0.0_001, 0.0_002, 0.0_002]       ).to(__a       )
										self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,__a       ,atol=1E-4       )       )









'''simple docstring'''







def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 9 / 5) + 32    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round(float(__lowercase							) + 273.15    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 9 / 5) + 491.67    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) - 32) * 5 / 9    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round(((float(__lowercase							) - 32) * 5 / 9) + 273.15    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round(float(__lowercase							) + 459.67    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round(float(__lowercase							) - 273.15    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round(((float(__lowercase							) - 273.15) * 9 / 5) + 32    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 9 / 5)    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) - 491.67) * 5 / 9    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round(float(__lowercase							) - 459.67    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 5 / 9)    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 1.25 + 273.15)    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 2.25 + 32)    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 1.25)    ,      __lowercase							)



def 					lowerCamelCase__							(			__lowercase    ,      __lowercase = 2							):
     return round((float(__lowercase							) * 2.25 + 32 + 459.67)    ,      __lowercase							)


if __name__ == "__main__":
      import doctest

      doctest.testmod()



