"""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 							snake_case			:




					"""simple docstring"""

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







					def 						__lowerCAmelCase		(							self  :      Union[str, Any]     ):
									UpperCAmelCase__			 =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

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







					def 						__lowerCAmelCase		(							self  :      str     ):
									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 						__lowerCAmelCase		(							self  :      int       ,lowerCamelCase__  :      Tuple       ,lowerCamelCase__  :      int       ,lowerCamelCase__  :      Optional[int]     ):
									UpperCAmelCase__			 =				EfficientNetModel(config=lowerCamelCase__     )
									model.to(lowerCamelCase__     )
									model.eval()
									UpperCAmelCase__			 =				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 						__lowerCAmelCase		(							self  :      Union[str, Any]       ,lowerCamelCase__  :      int       ,lowerCamelCase__  :      str       ,lowerCamelCase__  :      Tuple     ):
									UpperCAmelCase__			 =				EfficientNetForImageClassification(lowerCamelCase__     )
									model.to(lowerCamelCase__     )
									model.eval()
									UpperCAmelCase__			 =				model(lowerCamelCase__       ,labels=lowerCamelCase__     )
									self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)     )







					def 						__lowerCAmelCase		(							self  :      Dict     ):
									UpperCAmelCase__			 =				self.prepare_config_and_inputs()
									UpperCAmelCase__ ,      UpperCAmelCase__ ,      UpperCAmelCase__			 =				config_and_inputs
									UpperCAmelCase__			 =				{'pixel_values': pixel_values}
									return config, inputs_dict






@require_torch
class 							snake_case			(					__UpperCAmelCase ,       __UpperCAmelCase ,       unittest.TestCase							):




					"""simple docstring"""

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

					snake_case__							       =					False
					snake_case__							       =					False
					snake_case__							       =					False
					snake_case__							       =					False
					snake_case__							       =					False







					def 						__lowerCAmelCase		(							self  :      str     ):
									UpperCAmelCase__			 =				EfficientNetModelTester(self     )
									UpperCAmelCase__			 =				ConfigTester(
									    self       ,config_class=lowerCamelCase__       ,has_text_modality=lowerCamelCase__       ,hidden_size=37     )







					def 						__lowerCAmelCase		(							self  :      Any     ):
									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 						__lowerCAmelCase		(							self  :      List[Any]     ):
									return







					@unittest.skip(reason='EfficientNet does not use inputs_embeds'     )
					def 						__lowerCAmelCase		(							self  :      Union[str, Any]     ):
									pass







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







					@unittest.skip(reason='EfficientNet does not use feedforward chunking'     )
					def 						__lowerCAmelCase		(							self  :      Any     ):
									pass







					def 						__lowerCAmelCase		(							self  :      Tuple     ):
									UpperCAmelCase__ ,      UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs_for_common()

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

													UpperCAmelCase__			 =				['pixel_values']
													self.assertListEqual(arg_names[:1]       ,lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      List[str]     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      List[str]     ):
									def check_hidden_states_output(lowerCamelCase__  :      int       ,lowerCamelCase__  :      str       ,lowerCamelCase__  :      List[Any]     ):
													UpperCAmelCase__			 =				model_class(lowerCamelCase__     )
													model.to(lowerCamelCase__     )
													model.eval()

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

													UpperCAmelCase__			 =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
													UpperCAmelCase__			 =				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]       ,)

									UpperCAmelCase__ ,      UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs_for_common()

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

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

													check_hidden_states_output(lowerCamelCase__       ,lowerCamelCase__       ,lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      Union[str, Any]     ):
									UpperCAmelCase__			 =				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 						__lowerCAmelCase		(							self  :      Any     ):
									pass







					@slow
					def 						__lowerCAmelCase		(							self  :      Optional[int]     ):
									for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCAmelCase__			 =				EfficientNetModel.from_pretrained(lowerCamelCase__     )
													self.assertIsNotNone(lowerCamelCase__     )







					@is_pipeline_test
					@require_vision
					@slow
					def 						__lowerCAmelCase		(							self  :      List[str]     ):
									super().test_pipeline_image_classification()






def     a_		(   ):
				UpperCAmelCase__			 =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
				return image







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




					"""simple docstring"""

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







					@slow
					def 						__lowerCAmelCase		(							self  :      int     ):
									UpperCAmelCase__			 =				EfficientNetForImageClassification.from_pretrained('google/efficientnet-b7'     ).to(lowerCamelCase__     )

									UpperCAmelCase__			 =				self.default_image_processor
									UpperCAmelCase__			 =				prepare_img()
									UpperCAmelCase__			 =				image_processor(images=lowerCamelCase__       ,return_tensors='pt'     ).to(lowerCamelCase__     )

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

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

									UpperCAmelCase__			 =				torch.tensor([0.0_0_0_1, 0.0_0_0_2, 0.0_0_0_2]     ).to(lowerCamelCase__     )
									self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,lowerCamelCase__       ,atol=1e-4     )     )




"""simple docstring"""






from sklearn.neural_network import MLPClassifier

lowerCAmelCase__    :    Any			=      [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]]
lowerCAmelCase__    :    Optional[int]			=      [0, 1, 0, 0]


lowerCAmelCase__    :    Dict			=      MLPClassifier(
    solver='lbfgs', alpha=1E-5, hidden_layer_sizes=(5, 2), random_state=1
)

clf.fit(X, y)


lowerCAmelCase__    :    Optional[int]			=      [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]]
lowerCAmelCase__    :    Union[str, Any]			=      clf.predict(test)



def     a_		(   lowerCamelCase			):
				return list(lowerCamelCase			)


if __name__ == "__main__":
						import doctest

						doctest.testmod()



