
"""simple docstring"""


from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


A_    							=					logging.get_logger(__name__)

# General docstring
A_    							=					"""ResNetConfig"""

# Base docstring
A_    							=					"""microsoft/resnet-50"""
A_    							=					[1, 2048, 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  __lowerCamelCase  ( nn.Module			):




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase = 3							,						UpperCAmelCase = 1							,						UpperCAmelCase = "relu"   ):
							super().__init__()
							lowerCamelCase_					=  nn.Convad(
							    UpperCAmelCase							,						UpperCAmelCase							,						kernel_size=UpperCAmelCase							,						stride=UpperCAmelCase							,						padding=kernel_size // 2							,						bias=UpperCAmelCase   )
							lowerCamelCase_					=  nn.BatchNormad(UpperCAmelCase   )
							lowerCamelCase_					=  ACTaFN[activation] if activation is not None else nn.Identity()





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							lowerCamelCase_					=  self.convolution(UpperCAmelCase   )
							lowerCamelCase_					=  self.normalization(UpperCAmelCase   )
							lowerCamelCase_					=  self.activation(UpperCAmelCase   )
							return hidden_state




class  __lowerCamelCase  ( nn.Module			):




				def __init__(				self							,						UpperCAmelCase   ):
							super().__init__()
							lowerCamelCase_					=  ResNetConvLayer(
							    config.num_channels							,						config.embedding_size							,						kernel_size=7							,						stride=2							,						activation=config.hidden_act   )
							lowerCamelCase_					=  nn.MaxPoolad(kernel_size=3							,						stride=2							,						padding=1   )
							lowerCamelCase_					=  config.num_channels





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							lowerCamelCase_					=  pixel_values.shape[1]
							if 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.'''   )
							lowerCamelCase_					=  self.embedder(UpperCAmelCase   )
							lowerCamelCase_					=  self.pooler(UpperCAmelCase   )
							return embedding




class  __lowerCamelCase  ( nn.Module			):




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase = 2   ):
							super().__init__()
							lowerCamelCase_					=  nn.Convad(UpperCAmelCase							,						UpperCAmelCase							,						kernel_size=1							,						stride=UpperCAmelCase							,						bias=UpperCAmelCase   )
							lowerCamelCase_					=  nn.BatchNormad(UpperCAmelCase   )





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							lowerCamelCase_					=  self.convolution(UpperCAmelCase   )
							lowerCamelCase_					=  self.normalization(UpperCAmelCase   )
							return hidden_state




class  __lowerCamelCase  ( nn.Module			):




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase = 1							,						UpperCAmelCase = "relu"   ):
							super().__init__()
							lowerCamelCase_					=  in_channels != out_channels or stride != 1
							lowerCamelCase_					=  (
							    ResNetShortCut(UpperCAmelCase							,						UpperCAmelCase							,						stride=UpperCAmelCase   ) if should_apply_shortcut else nn.Identity()
							)
							lowerCamelCase_					=  nn.Sequential(
							    ResNetConvLayer(UpperCAmelCase							,						UpperCAmelCase							,						stride=UpperCAmelCase   )							,						ResNetConvLayer(UpperCAmelCase							,						UpperCAmelCase							,						activation=UpperCAmelCase   )							,						)
							lowerCamelCase_					=  ACTaFN[activation]





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							lowerCamelCase_					=  hidden_state
							lowerCamelCase_					=  self.layer(UpperCAmelCase   )
							lowerCamelCase_					=  self.shortcut(UpperCAmelCase   )
							hidden_state += residual
							lowerCamelCase_					=  self.activation(UpperCAmelCase   )
							return hidden_state




class  __lowerCamelCase  ( nn.Module			):




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase = 1							,						UpperCAmelCase = "relu"							,						UpperCAmelCase = 4   ):
							super().__init__()
							lowerCamelCase_					=  in_channels != out_channels or stride != 1
							lowerCamelCase_					=  out_channels // reduction
							lowerCamelCase_					=  (
							    ResNetShortCut(UpperCAmelCase							,						UpperCAmelCase							,						stride=UpperCAmelCase   ) if should_apply_shortcut else nn.Identity()
							)
							lowerCamelCase_					=  nn.Sequential(
							    ResNetConvLayer(UpperCAmelCase							,						UpperCAmelCase							,						kernel_size=1   )							,						ResNetConvLayer(UpperCAmelCase							,						UpperCAmelCase							,						stride=UpperCAmelCase   )							,						ResNetConvLayer(UpperCAmelCase							,						UpperCAmelCase							,						kernel_size=1							,						activation=UpperCAmelCase   )							,						)
							lowerCamelCase_					=  ACTaFN[activation]





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							lowerCamelCase_					=  hidden_state
							lowerCamelCase_					=  self.layer(UpperCAmelCase   )
							lowerCamelCase_					=  self.shortcut(UpperCAmelCase   )
							hidden_state += residual
							lowerCamelCase_					=  self.activation(UpperCAmelCase   )
							return hidden_state




class  __lowerCamelCase  ( nn.Module			):




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase = 2							,						UpperCAmelCase = 2							,						):
							super().__init__()

							lowerCamelCase_					=  ResNetBottleNeckLayer if config.layer_type == '''bottleneck''' else ResNetBasicLayer

							lowerCamelCase_					=  nn.Sequential(
							    # downsampling is done in the first layer with stride of 2
							    layer(UpperCAmelCase							,						UpperCAmelCase							,						stride=UpperCAmelCase							,						activation=config.hidden_act   )							,						*[layer(UpperCAmelCase							,						UpperCAmelCase							,						activation=config.hidden_act   ) for _ in range(depth - 1   )]							,						)





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							lowerCamelCase_					=  input
							for layer in self.layers:
										lowerCamelCase_					=  layer(UpperCAmelCase   )
							return hidden_state




class  __lowerCamelCase  ( nn.Module			):




				def __init__(				self							,						UpperCAmelCase   ):
							super().__init__()
							lowerCamelCase_					=  nn.ModuleList([]   )
							# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
							self.stages.append(
							    ResNetStage(
							        UpperCAmelCase							,						config.embedding_size							,						config.hidden_sizes[0]							,						stride=2 if config.downsample_in_first_stage else 1							,						depth=config.depths[0]							,						)   )
							lowerCamelCase_					=  zip(config.hidden_sizes							,						config.hidden_sizes[1:]   )
							for (in_channels, out_channels), depth in zip(UpperCAmelCase							,						config.depths[1:]   ):
										self.stages.append(ResNetStage(UpperCAmelCase							,						UpperCAmelCase							,						UpperCAmelCase							,						depth=UpperCAmelCase   )   )





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase = False							,						UpperCAmelCase = True   ):
							lowerCamelCase_					=  () if output_hidden_states else None

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

										lowerCamelCase_					=  stage_module(UpperCAmelCase   )

							if output_hidden_states:
										lowerCamelCase_					=  hidden_states + (hidden_state,)

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

							return BaseModelOutputWithNoAttention(
							    last_hidden_state=UpperCAmelCase							,						hidden_states=UpperCAmelCase							,						)




class  __lowerCamelCase  ( lowerCAmelCase			):
				a__:      Tuple          =    ResNetConfig
				a__:      Tuple          =    'resnet'
				a__:      List[Any]          =    'pixel_values'
				a__:      Union[str, Any]          =    True




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							if isinstance(UpperCAmelCase							,						nn.Convad   ):
										nn.init.kaiming_normal_(module.weight							,						mode='''fan_out'''							,						nonlinearity='''relu'''   )
							elif isinstance(UpperCAmelCase							,						(nn.BatchNormad, nn.GroupNorm)   ):
										nn.init.constant_(module.weight							,						1   )
										nn.init.constant_(module.bias							,						0   )





				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase=False   ):
							if isinstance(UpperCAmelCase							,						UpperCAmelCase   ):
										lowerCamelCase_					=  value

A_    							=					R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

A_    							=					R"""
    Args:
        pixel_values (`torch.FloatTensor` 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.
"""




@add_start_docstrings(
    'The bare ResNet model outputting raw features without any specific head on top.'						,	lowerCAmelCase						,	)
class  __lowerCamelCase  ( lowerCAmelCase			):




				def __init__(				self							,						UpperCAmelCase   ):
							super().__init__(UpperCAmelCase   )
							lowerCamelCase_					=  config
							lowerCamelCase_					=  ResNetEmbeddings(UpperCAmelCase   )
							lowerCamelCase_					=  ResNetEncoder(UpperCAmelCase   )
							lowerCamelCase_					=  nn.AdaptiveAvgPoolad((1, 1)   )
							# Initialize weights and apply final processing
							self.post_init()





				@add_start_docstrings_to_model_forward(UpperCAmelCase   )
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC							,						output_type=UpperCAmelCase							,						config_class=_CONFIG_FOR_DOC							,						modality='''vision'''							,						expected_output=_EXPECTED_OUTPUT_SHAPE							,						)
				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase = None							,						UpperCAmelCase = None   ):
							lowerCamelCase_					=  (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowerCamelCase_					=  return_dict if return_dict is not None else self.config.use_return_dict

							lowerCamelCase_					=  self.embedder(UpperCAmelCase   )

							lowerCamelCase_					=  self.encoder(
							    UpperCAmelCase							,						output_hidden_states=UpperCAmelCase							,						return_dict=UpperCAmelCase   )

							lowerCamelCase_					=  encoder_outputs[0]

							lowerCamelCase_					=  self.pooler(UpperCAmelCase   )

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

							return BaseModelOutputWithPoolingAndNoAttention(
							    last_hidden_state=UpperCAmelCase							,						pooler_output=UpperCAmelCase							,						hidden_states=encoder_outputs.hidden_states							,						)




@add_start_docstrings(
    '\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    '						,	lowerCAmelCase						,	)
class  __lowerCamelCase  ( lowerCAmelCase			):




				def __init__(				self							,						UpperCAmelCase   ):
							super().__init__(UpperCAmelCase   )
							lowerCamelCase_					=  config.num_labels
							lowerCamelCase_					=  ResNetModel(UpperCAmelCase   )
							# classification head
							lowerCamelCase_					=  nn.Sequential(
							    nn.Flatten()							,						nn.Linear(config.hidden_sizes[-1]							,						config.num_labels   ) if config.num_labels > 0 else nn.Identity()							,						)
							# initialize weights and apply final processing
							self.post_init()





				@add_start_docstrings_to_model_forward(UpperCAmelCase   )
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT							,						output_type=UpperCAmelCase							,						config_class=_CONFIG_FOR_DOC							,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							,						)
				def        UpperCAmelCase__						(				self							,						UpperCAmelCase = None							,						UpperCAmelCase = None							,						UpperCAmelCase = None							,						UpperCAmelCase = None							,						):
							lowerCamelCase_					=  return_dict if return_dict is not None else self.config.use_return_dict

							lowerCamelCase_					=  self.resnet(UpperCAmelCase							,						output_hidden_states=UpperCAmelCase							,						return_dict=UpperCAmelCase   )

							lowerCamelCase_					=  outputs.pooler_output if return_dict else outputs[1]

							lowerCamelCase_					=  self.classifier(UpperCAmelCase   )

							lowerCamelCase_					=  None

							if labels is not None:
										if self.config.problem_type is None:
													if self.num_labels == 1:
																lowerCamelCase_					=  '''regression'''
													elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																lowerCamelCase_					=  '''single_label_classification'''
													else:
																lowerCamelCase_					=  '''multi_label_classification'''
										if self.config.problem_type == "regression":
													lowerCamelCase_					=  MSELoss()
													if self.num_labels == 1:
																lowerCamelCase_					=  loss_fct(logits.squeeze()							,						labels.squeeze()   )
													else:
																lowerCamelCase_					=  loss_fct(UpperCAmelCase							,						UpperCAmelCase   )
										elif self.config.problem_type == "single_label_classification":
													lowerCamelCase_					=  CrossEntropyLoss()
													lowerCamelCase_					=  loss_fct(logits.view(-1							,						self.num_labels   )							,						labels.view(-1   )   )
										elif self.config.problem_type == "multi_label_classification":
													lowerCamelCase_					=  BCEWithLogitsLoss()
													lowerCamelCase_					=  loss_fct(UpperCAmelCase							,						UpperCAmelCase   )

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

							return ImageClassifierOutputWithNoAttention(loss=UpperCAmelCase							,						logits=UpperCAmelCase							,						hidden_states=outputs.hidden_states   )




@add_start_docstrings(
    '\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    '						,	lowerCAmelCase						,	)
class  __lowerCamelCase  ( lowerCAmelCase						,	lowerCAmelCase			):




				def __init__(				self							,						UpperCAmelCase   ):
							super().__init__(UpperCAmelCase   )
							super()._init_backbone(UpperCAmelCase   )

							lowerCamelCase_					=  [config.embedding_size] + config.hidden_sizes
							lowerCamelCase_					=  ResNetEmbeddings(UpperCAmelCase   )
							lowerCamelCase_					=  ResNetEncoder(UpperCAmelCase   )

							# initialize weights and apply final processing
							self.post_init()





				@add_start_docstrings_to_model_forward(UpperCAmelCase   )
				@replace_return_docstrings(output_type=UpperCAmelCase							,						config_class=_CONFIG_FOR_DOC   )
				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase = None							,						UpperCAmelCase = None   ):
							lowerCamelCase_					=  return_dict if return_dict is not None else self.config.use_return_dict
							lowerCamelCase_					=  (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)

							lowerCamelCase_					=  self.embedder(UpperCAmelCase   )

							lowerCamelCase_					=  self.encoder(UpperCAmelCase							,						output_hidden_states=UpperCAmelCase							,						return_dict=UpperCAmelCase   )

							lowerCamelCase_					=  outputs.hidden_states

							lowerCamelCase_					=  ()
							for idx, stage in enumerate(self.stage_names   ):
										if stage in self.out_features:
													feature_maps += (hidden_states[idx],)

							if not return_dict:
										lowerCamelCase_					=  (feature_maps,)
										if output_hidden_states:
													output += (outputs.hidden_states,)
										return output

							return BackboneOutput(
							    feature_maps=UpperCAmelCase							,						hidden_states=outputs.hidden_states if output_hidden_states else None							,						attentions=UpperCAmelCase							,						)





"""simple docstring"""


import copy
import fnmatch
import json
import os
import pickle as pkl
import shutil
import sys
import tarfile
import tempfile
from collections import OrderedDict
from contextlib import contextmanager
from functools import partial
from hashlib import shaaaa
from io import BytesIO
from pathlib import Path
from urllib.parse import urlparse
from zipfile import ZipFile, is_zipfile

import cva
import numpy as np
import requests
import wget
from filelock import FileLock
from PIL import Image
from tqdm.auto import tqdm
from yaml import Loader, dump, load


try:
						import torch

						A_    							=					True
except ImportError:
						A_    							=					False


try:
						from torch.hub import _get_torch_home

						A_    							=					_get_torch_home()
except ImportError:
						A_    							=					os.path.expanduser(
						    os.getenv("""TORCH_HOME""", os.path.join(os.getenv("""XDG_CACHE_HOME""", """~/.cache"""), """torch"""))
						)

A_    							=					os.path.join(torch_cache_home, """transformers""")

A_    							=					"""https://cdn.huggingface.co"""
A_    							=					"""https://s3.amazonaws.com/models.huggingface.co/bert"""
A_    							=					"""/""".join(str(Path(__file__).resolve()).split("""/""")[:-1])
A_    							=					os.path.join(PATH, """config.yaml""")
A_    							=					os.path.join(PATH, """attributes.txt""")
A_    							=					os.path.join(PATH, """objects.txt""")
A_    							=					os.getenv("""PYTORCH_PRETRAINED_BERT_CACHE""", default_cache_path)
A_    							=					os.getenv("""PYTORCH_TRANSFORMERS_CACHE""", PYTORCH_PRETRAINED_BERT_CACHE)
A_    							=					os.getenv("""TRANSFORMERS_CACHE""", PYTORCH_TRANSFORMERS_CACHE)
A_    							=					"""pytorch_model.bin"""
A_    							=					"""config.yaml"""






def  lowercase						(	lowerCAmelCase__=OBJECTS	,lowerCAmelCase__=ATTRIBUTES						):
			lowerCamelCase_					=  []
			with open(lowerCAmelCase__						) as f:
						for object in f.readlines():
									vg_classes.append(object.split(''','''						)[0].lower().strip()						)

			lowerCamelCase_					=  []
			with open(lowerCAmelCase__						) as f:
						for object in f.readlines():
									vg_attrs.append(object.split(''','''						)[0].lower().strip()						)
			return vg_classes, vg_attrs






def  lowercase						(	lowerCAmelCase__						):
			lowerCamelCase_					=  OrderedDict()
			with open(lowerCAmelCase__	,'''rb'''						) as f:
						lowerCamelCase_					=  pkl.load(lowerCAmelCase__						)['''model''']
			for k in copy.deepcopy(list(ckp.keys()						)						):
						lowerCamelCase_					=  ckp.pop(lowerCAmelCase__						)
						if isinstance(lowerCAmelCase__	,np.ndarray						):
									lowerCamelCase_					=  torch.tensor(lowerCAmelCase__						)
						else:
									assert isinstance(lowerCAmelCase__	,torch.tensor						), type(lowerCAmelCase__						)
						lowerCamelCase_					=  v
			return r




class  __lowerCamelCase  :
				a__:      Union[str, Any]          =    {}




				def __init__(				self							,						UpperCAmelCase							,						UpperCAmelCase = "root"							,						UpperCAmelCase=0   ):
							lowerCamelCase_					=  name
							lowerCamelCase_					=  level
							lowerCamelCase_					=  {}
							for k, v in dictionary.items():
										if v is None:
													raise ValueError()
										lowerCamelCase_					=  copy.deepcopy(UpperCAmelCase   )
										lowerCamelCase_					=  copy.deepcopy(UpperCAmelCase   )
										if isinstance(UpperCAmelCase							,						UpperCAmelCase   ):
													lowerCamelCase_					=  Config(UpperCAmelCase							,						name=UpperCAmelCase							,						level=level + 1   )
										lowerCamelCase_					=  v
										setattr(self							,						UpperCAmelCase							,						UpperCAmelCase   )

							lowerCamelCase_					=  d




				def __repr__(				self   ):
							return str(list((self._pointer.keys())   )   )




				def __setattr__(				self							,						UpperCAmelCase							,						UpperCAmelCase   ):
							lowerCamelCase_					=  val
							lowerCamelCase_					=  val
							lowerCamelCase_					=  key.split('''.'''   )
							lowerCamelCase_					=  len(UpperCAmelCase   ) - 1
							lowerCamelCase_					=  self._pointer
							if len(UpperCAmelCase   ) > 1:
										for i, l in enumerate(UpperCAmelCase   ):
													if hasattr(self							,						UpperCAmelCase   ) and isinstance(getattr(self							,						UpperCAmelCase   )							,						UpperCAmelCase   ):
																setattr(getattr(self							,						UpperCAmelCase   )							,						'''.'''.join(levels[i:]   )							,						UpperCAmelCase   )
													if l == last_level:
																lowerCamelCase_					=  val
													else:
																lowerCamelCase_					=  pointer[l]




				def        UpperCAmelCase__						(				self   ):
							return self._pointer




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase   ):
							with open(f"{file_name}"							,						'''w'''   ) as stream:
										dump(UpperCAmelCase							,						UpperCAmelCase   )




				def        UpperCAmelCase__						(				self							,						UpperCAmelCase							,						UpperCAmelCase   ):
							with open(f"{file_name}"							,						'''w'''   ) as stream:
										json.dump(UpperCAmelCase							,						UpperCAmelCase   )




				@staticmethod
				def        UpperCAmelCase__						(				UpperCAmelCase   ):
							with open(UpperCAmelCase   ) as stream:
										lowerCamelCase_					=  load(UpperCAmelCase							,						Loader=UpperCAmelCase   )
							return data




				def __str__(				self   ):
							lowerCamelCase_					=  '''    '''
							if self._name != "root":
										lowerCamelCase_					=  f"{t * (self._level-1)}{self._name}:\n"
							else:
										lowerCamelCase_					=  ''''''
							lowerCamelCase_					=  self._level
							for i, (k, v) in enumerate(self._pointer.items()   ):
										if isinstance(UpperCAmelCase							,						UpperCAmelCase   ):
													r += f"{t * (self._level)}{v}\n"
													self._level += 1
										else:
													r += f"{t * (self._level)}{k}: {v} ({type(UpperCAmelCase   ).__name__})\n"
										lowerCamelCase_					=  level
							return r[:-1]




				@classmethod
				def        UpperCAmelCase__						(				cls							,						UpperCAmelCase							,						**UpperCAmelCase   ):
							lowerCamelCase_		, lowerCamelCase_					=  cls.get_config_dict(UpperCAmelCase							,						**UpperCAmelCase   )
							return cls(UpperCAmelCase   )





				@classmethod
				def        UpperCAmelCase__						(				cls							,						UpperCAmelCase							,						**UpperCAmelCase   ):
							lowerCamelCase_					=  kwargs.pop('''cache_dir'''							,						UpperCAmelCase   )
							lowerCamelCase_					=  kwargs.pop('''force_download'''							,						UpperCAmelCase   )
							lowerCamelCase_					=  kwargs.pop('''resume_download'''							,						UpperCAmelCase   )
							lowerCamelCase_					=  kwargs.pop('''proxies'''							,						UpperCAmelCase   )
							lowerCamelCase_					=  kwargs.pop('''local_files_only'''							,						UpperCAmelCase   )

							if os.path.isdir(UpperCAmelCase   ):
										lowerCamelCase_					=  os.path.join(UpperCAmelCase							,						UpperCAmelCase   )
							elif os.path.isfile(UpperCAmelCase   ) or is_remote_url(UpperCAmelCase   ):
										lowerCamelCase_					=  pretrained_model_name_or_path
							else:
										lowerCamelCase_					=  hf_bucket_url(UpperCAmelCase							,						filename=UpperCAmelCase							,						use_cdn=UpperCAmelCase   )

							try:
										# Load from URL or cache if already cached
										lowerCamelCase_					=  cached_path(
										    UpperCAmelCase							,						cache_dir=UpperCAmelCase							,						force_download=UpperCAmelCase							,						proxies=UpperCAmelCase							,						resume_download=UpperCAmelCase							,						local_files_only=UpperCAmelCase							,						)
										# Load config dict
										if resolved_config_file is None:
													raise EnvironmentError

										lowerCamelCase_					=  Config.load_yaml(UpperCAmelCase   )

							except EnvironmentError:
										lowerCamelCase_					=  '''Can\'t load config for'''
										raise EnvironmentError(UpperCAmelCase   )

							if resolved_config_file == config_file:
										print('''loading configuration file from path'''   )
							else:
										print('''loading configuration file cache'''   )

							return Config.load_yaml(UpperCAmelCase   ), kwargs






def  lowercase						(	lowerCAmelCase__						):
			lowerCamelCase_					=  torch.load('''dump.pt'''	,map_location=in_tensor.device						)
			lowerCamelCase_					=  in_tensor.numpy()
			lowerCamelCase_					=  out_tensor.numpy()[0]
			print(na.shape	,na[0, 0, :5]						)
			print(na.shape	,na[0, 0, :5]						)
			assert np.allclose(lowerCAmelCase__	,lowerCAmelCase__	,rtol=0.01	,atol=0.1						), (
			    f"{sum([1 for x in np.isclose(lowerCAmelCase__	,lowerCAmelCase__	,rtol=0.01	,atol=0.1						).flatten() if x is False]						)/len(na.flatten()						)*100:.4f} %"
			    " element-wise mismatch"
			)
			raise Exception('''tensors are all good'''						)

			# Hugging face functions below






def  lowercase						(	lowerCAmelCase__						):
			lowerCamelCase_					=  urlparse(lowerCAmelCase__						)
			return parsed.scheme in ("http", "https")






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__=True						):
			lowerCamelCase_					=  CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX
			lowerCamelCase_					=  '''/''' not in model_id
			if legacy_format:
						return f"{endpoint}/{model_id}-{filename}"
			else:
						return f"{endpoint}/{model_id}/{filename}"






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__=None	,lowerCAmelCase__=0	,lowerCAmelCase__=None	,):
			lowerCamelCase_					=  '''python/{}'''.format(sys.version.split()[0]						)
			if _torch_available:
						ua += "; torch/{}".format(torch.__version__						)
			if isinstance(lowerCAmelCase__	,lowerCAmelCase__						):
						ua += "; " + "; ".join('''{}/{}'''.format(lowerCAmelCase__	,lowerCAmelCase__						) for k, v in user_agent.items()						)
			elif isinstance(lowerCAmelCase__	,lowerCAmelCase__						):
						ua += "; " + user_agent
			lowerCamelCase_					=  {'''user-agent''': ua}
			if resume_size > 0:
						lowerCamelCase_					=  '''bytes=%d-''' % (resume_size,)
			lowerCamelCase_					=  requests.get(lowerCAmelCase__	,stream=lowerCAmelCase__	,proxies=lowerCAmelCase__	,headers=lowerCAmelCase__						)
			if response.status_code == 416:  # Range not satisfiable
						return
			lowerCamelCase_					=  response.headers.get('''Content-Length'''						)
			lowerCamelCase_					=  resume_size + int(lowerCAmelCase__						) if content_length is not None else None
			lowerCamelCase_					=  tqdm(
			    unit='''B'''	,unit_scale=lowerCAmelCase__	,total=lowerCAmelCase__	,initial=lowerCAmelCase__	,desc='''Downloading'''	,)
			for chunk in response.iter_content(chunk_size=1_024						):
						if chunk:  # filter out keep-alive new chunks
									progress.update(len(lowerCAmelCase__						)						)
									temp_file.write(lowerCAmelCase__						)
			progress.close()






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__=None	,lowerCAmelCase__=False	,lowerCAmelCase__=None	,lowerCAmelCase__=10	,lowerCAmelCase__=False	,lowerCAmelCase__=None	,lowerCAmelCase__=False	,):
			if cache_dir is None:
						lowerCamelCase_					=  TRANSFORMERS_CACHE
			if isinstance(lowerCAmelCase__	,lowerCAmelCase__						):
						lowerCamelCase_					=  str(lowerCAmelCase__						)

			os.makedirs(lowerCAmelCase__	,exist_ok=lowerCAmelCase__						)

			lowerCamelCase_					=  None
			if not local_files_only:
						try:
									lowerCamelCase_					=  requests.head(lowerCAmelCase__	,allow_redirects=lowerCAmelCase__	,proxies=lowerCAmelCase__	,timeout=lowerCAmelCase__						)
									if response.status_code == 200:
												lowerCamelCase_					=  response.headers.get('''ETag'''						)
						except (EnvironmentError, requests.exceptions.Timeout):
									# etag is already None
									pass

			lowerCamelCase_					=  url_to_filename(lowerCAmelCase__	,lowerCAmelCase__						)

			# get cache path to put the file
			lowerCamelCase_					=  os.path.join(lowerCAmelCase__	,lowerCAmelCase__						)

			# etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible.
			# try to get the last downloaded one
			if etag is None:
						if os.path.exists(lowerCAmelCase__						):
									return cache_path
						else:
									lowerCamelCase_					=  [
									    file
									    for file in fnmatch.filter(os.listdir(lowerCAmelCase__						)	,filename + '''.*'''						)
									    if not file.endswith('''.json'''						) and not file.endswith('''.lock'''						)
									]
									if len(lowerCAmelCase__						) > 0:
												return os.path.join(lowerCAmelCase__	,matching_files[-1]						)
									else:
												# If files cannot be found and local_files_only=True,
												# the models might've been found if local_files_only=False
												# Notify the user about that
												if local_files_only:
															raise ValueError(
															    '''Cannot find the requested files in the cached path and outgoing traffic has been'''
															    ''' disabled. To enable model look-ups and downloads online, set \'local_files_only\''''
															    ''' to False.'''						)
												return None

    # From now on, etag is not None.
			if os.path.exists(lowerCAmelCase__						) and not force_download:
						return cache_path

			# Prevent parallel downloads of the same file with a lock.
			lowerCamelCase_					=  cache_path + '''.lock'''
			with FileLock(lowerCAmelCase__						):
						# If the download just completed while the lock was activated.
						if os.path.exists(lowerCAmelCase__						) and not force_download:
									# Even if returning early like here, the lock will be released.
									return cache_path

						if resume_download:
									lowerCamelCase_					=  cache_path + '''.incomplete'''

									@contextmanager
									def _resumable_file_manager():
												with open(lowerCAmelCase__	,'''a+b'''						) as f:
															yield f

									lowerCamelCase_					=  _resumable_file_manager
									if os.path.exists(lowerCAmelCase__						):
												lowerCamelCase_					=  os.stat(lowerCAmelCase__						).st_size
									else:
												lowerCamelCase_					=  0
						else:
									lowerCamelCase_					=  partial(tempfile.NamedTemporaryFile	,dir=lowerCAmelCase__	,delete=lowerCAmelCase__						)
									lowerCamelCase_					=  0

						# Download to temporary file, then copy to cache dir once finished.
						# Otherwise you get corrupt cache entries if the download gets interrupted.
						with temp_file_manager() as temp_file:
									print(
									    '''%s not found in cache or force_download set to True, downloading to %s'''	,lowerCAmelCase__	,temp_file.name	,)

									http_get(
									    lowerCAmelCase__	,lowerCAmelCase__	,proxies=lowerCAmelCase__	,resume_size=lowerCAmelCase__	,user_agent=lowerCAmelCase__	,)

						os.replace(temp_file.name	,lowerCAmelCase__						)

						lowerCamelCase_					=  {'''url''': url, '''etag''': etag}
						lowerCamelCase_					=  cache_path + '''.json'''
						with open(lowerCAmelCase__	,'''w'''						) as meta_file:
									json.dump(lowerCAmelCase__	,lowerCAmelCase__						)

			return cache_path






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__=None						):
			lowerCamelCase_					=  url.encode('''utf-8'''						)
			lowerCamelCase_					=  shaaaa(lowerCAmelCase__						)
			lowerCamelCase_					=  url_hash.hexdigest()

			if etag:
						lowerCamelCase_					=  etag.encode('''utf-8'''						)
						lowerCamelCase_					=  shaaaa(lowerCAmelCase__						)
						filename += "." + etag_hash.hexdigest()

			if url.endswith('''.h5'''						):
						filename += ".h5"

			return filename






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__=None	,lowerCAmelCase__=False	,lowerCAmelCase__=None	,lowerCAmelCase__=False	,lowerCAmelCase__=None	,lowerCAmelCase__=False	,lowerCAmelCase__=False	,lowerCAmelCase__=False	,):
			if cache_dir is None:
						lowerCamelCase_					=  TRANSFORMERS_CACHE
			if isinstance(lowerCAmelCase__	,lowerCAmelCase__						):
						lowerCamelCase_					=  str(lowerCAmelCase__						)
			if isinstance(lowerCAmelCase__	,lowerCAmelCase__						):
						lowerCamelCase_					=  str(lowerCAmelCase__						)

			if is_remote_url(lowerCAmelCase__						):
						# URL, so get it from the cache (downloading if necessary)
						lowerCamelCase_					=  get_from_cache(
						    lowerCAmelCase__	,cache_dir=lowerCAmelCase__	,force_download=lowerCAmelCase__	,proxies=lowerCAmelCase__	,resume_download=lowerCAmelCase__	,user_agent=lowerCAmelCase__	,local_files_only=lowerCAmelCase__	,)
			elif os.path.exists(lowerCAmelCase__						):
						# File, and it exists.
						lowerCamelCase_					=  url_or_filename
			elif urlparse(lowerCAmelCase__						).scheme == "":
						# File, but it doesn't exist.
						raise EnvironmentError('''file {} not found'''.format(lowerCAmelCase__						)						)
			else:
						# Something unknown
						raise ValueError('''unable to parse {} as a URL or as a local path'''.format(lowerCAmelCase__						)						)

			if extract_compressed_file:
						if not is_zipfile(lowerCAmelCase__						) and not tarfile.is_tarfile(lowerCAmelCase__						):
									return output_path

						# Path where we extract compressed archives
						# We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/"
						lowerCamelCase_		, lowerCamelCase_					=  os.path.split(lowerCAmelCase__						)
						lowerCamelCase_					=  output_file.replace('''.'''	,'''-'''						) + '''-extracted'''
						lowerCamelCase_					=  os.path.join(lowerCAmelCase__	,lowerCAmelCase__						)

						if os.path.isdir(lowerCAmelCase__						) and os.listdir(lowerCAmelCase__						) and not force_extract:
									return output_path_extracted

						# Prevent parallel extractions
						lowerCamelCase_					=  output_path + '''.lock'''
						with FileLock(lowerCAmelCase__						):
									shutil.rmtree(lowerCAmelCase__	,ignore_errors=lowerCAmelCase__						)
									os.makedirs(lowerCAmelCase__						)
									if is_zipfile(lowerCAmelCase__						):
												with ZipFile(lowerCAmelCase__	,'''r'''						) as zip_file:
															zip_file.extractall(lowerCAmelCase__						)
															zip_file.close()
									elif tarfile.is_tarfile(lowerCAmelCase__						):
												lowerCamelCase_					=  tarfile.open(lowerCAmelCase__						)
												tar_file.extractall(lowerCAmelCase__						)
												tar_file.close()
									else:
												raise EnvironmentError('''Archive format of {} could not be identified'''.format(lowerCAmelCase__						)						)

						return output_path_extracted

			return output_path






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__=","						):
			assert isinstance(lowerCAmelCase__	,lowerCAmelCase__						)
			if os.path.isfile(lowerCAmelCase__						):
						with open(lowerCAmelCase__						) as f:
									lowerCamelCase_					=  eval(f.read()						)
			else:
						lowerCamelCase_					=  requests.get(lowerCAmelCase__						)
						try:
									lowerCamelCase_					=  requests.json()
						except Exception:
									lowerCamelCase_					=  req.content.decode()
									assert data is not None, "could not connect"
									try:
												lowerCamelCase_					=  eval(lowerCAmelCase__						)
									except Exception:
												lowerCamelCase_					=  data.split('''\n'''						)
						req.close()
			return data






def  lowercase						(	lowerCAmelCase__						):
			lowerCamelCase_					=  requests.get(lowerCAmelCase__						)
			lowerCamelCase_					=  np.array(Image.open(BytesIO(response.content						)						)						)
			return img






def  lowercase						(	lowerCAmelCase__						):
			lowerCamelCase_					=  url.split('''/'''						)[-1]
			if fn not in os.listdir(os.getcwd()						):
						wget.download(lowerCAmelCase__						)
			with open(lowerCAmelCase__	,'''rb'''						) as stream:
						lowerCamelCase_					=  pkl.load(lowerCAmelCase__						)
			lowerCamelCase_					=  weights.pop('''model'''						)
			lowerCamelCase_					=  {}
			for k, v in model.items():
						lowerCamelCase_					=  torch.from_numpy(lowerCAmelCase__						)
						if "running_var" in k:
									lowerCamelCase_					=  torch.tensor([0]						)
									lowerCamelCase_					=  k.replace('''running_var'''	,'''num_batches_tracked'''						)
									lowerCamelCase_					=  zero
			return new






def  lowercase						(	):
			print(f"{os.path.abspath(os.path.join(lowerCAmelCase__	,os.pardir						)						)}/demo.ipynb"						)






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__="RGB"						):
			assert isinstance(lowerCAmelCase__	,lowerCAmelCase__						)
			if os.path.isfile(lowerCAmelCase__						):
						lowerCamelCase_					=  cva.imread(lowerCAmelCase__						)
			else:
						lowerCamelCase_					=  get_image_from_url(lowerCAmelCase__						)
						assert img is not None, f"could not connect to: {im}"
			lowerCamelCase_					=  cva.cvtColor(lowerCAmelCase__	,cva.COLOR_BGR2RGB						)
			if input_format == "RGB":
						lowerCamelCase_					=  img[:, :, ::-1]
			return img






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__=1						):
			return (images[i : i + batch] for i in range(0	,len(lowerCAmelCase__						)	,lowerCAmelCase__						))



