




import argparse
from collections import OrderedDict
from pathlib import Path

import torch

from transformers import (
    VisualBertConfig,
    VisualBertForMultipleChoice,
    VisualBertForPreTraining,
    VisualBertForQuestionAnswering,
    VisualBertForVisualReasoning,
)
from transformers.utils import logging


logging.set_verbosity_info()
__a							     =					logging.get_logger(__name__)

__a							     =					[
    ('bert.bert', 'visual_bert'),
    ('bert.cls', 'cls'),
    ('bert.classifier', 'cls'),
    ('token_type_embeddings_visual', 'visual_token_type_embeddings'),
    ('position_embeddings_visual', 'visual_position_embeddings'),
    ('projection', 'visual_projection'),
]

__a							     =					[
    'nlvr2_coco_pre_trained.th',
    'nlvr2_fine_tuned.th',
    'nlvr2_pre_trained.th',
    'vcr_coco_pre_train.th',
    'vcr_fine_tune.th',
    'vcr_pre_train.th',
    'vqa_coco_pre_trained.th',
    'vqa_fine_tuned.th',
    'vqa_pre_trained.th',
]







def lowerCamelCase__  (     _lowercase ):

     '''simple docstring'''


     UpperCAmelCase_     :				int  =     torch.load(_lowercase					,		map_location='''cpu''' )
     return sd







def lowerCamelCase__  (     _lowercase					,		_lowercase					,		_lowercase=rename_keys_prefix ):

     '''simple docstring'''


     UpperCAmelCase_     :				Optional[Any]  =     OrderedDict()
     UpperCAmelCase_     :				List[str]  =     torch.arange(config.max_position_embeddings ).expand((1, -1) )
     # detector_d = OrderedDict()
     for key in d:
          if "detector" in key:
               # detector_d[key.replace('detector.','')] = d[key]
               continue
          UpperCAmelCase_     :				Tuple  =     key
          for name_pair in rename_keys_prefix:
               UpperCAmelCase_     :				Optional[int]  =     new_key.replace(name_pair[0]					,		name_pair[1] )
          UpperCAmelCase_     :				Union[str, Any]  =     d[key]
          if key == "bert.cls.predictions.decoder.weight":
               # Old bert code didn't have `decoder.bias`, but was added separately
               UpperCAmelCase_     :				Tuple  =     new_d['''cls.predictions.bias''']
     return new_d







@torch.no_grad()
def lowerCamelCase__  (     _lowercase					,		_lowercase ):

     '''simple docstring'''


     assert (
         checkpoint_path.split('''/''' )[-1] in ACCEPTABLE_CHECKPOINTS
     ), f'''The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.'''

     # Get Config
     if "pre" in checkpoint_path:
          UpperCAmelCase_     :				Dict  =     '''pretraining'''
          if "vcr" in checkpoint_path:
               UpperCAmelCase_     :				Optional[int]  =     {'''visual_embedding_dim''': 512}
          elif "vqa_advanced" in checkpoint_path:
               UpperCAmelCase_     :				Any  =     {'''visual_embedding_dim''': 2048}
          elif "vqa" in checkpoint_path:
               UpperCAmelCase_     :				Any  =     {'''visual_embedding_dim''': 2048}
          elif "nlvr" in checkpoint_path:
               UpperCAmelCase_     :				str  =     {'''visual_embedding_dim''': 1024}
          else:
               raise NotImplementedError(f'''No implementation found for `{checkpoint_path}`.''' )
     else:
          if "vcr" in checkpoint_path:
               UpperCAmelCase_     :				str  =     {'''visual_embedding_dim''': 512}
               UpperCAmelCase_     :				List[Any]  =     '''multichoice'''
          elif "vqa_advanced" in checkpoint_path:
               UpperCAmelCase_     :				Optional[int]  =     {'''visual_embedding_dim''': 2048}
               UpperCAmelCase_     :				List[Any]  =     '''vqa_advanced'''
          elif "vqa" in checkpoint_path:
               UpperCAmelCase_     :				str  =     {'''visual_embedding_dim''': 2048, '''num_labels''': 3129}
               UpperCAmelCase_     :				Union[str, Any]  =     '''vqa'''
          elif "nlvr" in checkpoint_path:
               UpperCAmelCase_     :				Union[str, Any]  =     {
                   '''visual_embedding_dim''': 1024,
                   '''num_labels''': 2,
               }
               UpperCAmelCase_     :				Optional[int]  =     '''nlvr'''

     UpperCAmelCase_     :				Dict  =     VisualBertConfig(**_lowercase )

     # Load State Dict
     UpperCAmelCase_     :				Any  =     load_state_dict(_lowercase )

     UpperCAmelCase_     :				Any  =     get_new_dict(_lowercase					,		_lowercase )

     if model_type == "pretraining":
          UpperCAmelCase_     :				Dict  =     VisualBertForPreTraining(_lowercase )
     elif model_type == "vqa":
          UpperCAmelCase_     :				str  =     VisualBertForQuestionAnswering(_lowercase )
     elif model_type == "nlvr":
          UpperCAmelCase_     :				Any  =     VisualBertForVisualReasoning(_lowercase )
     elif model_type == "multichoice":
          UpperCAmelCase_     :				Union[str, Any]  =     VisualBertForMultipleChoice(_lowercase )

     model.load_state_dict(_lowercase )
     # Save Checkpoints
     Path(_lowercase ).mkdir(exist_ok=_lowercase )
     model.save_pretrained(_lowercase )


if __name__ == "__main__":
     __a							     =					argparse.ArgumentParser()
     # Required parameters
     parser.add_argument('orig_checkpoint_path', type=str, help='A path to .th on local filesystem.')
     parser.add_argument('pytorch_dump_folder_path', type=str, help='Path to the output PyTorch model.')
     __a							     =					parser.parse_args()
     convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import PoolFormerImageProcessor

class     __a(	unittest.TestCase  ):






    """simple docstring"""





    def __init__( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE=7  ,_SCREAMING_SNAKE_CASE=3  ,_SCREAMING_SNAKE_CASE=30  ,_SCREAMING_SNAKE_CASE=400  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=0.9  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]  ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]  ,)					->						Optional[int]:
         UpperCAmelCase_     :				int  =     size if size is not None else {'''shortest_edge''': 30}
         UpperCAmelCase_     :				List[str]  =     crop_size if crop_size is not None else {'''height''': 30, '''width''': 30}
         UpperCAmelCase_     :				Dict  =     parent
         UpperCAmelCase_     :				int  =     batch_size
         UpperCAmelCase_     :				int  =     num_channels
         UpperCAmelCase_     :				Any  =     min_resolution
         UpperCAmelCase_     :				Tuple  =     max_resolution
         UpperCAmelCase_     :				Optional[int]  =     do_resize_and_center_crop
         UpperCAmelCase_     :				Tuple  =     size
         UpperCAmelCase_     :				List[str]  =     crop_pct
         UpperCAmelCase_     :				List[str]  =     crop_size
         UpperCAmelCase_     :				Any  =     do_normalize
         UpperCAmelCase_     :				str  =     image_mean
         UpperCAmelCase_     :				List[Any]  =     image_std





    def 				a__ ( self    )					->						str:
         return {
             "size": self.size,
             "do_resize_and_center_crop": self.do_resize_and_center_crop,
             "crop_pct": self.crop_pct,
             "crop_size": self.crop_size,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
         }


@require_torch
@require_vision
class     __a(	_a							,		unittest.TestCase  ):






    """simple docstring"""





    lowerCAmelCase         = PoolFormerImageProcessor if is_vision_available() else None





    def 				a__ ( self    )					->						Dict:
         UpperCAmelCase_     :				str  =     PoolFormerImageProcessingTester(self    )





    @property
    def 				a__ ( self    )					->						Optional[int]:
         return self.image_processor_tester.prepare_image_processor_dict()





    def 				a__ ( self    )					->						Optional[int]:
         UpperCAmelCase_     :				List[Any]  =     self.image_processing_class(**self.image_processor_dict    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''do_resize_and_center_crop'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''size'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''crop_pct'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''do_normalize'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''image_mean'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''image_std'''    )    )





    def 				a__ ( self    )					->						Union[str, Any]:
         UpperCAmelCase_     :				Any  =     self.image_processing_class.from_dict(self.image_processor_dict    )
         self.assertEqual(image_processor.size  ,{'''shortest_edge''': 30}    )
         self.assertEqual(image_processor.crop_size  ,{'''height''': 30, '''width''': 30}    )

         UpperCAmelCase_     :				Optional[Any]  =     self.image_processing_class.from_dict(self.image_processor_dict  ,size=42  ,crop_size=84    )
         self.assertEqual(image_processor.size  ,{'''shortest_edge''': 42}    )
         self.assertEqual(image_processor.crop_size  ,{'''height''': 84, '''width''': 84}    )





    def 				a__ ( self    )					->						Optional[int]:
         pass





    def 				a__ ( self    )					->						Dict:
         # Initialize image_processing
         UpperCAmelCase_     :				str  =     self.image_processing_class(**self.image_processor_dict    )
         # create random PIL images
         UpperCAmelCase_     :				int  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,Image.Image    )

         # Test not batched input
         UpperCAmelCase_     :				Optional[int]  =     image_processing(image_inputs[0]  ,return_tensors='''pt'''    ).pixel_values

         self.assertEqual(
             encoded_images.shape  ,(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)

         # Test batched
         UpperCAmelCase_     :				Union[str, Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)





    def 				a__ ( self    )					->						List[Any]:
         # Initialize image_processing
         UpperCAmelCase_     :				Any  =     self.image_processing_class(**self.image_processor_dict    )
         # create random numpy tensors
         UpperCAmelCase_     :				List[str]  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE  ,numpify=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,np.ndarray    )

         # Test not batched input
         UpperCAmelCase_     :				Tuple  =     image_processing(image_inputs[0]  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)

         # Test batched
         UpperCAmelCase_     :				List[Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)





    def 				a__ ( self    )					->						Union[str, Any]:
         # Initialize image_processing
         UpperCAmelCase_     :				List[Any]  =     self.image_processing_class(**self.image_processor_dict    )
         # create random PyTorch tensors
         UpperCAmelCase_     :				Optional[Any]  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE  ,torchify=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,torch.Tensor    )

         # Test not batched input
         UpperCAmelCase_     :				Tuple  =     image_processing(image_inputs[0]  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)

         # Test batched
         UpperCAmelCase_     :				List[Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)