





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





def      lowerCAmelCase_ (       __a	,    __a	,    __a		)     ->		Dict:







  """simple docstring"""





  lowerCamelCase__:      str			  =os.path.abspath(__a		)
  logger.info(F"""Converting TensorFlow checkpoint from {tf_path}"""		)
  # Load weights from TF model
  lowerCamelCase__:      Any			  =tf.train.list_variables(__a		)
  lowerCamelCase__:      Dict			  =[]
  lowerCamelCase__:      List[Any]			  =[]
  lowerCamelCase__:      Optional[int]			  =[]
  for full_name, shape in init_vars:
    # logger.info(f"Loading TF weight {name} with shape {shape}")
    lowerCamelCase__:      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'
      lowerCamelCase__:      int			  =name[1:]
    # figure out how many levels deep the name is
    lowerCamelCase__:      Optional[int]			  =0
    for _name in name:
      if _name.startswith("layer_with_weights"		):
        depth += 1
      else:
        break
    layer_depth.append(__a		)
    # read data
    lowerCamelCase__:      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		)		)})"""		)
  lowerCamelCase__:      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		):
    lowerCamelCase__:      Tuple			  =full_name.split("/"		)
    lowerCamelCase__:      Union[str, Any]			  =model
    lowerCamelCase__:      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"		):
        lowerCamelCase__:      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"]		)
          lowerCamelCase__:      Optional[Any]			  =getattr(__a	,    "embeddings"		)
          lowerCamelCase__:      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		)]		)
          lowerCamelCase__:      Any			  =getattr(__a	,    "encoder"		)
          lowerCamelCase__:      str			  =getattr(__a	,    "layer"		)
          lowerCamelCase__:      Optional[Any]			  =pointer[layer_num - 4]
        elif layer_num == config.num_hidden_layers + 4:
          # pooler layer
          trace.extend(["pooler", "dense"]		)
          lowerCamelCase__:      Any			  =getattr(__a	,    "pooler"		)
          lowerCamelCase__:      Tuple			  =getattr(__a	,    "dense"		)
      elif m_name == "embeddings":
        trace.append("embeddings"		)
        lowerCamelCase__:      List[Any]			  =getattr(__a	,    "embeddings"		)
        if layer_num == 0:
          trace.append("word_embeddings"		)
          lowerCamelCase__:      List[str]			  =getattr(__a	,    "word_embeddings"		)
        elif layer_num == 1:
          trace.append("position_embeddings"		)
          lowerCamelCase__:      str			  =getattr(__a	,    "position_embeddings"		)
        elif layer_num == 2:
          trace.append("token_type_embeddings"		)
          lowerCamelCase__:      Dict			  =getattr(__a	,    "token_type_embeddings"		)
        else:
          raise ValueError(F"""Unknown embedding layer with name {full_name}"""		)
        trace.append("weight"		)
        lowerCamelCase__:      Optional[int]			  =getattr(__a	,    "weight"		)
      elif m_name == "_attention_layer":
        # self-attention layer
        trace.extend(["attention", "self"]		)
        lowerCamelCase__:      Any			  =getattr(__a	,    "attention"		)
        lowerCamelCase__:      Tuple			  =getattr(__a	,    "self"		)
      elif m_name == "_attention_layer_norm":
        # output attention norm
        trace.extend(["attention", "output", "LayerNorm"]		)
        lowerCamelCase__:      Optional[int]			  =getattr(__a	,    "attention"		)
        lowerCamelCase__:      Any			  =getattr(__a	,    "output"		)
        lowerCamelCase__:      Union[str, Any]			  =getattr(__a	,    "LayerNorm"		)
      elif m_name == "_attention_output_dense":
        # output attention dense
        trace.extend(["attention", "output", "dense"]		)
        lowerCamelCase__:      List[str]			  =getattr(__a	,    "attention"		)
        lowerCamelCase__:      Dict			  =getattr(__a	,    "output"		)
        lowerCamelCase__:      str			  =getattr(__a	,    "dense"		)
      elif m_name == "_output_dense":
        # output dense
        trace.extend(["output", "dense"]		)
        lowerCamelCase__:      Optional[Any]			  =getattr(__a	,    "output"		)
        lowerCamelCase__:      Tuple			  =getattr(__a	,    "dense"		)
      elif m_name == "_output_layer_norm":
        # output dense
        trace.extend(["output", "LayerNorm"]		)
        lowerCamelCase__:      List[Any]			  =getattr(__a	,    "output"		)
        lowerCamelCase__:      Optional[Any]			  =getattr(__a	,    "LayerNorm"		)
      elif m_name == "_key_dense":
        # attention key
        trace.append("key"		)
        lowerCamelCase__:      List[Any]			  =getattr(__a	,    "key"		)
      elif m_name == "_query_dense":
        # attention query
        trace.append("query"		)
        lowerCamelCase__:      Tuple			  =getattr(__a	,    "query"		)
      elif m_name == "_value_dense":
        # attention value
        trace.append("value"		)
        lowerCamelCase__:      Any			  =getattr(__a	,    "value"		)
      elif m_name == "_intermediate_dense":
        # attention intermediate dense
        trace.extend(["intermediate", "dense"]		)
        lowerCamelCase__:      Union[str, Any]			  =getattr(__a	,    "intermediate"		)
        lowerCamelCase__:      List[str]			  =getattr(__a	,    "dense"		)
      elif m_name == "_output_layer_norm":
        # output layer norm
        trace.append("output"		)
        lowerCamelCase__:      List[Any]			  =getattr(__a	,    "output"		)
      # weights & biases
      elif m_name in ["bias", "beta"]:
        trace.append("bias"		)
        lowerCamelCase__:      Any			  =getattr(__a	,    "bias"		)
      elif m_name in ["kernel", "gamma"]:
        trace.append("weight"		)
        lowerCamelCase__:      Any			  =getattr(__a	,    "weight"		)
      else:
        logger.warning(F"""Ignored {m_name}"""		)
        # for certain layers reshape is necessary
    lowerCamelCase__:      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		):
      lowerCamelCase__:      str			  =array.reshape(pointer.data.shape		)
    if "kernel" in full_name:
      lowerCamelCase__:      Tuple			  =array.transpose()
    if pointer.shape == array.shape:
      lowerCamelCase__:      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      lowerCAmelCase_ (       __a	,    __a	,    __a		)     ->		Tuple:







  """simple docstring"""





  logger.info(F"""Loading model based on config from {config_path}..."""		)
  lowerCamelCase__:      str			  =BertConfig.from_json_file(__a		)
  lowerCamelCase__:      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									=				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									=				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

