

"""simple docstring"""






import sys
from typing import Tuple

import numpy as np
import torch
from PIL import Image
from torch import nn

from transformers.image_utils import PILImageResampling
from utils import img_tensorize
class        _lowercase					:







     def __init__(      self ,						UpperCamelCase_ ,						UpperCamelCase_=sys.maxsize       ):
      __magic_name__     =      '''bilinear'''
      __magic_name__     =      max_size
      __magic_name__     =      short_edge_length







     def __call__(      self ,						UpperCamelCase_       ):
      __magic_name__     =      []
      for img in imgs:
       __magic_name__   ,						__magic_name__     =      img.shape[:2]
       # later: provide list and randomly choose index for resize
       __magic_name__     =      np.random.randint(self.short_edge_length[0] ,						self.short_edge_length[1] + 1       )
       if size == 0:
        return img
       __magic_name__     =      size * 1.0 / min(UpperCamelCase_ ,						UpperCamelCase_       )
       if h < w:
        __magic_name__   ,						__magic_name__     =      size, scale * w
       else:
        __magic_name__   ,						__magic_name__     =      scale * h, size
       if max(UpperCamelCase_ ,						UpperCamelCase_       ) > self.max_size:
        __magic_name__     =      self.max_size * 1.0 / max(UpperCamelCase_ ,						UpperCamelCase_       )
        __magic_name__     =      newh * scale
        __magic_name__     =      neww * scale
       __magic_name__     =      int(neww + 0.5       )
       __magic_name__     =      int(newh + 0.5       )

       if img.dtype == np.uinta:
        __magic_name__     =      Image.fromarray(UpperCamelCase_       )
        __magic_name__     =      pil_image.resize((neww, newh) ,						PILImageResampling.BILINEAR       )
        __magic_name__     =      np.asarray(UpperCamelCase_       )
       else:
        __magic_name__     =      img.permute(2 ,						0 ,						1       ).unsqueeze(0       )  # 3, 0, 1)  # hw(c) -> nchw
        __magic_name__     =      nn.functional.interpolate(
            UpperCamelCase_ ,						(newh, neww) ,						mode=self.interp_method ,						align_corners=UpperCamelCase_       ).squeeze(0       )
       img_augs.append(UpperCamelCase_       )

      return img_augs


class        _lowercase					:







     def __init__(      self ,						UpperCamelCase_       ):
      __magic_name__     =      ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST] ,						cfg.INPUT.MAX_SIZE_TEST       )
      __magic_name__     =      cfg.INPUT.FORMAT
      __magic_name__     =      cfg.SIZE_DIVISIBILITY
      __magic_name__     =      cfg.PAD_VALUE
      __magic_name__     =      cfg.INPUT.MAX_SIZE_TEST
      __magic_name__     =      cfg.MODEL.DEVICE
      __magic_name__     =      torch.tensor(cfg.MODEL.PIXEL_STD       ).to(self.device       ).view(len(cfg.MODEL.PIXEL_STD       ) ,						1 ,						1       )
      __magic_name__     =      torch.tensor(cfg.MODEL.PIXEL_MEAN       ).to(self.device       ).view(len(cfg.MODEL.PIXEL_STD       ) ,						1 ,						1       )
      __magic_name__     =      lambda UpperCamelCase_       : (x - self.pixel_mean) / self.pixel_std







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_       ):
      __magic_name__     =      tuple(max(UpperCamelCase_       ) for s in zip(*[img.shape for img in images]       )       )
      __magic_name__     =      [im.shape[-2:] for im in images]
      __magic_name__     =      [
          nn.functional.pad(
              UpperCamelCase_ ,						[0, max_size[-1] - size[1], 0, max_size[-2] - size[0]] ,						value=self.pad_value ,						)
          for size, im in zip(UpperCamelCase_ ,						UpperCamelCase_       )
      ]

      return torch.stack(UpperCamelCase_       ), torch.tensor(UpperCamelCase_       )







     def __call__(      self ,						UpperCamelCase_ ,						UpperCamelCase_=False       ):
      with torch.no_grad():
       if not isinstance(UpperCamelCase_ ,						UpperCamelCase_       ):
        __magic_name__     =      [images]
       if single_image:
        assert len(UpperCamelCase_       ) == 1
       for i in range(len(UpperCamelCase_       )       ):
        if isinstance(images[i] ,						torch.Tensor       ):
         images.insert(UpperCamelCase_ ,						images.pop(UpperCamelCase_       ).to(self.device       ).float()       )
        elif not isinstance(images[i] ,						torch.Tensor       ):
         images.insert(
             UpperCamelCase_ ,						torch.as_tensor(img_tensorize(images.pop(UpperCamelCase_       ) ,						input_format=self.input_format       )       )
             .to(self.device       )
             .float() ,						)
            # resize smallest edge
       __magic_name__     =      torch.tensor([im.shape[:2] for im in images]       )
       __magic_name__     =      self.aug(UpperCamelCase_       )
       # transpose images and convert to torch tensors
       # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images]
       # now normalize before pad to avoid useless arithmetic
       __magic_name__     =      [self.normalizer(UpperCamelCase_       ) for x in images]
       # now pad them to do the following operations
       __magic_name__   ,						__magic_name__     =      self.pad(UpperCamelCase_       )
       # Normalize

       if self.size_divisibility > 0:
        raise NotImplementedError()
       # pad
       __magic_name__     =      torch.true_divide(UpperCamelCase_ ,						UpperCamelCase_       )
       if single_image:
        return images[0], sizes[0], scales_yx[0]
       else:
        return images, sizes, scales_yx





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase						) ->      List[str]:
 boxes[:, 0::2] *= scale_yx[:, 1]
 boxes[:, 1::2] *= scale_yx[:, 0]
 return boxes





def 	lowercase      (   __UpperCamelCase      ,   __UpperCamelCase						) ->      int:
 assert torch.isfinite(__UpperCamelCase						).all(), "Box tensor contains infinite or NaN!"
 __magic_name__   ,						__magic_name__     =      box_size
 tensor[:, 0].clamp_(min=0      ,   max=__UpperCamelCase						)
 tensor[:, 1].clamp_(min=0      ,   max=__UpperCamelCase						)
 tensor[:, 2].clamp_(min=0      ,   max=__UpperCamelCase						)
 tensor[:, 3].clamp_(min=0      ,   max=__UpperCamelCase						)




"""simple docstring"""






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


__lowerCamelCase										=		logging.get_logger(__name__)

__lowerCamelCase										=		{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

__lowerCamelCase										=		{
    "vocab_file": {
        "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt",
        "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt",
        "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/vocab.txt",
        "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/vocab.txt",
        "bert-base-multilingual-uncased": (
            "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt"
        ),
        "bert-base-multilingual-cased": "https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt",
        "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt",
        "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt",
        "bert-large-uncased-whole-word-masking": (
            "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt"
        ),
        "bert-large-cased-whole-word-masking": (
            "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt"
        ),
        "bert-large-uncased-whole-word-masking-finetuned-squad": (
            "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt"
        ),
        "bert-large-cased-whole-word-masking-finetuned-squad": (
            "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt"
        ),
        "bert-base-cased-finetuned-mrpc": (
            "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt"
        ),
        "bert-base-german-dbmdz-cased": "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt",
        "bert-base-german-dbmdz-uncased": (
            "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt"
        ),
        "TurkuNLP/bert-base-finnish-cased-v1": (
            "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt"
        ),
        "TurkuNLP/bert-base-finnish-uncased-v1": (
            "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt"
        ),
        "wietsedv/bert-base-dutch-cased": (
            "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json",
        "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json",
        "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json",
        "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json",
        "bert-base-multilingual-uncased": (
            "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json"
        ),
        "bert-base-multilingual-cased": (
            "https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json"
        ),
        "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json",
        "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json",
        "bert-large-uncased-whole-word-masking": (
            "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json"
        ),
        "bert-large-cased-whole-word-masking": (
            "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json"
        ),
        "bert-large-uncased-whole-word-masking-finetuned-squad": (
            "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json"
        ),
        "bert-large-cased-whole-word-masking-finetuned-squad": (
            "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json"
        ),
        "bert-base-cased-finetuned-mrpc": (
            "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json"
        ),
        "bert-base-german-dbmdz-cased": (
            "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json"
        ),
        "bert-base-german-dbmdz-uncased": (
            "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json"
        ),
        "TurkuNLP/bert-base-finnish-cased-v1": (
            "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json"
        ),
        "TurkuNLP/bert-base-finnish-uncased-v1": (
            "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json"
        ),
        "wietsedv/bert-base-dutch-cased": (
            "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json"
        ),
    },
}

__lowerCamelCase										=		{
    "bert-base-uncased": 5_12,
    "bert-large-uncased": 5_12,
    "bert-base-cased": 5_12,
    "bert-large-cased": 5_12,
    "bert-base-multilingual-uncased": 5_12,
    "bert-base-multilingual-cased": 5_12,
    "bert-base-chinese": 5_12,
    "bert-base-german-cased": 5_12,
    "bert-large-uncased-whole-word-masking": 5_12,
    "bert-large-cased-whole-word-masking": 5_12,
    "bert-large-uncased-whole-word-masking-finetuned-squad": 5_12,
    "bert-large-cased-whole-word-masking-finetuned-squad": 5_12,
    "bert-base-cased-finetuned-mrpc": 5_12,
    "bert-base-german-dbmdz-cased": 5_12,
    "bert-base-german-dbmdz-uncased": 5_12,
    "TurkuNLP/bert-base-finnish-cased-v1": 5_12,
    "TurkuNLP/bert-base-finnish-uncased-v1": 5_12,
    "wietsedv/bert-base-dutch-cased": 5_12,
}

__lowerCamelCase										=		{
    "bert-base-uncased": {"do_lower_case": True},
    "bert-large-uncased": {"do_lower_case": True},
    "bert-base-cased": {"do_lower_case": False},
    "bert-large-cased": {"do_lower_case": False},
    "bert-base-multilingual-uncased": {"do_lower_case": True},
    "bert-base-multilingual-cased": {"do_lower_case": False},
    "bert-base-chinese": {"do_lower_case": False},
    "bert-base-german-cased": {"do_lower_case": False},
    "bert-large-uncased-whole-word-masking": {"do_lower_case": True},
    "bert-large-cased-whole-word-masking": {"do_lower_case": False},
    "bert-large-uncased-whole-word-masking-finetuned-squad": {"do_lower_case": True},
    "bert-large-cased-whole-word-masking-finetuned-squad": {"do_lower_case": False},
    "bert-base-cased-finetuned-mrpc": {"do_lower_case": False},
    "bert-base-german-dbmdz-cased": {"do_lower_case": False},
    "bert-base-german-dbmdz-uncased": {"do_lower_case": True},
    "TurkuNLP/bert-base-finnish-cased-v1": {"do_lower_case": False},
    "TurkuNLP/bert-base-finnish-uncased-v1": {"do_lower_case": True},
    "wietsedv/bert-base-dutch-cased": {"do_lower_case": False},
}
class        _lowercase					(     __UpperCAmelCase     ):
     _lowerCamelCase     =       VOCAB_FILES_NAMES
     _lowerCamelCase     =       PRETRAINED_VOCAB_FILES_MAP
     _lowerCamelCase     =       PRETRAINED_INIT_CONFIGURATION
     _lowerCamelCase     =       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     _lowerCamelCase     =       BertTokenizer







     def __init__(      self ,						UpperCamelCase_=None ,						UpperCamelCase_=None ,						UpperCamelCase_=True ,						UpperCamelCase_="[UNK]" ,						UpperCamelCase_="[SEP]" ,						UpperCamelCase_="[PAD]" ,						UpperCamelCase_="[CLS]" ,						UpperCamelCase_="[MASK]" ,						UpperCamelCase_=True ,						UpperCamelCase_=None ,						**UpperCamelCase_ ,						):
      super().__init__(
          UpperCamelCase_ ,						tokenizer_file=UpperCamelCase_ ,						do_lower_case=UpperCamelCase_ ,						unk_token=UpperCamelCase_ ,						sep_token=UpperCamelCase_ ,						pad_token=UpperCamelCase_ ,						cls_token=UpperCamelCase_ ,						mask_token=UpperCamelCase_ ,						tokenize_chinese_chars=UpperCamelCase_ ,						strip_accents=UpperCamelCase_ ,						**UpperCamelCase_ ,						)

      __magic_name__     =      json.loads(self.backend_tokenizer.normalizer.__getstate__()       )
      if (
          normalizer_state.get('''lowercase''' ,						UpperCamelCase_       ) != do_lower_case
          or normalizer_state.get('''strip_accents''' ,						UpperCamelCase_       ) != strip_accents
          or normalizer_state.get('''handle_chinese_chars''' ,						UpperCamelCase_       ) != tokenize_chinese_chars
      ):
       __magic_name__     =      getattr(UpperCamelCase_ ,						normalizer_state.pop('''type'''       )       )
       __magic_name__     =      do_lower_case
       __magic_name__     =      strip_accents
       __magic_name__     =      tokenize_chinese_chars
       __magic_name__     =      normalizer_class(**UpperCamelCase_       )

      __magic_name__     =      do_lower_case







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_=None       ):
      __magic_name__     =      [self.cls_token_id] + token_ids_a + [self.sep_token_id]

      if token_ids_a:
       output += token_ids_a + [self.sep_token_id]

      return output







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ = None       ):
      __magic_name__     =      [self.sep_token_id]
      __magic_name__     =      [self.cls_token_id]
      if token_ids_a is None:
       return len(cls + token_ids_a + sep       ) * [0]
      return len(cls + token_ids_a + sep       ) * [0] + len(token_ids_a + sep       ) * [1]







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ = None       ):
      __magic_name__     =      self._tokenizer.model.save(UpperCamelCase_ ,						name=UpperCamelCase_       )
      return tuple(UpperCamelCase_       )

