"""simple docstring"""




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()
__lowerCamelCase											=  logging.get_logger(__name__)

__lowerCamelCase											=  [
    ("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"),
]

__lowerCamelCase											=  [
    "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   UpperCAmelCase					(	UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							torch.load(UpperCamelCase__       ,    map_location='cpu'				)
							return sd






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__=rename_keys_prefix				):






							"""simple docstring"""





							A__	     =							OrderedDict()
							A__	     =							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
														A__	     =							key
														for name_pair in rename_keys_prefix:
																					A__	     =							new_key.replace(name_pair[0]       ,    name_pair[1]				)
														A__	     =							d[key]
														if key == "bert.cls.predictions.decoder.weight":
																					# Old bert code didn't have `decoder.bias`, but was added separately
																					A__	     =							new_d['cls.predictions.bias']
							return new_d






@torch.no_grad()
def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__				):






							"""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:
														A__	     =							'pretraining'
														if "vcr" in checkpoint_path:
																					A__	     =							{'visual_embedding_dim': 512}
														elif "vqa_advanced" in checkpoint_path:
																					A__	     =							{'visual_embedding_dim': 2_048}
														elif "vqa" in checkpoint_path:
																					A__	     =							{'visual_embedding_dim': 2_048}
														elif "nlvr" in checkpoint_path:
																					A__	     =							{'visual_embedding_dim': 1_024}
														else:
																					raise NotImplementedError(F'''No implementation found for `{checkpoint_path}`.'''				)
							else:
														if "vcr" in checkpoint_path:
																					A__	     =							{'visual_embedding_dim': 512}
																					A__	     =							'multichoice'
														elif "vqa_advanced" in checkpoint_path:
																					A__	     =							{'visual_embedding_dim': 2_048}
																					A__	     =							'vqa_advanced'
														elif "vqa" in checkpoint_path:
																					A__	     =							{'visual_embedding_dim': 2_048, 'num_labels': 3_129}
																					A__	     =							'vqa'
														elif "nlvr" in checkpoint_path:
																					A__	     =							{
																					    'visual_embedding_dim': 1_024,
																					    'num_labels': 2,
																					}
																					A__	     =							'nlvr'

							A__	     =							VisualBertConfig(**UpperCamelCase__				)

							# Load State Dict
							A__	     =							load_state_dict(UpperCamelCase__				)

							A__	     =							get_new_dict(UpperCamelCase__       ,    UpperCamelCase__				)

							if model_type == "pretraining":
														A__	     =							VisualBertForPreTraining(UpperCamelCase__				)
							elif model_type == "vqa":
														A__	     =							VisualBertForQuestionAnswering(UpperCamelCase__				)
							elif model_type == "nlvr":
														A__	     =							VisualBertForVisualReasoning(UpperCamelCase__				)
							elif model_type == "multichoice":
														A__	     =							VisualBertForMultipleChoice(UpperCamelCase__				)

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


if __name__ == "__main__":
				__lowerCamelCase											=  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.")
				__lowerCamelCase											=  parser.parse_args()
				convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)






"""simple docstring"""




import numpy as np
from PIL import Image






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							np.array(UpperCamelCase__				)
							if arr.shape[0] != arr.shape[1]:
														raise ValueError('The input array is not a square matrix'				)
							A__	     =							0
							A__	     =							0
							A__	     =							0
							A__	     =							0

							# compute the shape of the output matrix
							A__	     =							(arr.shape[0] - size) // stride + 1
							# initialize the output matrix with zeros of shape maxpool_shape
							A__	     =							np.zeros((maxpool_shape, maxpool_shape)				)

							while i < arr.shape[0]:
														if i + size > arr.shape[0]:
																					# if the end of the matrix is reached, break
																					break
														while j < arr.shape[1]:
																					# if the end of the matrix is reached, break
																					if j + size > arr.shape[1]:
																												break
																					# compute the maximum of the pooling matrix
																					A__	     =							np.max(arr[i : i + size, j : j + size]				)
																					# shift the pooling matrix by stride of column pixels
																					j += stride
																					mat_j += 1

														# shift the pooling matrix by stride of row pixels
														i += stride
														mat_i += 1

														# reset the column index to 0
														A__	     =							0
														A__	     =							0

							return updated_arr






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							np.array(UpperCamelCase__				)
							if arr.shape[0] != arr.shape[1]:
														raise ValueError('The input array is not a square matrix'				)
							A__	     =							0
							A__	     =							0
							A__	     =							0
							A__	     =							0

							# compute the shape of the output matrix
							A__	     =							(arr.shape[0] - size) // stride + 1
							# initialize the output matrix with zeros of shape avgpool_shape
							A__	     =							np.zeros((avgpool_shape, avgpool_shape)				)

							while i < arr.shape[0]:
														# if the end of the matrix is reached, break
														if i + size > arr.shape[0]:
																					break
														while j < arr.shape[1]:
																					# if the end of the matrix is reached, break
																					if j + size > arr.shape[1]:
																												break
																					# compute the average of the pooling matrix
																					A__	     =							int(np.average(arr[i : i + size, j : j + size]				)				)
																					# shift the pooling matrix by stride of column pixels
																					j += stride
																					mat_j += 1

														# shift the pooling matrix by stride of row pixels
														i += stride
														mat_i += 1
														# reset the column index to 0
														A__	     =							0
														A__	     =							0

							return updated_arr


# Main Function
if __name__ == "__main__":
				from doctest import testmod

				testmod(name="avgpooling", verbose=True)

				# Loading the image
				__lowerCamelCase											=  Image.open("path_to_image")

				# Converting the image to numpy array and maxpooling, displaying the result
				# Ensure that the image is a square matrix

				Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

				# Converting the image to numpy array and averagepooling, displaying the result
				# Ensure that the image is a square matrix

				Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()





