






from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
			from PIL import Image

			from ..image_utils import load_image

if is_torch_available():
			import torch

			from transformers.modeling_outputs import BaseModelOutput

			from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

_A     :      int   					=logging.get_logger(__name__)

@add_end_docstrings(__lowercase		)
class _lowercase     (    __lowercase		):




							def __init__(							self:    Dict      ,							**UpperCamelCase__:    int					):
														super().__init__(**UpperCamelCase__					)

														if self.framework == "tf":
																					raise ValueError(F'''The {self.__class__} is only available in PyTorch.'''					)

														requires_backends(self      ,							"""vision"""					)
														self.check_model_type(UpperCamelCase__					)




							def __call__(							self:    List[str]      ,							UpperCamelCase__:    Union[str, "Image.Image", List[Dict[str, Any]]]      ,							UpperCamelCase__:    Union[str, List[str]] = None      ,							**UpperCamelCase__:    Union[str, Any]      ,							):

														if "text_queries" in kwargs:
																					lowerCamelCase__ :     str	  =	kwargs.pop("""text_queries"""					)

														if isinstance(UpperCamelCase__      ,							(str, Image.Image)					):
																					lowerCamelCase__ :     Optional[Any]	  =	{"""image""": image, """candidate_labels""": candidate_labels}
														else:
																					lowerCamelCase__ :     List[str]	  =	image
														lowerCamelCase__ :     Dict	  =	super().__call__(UpperCamelCase__      ,							**UpperCamelCase__					)
														return results




							def 		lowerCamelCase_    (							self:    Any      ,							**UpperCamelCase__:    Optional[Any]					):
														lowerCamelCase__ :     Union[str, Any]	  =	{}
														if "threshold" in kwargs:
																					lowerCamelCase__ :     Optional[int]	  =	kwargs["""threshold"""]
														if "top_k" in kwargs:
																					lowerCamelCase__ :     Dict	  =	kwargs["""top_k"""]
														return {}, {}, postprocess_params




							def 		lowerCamelCase_    (							self:    Any      ,							UpperCamelCase__:    List[Any]					):
														lowerCamelCase__ :     int	  =	load_image(inputs["""image"""]					)
														lowerCamelCase__ :     Tuple	  =	inputs["""candidate_labels"""]
														if isinstance(UpperCamelCase__      ,							UpperCamelCase__					):
																					lowerCamelCase__ :     Union[str, Any]	  =	candidate_labels.split(""","""					)

														lowerCamelCase__ :     Optional[int]	  =	torch.tensor([[image.height, image.width]]      ,							dtype=torch.intaa					)
														for i, candidate_label in enumerate(UpperCamelCase__					):
																					lowerCamelCase__ :     int	  =	self.tokenizer(UpperCamelCase__      ,							return_tensors=self.framework					)
																					lowerCamelCase__ :     Union[str, Any]	  =	self.image_processor(UpperCamelCase__      ,							return_tensors=self.framework					)
																					yield {
																					    "is_last": i == len(UpperCamelCase__					) - 1,
																					    "target_size": target_size,
																					    "candidate_label": candidate_label,
																					    **text_inputs,
																					    **image_features,
																					}




							def 		lowerCamelCase_    (							self:    Any      ,							UpperCamelCase__:    str					):
														lowerCamelCase__ :     Optional[Any]	  =	model_inputs.pop("""target_size"""					)
														lowerCamelCase__ :     Dict	  =	model_inputs.pop("""candidate_label"""					)
														lowerCamelCase__ :     List[str]	  =	model_inputs.pop("""is_last"""					)

														lowerCamelCase__ :     str	  =	self.model(**UpperCamelCase__					)

														lowerCamelCase__ :     Dict	  =	{"""target_size""": target_size, """candidate_label""": candidate_label, """is_last""": is_last, **outputs}
														return model_outputs




							def 		lowerCamelCase_    (							self:    int      ,							UpperCamelCase__:    Union[str, Any]      ,							UpperCamelCase__:    Union[str, Any]=0.1      ,							UpperCamelCase__:    Tuple=None					):
														lowerCamelCase__ :     Optional[int]	  =	[]
														for model_output in model_outputs:
																					lowerCamelCase__ :     Dict	  =	model_output["""candidate_label"""]
																					lowerCamelCase__ :     Union[str, Any]	  =	BaseModelOutput(UpperCamelCase__					)
																					lowerCamelCase__ :     int	  =	self.image_processor.post_process_object_detection(
																					    outputs=UpperCamelCase__      ,							threshold=UpperCamelCase__      ,							target_sizes=model_output["""target_size"""]					)[0]

																					for index in outputs["scores"].nonzero():
																												lowerCamelCase__ :     Tuple	  =	outputs["""scores"""][index].item()
																												lowerCamelCase__ :     str	  =	self._get_bounding_box(outputs["""boxes"""][index][0]					)

																												lowerCamelCase__ :     List[str]	  =	{"""score""": score, """label""": label, """box""": box}
																												results.append(UpperCamelCase__					)

														lowerCamelCase__ :     Dict	  =	sorted(UpperCamelCase__      ,							key=lambda UpperCamelCase__					: x["score"]      ,							reverse=UpperCamelCase__					)
														if top_k:
																					lowerCamelCase__ :     str	  =	results[:top_k]

														return results




							def 		lowerCamelCase_    (							self:    Union[str, Any]      ,							UpperCamelCase__:    "torch.Tensor"					):

														if self.framework != "pt":
																					raise ValueError("""The ZeroShotObjectDetectionPipeline is only available in PyTorch."""					)
														lowerCamelCase__ :     List[str]	  =	box.int().tolist()
														lowerCamelCase__ :     List[Any]	  =	{
														    """xmin""": xmin,
														    """ymin""": ymin,
														    """xmax""": xmax,
														    """ymax""": ymax,
														}
														return bbox









'''simple docstring'''







import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor





def 			SCREAMING_SNAKE_CASE_	(UpperCamelCase				)				->	List[Any]:
							if "cls_token" in name:
														lowerCamelCase__ :     Any	  =	name.replace("""cls_token"""    ,      """vit.embeddings.cls_token"""				)
							if "mask_token" in name:
														lowerCamelCase__ :     Union[str, Any]	  =	name.replace("""mask_token"""    ,      """decoder.mask_token"""				)
							if "decoder_pos_embed" in name:
														lowerCamelCase__ :     Tuple	  =	name.replace("""decoder_pos_embed"""    ,      """decoder.decoder_pos_embed"""				)
							if "pos_embed" in name and "decoder" not in name:
														lowerCamelCase__ :     str	  =	name.replace("""pos_embed"""    ,      """vit.embeddings.position_embeddings"""				)
							if "patch_embed.proj" in name:
														lowerCamelCase__ :     Optional[int]	  =	name.replace("""patch_embed.proj"""    ,      """vit.embeddings.patch_embeddings.projection"""				)
							if "patch_embed.norm" in name:
														lowerCamelCase__ :     Any	  =	name.replace("""patch_embed.norm"""    ,      """vit.embeddings.norm"""				)
							if "decoder_blocks" in name:
														lowerCamelCase__ :     Dict	  =	name.replace("""decoder_blocks"""    ,      """decoder.decoder_layers"""				)
							if "blocks" in name:
														lowerCamelCase__ :     Union[str, Any]	  =	name.replace("""blocks"""    ,      """vit.encoder.layer"""				)
							if "attn.proj" in name:
														lowerCamelCase__ :     List[Any]	  =	name.replace("""attn.proj"""    ,      """attention.output.dense"""				)
							if "attn" in name:
														lowerCamelCase__ :     List[str]	  =	name.replace("""attn"""    ,      """attention.self"""				)
							if "norm1" in name:
														lowerCamelCase__ :     Any	  =	name.replace("""norm1"""    ,      """layernorm_before"""				)
							if "norm2" in name:
														lowerCamelCase__ :     Dict	  =	name.replace("""norm2"""    ,      """layernorm_after"""				)
							if "mlp.fc1" in name:
														lowerCamelCase__ :     str	  =	name.replace("""mlp.fc1"""    ,      """intermediate.dense"""				)
							if "mlp.fc2" in name:
														lowerCamelCase__ :     Union[str, Any]	  =	name.replace("""mlp.fc2"""    ,      """output.dense"""				)
							if "decoder_embed" in name:
														lowerCamelCase__ :     Tuple	  =	name.replace("""decoder_embed"""    ,      """decoder.decoder_embed"""				)
							if "decoder_norm" in name:
														lowerCamelCase__ :     Optional[int]	  =	name.replace("""decoder_norm"""    ,      """decoder.decoder_norm"""				)
							if "decoder_pred" in name:
														lowerCamelCase__ :     int	  =	name.replace("""decoder_pred"""    ,      """decoder.decoder_pred"""				)
							if "norm.weight" in name and "decoder" not in name:
														lowerCamelCase__ :     Union[str, Any]	  =	name.replace("""norm.weight"""    ,      """vit.layernorm.weight"""				)
							if "norm.bias" in name and "decoder" not in name:
														lowerCamelCase__ :     Dict	  =	name.replace("""norm.bias"""    ,      """vit.layernorm.bias"""				)

							return name





def 			SCREAMING_SNAKE_CASE_	(UpperCamelCase    ,      UpperCamelCase				)				->	Tuple:
							for key in orig_state_dict.copy().keys():
														lowerCamelCase__ :     List[str]	  =	orig_state_dict.pop(UpperCamelCase				)

														if "qkv" in key:
																					lowerCamelCase__ :     List[Any]	  =	key.split("""."""				)
																					lowerCamelCase__ :     Optional[int]	  =	int(key_split[1]				)
																					if "decoder_blocks" in key:
																												lowerCamelCase__ :     str	  =	config.decoder_hidden_size
																												lowerCamelCase__ :     List[Any]	  =	"""decoder.decoder_layers."""
																												if "weight" in key:
																																			lowerCamelCase__ :     int	  =	val[:dim, :]
																																			lowerCamelCase__ :     int	  =	val[dim : dim * 2, :]
																																			lowerCamelCase__ :     Tuple	  =	val[-dim:, :]
																												elif "bias" in key:
																																			lowerCamelCase__ :     Tuple	  =	val[:dim]
																																			lowerCamelCase__ :     Optional[int]	  =	val[dim : dim * 2]
																																			lowerCamelCase__ :     List[Any]	  =	val[-dim:]
																					else:
																												lowerCamelCase__ :     List[Any]	  =	config.hidden_size
																												lowerCamelCase__ :     Optional[int]	  =	"""vit.encoder.layer."""
																												if "weight" in key:
																																			lowerCamelCase__ :     str	  =	val[:dim, :]
																																			lowerCamelCase__ :     List[Any]	  =	val[dim : dim * 2, :]
																																			lowerCamelCase__ :     Optional[int]	  =	val[-dim:, :]
																												elif "bias" in key:
																																			lowerCamelCase__ :     int	  =	val[:dim]
																																			lowerCamelCase__ :     List[Any]	  =	val[dim : dim * 2]
																																			lowerCamelCase__ :     Optional[int]	  =	val[-dim:]

														else:
																					lowerCamelCase__ :     int	  =	val

							return orig_state_dict





def 			SCREAMING_SNAKE_CASE_	(UpperCamelCase    ,      UpperCamelCase				)				->	int:
							lowerCamelCase__ :     Any	  =	ViTMAEConfig()
							if "large" in checkpoint_url:
														lowerCamelCase__ :     Any	  =	1024
														lowerCamelCase__ :     Optional[Any]	  =	4096
														lowerCamelCase__ :     List[str]	  =	24
														lowerCamelCase__ :     Union[str, Any]	  =	16
							elif "huge" in checkpoint_url:
														lowerCamelCase__ :     List[str]	  =	14
														lowerCamelCase__ :     Dict	  =	1280
														lowerCamelCase__ :     Tuple	  =	5120
														lowerCamelCase__ :     List[str]	  =	32
														lowerCamelCase__ :     Union[str, Any]	  =	16

							lowerCamelCase__ :     List[Any]	  =	ViTMAEForPreTraining(UpperCamelCase				)

							lowerCamelCase__ :     str	  =	torch.hub.load_state_dict_from_url(UpperCamelCase    ,      map_location="""cpu"""				)["""model"""]

							lowerCamelCase__ :     Union[str, Any]	  =	ViTMAEImageProcessor(size=config.image_size				)

							lowerCamelCase__ :     List[str]	  =	convert_state_dict(UpperCamelCase    ,      UpperCamelCase				)

							model.load_state_dict(UpperCamelCase				)
							model.eval()

							lowerCamelCase__ :     Union[str, Any]	  =	"""https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg"""

							lowerCamelCase__ :     List[Any]	  =	Image.open(requests.get(UpperCamelCase    ,      stream=UpperCamelCase				).raw				)
							lowerCamelCase__ :     str	  =	ViTMAEImageProcessor(size=config.image_size				)
							lowerCamelCase__ :     Any	  =	image_processor(images=UpperCamelCase    ,      return_tensors="""pt"""				)

							# forward pass
							torch.manual_seed(2				)
							lowerCamelCase__ :     Optional[Any]	  =	model(**UpperCamelCase				)
							lowerCamelCase__ :     Optional[Any]	  =	outputs.logits

							if "large" in checkpoint_url:
														lowerCamelCase__ :     List[Any]	  =	torch.tensor(
														    [[-0.7309, -0.7128, -1.0169], [-1.0161, -0.9058, -1.1878], [-1.0478, -0.9411, -1.1911]]				)
							elif "huge" in checkpoint_url:
														lowerCamelCase__ :     Optional[Any]	  =	torch.tensor(
														    [[-1.1599, -0.9199, -1.2221], [-1.1952, -0.9269, -1.2307], [-1.2143, -0.9337, -1.2262]]				)
							else:
														lowerCamelCase__ :     int	  =	torch.tensor(
														    [[-0.9192, -0.8481, -1.1259], [-1.1349, -1.0034, -1.2599], [-1.1757, -1.0429, -1.2726]]				)

							# verify logits
							assert torch.allclose(logits[0, :3, :3]    ,      UpperCamelCase    ,      atol=1E-4				)

							print(f'''Saving model to {pytorch_dump_folder_path}'''				)
							model.save_pretrained(UpperCamelCase				)

							print(f'''Saving image processor to {pytorch_dump_folder_path}'''				)
							image_processor.save_pretrained(UpperCamelCase				)


if __name__ == "__main__":
			_A     :      str   					=argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    '''--checkpoint_url''',
			    default='''https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth''',
			    type=str,
			    help='''URL of the checkpoint you\'d like to convert.''',
			)
			parser.add_argument(
			    '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.'''
			)

			_A     :      Tuple   					=parser.parse_args()
			convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)

