


'''simple docstring'''





import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from torchvision import transforms

from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling
def      snake_case_						( __lowercase       ):
   UpperCAmelCase_							:      Union[str, Any]						=   [2, 2, 6, 2] if '''tiny''' in model_name else [2, 2, 1_8, 2]
   UpperCAmelCase_							:      List[Any]						=   True if '''large''' in model_name or '''huge''' in model_name else False
   UpperCAmelCase_							:      Tuple						=   True if '''large''' in model_name or '''huge''' in model_name else False
   UpperCAmelCase_							:      List[Any]						=   True if '''large''' in model_name or '''huge''' in model_name else False

   if "large" in model_name or "xlarge" in model_name or "huge" in model_name:
      if "fl3" in model_name:
         UpperCAmelCase_							:      List[Any]						=   [3, 3, 3, 3]
         UpperCAmelCase_							:      Union[str, Any]						=   [5, 5, 5, 5]
      elif "fl4" in model_name:
         UpperCAmelCase_							:      Tuple						=   [4, 4, 4, 4]
         UpperCAmelCase_							:      List[str]						=   [3, 3, 3, 3]

   if "tiny" in model_name or "small" in model_name or "base" in model_name:
      UpperCAmelCase_							:      Tuple						=   [3, 3, 3, 3]
      if "lrf" in model_name:
         UpperCAmelCase_							:      Dict						=   [3, 3, 3, 3]
      else:
         UpperCAmelCase_							:      Tuple						=   [2, 2, 2, 2]

   if "tiny" in model_name:
      UpperCAmelCase_							:      Optional[int]						=   9_6
   elif "small" in model_name:
      UpperCAmelCase_							:      Tuple						=   9_6
   elif "base" in model_name:
      UpperCAmelCase_							:      Optional[Any]						=   1_2_8
   elif "large" in model_name:
      UpperCAmelCase_							:      int						=   1_9_2
   elif "xlarge" in model_name:
      UpperCAmelCase_							:      Dict						=   2_5_6
   elif "huge" in model_name:
      UpperCAmelCase_							:      Union[str, Any]						=   3_5_2

   # set label information
   UpperCAmelCase_							:      Dict						=   '''huggingface/label-files'''
   if "large" in model_name or "huge" in model_name:
      UpperCAmelCase_							:      List[Any]						=   '''imagenet-22k-id2label.json'''
   else:
      UpperCAmelCase_							:      Union[str, Any]						=   '''imagenet-1k-id2label.json'''

   UpperCAmelCase_							:      int						=   json.load(open(hf_hub_download(__lowercase			,       __lowercase			,       repo_type='''dataset'''       )			,       '''r'''       )       )
   UpperCAmelCase_							:      Optional[Any]						=   {int(__lowercase       ): v for k, v in idalabel.items()}
   UpperCAmelCase_							:      Optional[Any]						=   {v: k for k, v in idalabel.items()}

   UpperCAmelCase_							:      List[str]						=   FocalNetConfig(
       embed_dim=__lowercase			,       depths=__lowercase			,       focal_levels=__lowercase			,       focal_windows=__lowercase			,       use_conv_embed=__lowercase			,       idalabel=__lowercase			,       labelaid=__lowercase			,       use_post_layernorm=__lowercase			,       use_layerscale=__lowercase			,       )

   return config
def      snake_case_						( __lowercase       ):
   if "patch_embed.proj" in name:
      UpperCAmelCase_							:      int						=   name.replace('''patch_embed.proj'''			,       '''embeddings.patch_embeddings.projection'''       )
   if "patch_embed.norm" in name:
      UpperCAmelCase_							:      Union[str, Any]						=   name.replace('''patch_embed.norm'''			,       '''embeddings.norm'''       )
   if "layers" in name:
      UpperCAmelCase_							:      Any						=   '''encoder.''' + name
   if "encoder.layers" in name:
      UpperCAmelCase_							:      Union[str, Any]						=   name.replace('''encoder.layers'''			,       '''encoder.stages'''       )
   if "downsample.proj" in name:
      UpperCAmelCase_							:      int						=   name.replace('''downsample.proj'''			,       '''downsample.projection'''       )
   if "blocks" in name:
      UpperCAmelCase_							:      List[str]						=   name.replace('''blocks'''			,       '''layers'''       )
   if "modulation.f.weight" in name or "modulation.f.bias" in name:
      UpperCAmelCase_							:      List[str]						=   name.replace('''modulation.f'''			,       '''modulation.projection_in'''       )
   if "modulation.h.weight" in name or "modulation.h.bias" in name:
      UpperCAmelCase_							:      Tuple						=   name.replace('''modulation.h'''			,       '''modulation.projection_context'''       )
   if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
      UpperCAmelCase_							:      str						=   name.replace('''modulation.proj'''			,       '''modulation.projection_out'''       )

   if name == "norm.weight":
      UpperCAmelCase_							:      Tuple						=   '''layernorm.weight'''
   if name == "norm.bias":
      UpperCAmelCase_							:      Dict						=   '''layernorm.bias'''

   if "head" in name:
      UpperCAmelCase_							:      Tuple						=   name.replace('''head'''			,       '''classifier'''       )
   else:
      UpperCAmelCase_							:      str						=   '''focalnet.''' + name

   return name
def      snake_case_						( __lowercase			,       __lowercase			,       __lowercase=False       ):
   # fmt: off
   UpperCAmelCase_							:      Union[str, Any]						=   {
       '''focalnet-tiny''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth''',
       '''focalnet-tiny-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth''',
       '''focalnet-small''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth''',
       '''focalnet-small-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth''',
       '''focalnet-base''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth''',
       '''focalnet-base-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth''',
       '''focalnet-large-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth''',
       '''focalnet-large-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth''',
       '''focalnet-xlarge-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth''',
       '''focalnet-xlarge-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth''',
   }
   # fmt: on

   UpperCAmelCase_							:      List[Any]						=   model_name_to_url[model_name]
   print('''Checkpoint URL: '''			,       __lowercase       )
   UpperCAmelCase_							:      List[str]						=   torch.hub.load_state_dict_from_url(__lowercase			,       map_location='''cpu'''       )['''model''']

   # rename keys
   for key in state_dict.copy().keys():
      UpperCAmelCase_							:      int						=   state_dict.pop(__lowercase       )
      UpperCAmelCase_							:      Optional[Any]						=   val

   UpperCAmelCase_							:      Optional[Any]						=   get_focalnet_config(__lowercase       )
   UpperCAmelCase_							:      Optional[int]						=   FocalNetForImageClassification(__lowercase       )
   model.eval()

   # load state dict
   model.load_state_dict(__lowercase       )

   # verify conversion
   UpperCAmelCase_							:      List[str]						=   '''http://images.cocodataset.org/val2017/000000039769.jpg'''

   UpperCAmelCase_							:      Optional[int]						=   BitImageProcessor(
       do_resize=__lowercase			,       size={'''shortest_edge''': 2_5_6}			,       resample=PILImageResampling.BILINEAR			,       do_center_crop=__lowercase			,       crop_size=2_2_4			,       do_normalize=__lowercase			,       image_mean=__lowercase			,       image_std=__lowercase			,       )
   UpperCAmelCase_							:      Optional[Any]						=   Image.open(requests.get(__lowercase			,       stream=__lowercase       ).raw       )
   UpperCAmelCase_							:      Union[str, Any]						=   processor(images=__lowercase			,       return_tensors='''pt'''       )

   UpperCAmelCase_							:      List[str]						=   transforms.Compose(
       [
           transforms.Resize(2_5_6       ),
           transforms.CenterCrop(2_2_4       ),
           transforms.ToTensor(),
           transforms.Normalize(mean=[0.4_8_5, 0.4_5_6, 0.4_0_6]			,       std=[0.2_2_9, 0.2_2_4, 0.2_2_5]       ),
       ]       )

   UpperCAmelCase_							:      str						=   image_transforms(__lowercase       ).unsqueeze(0       )

   # verify pixel_values
   assert torch.allclose(inputs.pixel_values			,       __lowercase			,       atol=1e-4       )

   UpperCAmelCase_							:      Union[str, Any]						=   model(**__lowercase       )

   UpperCAmelCase_							:      Any						=   outputs.logits.argmax(-1       ).item()
   print('''Predicted class:'''			,       model.config.idalabel[predicted_class_idx]       )

   print('''First values of logits:'''			,       outputs.logits[0, :3]       )

   if model_name == "focalnet-tiny":
      UpperCAmelCase_							:      List[Any]						=   torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]       )
   elif model_name == "focalnet-tiny-lrf":
      UpperCAmelCase_							:      Optional[Any]						=   torch.tensor([1.1_6_6_9, 0.0_1_2_5, -0.1_6_9_5]       )
   elif model_name == "focalnet-small":
      UpperCAmelCase_							:      Optional[Any]						=   torch.tensor([0.4_9_1_7, -0.0_4_3_0, 0.1_3_4_1]       )
   elif model_name == "focalnet-small-lrf":
      UpperCAmelCase_							:      Union[str, Any]						=   torch.tensor([-0.2_5_8_8, -0.5_3_4_2, -0.2_3_3_1]       )
   elif model_name == "focalnet-base":
      UpperCAmelCase_							:      Any						=   torch.tensor([-0.1_6_5_5, -0.4_0_9_0, -0.1_7_3_0]       )
   elif model_name == "focalnet-base-lrf":
      UpperCAmelCase_							:      List[Any]						=   torch.tensor([0.5_3_0_6, -0.0_4_8_3, -0.3_9_2_8]       )
   assert torch.allclose(outputs.logits[0, :3]			,       __lowercase			,       atol=1e-4       )
   print('''Looks ok!'''       )

   if pytorch_dump_folder_path is not None:
      print(F'''Saving model and processor of {model_name} to {pytorch_dump_folder_path}'''       )
      model.save_pretrained(__lowercase       )
      processor.save_pretrained(__lowercase       )

   if push_to_hub:
      print(F'''Pushing model and processor of {model_name} to the hub...'''       )
      model.push_to_hub(F'''{model_name}'''       )
      processor.push_to_hub(F'''{model_name}'''       )


if __name__ == "__main__":
       __UpperCamelCase				:  Optional[int]      					=							argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           '--model_name',
           default='focalnet-tiny',
           type=str,
           help='Name of the FocalNet model you\'d like to convert.',
       )
       parser.add_argument(
           '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.'
       )
       parser.add_argument(
           '--push_to_hub',
           action='store_true',
           help='Whether to push the model and processor to the hub.',
       )

       __UpperCamelCase				:  Tuple      					=							parser.parse_args()
       convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)



import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     str			= ['image_processor', 'tokenizer']
    A_  :     int			= 'LayoutLMv2ImageProcessor'
    A_  :     str			= ('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast')





    def __init__(	self				:   Union[str, Any]       ,   __snake_case				:   List[Any]=None       ,   __snake_case				:   List[str]=None       ,   **__snake_case				:   Optional[int]	):





       '''simple docstring'''
       if "feature_extractor" in kwargs:
          warnings.warn(
              '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
              ''' instead.'''       ,   __snake_case       ,   )
          UpperCAmelCase_							:      List[Any]						=   kwargs.pop('''feature_extractor'''	)

       UpperCAmelCase_							:      Optional[int]						=   image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
          raise ValueError('''You need to specify an `image_processor`.'''	)
       if tokenizer is None:
          raise ValueError('''You need to specify a `tokenizer`.'''	)

       super().__init__(__snake_case       ,   __snake_case	)





    def __call__(	self				:   List[str]       ,   __snake_case				:   Dict       ,   __snake_case				:   Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None       ,   __snake_case				:   Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None       ,   __snake_case				:   Union[List[List[int]], List[List[List[int]]]] = None       ,   __snake_case				:   Optional[Union[List[int], List[List[int]]]] = None       ,   __snake_case				:   bool = True       ,   __snake_case				:   Union[bool, str, PaddingStrategy] = False       ,   __snake_case				:   Union[bool, str, TruncationStrategy] = None       ,   __snake_case				:   Optional[int] = None       ,   __snake_case				:   int = 0       ,   __snake_case				:   Optional[int] = None       ,   __snake_case				:   Optional[bool] = None       ,   __snake_case				:   Optional[bool] = None       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = False       ,   __snake_case				:   bool = True       ,   __snake_case				:   Optional[Union[str, TensorType]] = None       ,   **__snake_case				:   Optional[int]       ,   ):





       '''simple docstring'''
       # verify input
       if self.image_processor.apply_ocr and (boxes is not None):
          raise ValueError(
              '''You cannot provide bounding boxes '''
              '''if you initialized the image processor with apply_ocr set to True.'''	)

       if self.image_processor.apply_ocr and (word_labels is not None):
          raise ValueError(
              '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'''	)

       if return_overflowing_tokens is True and return_offsets_mapping is False:
          raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.'''	)

       # first, apply the image processor
       UpperCAmelCase_							:      Tuple						=   self.image_processor(images=__snake_case       ,   return_tensors=__snake_case	)

       # second, apply the tokenizer
       if text is not None and self.image_processor.apply_ocr and text_pair is None:
          if isinstance(__snake_case       ,   __snake_case	):
             UpperCAmelCase_							:      Optional[Any]						=   [text]  # add batch dimension (as the image processor always adds a batch dimension)
          UpperCAmelCase_							:      Any						=   features['''words''']

       UpperCAmelCase_							:      str						=   self.tokenizer(
           text=text if text is not None else features['''words''']       ,   text_pair=text_pair if text_pair is not None else None       ,   boxes=boxes if boxes is not None else features['''boxes''']       ,   word_labels=__snake_case       ,   add_special_tokens=__snake_case       ,   padding=__snake_case       ,   truncation=__snake_case       ,   max_length=__snake_case       ,   stride=__snake_case       ,   pad_to_multiple_of=__snake_case       ,   return_token_type_ids=__snake_case       ,   return_attention_mask=__snake_case       ,   return_overflowing_tokens=__snake_case       ,   return_special_tokens_mask=__snake_case       ,   return_offsets_mapping=__snake_case       ,   return_length=__snake_case       ,   verbose=__snake_case       ,   return_tensors=__snake_case       ,   **__snake_case       ,   )

       # add pixel values
       UpperCAmelCase_							:      List[str]						=   features.pop('''pixel_values'''	)
       if return_overflowing_tokens is True:
          UpperCAmelCase_							:      Optional[int]						=   self.get_overflowing_images(__snake_case       ,   encoded_inputs['''overflow_to_sample_mapping''']	)
       UpperCAmelCase_							:      List[Any]						=   images

       return encoded_inputs





    def 					_lowerCamelCase (	self				:   Union[str, Any]       ,   __snake_case				:   str       ,   __snake_case				:   List[Any]	):





       '''simple docstring'''
       # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
       UpperCAmelCase_							:      List[str]						=   []
       for sample_idx in overflow_to_sample_mapping:
          images_with_overflow.append(images[sample_idx]	)

       if len(__snake_case	) != len(__snake_case	):
          raise ValueError(
              '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
              f''' {len(__snake_case	)} and {len(__snake_case	)}'''	)

       return images_with_overflow





    def 					_lowerCamelCase (	self				:   List[Any]       ,   *__snake_case				:   Optional[int]       ,   **__snake_case				:   List[Any]	):





       '''simple docstring'''
       return self.tokenizer.batch_decode(*__snake_case       ,   **__snake_case	)





    def 					_lowerCamelCase (	self				:   str       ,   *__snake_case				:   Optional[Any]       ,   **__snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       return self.tokenizer.decode(*__snake_case       ,   **__snake_case	)





    @property
    def 					_lowerCamelCase (	self				:   Optional[Any]	):





       '''simple docstring'''
       return ["input_ids", "bbox", "attention_mask", "image"]





    @property
    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       warnings.warn(
           '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''       ,   __snake_case       ,   )
       return self.image_processor_class





    @property
    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       warnings.warn(
           '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''       ,   __snake_case       ,   )
       return self.image_processor