

'''simple docstring'''

import argparse
import os
import re

import tensorflow as tf
import torch

from transformers import BertConfig, BertModel
from transformers.utils import logging


logging.set_verbosity_info()
a   :			Optional[int]         = logging.get_logger(__name__)





def       lowercase					(     __magic_name__ ,    __magic_name__ ,    __magic_name__							):



					'''simple docstring'''







					UpperCAmelCase		:							str   					=   os.path.abspath(__a							)
					logger.info(F"Converting TensorFlow checkpoint from {tf_path}"							)
					# Load weights from TF model
					UpperCAmelCase		:							Any   					=   tf.train.list_variables(__a							)
					UpperCAmelCase		:							Dict   					=   []
					UpperCAmelCase		:							List[Any]   					=   []
					UpperCAmelCase		:							Optional[int]   					=   []
					for full_name, shape in init_vars:
										# logger.info(f"Loading TF weight {name} with shape {shape}")
										UpperCAmelCase		:							List[str]   					=   full_name.split("/"							)
										if full_name == "_CHECKPOINTABLE_OBJECT_GRAPH" or name[0] in ["global_step", "save_counter"]:
															logger.info(F"Skipping non-model layer {full_name}"							)
															continue
										if "optimizer" in full_name:
															logger.info(F"Skipping optimization layer {full_name}"							)
															continue
										if name[0] == "model":
															# ignore initial 'model'
															UpperCAmelCase		:							int   					=   name[1:]
										# figure out how many levels deep the name is
										UpperCAmelCase		:							Optional[int]   					=   0
										for _name in name:
															if _name.startswith("layer_with_weights"							):
																				depth += 1
															else:
																				break
										layer_depth.append(__a							)
										# read data
										UpperCAmelCase		:							List[Any]   					=   tf.train.load_variable(__a ,    __a							)
										names.append("/".join(__a							)							)
										arrays.append(__a							)
					logger.info(F"Read a total of {len(__a							):,} layers"							)

					# Sanity check
					if len(set(__a							)							) != 1:
										raise ValueError(F"Found layer names with different depths (layer depth {list(set(__a							)							)})"							)
					UpperCAmelCase		:							Optional[Any]   					=   list(set(__a							)							)[0]
					if layer_depth != 1:
										raise ValueError(
										    "The model contains more than just the embedding/encoder layers. This script does not handle MLM/NSP"
										    " heads."							)

					# convert layers
					logger.info("Converting weights..."							)
					for full_name, array in zip(__a ,    __a							):
										UpperCAmelCase		:							Tuple   					=   full_name.split("/"							)
										UpperCAmelCase		:							Union[str, Any]   					=   model
										UpperCAmelCase		:							Dict   					=   []
										for i, m_name in enumerate(__a							):
															if m_name == ".ATTRIBUTES":
																				# variable names end with .ATTRIBUTES/VARIABLE_VALUE
																				break
															if m_name.startswith("layer_with_weights"							):
																				UpperCAmelCase		:							Optional[Any]   					=   int(m_name.split("-"							)[-1]							)
																				if layer_num <= 2:
																									# embedding layers
																									# layer_num 0: word_embeddings
																									# layer_num 1: position_embeddings
																									# layer_num 2: token_type_embeddings
																									continue
																				elif layer_num == 3:
																									# embedding LayerNorm
																									trace.extend(["embeddings", "LayerNorm"]							)
																									UpperCAmelCase		:							Optional[Any]   					=   getattr(__a ,    "embeddings"							)
																									UpperCAmelCase		:							Union[str, Any]   					=   getattr(__a ,    "LayerNorm"							)
																				elif layer_num > 3 and layer_num < config.num_hidden_layers + 4:
																									# encoder layers
																									trace.extend(["encoder", "layer", str(layer_num - 4							)]							)
																									UpperCAmelCase		:							Any   					=   getattr(__a ,    "encoder"							)
																									UpperCAmelCase		:							str   					=   getattr(__a ,    "layer"							)
																									UpperCAmelCase		:							Optional[Any]   					=   pointer[layer_num - 4]
																				elif layer_num == config.num_hidden_layers + 4:
																									# pooler layer
																									trace.extend(["pooler", "dense"]							)
																									UpperCAmelCase		:							Any   					=   getattr(__a ,    "pooler"							)
																									UpperCAmelCase		:							Tuple   					=   getattr(__a ,    "dense"							)
															elif m_name == "embeddings":
																				trace.append("embeddings"							)
																				UpperCAmelCase		:							List[Any]   					=   getattr(__a ,    "embeddings"							)
																				if layer_num == 0:
																									trace.append("word_embeddings"							)
																									UpperCAmelCase		:							List[str]   					=   getattr(__a ,    "word_embeddings"							)
																				elif layer_num == 1:
																									trace.append("position_embeddings"							)
																									UpperCAmelCase		:							str   					=   getattr(__a ,    "position_embeddings"							)
																				elif layer_num == 2:
																									trace.append("token_type_embeddings"							)
																									UpperCAmelCase		:							Dict   					=   getattr(__a ,    "token_type_embeddings"							)
																				else:
																									raise ValueError(F"Unknown embedding layer with name {full_name}"							)
																				trace.append("weight"							)
																				UpperCAmelCase		:							Optional[int]   					=   getattr(__a ,    "weight"							)
															elif m_name == "_attention_layer":
																				# self-attention layer
																				trace.extend(["attention", "self"]							)
																				UpperCAmelCase		:							Any   					=   getattr(__a ,    "attention"							)
																				UpperCAmelCase		:							Tuple   					=   getattr(__a ,    "self"							)
															elif m_name == "_attention_layer_norm":
																				# output attention norm
																				trace.extend(["attention", "output", "LayerNorm"]							)
																				UpperCAmelCase		:							Optional[int]   					=   getattr(__a ,    "attention"							)
																				UpperCAmelCase		:							Any   					=   getattr(__a ,    "output"							)
																				UpperCAmelCase		:							Union[str, Any]   					=   getattr(__a ,    "LayerNorm"							)
															elif m_name == "_attention_output_dense":
																				# output attention dense
																				trace.extend(["attention", "output", "dense"]							)
																				UpperCAmelCase		:							List[str]   					=   getattr(__a ,    "attention"							)
																				UpperCAmelCase		:							Dict   					=   getattr(__a ,    "output"							)
																				UpperCAmelCase		:							str   					=   getattr(__a ,    "dense"							)
															elif m_name == "_output_dense":
																				# output dense
																				trace.extend(["output", "dense"]							)
																				UpperCAmelCase		:							Optional[Any]   					=   getattr(__a ,    "output"							)
																				UpperCAmelCase		:							Tuple   					=   getattr(__a ,    "dense"							)
															elif m_name == "_output_layer_norm":
																				# output dense
																				trace.extend(["output", "LayerNorm"]							)
																				UpperCAmelCase		:							List[Any]   					=   getattr(__a ,    "output"							)
																				UpperCAmelCase		:							Optional[Any]   					=   getattr(__a ,    "LayerNorm"							)
															elif m_name == "_key_dense":
																				# attention key
																				trace.append("key"							)
																				UpperCAmelCase		:							List[Any]   					=   getattr(__a ,    "key"							)
															elif m_name == "_query_dense":
																				# attention query
																				trace.append("query"							)
																				UpperCAmelCase		:							Tuple   					=   getattr(__a ,    "query"							)
															elif m_name == "_value_dense":
																				# attention value
																				trace.append("value"							)
																				UpperCAmelCase		:							Any   					=   getattr(__a ,    "value"							)
															elif m_name == "_intermediate_dense":
																				# attention intermediate dense
																				trace.extend(["intermediate", "dense"]							)
																				UpperCAmelCase		:							Union[str, Any]   					=   getattr(__a ,    "intermediate"							)
																				UpperCAmelCase		:							List[str]   					=   getattr(__a ,    "dense"							)
															elif m_name == "_output_layer_norm":
																				# output layer norm
																				trace.append("output"							)
																				UpperCAmelCase		:							List[Any]   					=   getattr(__a ,    "output"							)
															# weights & biases
															elif m_name in ["bias", "beta"]:
																				trace.append("bias"							)
																				UpperCAmelCase		:							Any   					=   getattr(__a ,    "bias"							)
															elif m_name in ["kernel", "gamma"]:
																				trace.append("weight"							)
																				UpperCAmelCase		:							Any   					=   getattr(__a ,    "weight"							)
															else:
																				logger.warning(F"Ignored {m_name}"							)
																				# for certain layers reshape is necessary
										UpperCAmelCase		:							Dict   					=   ".".join(__a							)
										if re.match(R"(\S+)\.attention\.self\.(key|value|query)\.(bias|weight)" ,    __a							) or re.match(
										    R"(\S+)\.attention\.output\.dense\.weight" ,    __a							):
															UpperCAmelCase		:							str   					=   array.reshape(pointer.data.shape							)
										if "kernel" in full_name:
															UpperCAmelCase		:							Tuple   					=   array.transpose()
										if pointer.shape == array.shape:
															UpperCAmelCase		:							Optional[int]   					=   torch.from_numpy(__a							)
										else:
															raise ValueError(
															    F"Shape mismatch in layer {full_name}: Model expects shape {pointer.shape} but layer contains shape:"
															    F" {array.shape}"							)
										logger.info(F"Successfully set variable {full_name} to PyTorch layer {trace}"							)
					return model





def       lowercase					(     __magic_name__ ,    __magic_name__ ,    __magic_name__							):



					'''simple docstring'''







					logger.info(F"Loading model based on config from {config_path}..."							)
					UpperCAmelCase		:							str   					=   BertConfig.from_json_file(__a							)
					UpperCAmelCase		:							Union[str, Any]   					=   BertModel(__a							)

					# Load weights from checkpoint
					logger.info(F"Loading weights from checkpoint {tf_checkpoint_path}..."							)
					load_tfa_weights_in_bert(__a ,    __a ,    __a							)

					# Save pytorch-model
					logger.info(F"Saving PyTorch model to {pytorch_dump_path}..."							)
					torch.save(model.state_dict() ,    __a							)


if __name__ == "__main__":
				a   :			str         = argparse.ArgumentParser()
				parser.add_argument(
				    "--tf_checkpoint_path", type=str, required=True, help="Path to the TensorFlow 2.x checkpoint path."
				)
				parser.add_argument(
				    "--bert_config_file",
				    type=str,
				    required=True,
				    help="The config json file corresponding to the BERT model. This specifies the model architecture.",
				)
				parser.add_argument(
				    "--pytorch_dump_path",
				    type=str,
				    required=True,
				    help="Path to the output PyTorch model (must include filename).",
				)
				a   :			Union[str, Any]         = parser.parse_args()
				convert_tfa_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)







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									=				logging.get_logger(__name__)



@add_end_docstrings(__SCREAMING_SNAKE_CASE    )
class    _SCREAMING_SNAKE_CASE						(	__SCREAMING_SNAKE_CASE    ):







 '''simple docstring'''

 def __init__(self				:   Tuple       , **UpperCAmelCase_				:   Tuple)       ->Any:



   '''simple docstring'''



   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				:   Optional[int]       , UpperCAmelCase_				:   Union[str, "Image.Image", List[Dict[str, Any]]]       , UpperCAmelCase_				:   Union[str, List[str]] = None       , **UpperCAmelCase_				:   List[str]       , )       ->Union[str, Any]:



   '''simple docstring'''



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

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

 def 							SCREAMING_SNAKE_CASE_					(self				:   Optional[int]       , **UpperCAmelCase_				:   Union[str, Any])       ->Dict:



   '''simple docstring'''



   lowerCamelCase__:      List[str]			  ={}
   if "threshold" in kwargs:
     lowerCamelCase__:      List[Any]			  =kwargs["threshold"]
   if "top_k" in kwargs:
     lowerCamelCase__:      Any			  =kwargs["top_k"]
   return {}, {}, postprocess_params

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



   '''simple docstring'''



   lowerCamelCase__:      List[Any]			  =load_image(inputs["image"])
   lowerCamelCase__:      Dict			  =inputs["candidate_labels"]
   if isinstance(UpperCAmelCase_       , UpperCAmelCase_):
     lowerCamelCase__:      Any			  =candidate_labels.split(",")

   lowerCamelCase__:      Optional[int]			  =torch.tensor([[image.height, image.width]]       , dtype=torch.intaa)
   for i, candidate_label in enumerate(UpperCAmelCase_):
     lowerCamelCase__:      Dict			  =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 							SCREAMING_SNAKE_CASE_					(self				:   Optional[Any]       , UpperCAmelCase_				:   Tuple)       ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:      Dict			  =model_inputs.pop("target_size")
   lowerCamelCase__:      Dict			  =model_inputs.pop("candidate_label")
   lowerCamelCase__:      Dict			  =model_inputs.pop("is_last")

   lowerCamelCase__:      Union[str, Any]			  =self.model(**UpperCAmelCase_)

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

 def 							SCREAMING_SNAKE_CASE_					(self				:   Optional[int]       , UpperCAmelCase_				:   int       , UpperCAmelCase_				:   Any=0.1       , UpperCAmelCase_				:   str=None)       ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:      Union[str, Any]			  =[]
   for model_output in model_outputs:
     lowerCamelCase__:      Optional[Any]			  =model_output["candidate_label"]
     lowerCamelCase__:      Tuple			  =BaseModelOutput(UpperCAmelCase_)
     lowerCamelCase__:      Dict			  =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__:      Dict			  =outputs["scores"][index].item()
       lowerCamelCase__:      Dict			  =self._get_bounding_box(outputs["boxes"][index][0])

       lowerCamelCase__:      Optional[Any]			  ={"score": score, "label": label, "box": box}
       results.append(UpperCAmelCase_)

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

   return results







 def 							SCREAMING_SNAKE_CASE_					(self				:   str       , UpperCAmelCase_				:   "torch.Tensor")       ->Dict[str, int]:



   '''simple docstring'''



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

