
'''simple docstring'''





import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

      if is_vision_available():
            from transformers import MaskFormerImageProcessor

if is_vision_available():
      from PIL import Image






class 							UpperCAmelCase_							:

 '''simple docstring'''







 def __init__(					self		:  int			,				snake_case__		:  Any			,				snake_case__		:  List[str]=2			,				snake_case__		:  Dict=True			,				snake_case__		:  int=False			,				snake_case__		:  Union[str, Any]=10			,				snake_case__		:  Dict=3			,				snake_case__		:  Any=32 * 4			,				snake_case__		:  Tuple=32 * 6			,				snake_case__		:  Optional[Any]=4			,				snake_case__		:  Any=32			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Union[str, Any]				       =							parent
   UpperCAmelCase__      :					Tuple				       =							batch_size
   UpperCAmelCase__      :					Any				       =							is_training
   UpperCAmelCase__      :					int				       =							use_auxiliary_loss
   UpperCAmelCase__      :					List[str]				       =							num_queries
   UpperCAmelCase__      :					List[str]				       =							num_channels
   UpperCAmelCase__      :					Optional[int]				       =							min_size
   UpperCAmelCase__      :					Any				       =							max_size
   UpperCAmelCase__      :					Optional[int]				       =							num_labels
   UpperCAmelCase__      :					str				       =							mask_feature_size







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to(
       snake_case__ )

   UpperCAmelCase__      :					int				       =							torch.ones([self.batch_size, self.min_size, self.max_size]			,				device=snake_case__ )

   UpperCAmelCase__      :					Dict				       =							(
       torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]			,				device=snake_case__ ) > 0.5
   ).float()
   UpperCAmelCase__      :					int				       =							(torch.rand((self.batch_size, self.num_labels)			,				device=snake_case__ ) > 0.5).long()

   UpperCAmelCase__      :					Any				       =							self.get_config()
   return config, pixel_values, pixel_mask, mask_labels, class_labels







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   return MaskFormerConfig.from_backbone_and_decoder_configs(
       backbone_config=SwinConfig(
           depths=[1, 1, 1, 1]			,				)			,				decoder_config=DetrConfig(
           decoder_ffn_dim=1_28			,				num_queries=self.num_queries			,				decoder_attention_heads=2			,				d_model=self.mask_feature_size			,				)			,				mask_feature_size=self.mask_feature_size			,				fpn_feature_size=self.mask_feature_size			,				num_channels=self.num_channels			,				num_labels=self.num_labels			,				)







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__      :					Dict				       =							self.prepare_config_and_inputs()
   UpperCAmelCase__      :					List[Any]				       =							{"pixel_values": pixel_values, "pixel_mask": pixel_mask}
   return config, inputs_dict







 def 							UpperCamelCase  (					self		:  List[Any]			,				snake_case__		:  List[str]			,				snake_case__		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							output.encoder_hidden_states
   UpperCAmelCase__      :					int				       =							output.pixel_decoder_hidden_states
   UpperCAmelCase__      :					Any				       =							output.transformer_decoder_hidden_states

   self.parent.assertTrue(len(snake_case__ )			,				len(config.backbone_config.depths ) )
   self.parent.assertTrue(len(snake_case__ )			,				len(config.backbone_config.depths ) )
   self.parent.assertTrue(len(snake_case__ )			,				config.decoder_config.decoder_layers )







 def 							UpperCamelCase  (					self		:  Union[str, Any]			,				snake_case__		:  Optional[Any]			,				snake_case__		:  List[str]			,				snake_case__		:  Tuple			,				snake_case__		:  List[Any]=False ):



   '''simple docstring'''




   with torch.no_grad():
     UpperCAmelCase__      :					str				       =							MaskFormerModel(config=snake_case__ )
     model.to(snake_case__ )
     model.eval()

     UpperCAmelCase__      :					str				       =							model(pixel_values=snake_case__			,				pixel_mask=snake_case__ )
     UpperCAmelCase__      :					List[Any]				       =							model(snake_case__			,				output_hidden_states=snake_case__ )
   # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
   # encoder and pixel decoder
   self.parent.assertEqual(
       output.transformer_decoder_last_hidden_state.shape			,				(self.batch_size, self.num_queries, self.mask_feature_size)			,				)
   # let's ensure the other two hidden state exists
   self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None )
   self.parent.assertTrue(output.encoder_last_hidden_state is not None )

   if output_hidden_states:
     self.check_output_hidden_state(snake_case__			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  Union[str, Any]			,				snake_case__		:  int			,				snake_case__		:  Dict			,				snake_case__		:  int			,				snake_case__		:  List[str]			,				snake_case__		:  Optional[Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							MaskFormerForInstanceSegmentation(config=snake_case__ )
   model.to(snake_case__ )
   model.eval()

   def comm_check_on_output(snake_case__		:  Tuple ):
     # let's still check that all the required stuff is there
     self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None )
     self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None )
     self.parent.assertTrue(result.encoder_last_hidden_state is not None )
     # okay, now we need to check the logits shape
     # due to the encoder compression, masks have a //4 spatial size
     self.parent.assertEqual(
         result.masks_queries_logits.shape			,				(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)			,				)
     # + 1 for null class
     self.parent.assertEqual(
         result.class_queries_logits.shape			,				(self.batch_size, self.num_queries, self.num_labels + 1) )

   with torch.no_grad():
     UpperCAmelCase__      :					List[Any]				       =							model(pixel_values=snake_case__			,				pixel_mask=snake_case__ )
     UpperCAmelCase__      :					str				       =							model(snake_case__ )

     comm_check_on_output(snake_case__ )

     UpperCAmelCase__      :					Dict				       =							model(
         pixel_values=snake_case__			,				pixel_mask=snake_case__			,				mask_labels=snake_case__			,				class_labels=snake_case__ )

   comm_check_on_output(snake_case__ )

   self.parent.assertTrue(result.loss is not None )
   self.parent.assertEqual(result.loss.shape			,				torch.Size([1] ) )






@require_torch
class 							UpperCAmelCase_							(    A					,				A					,				unittest.TestCase				):

 '''simple docstring'''
 lowercase_					:    Optional[int]				=							(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
 lowercase_					:    List[Any]				=							(
     {"feature-extraction": MaskFormerModel, "image-segmentation": MaskFormerForInstanceSegmentation}
     if is_torch_available()
     else {}
 )

 lowercase_					:    Optional[int]				=							False
 lowercase_					:    Union[str, Any]				=							False
 lowercase_					:    Dict				=							False
 lowercase_					:    str				=							False







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							MaskFormerModelTester(self )
   UpperCAmelCase__      :					List[str]				       =							ConfigTester(self			,				config_class=snake_case__			,				has_text_modality=snake_case__ )







 def 							UpperCamelCase  (					self		:  Tuple ):



   '''simple docstring'''




   self.config_tester.run_common_tests()







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__       ,			UpperCAmelCase__      :					int				       =							self.model_tester.prepare_config_and_inputs_for_common()
   self.model_tester.create_and_check_maskformer_model(snake_case__			,				**snake_case__			,				output_hidden_states=snake_case__ )







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*snake_case__ )







 @unittest.skip(reason="MaskFormer does not use inputs_embeds" )
 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   pass







 @unittest.skip(reason="MaskFormer does not have a get_input_embeddings method" )
 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   pass







 @unittest.skip(reason="MaskFormer is not a generative model" )
 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




   pass







 @unittest.skip(reason="MaskFormer does not use token embeddings" )
 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   pass







 @require_torch_multi_gpu
 @unittest.skip(
     reason="MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`" )
 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   pass







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



   '''simple docstring'''




   pass







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__       ,			UpperCAmelCase__      :					Optional[int]				       =							self.model_tester.prepare_config_and_inputs_for_common()

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

     UpperCAmelCase__      :					Dict				       =							["pixel_values"]
     self.assertListEqual(arg_names[:1]			,				snake_case__ )







 @slow
 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   for model_name in ["facebook/maskformer-swin-small-coco"]:
     UpperCAmelCase__      :					Dict				       =							MaskFormerModel.from_pretrained(snake_case__ )
     self.assertIsNotNone(snake_case__ )







 def 							UpperCamelCase  (					self		:  Optional[Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							(self.model_tester.min_size,) * 2
   UpperCAmelCase__      :					List[str]				       =							{
       "pixel_values": torch.randn((2, 3, *size)			,				device=snake_case__ ),
       "mask_labels": torch.randn((2, 10, *size)			,				device=snake_case__ ),
       "class_labels": torch.zeros(2			,				10			,				device=snake_case__ ).long(),
   }

   UpperCAmelCase__      :					List[str]				       =							MaskFormerForInstanceSegmentation(MaskFormerConfig() ).to(snake_case__ )
   UpperCAmelCase__      :					List[Any]				       =							model(**snake_case__ )
   self.assertTrue(outputs.loss is not None )







 def 							UpperCamelCase  (					self		:  Optional[int] ):



   '''simple docstring'''




   UpperCAmelCase__       ,			UpperCAmelCase__      :					Optional[int]				       =							self.model_tester.prepare_config_and_inputs_for_common()
   self.model_tester.create_and_check_maskformer_model(snake_case__			,				**snake_case__			,				output_hidden_states=snake_case__ )







 def 							UpperCamelCase  (					self		:  Optional[int] ):



   '''simple docstring'''




   UpperCAmelCase__       ,			UpperCAmelCase__      :					Union[str, Any]				       =							self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     UpperCAmelCase__      :					List[str]				       =							model_class(snake_case__ ).to(snake_case__ )
     UpperCAmelCase__      :					List[Any]				       =							model(**snake_case__			,				output_attentions=snake_case__ )
     self.assertTrue(outputs.attentions is not None )







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   if not self.model_tester.is_training:
     return
   # only MaskFormerForInstanceSegmentation has the loss
   UpperCAmelCase__      :					int				       =							self.all_model_classes[1]
   UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__      :					Optional[int]				       =							self.model_tester.prepare_config_and_inputs()

   UpperCAmelCase__      :					Optional[int]				       =							model_class(snake_case__ )
   model.to(snake_case__ )
   model.train()

   UpperCAmelCase__      :					List[Any]				       =							model(snake_case__			,				mask_labels=snake_case__			,				class_labels=snake_case__ ).loss
   loss.backward()







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							self.all_model_classes[1]
   UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__       ,			UpperCAmelCase__      :					List[Any]				       =							self.model_tester.prepare_config_and_inputs()
   UpperCAmelCase__      :					int				       =							True
   UpperCAmelCase__      :					List[str]				       =							True

   UpperCAmelCase__      :					List[str]				       =							model_class(snake_case__ )
   model.to(snake_case__ )
   model.train()

   UpperCAmelCase__      :					Optional[Any]				       =							model(snake_case__			,				mask_labels=snake_case__			,				class_labels=snake_case__ )

   UpperCAmelCase__      :					int				       =							outputs.encoder_hidden_states[0]
   encoder_hidden_states.retain_grad()

   UpperCAmelCase__      :					Union[str, Any]				       =							outputs.pixel_decoder_hidden_states[0]
   pixel_decoder_hidden_states.retain_grad()
   # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
   UpperCAmelCase__      :					Union[str, Any]				       =							outputs.transformer_decoder_hidden_states[0]
   transformer_decoder_hidden_states.retain_grad()

   UpperCAmelCase__      :					Union[str, Any]				       =							outputs.attentions[0]
   attentions.retain_grad()

   outputs.loss.backward(retain_graph=snake_case__ )

   self.assertIsNotNone(encoder_hidden_states.grad )
   self.assertIsNotNone(pixel_decoder_hidden_states.grad )
   self.assertIsNotNone(transformer_decoder_hidden_states.grad )
   self.assertIsNotNone(attentions.grad )



SCREAMING_SNAKE_CASE     =       1E-4







def snake_case_     (					):
  UpperCAmelCase__      :					Dict				       =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
  return image






@require_vision
@slow
class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 @cached_property
 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   return (
       MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco" )
       if is_vision_available()
       else None
   )







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   UpperCAmelCase__      :					Any				       =							MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco" ).to(snake_case__ )
   UpperCAmelCase__      :					int				       =							self.default_image_processor
   UpperCAmelCase__      :					Union[str, Any]				       =							prepare_img()
   UpperCAmelCase__      :					Optional[Any]				       =							image_processor(snake_case__			,				return_tensors="pt" ).to(snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							inputs["pixel_values"].shape
   # check size is divisible by 32
   self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 )
   # check size
   self.assertEqual(snake_case__			,				(1, 3, 8_00, 10_88) )

   with torch.no_grad():
     UpperCAmelCase__      :					str				       =							model(**snake_case__ )

   UpperCAmelCase__      :					str				       =							torch.tensor(
       [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]] ).to(snake_case__ )
   self.assertTrue(
       torch.allclose(
           outputs.encoder_last_hidden_state[0, 0, :3, :3]			,				snake_case__			,				atol=snake_case__ ) )

   UpperCAmelCase__      :					Union[str, Any]				       =							torch.tensor(
       [[-0.8422, -0.8434, -0.9718], [-1.0144, -0.5565, -0.4195], [-1.0038, -0.4484, -0.1961]] ).to(snake_case__ )
   self.assertTrue(
       torch.allclose(
           outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]			,				snake_case__			,				atol=snake_case__ ) )

   UpperCAmelCase__      :					str				       =							torch.tensor(
       [[0.2852, -0.0159, 0.9735], [0.6254, 0.1858, 0.8529], [-0.0680, -0.4116, 1.8413]] ).to(snake_case__ )
   self.assertTrue(
       torch.allclose(
           outputs.transformer_decoder_last_hidden_state[0, :3, :3]			,				snake_case__			,				atol=snake_case__ ) )







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							(
       MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" )
       .to(snake_case__ )
       .eval()
   )
   UpperCAmelCase__      :					Union[str, Any]				       =							self.default_image_processor
   UpperCAmelCase__      :					List[str]				       =							prepare_img()
   UpperCAmelCase__      :					Union[str, Any]				       =							image_processor(snake_case__			,				return_tensors="pt" ).to(snake_case__ )
   UpperCAmelCase__      :					Dict				       =							inputs["pixel_values"].shape
   # check size is divisible by 32
   self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 )
   # check size
   self.assertEqual(snake_case__			,				(1, 3, 8_00, 10_88) )

   with torch.no_grad():
     UpperCAmelCase__      :					Any				       =							model(**snake_case__ )
   # masks_queries_logits
   UpperCAmelCase__      :					Any				       =							outputs.masks_queries_logits
   self.assertEqual(
       masks_queries_logits.shape			,				(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)			,				)
   UpperCAmelCase__      :					List[str]				       =							[
       [-1.3737124, -1.7724937, -1.9364233],
       [-1.5977281, -1.9867939, -2.1523695],
       [-1.5795398, -1.9269832, -2.093942],
   ]
   UpperCAmelCase__      :					str				       =							torch.tensor(snake_case__ ).to(snake_case__ )
   self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]			,				snake_case__			,				atol=snake_case__ ) )
   # class_queries_logits
   UpperCAmelCase__      :					Optional[Any]				       =							outputs.class_queries_logits
   self.assertEqual(
       class_queries_logits.shape			,				(1, model.config.decoder_config.num_queries, model.config.num_labels + 1) )
   UpperCAmelCase__      :					Optional[Any]				       =							torch.tensor(
       [
           [1.6_512e00, -5.2_572e00, -3.3_519e00],
           [3.6_169e-02, -5.9_025e00, -2.9_313e00],
           [1.0_766e-04, -7.7_630e00, -5.1_263e00],
       ] ).to(snake_case__ )
   self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]			,				snake_case__			,				atol=snake_case__ ) )







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							(
       MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff" )
       .to(snake_case__ )
       .eval()
   )
   UpperCAmelCase__      :					List[Any]				       =							self.default_image_processor
   UpperCAmelCase__      :					List[str]				       =							prepare_img()
   UpperCAmelCase__      :					List[Any]				       =							image_processor(snake_case__			,				return_tensors="pt" ).to(snake_case__ )
   UpperCAmelCase__      :					Union[str, Any]				       =							inputs["pixel_values"].shape
   # check size is divisible by 32
   self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 )
   # check size
   self.assertEqual(snake_case__			,				(1, 3, 8_00, 10_88) )

   with torch.no_grad():
     UpperCAmelCase__      :					Optional[Any]				       =							model(**snake_case__ )
   # masks_queries_logits
   UpperCAmelCase__      :					Dict				       =							outputs.masks_queries_logits
   self.assertEqual(
       masks_queries_logits.shape			,				(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)			,				)
   UpperCAmelCase__      :					Dict				       =							[[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]]
   UpperCAmelCase__      :					Optional[int]				       =							torch.tensor(snake_case__ ).to(snake_case__ )
   self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]			,				snake_case__			,				atol=snake_case__ ) )
   # class_queries_logits
   UpperCAmelCase__      :					Optional[int]				       =							outputs.class_queries_logits
   self.assertEqual(
       class_queries_logits.shape			,				(1, model.config.decoder_config.num_queries, model.config.num_labels + 1) )
   UpperCAmelCase__      :					Union[str, Any]				       =							torch.tensor(
       [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]] ).to(snake_case__ )
   self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]			,				snake_case__			,				atol=snake_case__ ) )







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							(
       MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" )
       .to(snake_case__ )
       .eval()
   )
   UpperCAmelCase__      :					Optional[int]				       =							self.default_image_processor

   UpperCAmelCase__      :					Optional[Any]				       =							image_processor(
       [np.zeros((3, 8_00, 13_33) ), np.zeros((3, 8_00, 13_33) )]			,				segmentation_maps=[np.zeros((3_84, 3_84) ).astype(np.floataa ), np.zeros((3_84, 3_84) ).astype(np.floataa )]			,				return_tensors="pt"			,				)

   UpperCAmelCase__      :					str				       =							inputs["pixel_values"].to(snake_case__ )
   UpperCAmelCase__      :					Union[str, Any]				       =							[el.to(snake_case__ ) for el in inputs["mask_labels"]]
   UpperCAmelCase__      :					Union[str, Any]				       =							[el.to(snake_case__ ) for el in inputs["class_labels"]]

   with torch.no_grad():
     UpperCAmelCase__      :					List[str]				       =							model(**snake_case__ )

   self.assertTrue(outputs.loss is not None )





'''simple docstring'''





from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


SCREAMING_SNAKE_CASE     =       logging.get_logger(__name__)

# General docstring
SCREAMING_SNAKE_CASE     =       """RegNetConfig"""

# Base docstring
SCREAMING_SNAKE_CASE     =       """facebook/regnet-y-040"""
SCREAMING_SNAKE_CASE     =       [1, 1_0_8_8, 7, 7]

# Image classification docstring
SCREAMING_SNAKE_CASE     =       """facebook/regnet-y-040"""
SCREAMING_SNAKE_CASE     =       """tabby, tabby cat"""

SCREAMING_SNAKE_CASE     =       [
    """facebook/regnet-y-040""",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  str			,				snake_case__		:  int			,				snake_case__		:  int = 3			,				snake_case__		:  int = 1			,				snake_case__		:  int = 1			,				snake_case__		:  Optional[str] = "relu"			,				**snake_case__		:  Dict			,				):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   # The padding and conv has been verified in
   # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
   UpperCAmelCase__      :					Any				       =							tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 )
   UpperCAmelCase__      :					Union[str, Any]				       =							tf.keras.layers.ConvaD(
       filters=snake_case__			,				kernel_size=snake_case__			,				strides=snake_case__			,				padding="VALID"			,				groups=snake_case__			,				use_bias=snake_case__			,				name="convolution"			,				)
   UpperCAmelCase__      :					Any				       =							tf.keras.layers.BatchNormalization(epsilon=1e-5			,				momentum=0.9			,				name="normalization" )
   UpperCAmelCase__      :					Tuple				       =							ACTaFN[activation] if activation is not None else tf.identity







 def 							UpperCamelCase  (					self		:  str			,				snake_case__		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					str				       =							self.convolution(self.padding(snake_case__ ) )
   UpperCAmelCase__      :					Dict				       =							self.normalization(snake_case__ )
   UpperCAmelCase__      :					Any				       =							self.activation(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[Any]			,				snake_case__		:  RegNetConfig			,				**snake_case__		:  Optional[int] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Any				       =							config.num_channels
   UpperCAmelCase__      :					Optional[Any]				       =							TFRegNetConvLayer(
       out_channels=config.embedding_size			,				kernel_size=3			,				stride=2			,				activation=config.hidden_act			,				name="embedder"			,				)







 def 							UpperCamelCase  (					self		:  int			,				snake_case__		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							shape_list(snake_case__ )[1]
   if tf.executing_eagerly() and num_channels != self.num_channels:
     raise ValueError(
         "Make sure that the channel dimension of the pixel values match with the one set in the configuration." )

   # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
   # So change the input format from `NCHW` to `NHWC`.
   # shape = (batch_size, in_height, in_width, in_channels=num_channels)
   UpperCAmelCase__      :					List[str]				       =							tf.transpose(snake_case__			,				perm=(0, 2, 3, 1) )
   UpperCAmelCase__      :					Dict				       =							self.embedder(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  Optional[int]			,				snake_case__		:  int			,				snake_case__		:  int = 2			,				**snake_case__		:  Optional[int] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					int				       =							tf.keras.layers.ConvaD(
       filters=snake_case__			,				kernel_size=1			,				strides=snake_case__			,				use_bias=snake_case__			,				name="convolution" )
   UpperCAmelCase__      :					str				       =							tf.keras.layers.BatchNormalization(epsilon=1e-5			,				momentum=0.9			,				name="normalization" )







 def 							UpperCamelCase  (					self		:  str			,				snake_case__		:  tf.Tensor			,				snake_case__		:  bool = False ):



   '''simple docstring'''




   return self.normalization(self.convolution(snake_case__ )			,				training=snake_case__ )






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[str]			,				snake_case__		:  int			,				snake_case__		:  int			,				**snake_case__		:  Union[str, Any] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Any				       =							tf.keras.layers.GlobalAveragePoolingaD(keepdims=snake_case__			,				name="pooler" )
   UpperCAmelCase__      :					Optional[Any]				       =							[
       tf.keras.layers.ConvaD(filters=snake_case__			,				kernel_size=1			,				activation="relu"			,				name="attention.0" ),
       tf.keras.layers.ConvaD(filters=snake_case__			,				kernel_size=1			,				activation="sigmoid"			,				name="attention.2" ),
   ]







 def 							UpperCamelCase  (					self		:  Optional[int]			,				snake_case__		:  Any ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							self.pooler(snake_case__ )
   for layer_module in self.attention:
     UpperCAmelCase__      :					Dict				       =							layer_module(snake_case__ )
   UpperCAmelCase__      :					int				       =							hidden_state * pooled
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[str]			,				snake_case__		:  RegNetConfig			,				snake_case__		:  int			,				snake_case__		:  int			,				snake_case__		:  int = 1			,				**snake_case__		:  str ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							in_channels != out_channels or stride != 1
   UpperCAmelCase__      :					Optional[int]				       =							max(1			,				out_channels // config.groups_width )
   UpperCAmelCase__      :					Tuple				       =							(
       TFRegNetShortCut(snake_case__			,				stride=snake_case__			,				name="shortcut" )
       if should_apply_shortcut
       else tf.keras.layers.Activation("linear"			,				name="shortcut" )
   )
   # `self.layers` instead of `self.layer` because that is a reserved argument.
   UpperCAmelCase__      :					int				       =							[
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=config.hidden_act			,				name="layer.0" ),
       TFRegNetConvLayer(
           snake_case__			,				stride=snake_case__			,				groups=snake_case__			,				activation=config.hidden_act			,				name="layer.1" ),
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=snake_case__			,				name="layer.2" ),
   ]
   UpperCAmelCase__      :					List[Any]				       =							ACTaFN[config.hidden_act]







 def 							UpperCamelCase  (					self		:  Tuple			,				snake_case__		:  Optional[int] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Any				       =							hidden_state
   for layer_module in self.layers:
     UpperCAmelCase__      :					List[str]				       =							layer_module(snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							self.shortcut(snake_case__ )
   hidden_state += residual
   UpperCAmelCase__      :					Union[str, Any]				       =							self.activation(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[Any]			,				snake_case__		:  RegNetConfig			,				snake_case__		:  int			,				snake_case__		:  int			,				snake_case__		:  int = 1			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					int				       =							in_channels != out_channels or stride != 1
   UpperCAmelCase__      :					Union[str, Any]				       =							max(1			,				out_channels // config.groups_width )
   UpperCAmelCase__      :					Dict				       =							(
       TFRegNetShortCut(snake_case__			,				stride=snake_case__			,				name="shortcut" )
       if should_apply_shortcut
       else tf.keras.layers.Activation("linear"			,				name="shortcut" )
   )
   UpperCAmelCase__      :					Optional[Any]				       =							[
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=config.hidden_act			,				name="layer.0" ),
       TFRegNetConvLayer(
           snake_case__			,				stride=snake_case__			,				groups=snake_case__			,				activation=config.hidden_act			,				name="layer.1" ),
       TFRegNetSELayer(snake_case__			,				reduced_channels=int(round(in_channels / 4 ) )			,				name="layer.2" ),
       TFRegNetConvLayer(snake_case__			,				kernel_size=1			,				activation=snake_case__			,				name="layer.3" ),
   ]
   UpperCAmelCase__      :					Dict				       =							ACTaFN[config.hidden_act]







 def 							UpperCamelCase  (					self		:  Optional[int]			,				snake_case__		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							hidden_state
   for layer_module in self.layers:
     UpperCAmelCase__      :					Tuple				       =							layer_module(snake_case__ )
   UpperCAmelCase__      :					Dict				       =							self.shortcut(snake_case__ )
   hidden_state += residual
   UpperCAmelCase__      :					List[Any]				       =							self.activation(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  List[Any]			,				snake_case__		:  RegNetConfig			,				snake_case__		:  int			,				snake_case__		:  int			,				snake_case__		:  int = 2			,				snake_case__		:  int = 2			,				**snake_case__		:  Any ):



   '''simple docstring'''




   super().__init__(**snake_case__ )

   UpperCAmelCase__      :					Any				       =							TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer
   UpperCAmelCase__      :					int				       =							[
       # downsampling is done in the first layer with stride of 2
       layer(snake_case__			,				snake_case__			,				snake_case__			,				stride=snake_case__			,				name="layers.0" ),
       *[layer(snake_case__			,				snake_case__			,				snake_case__			,				name=F"""layers.{i+1}""" ) for i in range(depth - 1 )],
   ]







 def 							UpperCamelCase  (					self		:  Dict			,				snake_case__		:  Tuple ):



   '''simple docstring'''




   for layer_module in self.layers:
     UpperCAmelCase__      :					Tuple				       =							layer_module(snake_case__ )
   return hidden_state






class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''







 def __init__(					self		:  Dict			,				snake_case__		:  RegNetConfig			,				**snake_case__		:  Any ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					Union[str, Any]				       =							[]
   # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
   self.stages.append(
       TFRegNetStage(
           snake_case__			,				config.embedding_size			,				config.hidden_sizes[0]			,				stride=2 if config.downsample_in_first_stage else 1			,				depth=config.depths[0]			,				name="stages.0"			,				) )
   UpperCAmelCase__      :					List[Any]				       =							zip(config.hidden_sizes			,				config.hidden_sizes[1:] )
   for i, ((in_channels, out_channels), depth) in enumerate(zip(snake_case__			,				config.depths[1:] ) ):
     self.stages.append(TFRegNetStage(snake_case__			,				snake_case__			,				snake_case__			,				depth=snake_case__			,				name=F"""stages.{i+1}""" ) )







 def 							UpperCamelCase  (					self		:  Dict			,				snake_case__		:  tf.Tensor			,				snake_case__		:  bool = False			,				snake_case__		:  bool = True ):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							() if output_hidden_states else None

   for stage_module in self.stages:
     if output_hidden_states:
       UpperCAmelCase__      :					Optional[Any]				       =							hidden_states + (hidden_state,)

     UpperCAmelCase__      :					Dict				       =							stage_module(snake_case__ )

   if output_hidden_states:
     UpperCAmelCase__      :					Any				       =							hidden_states + (hidden_state,)

   if not return_dict:
     return tuple(v for v in [hidden_state, hidden_states] if v is not None )

   return TFBaseModelOutputWithNoAttention(last_hidden_state=snake_case__			,				hidden_states=snake_case__ )






@keras_serializable
class 							UpperCAmelCase_							(    tf.keras.layers.Layer				):

 '''simple docstring'''
 lowercase_					:    Dict				=							RegNetConfig







 def __init__(					self		:  int			,				snake_case__		:  str			,				**snake_case__		:  Union[str, Any] ):



   '''simple docstring'''




   super().__init__(**snake_case__ )
   UpperCAmelCase__      :					List[Any]				       =							config
   UpperCAmelCase__      :					Dict				       =							TFRegNetEmbeddings(snake_case__			,				name="embedder" )
   UpperCAmelCase__      :					int				       =							TFRegNetEncoder(snake_case__			,				name="encoder" )
   UpperCAmelCase__      :					int				       =							tf.keras.layers.GlobalAveragePoolingaD(keepdims=snake_case__			,				name="pooler" )







 @unpack_inputs
 def 							UpperCamelCase  (					self		:  Dict			,				snake_case__		:  tf.Tensor			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  bool = False			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[Any]				       =							(
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__      :					Tuple				       =							return_dict if return_dict is not None else self.config.use_return_dict

   UpperCAmelCase__      :					str				       =							self.embedder(snake_case__			,				training=snake_case__ )

   UpperCAmelCase__      :					Optional[Any]				       =							self.encoder(
       snake_case__			,				output_hidden_states=snake_case__			,				return_dict=snake_case__			,				training=snake_case__ )

   UpperCAmelCase__      :					Optional[int]				       =							encoder_outputs[0]
   UpperCAmelCase__      :					Optional[Any]				       =							self.pooler(snake_case__ )

   # Change to NCHW output format have uniformity in the modules
   UpperCAmelCase__      :					List[Any]				       =							tf.transpose(snake_case__			,				perm=(0, 3, 1, 2) )
   UpperCAmelCase__      :					Any				       =							tf.transpose(snake_case__			,				perm=(0, 3, 1, 2) )

   # Change the other hidden state outputs to NCHW as well
   if output_hidden_states:
     UpperCAmelCase__      :					Optional[int]				       =							tuple([tf.transpose(snake_case__			,				perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] )

   if not return_dict:
     return (last_hidden_state, pooled_output) + encoder_outputs[1:]

   return TFBaseModelOutputWithPoolingAndNoAttention(
       last_hidden_state=snake_case__			,				pooler_output=snake_case__			,				hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states			,				)






class 							UpperCAmelCase_							(    A				):

 '''simple docstring'''
 lowercase_					:    List[Any]				=							RegNetConfig
 lowercase_					:    Optional[int]				=							"regnet"
 lowercase_					:    Optional[Any]				=							"pixel_values"







 @property
 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




   return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24)			,				dtype=tf.floataa )}



SCREAMING_SNAKE_CASE     =       R"""
    Parameters:
    This model is a Tensorflow
    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and
    behavior.
        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
"""

SCREAMING_SNAKE_CASE     =       R"""
    Args:
        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConveNextImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""






@add_start_docstrings(
    "The bare RegNet model outputting raw features without any specific head on top."					,				A					,				)
class 							UpperCAmelCase_							(    A				):

 '''simple docstring'''







 def __init__(					self		:  Union[str, Any]			,				snake_case__		:  RegNetConfig			,				*snake_case__		:  Any			,				**snake_case__		:  List[str] ):



   '''simple docstring'''




   super().__init__(snake_case__			,				*snake_case__			,				**snake_case__ )
   UpperCAmelCase__      :					Any				       =							TFRegNetMainLayer(snake_case__			,				name="regnet" )







 @unpack_inputs
 @add_start_docstrings_to_model_forward(snake_case__ )
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC			,				output_type=snake_case__			,				config_class=_CONFIG_FOR_DOC			,				modality="vision"			,				expected_output=_EXPECTED_OUTPUT_SHAPE			,				)
 def 							UpperCamelCase  (					self		:  Optional[int]			,				snake_case__		:  tf.Tensor			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  int=False			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							(
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__      :					str				       =							return_dict if return_dict is not None else self.config.use_return_dict

   UpperCAmelCase__      :					List[str]				       =							self.regnet(
       pixel_values=snake_case__			,				output_hidden_states=snake_case__			,				return_dict=snake_case__			,				training=snake_case__			,				)
   if not return_dict:
     return (outputs[0],) + outputs[1:]

   return TFBaseModelOutputWithPoolingAndNoAttention(
       last_hidden_state=outputs.last_hidden_state			,				pooler_output=outputs.pooler_output			,				hidden_states=outputs.hidden_states			,				)






@add_start_docstrings(
    "\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "					,				A					,				)
class 							UpperCAmelCase_							(    A					,				A				):

 '''simple docstring'''







 def __init__(					self		:  List[str]			,				snake_case__		:  RegNetConfig			,				*snake_case__		:  Optional[Any]			,				**snake_case__		:  int ):



   '''simple docstring'''




   super().__init__(snake_case__			,				*snake_case__			,				**snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							config.num_labels
   UpperCAmelCase__      :					str				       =							TFRegNetMainLayer(snake_case__			,				name="regnet" )
   # classification head
   UpperCAmelCase__      :					Optional[int]				       =							[
       tf.keras.layers.Flatten(),
       tf.keras.layers.Dense(config.num_labels			,				name="classifier.1" ) if config.num_labels > 0 else tf.identity,
   ]







 @unpack_inputs
 @add_start_docstrings_to_model_forward(snake_case__ )
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT			,				output_type=snake_case__			,				config_class=_CONFIG_FOR_DOC			,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,				)
 def 							UpperCamelCase  (					self		:  List[str]			,				snake_case__		:  tf.Tensor = None			,				snake_case__		:  tf.Tensor = None			,				snake_case__		:  bool = None			,				snake_case__		:  bool = None			,				snake_case__		:  List[Any]=False			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							(
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__      :					List[Any]				       =							return_dict if return_dict is not None else self.config.use_return_dict

   UpperCAmelCase__      :					Dict				       =							self.regnet(
       snake_case__			,				output_hidden_states=snake_case__			,				return_dict=snake_case__			,				training=snake_case__ )

   UpperCAmelCase__      :					Dict				       =							outputs.pooler_output if return_dict else outputs[1]

   UpperCAmelCase__      :					Union[str, Any]				       =							self.classifier[0](snake_case__ )
   UpperCAmelCase__      :					str				       =							self.classifier[1](snake_case__ )

   UpperCAmelCase__      :					Any				       =							None if labels is None else self.hf_compute_loss(labels=snake_case__			,				logits=snake_case__ )

   if not return_dict:
     UpperCAmelCase__      :					Tuple				       =							(logits,) + outputs[2:]
     return ((loss,) + output) if loss is not None else output

   return TFSequenceClassifierOutput(loss=snake_case__			,				logits=snake_case__			,				hidden_states=outputs.hidden_states )



