





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 SCREAMING_SNAKE_CASE_			:





					"""simple docstring"""


					def __init__(			self						,	_lowerCAmelCase						,	_lowerCAmelCase=13						,	_lowerCAmelCase=32						,	_lowerCAmelCase=3						,	_lowerCAmelCase=[3, 3, 5]						,	_lowerCAmelCase=[32, 16, 24]						,	_lowerCAmelCase=[16, 24, 40]						,	_lowerCAmelCase=[1, 1, 2]						,	_lowerCAmelCase=[1, 1, 2]						,	_lowerCAmelCase=[1, 6, 6]						,	_lowerCAmelCase=True						,	_lowerCAmelCase=True						,	_lowerCAmelCase=37						,	_lowerCAmelCase="gelu"						,	_lowerCAmelCase=10						,	):
										lowerCamelCase__      =							parent
										lowerCamelCase__      =							batch_size
										lowerCamelCase__      =							image_size
										lowerCamelCase__      =							num_channels
										lowerCamelCase__      =							kernel_sizes
										lowerCamelCase__      =							in_channels
										lowerCamelCase__      =							out_channels
										lowerCamelCase__      =							strides
										lowerCamelCase__      =							num_block_repeats
										lowerCamelCase__      =							expand_ratios
										lowerCamelCase__      =							is_training
										lowerCamelCase__      =							hidden_act
										lowerCamelCase__      =							num_labels
										lowerCamelCase__      =							use_labels





					def        __magic_name__			(			self						):
										lowerCamelCase__      =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

										lowerCamelCase__      =							None
										if self.use_labels:
															lowerCamelCase__      =							ids_tensor([self.batch_size]						,	self.num_labels						)

										lowerCamelCase__      =							self.get_config()
										return config, pixel_values, labels





					def        __magic_name__			(			self						):
										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        __magic_name__			(			self						,	_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						):
										lowerCamelCase__      =							EfficientNetModel(config=_lowerCAmelCase						)
										model.to(_lowerCAmelCase						)
										model.eval()
										lowerCamelCase__      =							model(_lowerCAmelCase						)
										# 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        __magic_name__			(			self						,	_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						):
										lowerCamelCase__      =							EfficientNetForImageClassification(_lowerCAmelCase						)
										model.to(_lowerCAmelCase						)
										model.eval()
										lowerCamelCase__      =							model(_lowerCAmelCase						,	labels=_lowerCAmelCase						)
										self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)						)






					def        __magic_name__			(			self						):
										lowerCamelCase__      =							self.prepare_config_and_inputs()
										lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__      =							config_and_inputs
										lowerCamelCase__      =							{"pixel_values": pixel_values}
										return config, inputs_dict

@require_torch
class SCREAMING_SNAKE_CASE_			(       lowercase_				,							lowercase_				,							unittest.TestCase ):





					"""simple docstring"""


					A__								=     (EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
					A__								=     (
					    {"feature-extraction": EfficientNetModel, "image-classification": EfficientNetForImageClassification}
					    if is_torch_available()
					    else {}
					)

					A__								=     False
					A__								=     False
					A__								=     False
					A__								=     False
					A__								=     False





					def        __magic_name__			(			self						):
										lowerCamelCase__      =							EfficientNetModelTester(self						)
										lowerCamelCase__      =							ConfigTester(
										    self						,	config_class=_lowerCAmelCase						,	has_text_modality=_lowerCAmelCase						,	hidden_size=37						)





					def        __magic_name__			(			self						):
										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        __magic_name__			(			self						):
										return





					@unittest.skip(reason="EfficientNet does not use inputs_embeds"						)
					def        __magic_name__			(			self						):
										pass





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





					@unittest.skip(reason="EfficientNet does not use feedforward chunking"						)
					def        __magic_name__			(			self						):
										pass





					def        __magic_name__			(			self						):
										lowerCamelCase__     ,		lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs_for_common()

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

															lowerCamelCase__      =							["pixel_values"]
															self.assertListEqual(arg_names[:1]						,	_lowerCAmelCase						)





					def        __magic_name__			(			self						):
										lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCAmelCase						)





					def        __magic_name__			(			self						):
										def check_hidden_states_output(_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						):
															lowerCamelCase__      =							model_class(_lowerCAmelCase						)
															model.to(_lowerCAmelCase						)
															model.eval()

															with torch.no_grad():
																				lowerCamelCase__      =							model(**self._prepare_for_class(_lowerCAmelCase						,	_lowerCAmelCase						)						)

															lowerCamelCase__      =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
															lowerCamelCase__      =							sum(config.num_block_repeats						) * 4
															self.assertEqual(len(_lowerCAmelCase						)						,	_lowerCAmelCase						)

															# 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]						,	)

										lowerCamelCase__     ,		lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
															lowerCamelCase__      =							True
															check_hidden_states_output(_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															lowerCamelCase__      =							True

															check_hidden_states_output(_lowerCAmelCase						,	_lowerCAmelCase						,	_lowerCAmelCase						)





					def        __magic_name__			(			self						):
										lowerCamelCase__      =							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase						)





					@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
					def        __magic_name__			(			self						):
										pass





					@slow
					def        __magic_name__			(			self						):
										for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCamelCase__      =							EfficientNetModel.from_pretrained(_lowerCAmelCase						)
															self.assertIsNotNone(_lowerCAmelCase						)






					@is_pipeline_test
					@require_vision
					@slow
					def        __magic_name__			(			self						):
										super().test_pipeline_image_classification()





def  __UpperCamelCase   (     )			->Optional[int]:
					lowerCamelCase__      =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
					return image







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





					"""simple docstring"""


					@cached_property
					def        __magic_name__			(			self						):
										return AutoImageProcessor.from_pretrained("google/efficientnet-b7"						) if is_vision_available() else None






					@slow
					def        __magic_name__			(			self						):
										lowerCamelCase__      =							EfficientNetForImageClassification.from_pretrained("google/efficientnet-b7"						).to(_lowerCAmelCase						)

										lowerCamelCase__      =							self.default_image_processor
										lowerCamelCase__      =							prepare_img()
										lowerCamelCase__      =							image_processor(images=_lowerCAmelCase						,	return_tensors="pt"						).to(_lowerCAmelCase						)

										# forward pass
										with torch.no_grad():
															lowerCamelCase__      =							model(**_lowerCAmelCase						)

										# verify the logits
										lowerCamelCase__      =							torch.Size((1, 1000)						)
										self.assertEqual(outputs.logits.shape						,	_lowerCAmelCase						)

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








import enum
import inspect
import warnings
from functools import wraps
from typing import Callable, Optional

from .logging import get_logger


A_        =  set()
A_        =  get_logger(__name__)





def  __UpperCamelCase   (     a = None)			->str:

					def decorator(a):
										global _emitted_deprecation_warnings

										if inspect.isclass(a):
															lowerCamelCase__      =							deprecated_class_or_function.__init__
															lowerCamelCase__      =							deprecated_class_or_function.__name__
										else:
															lowerCamelCase__      =							deprecated_class_or_function
															lowerCamelCase__      =							deprecated_function.__name__
															# Support deprecating __init__ class method: class name instead
															lowerCamelCase__      =							name if name != "__init__" else deprecated_function.__qualname__.split(".")[-2]

										lowerCamelCase__      =							(
										    f"{name} is deprecated and will be removed in the next major version of datasets." + f" {help_message}"
										    if help_message
										    else ""
										)

										@wraps(a)
										def wrapper(*a,  **a):
															lowerCamelCase__      =							hash(a)
															if func_hash not in _emitted_deprecation_warnings:
																				warnings.warn(a,  category=a,  stacklevel=2)
																				_emitted_deprecation_warnings.add(a)
															return deprecated_function(*a,  **a)

										lowerCamelCase__      =							"deprecated"

										if inspect.isclass(a):
															lowerCamelCase__      =							wrapper
															return deprecated_class_or_function
										else:
															return wrapper

					return decorator

class SCREAMING_SNAKE_CASE_			(       enum.EnumMeta ):





					"""simple docstring"""


					def __getattribute__(			cls						,	_lowerCAmelCase						):
										lowerCamelCase__      =							super().__getattribute__(_lowerCAmelCase						)
										if isinstance(_lowerCAmelCase						,	enum.Enum						) and obj._on_access:
															obj._on_access()
										return obj





					def __getitem__(			cls						,	_lowerCAmelCase						):
										lowerCamelCase__      =							super().__getitem__(_lowerCAmelCase						)
										if member._on_access:
															member._on_access()
										return member






					def __call__(			cls						,	_lowerCAmelCase						,	_lowerCAmelCase=None						,	*, _lowerCAmelCase=None						,	_lowerCAmelCase=None						,	_lowerCAmelCase=None						,	_lowerCAmelCase=1						):
										lowerCamelCase__      =							super().__call__(_lowerCAmelCase						,	_lowerCAmelCase						,	module=_lowerCAmelCase						,	qualname=_lowerCAmelCase						,	type=_lowerCAmelCase						,	start=_lowerCAmelCase						)
										if isinstance(_lowerCAmelCase						,	enum.Enum						) and obj._on_access:
															obj._on_access()
										return obj

class SCREAMING_SNAKE_CASE_			(       enum.Enum				,							metaclass=lowercase_ ):





					"""simple docstring"""


					def __new__(			cls						,	_lowerCAmelCase						):
										lowerCamelCase__      =							object.__new__(cls						)
										lowerCamelCase__      =							value
										lowerCamelCase__      =							member.deprecate
										return member





					@property
					def        __magic_name__			(			self						):
										return ""






					def        __magic_name__			(			self						):
										lowerCamelCase__      =							F" {self.help_message}" if self.help_message else ""
										warnings.warn(
										    F"'{self.__objclass__.__name__}' is deprecated and will be removed in the next major version of datasets."
										    + help_message						,	_lowerCAmelCase						,	stacklevel=3						,	)

