





'''simple docstring'''







import textwrap

import pyarrow as pa
import pytest

from datasets import Features, Image
from datasets.packaged_modules.text.text import Text

from ..utils import require_pil





@pytest.fixture
def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      List[Any] )			->						str:
						UpperCAmelCase_      :   Tuple      =	tmp_path / '''text.txt'''
						UpperCAmelCase_      :   Optional[int]      =	textwrap.dedent(
						    '''\
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

        Second paragraph:
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        ''' )
						with open(SCREAMING_SNAKE_CASE__,      '''w''',      encoding='''utf-8''' ) as f:
												f.write(SCREAMING_SNAKE_CASE__ )
						return str(SCREAMING_SNAKE_CASE__ )





@pytest.fixture
def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Tuple,      SCREAMING_SNAKE_CASE__   :      List[str] )			->						Any:
						UpperCAmelCase_      :   Tuple      =	tmp_path / '''text_with_image.txt'''
						with open(SCREAMING_SNAKE_CASE__,      '''w''',      encoding='''utf-8''' ) as f:
												f.write(SCREAMING_SNAKE_CASE__ )
						return str(SCREAMING_SNAKE_CASE__ )





@pytest.mark.parametrize('''keep_linebreaks''',      [True, False] )
def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Dict,      SCREAMING_SNAKE_CASE__   :      str )			->						int:
						with open(SCREAMING_SNAKE_CASE__,      encoding='''utf-8''' ) as f:
												UpperCAmelCase_      :   Tuple      =	f.read().splitlines(keepends=SCREAMING_SNAKE_CASE__ )
						UpperCAmelCase_      :   str      =	Text(keep_linebreaks=SCREAMING_SNAKE_CASE__,      encoding='''utf-8''' )
						UpperCAmelCase_      :   Optional[Any]      =	text._generate_tables([[text_file]] )
						UpperCAmelCase_      :   Optional[Any]      =	pa.concat_tables([table for _, table in generator] ).to_pydict()['''text''']
						assert generated_content == expected_content





@require_pil
def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      List[Any] )			->						str:
						with open(SCREAMING_SNAKE_CASE__,      encoding='''utf-8''' ) as f:
												UpperCAmelCase_      :   Tuple      =	f.read().splitlines()[0]
						UpperCAmelCase_      :   Optional[int]      =	Text(encoding='''utf-8''',      features=Features({'''image''': Image()} ) )
						UpperCAmelCase_      :   Optional[int]      =	text._generate_tables([[text_file_with_image]] )
						UpperCAmelCase_      :   List[Any]      =	pa.concat_tables([table for _, table in generator] )
						assert pa_table.schema.field('''image''' ).type == Image()()
						UpperCAmelCase_      :   Tuple      =	pa_table.to_pydict()['''image''']
						assert generated_content == [{"path": image_file, "bytes": None}]





@pytest.mark.parametrize('''sample_by''',      ['''line''', '''paragraph''', '''document'''] )
def 							lowerCamelCase_   (	SCREAMING_SNAKE_CASE__   :      Optional[int],      SCREAMING_SNAKE_CASE__   :      Optional[int] )			->						Optional[int]:
						with open(SCREAMING_SNAKE_CASE__,      encoding='''utf-8''' ) as f:
												UpperCAmelCase_      :   List[Any]      =	f.read()
						if sample_by == "line":
												UpperCAmelCase_      :   Union[str, Any]      =	expected_content.splitlines()
						elif sample_by == "paragraph":
												UpperCAmelCase_      :   Optional[int]      =	expected_content.split('''\n\n''' )
						elif sample_by == "document":
												UpperCAmelCase_      :   List[str]      =	[expected_content]
						UpperCAmelCase_      :   Optional[int]      =	Text(sample_by=SCREAMING_SNAKE_CASE__,      encoding='''utf-8''',      chunksize=100 )
						UpperCAmelCase_      :   Dict      =	text._generate_tables([[text_file]] )
						UpperCAmelCase_      :   Union[str, Any]      =	pa.concat_tables([table for _, table in generator] ).to_pydict()['''text''']
						assert generated_content == expected_content













'''simple docstring'''






from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFImageClassifierOutputWithNoAttention,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_resnet import ResNetConfig


__A     						= logging.get_logger(__name__)

# General docstring
__A     						= '''ResNetConfig'''

# Base docstring
__A     						= '''microsoft/resnet-50'''
__A     						= [1, 2_0_4_8, 7, 7]

# Image classification docstring
__A     						= '''microsoft/resnet-50'''
__A     						= '''tiger cat'''

__A     						= [
    '''microsoft/resnet-50''',
    # See all resnet models at https://huggingface.co/models?filter=resnet
]




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







   def __init__(self       , __a       , __a = 3       , __a = 1       , __a = "relu"       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(**__a)
      __snake_case					:   str  				=			kernel_size // 2
      __snake_case					:   Any  				=			tf.keras.layers.ConvaD(
          __a       , kernel_size=__a       , strides=__a       , padding='valid'       , use_bias=__a       , name='convolution')
      # Use same default momentum and epsilon as PyTorch equivalent
      __snake_case					:   Union[str, Any]  				=			tf.keras.layers.BatchNormalization(epsilon=1E-5       , momentum=0.9       , name='normalization')
      __snake_case					:   int  				=			ACTaFN[activation] if activation is not None else tf.keras.layers.Activation('linear')







   def      SCREAMING_SNAKE_CASE__       (self       , __a)      ->   tf.Tensor:
      """simple docstring"""






      __snake_case					:   Union[str, Any]  				=			(self.pad_value, self.pad_value)
      __snake_case					:   int  				=			tf.pad(__a       , [(0, 0), height_pad, width_pad, (0, 0)])
      __snake_case					:   Any  				=			self.conv(__a)
      return hidden_state







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = False)      ->   tf.Tensor:
      """simple docstring"""






      __snake_case					:   List[Any]  				=			self.convolution(__a)
      __snake_case					:   Tuple  				=			self.normalization(__a       , training=__a)
      __snake_case					:   Union[str, Any]  				=			self.activation(__a)
      return hidden_state




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







   def __init__(self       , __a       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(**__a)
      __snake_case					:   Any  				=			TFResNetConvLayer(
          config.embedding_size       , kernel_size=7       , stride=2       , activation=config.hidden_act       , name='embedder'       , )
      __snake_case					:   Optional[Any]  				=			tf.keras.layers.MaxPoolaD(pool_size=3       , strides=2       , padding='valid'       , name='pooler')
      __snake_case					:   Any  				=			config.num_channels







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = False)      ->   tf.Tensor:
      """simple docstring"""






      __snake_case				,__snake_case				,__snake_case				,__snake_case					:   Dict  				=			shape_list(__a)
      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.')
      __snake_case					:   Optional[int]  				=			pixel_values
      __snake_case					:   int  				=			self.embedder(__a)
      __snake_case					:   Any  				=			tf.pad(__a       , [[0, 0], [1, 1], [1, 1], [0, 0]])
      __snake_case					:   Tuple  				=			self.pooler(__a)
      return hidden_state




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







   def __init__(self       , __a       , __a = 2       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(**__a)
      __snake_case					:   List[str]  				=			tf.keras.layers.ConvaD(
          __a       , kernel_size=1       , strides=__a       , use_bias=__a       , name='convolution')
      # Use same default momentum and epsilon as PyTorch equivalent
      __snake_case					:   Dict  				=			tf.keras.layers.BatchNormalization(epsilon=1E-5       , momentum=0.9       , name='normalization')







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = False)      ->   tf.Tensor:
      """simple docstring"""






      __snake_case					:   List[Any]  				=			x
      __snake_case					:   int  				=			self.convolution(__a)
      __snake_case					:   Tuple  				=			self.normalization(__a       , training=__a)
      return hidden_state




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







   def __init__(self       , __a       , __a       , __a = 1       , __a = "relu"       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(**__a)
      __snake_case					:   Optional[int]  				=			in_channels != out_channels or stride != 1
      __snake_case					:   List[Any]  				=			TFResNetConvLayer(__a       , stride=__a       , name='layer.0')
      __snake_case					:   Optional[Any]  				=			TFResNetConvLayer(__a       , activation=__a       , name='layer.1')
      __snake_case					:   List[Any]  				=			(
          TFResNetShortCut(__a       , stride=__a       , name='shortcut')
          if should_apply_shortcut
          else tf.keras.layers.Activation('linear'       , name='shortcut')
      )
      __snake_case					:   Tuple  				=			ACTaFN[activation]







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = False)      ->   tf.Tensor:
      """simple docstring"""






      __snake_case					:   Dict  				=			hidden_state
      __snake_case					:   List[str]  				=			self.conva(__a       , training=__a)
      __snake_case					:   List[str]  				=			self.conva(__a       , training=__a)
      __snake_case					:   str  				=			self.shortcut(__a       , training=__a)
      hidden_state += residual
      __snake_case					:   List[Any]  				=			self.activation(__a)
      return hidden_state




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







   def __init__(self       , __a       , __a       , __a = 1       , __a = "relu"       , __a = 4       , **__a       , )      ->   None:
      """simple docstring"""






      super().__init__(**__a)
      __snake_case					:   Optional[Any]  				=			in_channels != out_channels or stride != 1
      __snake_case					:   Union[str, Any]  				=			out_channels // reduction
      __snake_case					:   Optional[int]  				=			TFResNetConvLayer(__a       , kernel_size=1       , name='layer.0')
      __snake_case					:   Dict  				=			TFResNetConvLayer(__a       , stride=__a       , name='layer.1')
      __snake_case					:   List[str]  				=			TFResNetConvLayer(__a       , kernel_size=1       , activation=__a       , name='layer.2')
      __snake_case					:   Dict  				=			(
          TFResNetShortCut(__a       , stride=__a       , name='shortcut')
          if should_apply_shortcut
          else tf.keras.layers.Activation('linear'       , name='shortcut')
      )
      __snake_case					:   Tuple  				=			ACTaFN[activation]







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = False)      ->   tf.Tensor:
      """simple docstring"""






      __snake_case					:   str  				=			hidden_state
      __snake_case					:   List[str]  				=			self.conva(__a       , training=__a)
      __snake_case					:   Any  				=			self.conva(__a       , training=__a)
      __snake_case					:   str  				=			self.conva(__a       , training=__a)
      __snake_case					:   Tuple  				=			self.shortcut(__a       , training=__a)
      hidden_state += residual
      __snake_case					:   int  				=			self.activation(__a)
      return hidden_state




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







   def __init__(self       , __a       , __a       , __a       , __a = 2       , __a = 2       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(**__a)

      __snake_case					:   List[Any]  				=			TFResNetBottleNeckLayer if config.layer_type == 'bottleneck' else TFResNetBasicLayer

      __snake_case					:   int  				=			[layer(__a       , __a       , stride=__a       , activation=config.hidden_act       , name='layers.0')]
      layers += [
          layer(__a       , __a       , activation=config.hidden_act       , name=F"""layers.{i + 1}""")
          for i in range(depth - 1)
      ]
      __snake_case					:   Optional[int]  				=			layers







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = False)      ->   tf.Tensor:
      """simple docstring"""






      for layer in self.stage_layers:
         __snake_case					:   Tuple  				=			layer(__a       , training=__a)
      return hidden_state




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







   def __init__(self       , __a       , **__a)      ->   None:
      """simple docstring"""






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







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = False       , __a = True       , __a = False       , )      ->   TFBaseModelOutputWithNoAttention:
      """simple docstring"""






      __snake_case					:   Dict  				=			() if output_hidden_states else None

      for stage_module in self.stages:
         if output_hidden_states:
            __snake_case					:   Any  				=			hidden_states + (hidden_state,)

         __snake_case					:   Union[str, Any]  				=			stage_module(__a       , training=__a)

      if output_hidden_states:
         __snake_case					:   Dict  				=			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=__a       , hidden_states=__a)




class a_     (     UpperCamelCase_				):
   _snake_case	   =				ResNetConfig
   _snake_case	   =				"""resnet"""
   _snake_case	   =				"""pixel_values"""







   @property
   def      SCREAMING_SNAKE_CASE__       (self)      ->   List[Any]:
      """simple docstring"""






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


__A     						= r'''
    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.

    Parameters:
        config ([`ResNetConfig`]): 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.
'''


__A     						= r'''
    Args:
        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__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.
'''




@keras_serializable
class a_     (     tf.keras.layers.Layer				):
   _snake_case	   =				ResNetConfig







   def __init__(self       , __a       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(**__a)
      __snake_case					:   Tuple  				=			config
      __snake_case					:   Dict  				=			TFResNetEmbeddings(__a       , name='embedder')
      __snake_case					:   Union[str, Any]  				=			TFResNetEncoder(__a       , name='encoder')
      __snake_case					:   int  				=			tf.keras.layers.GlobalAveragePoolingaD(keepdims=__a)







   @unpack_inputs
   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = None       , __a = None       , __a = False       , )      ->   Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:
      """simple docstring"""






      __snake_case					:   Union[str, Any]  				=			(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      __snake_case					:   Any  				=			return_dict if return_dict is not None else self.config.use_return_dict

      # TF 2.0 image layers can't use NCHW format when running on CPU.
      # We transpose to NHWC format and then transpose back after the full forward pass.
      # (batch_size, num_channels, height, width) -> (batch_size, height, width, num_channels)
      __snake_case					:   Optional[Any]  				=			tf.transpose(__a       , perm=[0, 2, 3, 1])
      __snake_case					:   Union[str, Any]  				=			self.embedder(__a       , training=__a)

      __snake_case					:   Any  				=			self.encoder(
          __a       , output_hidden_states=__a       , return_dict=__a       , training=__a)

      __snake_case					:   Optional[Any]  				=			encoder_outputs[0]

      __snake_case					:   Any  				=			self.pooler(__a)

      # Transpose all the outputs to the NCHW format
      # (batch_size, height, width, num_channels) -> (batch_size, num_channels, height, width)
      __snake_case					:   str  				=			tf.transpose(__a       , (0, 3, 1, 2))
      __snake_case					:   int  				=			tf.transpose(__a       , (0, 3, 1, 2))
      __snake_case					:   str  				=			()
      for hidden_state in encoder_outputs[1:]:
         __snake_case					:   str  				=			hidden_states + tuple(tf.transpose(__a       , (0, 3, 1, 2)) for h in hidden_state)

      if not return_dict:
         return (last_hidden_state, pooled_output) + hidden_states

      __snake_case					:   Union[str, Any]  				=			hidden_states if output_hidden_states else None

      return TFBaseModelOutputWithPoolingAndNoAttention(
          last_hidden_state=__a       , pooler_output=__a       , hidden_states=__a       , )




@add_start_docstrings(
    """The bare ResNet model outputting raw features without any specific head on top."""		,   UpperCamelCase_		,   )
class a_     (     UpperCamelCase_				):







   def __init__(self       , __a       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(__a       , **__a)
      __snake_case					:   Union[str, Any]  				=			TFResNetMainLayer(config=__a       , name='resnet')







   @add_start_docstrings_to_model_forward(__a)
   @add_code_sample_docstrings(
       checkpoint=_CHECKPOINT_FOR_DOC       , output_type=__a       , config_class=_CONFIG_FOR_DOC       , modality='vision'       , expected_output=_EXPECTED_OUTPUT_SHAPE       , )
   @unpack_inputs
   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = None       , __a = None       , __a = False       , )      ->   Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:
      """simple docstring"""






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

      __snake_case					:   int  				=			self.resnet(
          pixel_values=__a       , output_hidden_states=__a       , return_dict=__a       , training=__a       , )
      return resnet_outputs




@add_start_docstrings(
    """
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """		,   UpperCamelCase_		,   )
class a_     (     UpperCamelCase_		,   UpperCamelCase_				):







   def __init__(self       , __a       , **__a)      ->   None:
      """simple docstring"""






      super().__init__(__a       , **__a)
      __snake_case					:   Union[str, Any]  				=			config.num_labels
      __snake_case					:   List[str]  				=			TFResNetMainLayer(__a       , name='resnet')
      # classification head
      __snake_case					:   Tuple  				=			(
          tf.keras.layers.Dense(config.num_labels       , name='classifier.1')
          if config.num_labels > 0
          else tf.keras.layers.Activation('linear'       , name='classifier.1')
      )







   def      SCREAMING_SNAKE_CASE__       (self       , __a)      ->   tf.Tensor:
      """simple docstring"""






      __snake_case					:   Dict  				=			tf.keras.layers.Flatten()(__a)
      __snake_case					:   str  				=			self.classifier_layer(__a)
      return logits







   @add_start_docstrings_to_model_forward(__a)
   @add_code_sample_docstrings(
       checkpoint=_IMAGE_CLASS_CHECKPOINT       , output_type=__a       , config_class=_CONFIG_FOR_DOC       , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       , )
   @unpack_inputs
   def      SCREAMING_SNAKE_CASE__       (self       , __a = None       , __a = None       , __a = None       , __a = None       , __a = False       , )      ->   Union[Tuple[tf.Tensor], TFImageClassifierOutputWithNoAttention]:
      """simple docstring"""






      __snake_case					:   Dict  				=			return_dict if return_dict is not None else self.config.use_return_dict

      __snake_case					:   Optional[int]  				=			self.resnet(
          __a       , output_hidden_states=__a       , return_dict=__a       , training=__a)

      __snake_case					:   Union[str, Any]  				=			outputs.pooler_output if return_dict else outputs[1]

      __snake_case					:   Optional[Any]  				=			self.classifier(__a)

      __snake_case					:   List[str]  				=			None if labels is None else self.hf_compute_loss(__a       , __a)

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

      return TFImageClassifierOutputWithNoAttention(loss=__a       , logits=__a       , hidden_states=outputs.hidden_states)