


import argparse
import csv
import logging
import os
import random

import numpy as np
import torch
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
from tqdm import tqdm, trange

from transformers import (
    CONFIG_NAME,
    WEIGHTS_NAME,
    AdamW,
    OpenAIGPTDoubleHeadsModel,
    OpenAIGPTTokenizer,
    get_linear_schedule_with_warmup,
)


logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO
)
A      :    List[Any]		=					logging.getLogger(__name__)

def       lowercase_		(		_A			:    List[Any]    ,					_A			:    Any						):

      """simple docstring"""




      lowerCamelCase__		:				List[str]    =  np.argmax(UpperCAmelCase_    ,					axis=1						)
      return np.sum(outputs == labels						)

def       lowercase_		(		_A			:    List[str]						):

      """simple docstring"""




      with open(UpperCAmelCase_    ,					encoding="utf_8"						) as f:
            lowerCamelCase__		:				Union[str, Any]    =  csv.reader(UpperCAmelCase_						)
            lowerCamelCase__		:				Any    =  []
            next(UpperCAmelCase_						)  # skip the first line
            for line in tqdm(UpperCAmelCase_						):
                  output.append((" ".join(line[1:5]						), line[5], line[6], int(line[-1]						) - 1)						)
      return output

def       lowercase_		(		_A			:    Union[str, Any]    ,					_A			:    List[str]    ,					_A			:    List[str]    ,					_A			:    Optional[int]    ,					_A			:    Union[str, Any]    ,					_A			:    Optional[int]						):

      """simple docstring"""




      lowerCamelCase__		:				Optional[Any]    =  []
      for dataset in encoded_datasets:
            lowerCamelCase__		:				Union[str, Any]    =  len(UpperCAmelCase_						)
            lowerCamelCase__		:				Any    =  np.zeros((n_batch, 2, input_len)    ,					dtype=np.intaa						)
            lowerCamelCase__		:				Optional[int]    =  np.zeros((n_batch, 2)    ,					dtype=np.intaa						)
            lowerCamelCase__		:				List[Any]    =  np.full((n_batch, 2, input_len)    ,					fill_value=-100    ,					dtype=np.intaa						)
            lowerCamelCase__		:				Dict    =  np.zeros((n_batch,)    ,					dtype=np.intaa						)
            for (
                i,
                (story, conta, conta, mc_label),
            ) in enumerate(UpperCAmelCase_						):
                  lowerCamelCase__		:				List[Any]    =  [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token]
                  lowerCamelCase__		:				Union[str, Any]    =  [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token]
                  lowerCamelCase__		:				int    =  with_conta
                  lowerCamelCase__		:				Union[str, Any]    =  with_conta
                  lowerCamelCase__		:				Optional[Any]    =  len(UpperCAmelCase_						) - 1
                  lowerCamelCase__		:				Optional[int]    =  len(UpperCAmelCase_						) - 1
                  lowerCamelCase__		:				Union[str, Any]    =  with_conta
                  lowerCamelCase__		:				Tuple    =  with_conta
                  lowerCamelCase__		:				Optional[Any]    =  mc_label
            lowerCamelCase__		:				int    =  (input_ids, mc_token_ids, lm_labels, mc_labels)
            tensor_datasets.append(tuple(torch.tensor(UpperCAmelCase_						) for t in all_inputs						)						)
      return tensor_datasets

def       lowercase_		(		):

      """simple docstring"""




      lowerCamelCase__		:				Union[str, Any]    =  argparse.ArgumentParser()
      parser.add_argument("--model_name"    ,					type=UpperCAmelCase_    ,					default="openai-gpt"    ,					help="pretrained model name"						)
      parser.add_argument("--do_train"    ,					action="store_true"    ,					help="Whether to run training."						)
      parser.add_argument("--do_eval"    ,					action="store_true"    ,					help="Whether to run eval on the dev set."						)
      parser.add_argument(
          "--output_dir"    ,					default=UpperCAmelCase_    ,					type=UpperCAmelCase_    ,					required=UpperCAmelCase_    ,					help="The output directory where the model predictions and checkpoints will be written."    ,					)
      parser.add_argument("--train_dataset"    ,					type=UpperCAmelCase_    ,					default=""						)
      parser.add_argument("--eval_dataset"    ,					type=UpperCAmelCase_    ,					default=""						)
      parser.add_argument("--seed"    ,					type=UpperCAmelCase_    ,					default=42						)
      parser.add_argument("--num_train_epochs"    ,					type=UpperCAmelCase_    ,					default=3						)
      parser.add_argument("--train_batch_size"    ,					type=UpperCAmelCase_    ,					default=8						)
      parser.add_argument("--eval_batch_size"    ,					type=UpperCAmelCase_    ,					default=16						)
      parser.add_argument("--adam_epsilon"    ,					default=1E-8    ,					type=UpperCAmelCase_    ,					help="Epsilon for Adam optimizer."						)
      parser.add_argument("--max_grad_norm"    ,					type=UpperCAmelCase_    ,					default=1						)
      parser.add_argument(
          "--max_steps"    ,					default=-1    ,					type=UpperCAmelCase_    ,					help=(
              "If > 0: set total number of training                         steps to perform. Override num_train_epochs."
          )    ,					)
      parser.add_argument(
          "--gradient_accumulation_steps"    ,					type=UpperCAmelCase_    ,					default=1    ,					help="Number of updates steps to accumulate before                        performing a backward/update pass."    ,					)
      parser.add_argument("--learning_rate"    ,					type=UpperCAmelCase_    ,					default=6.25E-5						)
      parser.add_argument("--warmup_steps"    ,					default=0    ,					type=UpperCAmelCase_    ,					help="Linear warmup over warmup_steps."						)
      parser.add_argument("--lr_schedule"    ,					type=UpperCAmelCase_    ,					default="warmup_linear"						)
      parser.add_argument("--weight_decay"    ,					type=UpperCAmelCase_    ,					default=0.01						)
      parser.add_argument("--lm_coef"    ,					type=UpperCAmelCase_    ,					default=0.9						)
      parser.add_argument("--n_valid"    ,					type=UpperCAmelCase_    ,					default=374						)

      parser.add_argument("--server_ip"    ,					type=UpperCAmelCase_    ,					default=""    ,					help="Can be used for distant debugging."						)
      parser.add_argument("--server_port"    ,					type=UpperCAmelCase_    ,					default=""    ,					help="Can be used for distant debugging."						)
      lowerCamelCase__		:				Any    =  parser.parse_args()
      print(UpperCAmelCase_						)

      if args.server_ip and args.server_port:
            # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script
            import ptvsd

            print("Waiting for debugger attach"						)
            ptvsd.enable_attach(address=(args.server_ip, args.server_port)    ,					redirect_output=UpperCAmelCase_						)
            ptvsd.wait_for_attach()

      random.seed(args.seed						)
      np.random.seed(args.seed						)
      torch.manual_seed(args.seed						)
      torch.cuda.manual_seed_all(args.seed						)

      lowerCamelCase__		:				Tuple    =  torch.device("cuda" if torch.cuda.is_available() else "cpu"						)
      lowerCamelCase__		:				Tuple    =  torch.cuda.device_count()
      logger.info("device: {}, n_gpu {}".format(UpperCAmelCase_    ,					UpperCAmelCase_						)						)

      if not args.do_train and not args.do_eval:
            raise ValueError("At least one of `do_train` or `do_eval` must be True."						)

      if not os.path.exists(args.output_dir						):
            os.makedirs(args.output_dir						)

      # Load tokenizer and model
      # This loading functions also add new tokens and embeddings called `special tokens`
      # These new embeddings will be fine-tuned on the RocStories dataset
      lowerCamelCase__		:				Union[str, Any]    =  ['_start_', '_delimiter_', '_classify_']
      lowerCamelCase__		:				Dict    =  OpenAIGPTTokenizer.from_pretrained(args.model_name						)
      tokenizer.add_tokens(UpperCAmelCase_						)
      lowerCamelCase__		:				int    =  tokenizer.convert_tokens_to_ids(UpperCAmelCase_						)
      lowerCamelCase__		:				List[str]    =  OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name						)
      model.resize_token_embeddings(len(UpperCAmelCase_						)						)
      model.to(UpperCAmelCase_						)

      # Load and encode the datasets
      def tokenize_and_encode(_A			:    str						):
            if isinstance(UpperCAmelCase_    ,					UpperCAmelCase_						):
                  return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(UpperCAmelCase_						)						)
            elif isinstance(UpperCAmelCase_    ,					UpperCAmelCase_						):
                  return obj
            return [tokenize_and_encode(UpperCAmelCase_						) for o in obj]

      logger.info("Encoding dataset..."						)
      lowerCamelCase__		:				Union[str, Any]    =  load_rocstories_dataset(args.train_dataset						)
      lowerCamelCase__		:				int    =  load_rocstories_dataset(args.eval_dataset						)
      lowerCamelCase__		:				Union[str, Any]    =  (train_dataset, eval_dataset)
      lowerCamelCase__		:				int    =  tokenize_and_encode(UpperCAmelCase_						)

      # Compute the max input length for the Transformer
      lowerCamelCase__		:				Optional[int]    =  model.config.n_positions // 2 - 2
      lowerCamelCase__		:				Union[str, Any]    =  max(
          len(story[:max_length]						) + max(len(conta[:max_length]						)    ,					len(conta[:max_length]						)						) + 3
          for dataset in encoded_datasets
          for story, conta, conta, _ in dataset						)
      lowerCamelCase__		:				Tuple    =  min(UpperCAmelCase_    ,					model.config.n_positions						)  # Max size of input for the pre-trained model

      # Prepare inputs tensors and dataloaders
      lowerCamelCase__		:				List[Any]    =  pre_process_datasets(UpperCAmelCase_    ,					UpperCAmelCase_    ,					UpperCAmelCase_    ,					*UpperCAmelCase_						)
      lowerCamelCase__		:				Optional[Any]    =  tensor_datasets[0], tensor_datasets[1]

      lowerCamelCase__		:				Optional[int]    =  TensorDataset(*UpperCAmelCase_						)
      lowerCamelCase__		:				List[str]    =  RandomSampler(UpperCAmelCase_						)
      lowerCamelCase__		:				str    =  DataLoader(UpperCAmelCase_    ,					sampler=UpperCAmelCase_    ,					batch_size=args.train_batch_size						)

      lowerCamelCase__		:				Union[str, Any]    =  TensorDataset(*UpperCAmelCase_						)
      lowerCamelCase__		:				int    =  SequentialSampler(UpperCAmelCase_						)
      lowerCamelCase__		:				List[str]    =  DataLoader(UpperCAmelCase_    ,					sampler=UpperCAmelCase_    ,					batch_size=args.eval_batch_size						)

      # Prepare optimizer
      if args.do_train:
            if args.max_steps > 0:
                  lowerCamelCase__		:				List[Any]    =  args.max_steps
                  lowerCamelCase__		:				Any    =  args.max_steps // (len(UpperCAmelCase_						) // args.gradient_accumulation_steps) + 1
            else:
                  lowerCamelCase__		:				Optional[Any]    =  len(UpperCAmelCase_						) // args.gradient_accumulation_steps * args.num_train_epochs

            lowerCamelCase__		:				Optional[Any]    =  list(model.named_parameters()						)
            lowerCamelCase__		:				Optional[int]    =  ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
            lowerCamelCase__		:				List[str]    =  [
                {
                    'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay						)],
                    'weight_decay': args.weight_decay,
                },
                {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay						)], 'weight_decay': 0.0},
            ]
            lowerCamelCase__		:				Tuple    =  AdamW(UpperCAmelCase_    ,					lr=args.learning_rate    ,					eps=args.adam_epsilon						)
            lowerCamelCase__		:				List[str]    =  get_linear_schedule_with_warmup(
                UpperCAmelCase_    ,					num_warmup_steps=args.warmup_steps    ,					num_training_steps=UpperCAmelCase_						)

      if args.do_train:
            lowerCamelCase__		:				Any    =  0, 0, None
            model.train()
            for _ in trange(int(args.num_train_epochs						)    ,					desc="Epoch"						):
                  lowerCamelCase__		:				Tuple    =  0
                  lowerCamelCase__		:				str    =  0
                  lowerCamelCase__		:				Tuple    =  tqdm(UpperCAmelCase_    ,					desc="Training"						)
                  for step, batch in enumerate(UpperCAmelCase_						):
                        lowerCamelCase__		:				Dict    =  tuple(t.to(UpperCAmelCase_						) for t in batch						)
                        lowerCamelCase__		:				List[str]    =  batch
                        lowerCamelCase__		:				Dict    =  model(UpperCAmelCase_    ,					mc_token_ids=UpperCAmelCase_    ,					lm_labels=UpperCAmelCase_    ,					mc_labels=UpperCAmelCase_						)
                        lowerCamelCase__		:				Union[str, Any]    =  args.lm_coef * losses[0] + losses[1]
                        loss.backward()
                        optimizer.step()
                        scheduler.step()
                        optimizer.zero_grad()
                        tr_loss += loss.item()
                        lowerCamelCase__		:				Dict    =  (
                            loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item()
                        )
                        nb_tr_steps += 1
                        lowerCamelCase__		:				Tuple    =  'Training loss: {:.2e} lr: {:.2e}'.format(UpperCAmelCase_    ,					scheduler.get_lr()[0]						)

    # Save a trained model
      if args.do_train:
            # Save a trained model, configuration and tokenizer
            lowerCamelCase__		:				Union[str, Any]    =  model.module if hasattr(UpperCAmelCase_    ,					"module"						) else model  # Only save the model itself

            # If we save using the predefined names, we can load using `from_pretrained`
            lowerCamelCase__		:				str    =  os.path.join(args.output_dir    ,					UpperCAmelCase_						)
            lowerCamelCase__		:				Union[str, Any]    =  os.path.join(args.output_dir    ,					UpperCAmelCase_						)

            torch.save(model_to_save.state_dict()    ,					UpperCAmelCase_						)
            model_to_save.config.to_json_file(UpperCAmelCase_						)
            tokenizer.save_vocabulary(args.output_dir						)

            # Load a trained model and vocabulary that you have fine-tuned
            lowerCamelCase__		:				Optional[int]    =  OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir						)
            lowerCamelCase__		:				List[str]    =  OpenAIGPTTokenizer.from_pretrained(args.output_dir						)
            model.to(UpperCAmelCase_						)

      if args.do_eval:
            model.eval()
            lowerCamelCase__		:				Union[str, Any]    =  0, 0
            lowerCamelCase__		:				Union[str, Any]    =  0, 0
            for batch in tqdm(UpperCAmelCase_    ,					desc="Evaluating"						):
                  lowerCamelCase__		:				List[Any]    =  tuple(t.to(UpperCAmelCase_						) for t in batch						)
                  lowerCamelCase__		:				Tuple    =  batch
                  with torch.no_grad():
                        lowerCamelCase__		:				Optional[Any]    =  model(
                            UpperCAmelCase_    ,					mc_token_ids=UpperCAmelCase_    ,					lm_labels=UpperCAmelCase_    ,					mc_labels=UpperCAmelCase_						)

                  lowerCamelCase__		:				Optional[Any]    =  mc_logits.detach().cpu().numpy()
                  lowerCamelCase__		:				List[str]    =  mc_labels.to("cpu"						).numpy()
                  lowerCamelCase__		:				str    =  accuracy(UpperCAmelCase_    ,					UpperCAmelCase_						)

                  eval_loss += mc_loss.mean().item()
                  eval_accuracy += tmp_eval_accuracy

                  nb_eval_examples += input_ids.size(0						)
                  nb_eval_steps += 1

            lowerCamelCase__		:				Any    =  eval_loss / nb_eval_steps
            lowerCamelCase__		:				List[str]    =  eval_accuracy / nb_eval_examples
            lowerCamelCase__		:				Any    =  tr_loss / nb_tr_steps if args.do_train else None
            lowerCamelCase__		:				Union[str, Any]    =  {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss}

            lowerCamelCase__		:				List[Any]    =  os.path.join(args.output_dir    ,					"eval_results.txt"						)
            with open(UpperCAmelCase_    ,					"w"						) as writer:
                  logger.info("***** Eval results *****"						)
                  for key in sorted(result.keys()						):
                        logger.info("  %s = %s"    ,					UpperCAmelCase_    ,					str(result[key]						)						)
                        writer.write("%s = %s\n" % (key, str(result[key]						))						)


if __name__ == "__main__":
    main()






'''simple docstring'''



from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
      import torch

if is_tf_available():
      import tensorflow as tf





class    UpperCAmelCase_  (_UpperCAmelCase  ):





    """simple docstring"""






    lowerCamelCase  :					Dict							     =			['image_processor']
    lowerCamelCase  :					str							     =			'SamImageProcessor'







    def __init__(							self			,     SCREAMING_SNAKE_CASE_	)				->     List[str]:
         super().__init__(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Any								=		self.image_processor
         __lowerCamelCase   :	str								=		-10
         __lowerCamelCase   :	List[str]								=		self.image_processor.size['longest_edge']







    def __call__(							self			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_ = None			,     **SCREAMING_SNAKE_CASE_			,     )				->     BatchEncoding:
         __lowerCamelCase   :	Union[str, Any]								=		self.image_processor(
             SCREAMING_SNAKE_CASE_			,     return_tensors=SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_			,     )

         # pop arguments that are not used in the foward but used nevertheless
         __lowerCamelCase   :	List[Any]								=		encoding_image_processor['original_sizes']

         if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):  # Checks if Torch or TF tensor
              __lowerCamelCase   :	Dict								=		original_sizes.numpy()

         __lowerCamelCase				,       __lowerCamelCase				,       __lowerCamelCase   :	str								=		self._check_and_preprocess_points(
             input_points=SCREAMING_SNAKE_CASE_			,     input_labels=SCREAMING_SNAKE_CASE_			,     input_boxes=SCREAMING_SNAKE_CASE_			,     )

         __lowerCamelCase   :	Tuple								=		self._normalize_and_convert(
             SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     input_points=SCREAMING_SNAKE_CASE_			,     input_labels=SCREAMING_SNAKE_CASE_			,     input_boxes=SCREAMING_SNAKE_CASE_			,     return_tensors=SCREAMING_SNAKE_CASE_			,     )

         return encoding_image_processor







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_="pt"			,     )				->     Optional[int]:
         if input_points is not None:
              if len(SCREAMING_SNAKE_CASE_	) != len(SCREAMING_SNAKE_CASE_	):
                   __lowerCamelCase   :	Union[str, Any]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     original_sizes[0]	) for point in input_points
                   ]
              else:
                   __lowerCamelCase   :	List[Any]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                       for point, original_size in zip(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                   ]
              # check that all arrays have the same shape
              if not all(point.shape == input_points[0].shape for point in input_points	):
                   if input_labels is not None:
                        __lowerCamelCase				,       __lowerCamelCase   :	Tuple								=		self._pad_points_and_labels(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)

              __lowerCamelCase   :	Optional[int]								=		np.array(SCREAMING_SNAKE_CASE_	)

         if input_labels is not None:
              __lowerCamelCase   :	Optional[int]								=		np.array(SCREAMING_SNAKE_CASE_	)

         if input_boxes is not None:
              if len(SCREAMING_SNAKE_CASE_	) != len(SCREAMING_SNAKE_CASE_	):
                   __lowerCamelCase   :	Union[str, Any]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     original_sizes[0]			,     is_bounding_box=SCREAMING_SNAKE_CASE_	)
                       for box in input_boxes
                   ]
              else:
                   __lowerCamelCase   :	Optional[int]								=		[
                       self._normalize_coordinates(self.target_size			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     is_bounding_box=SCREAMING_SNAKE_CASE_	)
                       for box, original_size in zip(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                   ]
              __lowerCamelCase   :	Optional[int]								=		np.array(SCREAMING_SNAKE_CASE_	)

         if input_boxes is not None:
              if return_tensors == "pt":
                   __lowerCamelCase   :	Any								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # boxes batch size of 1 by default
                   __lowerCamelCase   :	Union[str, Any]								=		input_boxes.unsqueeze(1	) if len(input_boxes.shape	) != 3 else input_boxes
              elif return_tensors == "tf":
                   __lowerCamelCase   :	List[str]								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # boxes batch size of 1 by default
                   __lowerCamelCase   :	str								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     1	) if len(input_boxes.shape	) != 3 else input_boxes
              encoding_image_processor.update({'input_boxes': input_boxes}	)
         if input_points is not None:
              if return_tensors == "pt":
                   __lowerCamelCase   :	int								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		input_points.unsqueeze(1	) if len(input_points.shape	) != 4 else input_points
              elif return_tensors == "tf":
                   __lowerCamelCase   :	Dict								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Tuple								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     1	) if len(input_points.shape	) != 4 else input_points
              encoding_image_processor.update({'input_points': input_points}	)
         if input_labels is not None:
              if return_tensors == "pt":
                   __lowerCamelCase   :	List[Any]								=		torch.from_numpy(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		input_labels.unsqueeze(1	) if len(input_labels.shape	) != 3 else input_labels
              elif return_tensors == "tf":
                   __lowerCamelCase   :	str								=		tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)
                   # point batch size of 1 by default
                   __lowerCamelCase   :	Dict								=		tf.expand_dims(SCREAMING_SNAKE_CASE_			,     1	) if len(input_labels.shape	) != 3 else input_labels
              encoding_image_processor.update({'input_labels': input_labels}	)

         return encoding_image_processor







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     Tuple:
         __lowerCamelCase   :	List[str]								=		max([point.shape[0] for point in input_points]	)
         __lowerCamelCase   :	Union[str, Any]								=		[]
         for i, point in enumerate(SCREAMING_SNAKE_CASE_	):
              if point.shape[0] != expected_nb_points:
                   __lowerCamelCase   :	Optional[int]								=		np.concatenate(
                       [point, np.zeros((expected_nb_points - point.shape[0], 2)	) + self.point_pad_value]			,     axis=0	)
                   __lowerCamelCase   :	List[Any]								=		np.append(input_labels[i]			,     [self.point_pad_value]	)
              processed_input_points.append(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	List[Any]								=		processed_input_points
         return input_points, input_labels







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=False	)				->     np.ndarray:
         __lowerCamelCase				,       __lowerCamelCase   :	Tuple								=		original_size
         __lowerCamelCase				,       __lowerCamelCase   :	Optional[Any]								=		self.image_processor._get_preprocess_shape(SCREAMING_SNAKE_CASE_			,     longest_edge=SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Dict								=		deepcopy(SCREAMING_SNAKE_CASE_	).astype(SCREAMING_SNAKE_CASE_	)

         if is_bounding_box:
              __lowerCamelCase   :	Optional[int]								=		coords.reshape(-1			,     2			,     2	)

         __lowerCamelCase   :	List[Any]								=		coords[..., 0] * (new_w / old_w)
         __lowerCamelCase   :	Dict								=		coords[..., 1] * (new_h / old_h)

         if is_bounding_box:
              __lowerCamelCase   :	Tuple								=		coords.reshape(-1			,     4	)

         return coords







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     SCREAMING_SNAKE_CASE_=None			,     )				->     Optional[Any]:
         if input_points is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):  # Checks for TF or Torch tensor
                   __lowerCamelCase   :	List[str]								=		input_points.numpy().tolist()

              if not isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	) or not isinstance(input_points[0]			,     SCREAMING_SNAKE_CASE_	):
                   raise ValueError('Input points must be a list of list of floating points.'	)
              __lowerCamelCase   :	str								=		[np.array(SCREAMING_SNAKE_CASE_	) for input_point in input_points]
         else:
              __lowerCamelCase   :	Optional[Any]								=		None

         if input_labels is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):
                   __lowerCamelCase   :	Union[str, Any]								=		input_labels.numpy().tolist()

              if not isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	) or not isinstance(input_labels[0]			,     SCREAMING_SNAKE_CASE_	):
                   raise ValueError('Input labels must be a list of list integers.'	)
              __lowerCamelCase   :	Any								=		[np.array(SCREAMING_SNAKE_CASE_	) for label in input_labels]
         else:
              __lowerCamelCase   :	Dict								=		None

         if input_boxes is not None:
              if hasattr(SCREAMING_SNAKE_CASE_			,     'numpy'	):
                   __lowerCamelCase   :	int								=		input_boxes.numpy().tolist()

              if (
                  not isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
                  or not isinstance(input_boxes[0]			,     SCREAMING_SNAKE_CASE_	)
                  or not isinstance(input_boxes[0][0]			,     SCREAMING_SNAKE_CASE_	)
              ):
                   raise ValueError('Input boxes must be a list of list of list of floating points.'	)
              __lowerCamelCase   :	List[Any]								=		[np.array(SCREAMING_SNAKE_CASE_	).astype(np.floataa	) for box in input_boxes]
         else:
              __lowerCamelCase   :	Tuple								=		None

         return input_points, input_labels, input_boxes







    @property
    def       lowercase_				(							self	)				->     Dict:
         __lowerCamelCase   :	Any								=		self.image_processor.model_input_names
         return list(dict.fromkeys(SCREAMING_SNAKE_CASE_	)	)







    def       lowercase_				(							self			,     *SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)				->     Optional[Any]:
         return self.image_processor.post_process_masks(*SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)






