



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







		'''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     ,)   ->						List[str]:






				"""simple docstring"""



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







		def    UpperCAmelCase       (      self		:    str		)   ->						Optional[int]:






				"""simple docstring"""



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

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

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

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







		def    UpperCAmelCase       (      self		:    str		)   ->						Dict:






				"""simple docstring"""



				return MaskFormerConfig.from_backbone_and_decoder_configs(
				    backbone_config=SwinConfig(
				        depths=[1, 1, 1, 1]     ,)     ,decoder_config=DetrConfig(
				        decoder_ffn_dim=128     ,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		)   ->						List[Any]:






				"""simple docstring"""



				lowercase__  :       Dict												=     self.prepare_config_and_inputs()
				lowercase__  :       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]		)   ->						str:






				"""simple docstring"""



				lowercase__  :       Optional[int]												=     output.encoder_hidden_states
				lowercase__  :       int												=     output.pixel_decoder_hidden_states
				lowercase__  :       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		)   ->						int:






				"""simple docstring"""



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

						lowercase__  :       str												=     model(pixel_values=snake_case__     ,pixel_mask=snake_case__		)
						lowercase__  :       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]		)   ->						Any:






				"""simple docstring"""



				lowercase__  :       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():
						lowercase__  :       List[Any]												=     model(pixel_values=snake_case__     ,pixel_mask=snake_case__		)
						lowercase__  :       str												=     model(snake_case__		)

						comm_check_on_output(snake_case__		)

						lowercase__  :       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 			__A			(      A_	,A_	,unittest.TestCase       ):







		'''simple docstring'''





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

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







		def    UpperCAmelCase       (      self		:    str		)   ->						Any:






				"""simple docstring"""



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







		def    UpperCAmelCase       (      self		:    Tuple		)   ->						Optional[int]:






				"""simple docstring"""



				self.config_tester.run_common_tests()







		def    UpperCAmelCase       (      self		:    str		)   ->						Optional[int]:






				"""simple docstring"""



				lowercase__  :       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]		)   ->						Optional[int]:






				"""simple docstring"""



				lowercase__  :       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]		)   ->						Dict:






				"""simple docstring"""



				pass







		@unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method'''		)
		def    UpperCAmelCase       (      self		:    str		)   ->						List[Any]:






				"""simple docstring"""



				pass







		@unittest.skip(reason='''MaskFormer is not a generative model'''		)
		def    UpperCAmelCase       (      self		:    Any		)   ->						List[Any]:






				"""simple docstring"""



				pass







		@unittest.skip(reason='''MaskFormer does not use token embeddings'''		)
		def    UpperCAmelCase       (      self		:    List[Any]		)   ->						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		)   ->						Tuple:






				"""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]		)   ->						Optional[int]:






				"""simple docstring"""



				pass







		def    UpperCAmelCase       (      self		:    List[str]		)   ->						Union[str, Any]:






				"""simple docstring"""



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

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

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







		@slow
		def    UpperCAmelCase       (      self		:    List[Any]		)   ->						int:






				"""simple docstring"""



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







		def    UpperCAmelCase       (      self		:    Optional[Any]		)   ->						List[Any]:






				"""simple docstring"""



				lowercase__  :       int												=     (self.model_tester.min_size,) * 2
				lowercase__  :       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(),
				}

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







		def    UpperCAmelCase       (      self		:    Optional[int]		)   ->						List[str]:






				"""simple docstring"""



				lowercase__  :       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]		)   ->						str:






				"""simple docstring"""



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

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







		def    UpperCAmelCase       (      self		:    Union[str, Any]		)   ->						Tuple:






				"""simple docstring"""



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

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

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







		def    UpperCAmelCase       (      self		:    Union[str, Any]		)   ->						Dict:






				"""simple docstring"""



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

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

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

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

				lowercase__  :       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
				lowercase__  :       Union[str, Any]												=     outputs.transformer_decoder_hidden_states[0]
				transformer_decoder_hidden_states.retain_grad()

				lowercase__  :       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		)





lowerCAmelCase_  		=  1E-4



def 	__UpperCAmelCase (       )				-> Tuple:
		lowercase__  :       Dict												=     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
		return image





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







		'''simple docstring'''





		@cached_property
		def    UpperCAmelCase       (      self		:    List[str]		)   ->						str:






				"""simple docstring"""



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







		def    UpperCAmelCase       (      self		:    Dict		)   ->						List[str]:






				"""simple docstring"""



				lowercase__  :       Any												=     MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''		).to(snake_case__		)
				lowercase__  :       int												=     self.default_image_processor
				lowercase__  :       Union[str, Any]												=     prepare_img()
				lowercase__  :       Optional[Any]												=     image_processor(snake_case__     ,return_tensors='''pt'''		).to(snake_case__		)
				lowercase__  :       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, 800, 1_088)		)

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

				lowercase__  :       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__		)		)

				lowercase__  :       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__		)		)

				lowercase__  :       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		)   ->						List[str]:






				"""simple docstring"""



				lowercase__  :       List[str]												=     (
				    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''		)
				    .to(snake_case__		)
				    .eval()
				)
				lowercase__  :       Union[str, Any]												=     self.default_image_processor
				lowercase__  :       List[str]												=     prepare_img()
				lowercase__  :       Union[str, Any]												=     image_processor(snake_case__     ,return_tensors='''pt'''		).to(snake_case__		)
				lowercase__  :       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, 800, 1_088)		)

				with torch.no_grad():
						lowercase__  :       Any												=     model(**snake_case__		)
				# masks_queries_logits
				lowercase__  :       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)     ,)
				lowercase__  :       List[str]												=     [
				    [-1.373_7124, -1.772_4937, -1.936_4233],
				    [-1.597_7281, -1.986_7939, -2.152_3695],
				    [-1.579_5398, -1.926_9832, -2.09_3942],
				]
				lowercase__  :       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
				lowercase__  :       Optional[Any]												=     outputs.class_queries_logits
				self.assertEqual(
				    class_queries_logits.shape     ,(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
				lowercase__  :       Optional[Any]												=     torch.tensor(
				    [
				        [1.6512e00, -5.2572e00, -3.3519e00],
				        [3.6169e-02, -5.9025e00, -2.9313e00],
				        [1.0766e-04, -7.7630e00, -5.1263e00],
				    ]		).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]		)   ->						Union[str, Any]:






				"""simple docstring"""



				lowercase__  :       Optional[int]												=     (
				    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''		)
				    .to(snake_case__		)
				    .eval()
				)
				lowercase__  :       List[Any]												=     self.default_image_processor
				lowercase__  :       List[str]												=     prepare_img()
				lowercase__  :       List[Any]												=     image_processor(snake_case__     ,return_tensors='''pt'''		).to(snake_case__		)
				lowercase__  :       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, 800, 1_088)		)

				with torch.no_grad():
						lowercase__  :       Optional[Any]												=     model(**snake_case__		)
				# masks_queries_logits
				lowercase__  :       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)     ,)
				lowercase__  :       Dict												=     [[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]]
				lowercase__  :       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
				lowercase__  :       Optional[int]												=     outputs.class_queries_logits
				self.assertEqual(
				    class_queries_logits.shape     ,(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
				lowercase__  :       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		)   ->						Optional[int]:






				"""simple docstring"""



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

				lowercase__  :       Optional[Any]												=     image_processor(
				    [np.zeros((3, 800, 1_333)		), np.zeros((3, 800, 1_333)		)]     ,segmentation_maps=[np.zeros((384, 384)		).astype(np.floataa		), np.zeros((384, 384)		).astype(np.floataa		)]     ,return_tensors='''pt'''     ,)

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

				with torch.no_grad():
						lowercase__  :       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 )



