




'''simple docstring'''



from collections import defaultdict
from typing import Optional

from ..image_utils import load_image
from ..utils import (
    add_end_docstrings,
    is_torch_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_torch_available():
 import torch

 from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING

lowerCamelCase_			=						logging.get_logger(__name__)







@add_end_docstrings(A       )
class 		lowercase_   (			A       ):


   """simple docstring"""

   def __init__(   self :     int  ,				**__lowerCamelCase :     Dict   ):

      """simple docstring"""
      super().__init__(**__lowerCamelCase   )
      requires_backends(self  ,				"vision"   )
      requires_backends(self  ,				"torch"   )

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

      self.check_model_type(__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Union[str, Any]  ,				**__lowerCamelCase :     List[str]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				{}
      _SCREAMING_SNAKE_CASE	=				{}
      _SCREAMING_SNAKE_CASE	=				{}
      # preprocess args
      if "points_per_batch" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["points_per_batch"]
      if "points_per_crop" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["points_per_crop"]
      if "crops_n_layers" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["crops_n_layers"]
      if "crop_overlap_ratio" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["crop_overlap_ratio"]
      if "crop_n_points_downscale_factor" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["crop_n_points_downscale_factor"]
      # postprocess args
      if "pred_iou_thresh" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["pred_iou_thresh"]
      if "stability_score_offset" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["stability_score_offset"]
      if "mask_threshold" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["mask_threshold"]
      if "stability_score_thresh" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["stability_score_thresh"]
      if "crops_nms_thresh" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["crops_nms_thresh"]
      if "output_rle_mask" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["output_rle_mask"]
      if "output_bboxes_mask" in kwargs:
         _SCREAMING_SNAKE_CASE	=				kwargs["output_bboxes_mask"]
      return preprocess_kwargs, forward_params, postprocess_kwargs



   def __call__(   self :     Optional[Any]  ,				__lowerCamelCase :     List[Any]  ,				*__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     Optional[int]=None  ,				__lowerCamelCase :     List[Any]=None  ,				**__lowerCamelCase :     Tuple   ):

      """simple docstring"""
      return super().__call__(__lowerCamelCase  ,				*__lowerCamelCase  ,				num_workers=__lowerCamelCase  ,				batch_size=__lowerCamelCase  ,				**__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     List[Any]  ,				__lowerCamelCase :     Tuple  ,				__lowerCamelCase :     List[Any]=6_4  ,				__lowerCamelCase :     int = 0  ,				__lowerCamelCase :     float = 5_1_2 / 1_5_0_0  ,				__lowerCamelCase :     Optional[int] = 3_2  ,				__lowerCamelCase :     Optional[int] = 1  ,				):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				load_image(__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				self.image_processor.size["longest_edge"]
      _SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE	=				self.image_processor.generate_crop_boxes(
          __lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				self.image_processor(images=__lowerCamelCase  ,				return_tensors="pt"   )

      with self.device_placement():
         if self.framework == "pt":
            _SCREAMING_SNAKE_CASE	=				self.get_inference_context()
            with inference_context():
               _SCREAMING_SNAKE_CASE	=				self._ensure_tensor_on_device(__lowerCamelCase  ,				device=self.device   )
               _SCREAMING_SNAKE_CASE	=				self.model.get_image_embeddings(model_inputs.pop("pixel_values"   )   )
               _SCREAMING_SNAKE_CASE	=				image_embeddings

      _SCREAMING_SNAKE_CASE	=				grid_points.shape[1]
      _SCREAMING_SNAKE_CASE	=				points_per_batch if points_per_batch is not None else n_points

      if points_per_batch <= 0:
         raise ValueError(
             "Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. "
             "To return all points at once, set points_per_batch to None"   )

      for i in range(0  ,				__lowerCamelCase  ,				__lowerCamelCase   ):
         _SCREAMING_SNAKE_CASE	=				grid_points[:, i : i + points_per_batch, :, :]
         _SCREAMING_SNAKE_CASE	=				input_labels[:, i : i + points_per_batch]
         _SCREAMING_SNAKE_CASE	=				i == n_points - points_per_batch
         yield {
             "input_points": batched_points,
             "input_labels": labels,
             "input_boxes": crop_boxes,
             "is_last": is_last,
             **model_inputs,
         }



   def 							lowerCAmelCase_      (   self :     Any  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     Optional[Any]=0.8_8  ,				__lowerCamelCase :     Tuple=0.9_5  ,				__lowerCamelCase :     Any=0  ,				__lowerCamelCase :     Tuple=1  ,				):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				model_inputs.pop("input_boxes"   )
      _SCREAMING_SNAKE_CASE	=				model_inputs.pop("is_last"   )
      _SCREAMING_SNAKE_CASE	=				model_inputs.pop("original_sizes"   ).tolist()
      _SCREAMING_SNAKE_CASE	=				model_inputs.pop("reshaped_input_sizes"   ).tolist()

      _SCREAMING_SNAKE_CASE	=				self.model(**__lowerCamelCase   )

      # post processing happens here in order to avoid CPU GPU copies of ALL the masks
      _SCREAMING_SNAKE_CASE	=				model_outputs["pred_masks"]
      _SCREAMING_SNAKE_CASE	=				self.image_processor.post_process_masks(
          __lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				binarize=__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				model_outputs["iou_scores"]
      _SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE	=				self.image_processor.filter_masks(
          masks[0]  ,				iou_scores[0]  ,				original_sizes[0]  ,				input_boxes[0]  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				)
      return {
          "masks": masks,
          "is_last": is_last,
          "boxes": boxes,
          "iou_scores": iou_scores,
      }



   def 							lowerCAmelCase_      (   self :     Any  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     str=False  ,				__lowerCamelCase :     Any=False  ,				__lowerCamelCase :     str=0.7  ,				):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[]
      _SCREAMING_SNAKE_CASE	=				[]
      _SCREAMING_SNAKE_CASE	=				[]
      for model_output in model_outputs:
         all_scores.append(model_output.pop("iou_scores"   )   )
         all_masks.extend(model_output.pop("masks"   )   )
         all_boxes.append(model_output.pop("boxes"   )   )

      _SCREAMING_SNAKE_CASE	=				torch.cat(__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				torch.cat(__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE	=				self.image_processor.post_process_for_mask_generation(
          __lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase  ,				__lowerCamelCase   )

      _SCREAMING_SNAKE_CASE	=				defaultdict(__lowerCamelCase   )
      for output in model_outputs:
         for k, v in output.items():
            extra[k].append(__lowerCamelCase   )

      _SCREAMING_SNAKE_CASE	=				{}
      if output_rle_mask:
         _SCREAMING_SNAKE_CASE	=				rle_mask

      if output_bboxes_mask:
         _SCREAMING_SNAKE_CASE	=				bounding_boxes

      return {"masks": output_masks, "scores": iou_scores, **optional, **extra}







'''simple docstring'''



import argparse
import os

import transformers

from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS
from .utils import logging


logging.set_verbosity_info()

lowerCamelCase_			=						logging.get_logger(__name__)


lowerCamelCase_			=						{name: getattr(transformers, name + 'Fast') for name in SLOW_TO_FAST_CONVERTERS}
def 			SCREAMING_SNAKE_CASE_			(							__A    :			Optional[int]	,					__A    :			Tuple	,					__A    :			List[str]	,					__A    :			List[str]  )			->			List[Any]:
   if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES:
      raise ValueError(f"""Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys()  )}."""  )

   if tokenizer_name is None:
      _SCREAMING_SNAKE_CASE	=				TOKENIZER_CLASSES
   else:
      _SCREAMING_SNAKE_CASE	=				{tokenizer_name: getattr(__A	,					tokenizer_name + "Fast"  )}

   logger.info(f"""Loading tokenizer classes: {tokenizer_names}"""  )

   for tokenizer_name in tokenizer_names:
      _SCREAMING_SNAKE_CASE	=				TOKENIZER_CLASSES[tokenizer_name]

      _SCREAMING_SNAKE_CASE	=				True
      if checkpoint_name is None:
         _SCREAMING_SNAKE_CASE	=				list(tokenizer_class.max_model_input_sizes.keys()  )
      else:
         _SCREAMING_SNAKE_CASE	=				[checkpoint_name]

      logger.info(f"""For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}"""  )

      for checkpoint in checkpoint_names:
         logger.info(f"""Loading {tokenizer_class.__class__.__name__} {checkpoint}"""  )

         # Load tokenizer
         _SCREAMING_SNAKE_CASE	=				tokenizer_class.from_pretrained(__A	,					force_download=__A  )

         # Save fast tokenizer
         logger.info(f"""Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}"""  )

         # For organization names we create sub-directories
         if "/" in checkpoint:
            _SCREAMING_SNAKE_CASE,	_SCREAMING_SNAKE_CASE	=				checkpoint.split("/"  )
            _SCREAMING_SNAKE_CASE	=				os.path.join(__A	,					__A  )
         elif add_prefix:
            _SCREAMING_SNAKE_CASE	=				checkpoint
            _SCREAMING_SNAKE_CASE	=				dump_path
         else:
            _SCREAMING_SNAKE_CASE	=				None
            _SCREAMING_SNAKE_CASE	=				dump_path

         logger.info(f"""=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}"""  )

         if checkpoint in list(tokenizer.pretrained_vocab_files_map.values()  )[0]:
            _SCREAMING_SNAKE_CASE	=				list(tokenizer.pretrained_vocab_files_map.values()  )[0][checkpoint]
            _SCREAMING_SNAKE_CASE	=				file_path.split(__A  )[-1][0]
            if next_char == "/":
               _SCREAMING_SNAKE_CASE	=				os.path.join(__A	,					__A  )
               _SCREAMING_SNAKE_CASE	=				None

            logger.info(f"""=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}"""  )

         _SCREAMING_SNAKE_CASE	=				tokenizer.save_pretrained(
             __A	,					legacy_format=__A	,					filename_prefix=__A  )
         logger.info(f"""=> File names {file_names}"""  )

         for file_name in file_names:
            if not file_name.endswith("tokenizer.json"  ):
               os.remove(__A  )
               logger.info(f"""=> removing {file_name}"""  )


if __name__ == "__main__":
 lowerCamelCase_			=						argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '--dump_path', default=None, type=str, required=True, help='Path to output generated fast tokenizer files.'
 )
 parser.add_argument(
     '--tokenizer_name',
     default=None,
     type=str,
     help=(
         f'''Optional tokenizer type selected in the list of {list(TOKENIZER_CLASSES.keys())}. If not given, will '''
         'download and convert all the checkpoints from AWS.'
     ),
 )
 parser.add_argument(
     '--checkpoint_name',
     default=None,
     type=str,
     help='Optional checkpoint name. If not given, will download and convert the canonical checkpoints from AWS.',
 )
 parser.add_argument(
     '--force_download',
     action='store_true',
     help='Re-download checkpoints.',
 )
 lowerCamelCase_			=						parser.parse_args()

 convert_slow_checkpoint_to_fast(args.tokenizer_name, args.checkpoint_name, args.dump_path, args.force_download)

