"""simple docstring"""




import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel






def   UpperCAmelCase					(	UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):






							"""simple docstring"""





							A__	     =							('dense.weight', 'attention.self.query', 'attention.self.key', 'attention.self.value')

							A__	     =							(
							    ('layer.', 'layer_'),
							    ('word_embeddings.weight', 'word_embeddings'),
							    ('position_embeddings.weight', 'position_embeddings'),
							    ('token_type_embeddings.weight', 'token_type_embeddings'),
							    ('.', '/'),
							    ('LayerNorm/weight', 'LayerNorm/gamma'),
							    ('LayerNorm/bias', 'LayerNorm/beta'),
							    ('weight', 'kernel'),
							)

							if not os.path.isdir(_A				):
														os.makedirs(_A				)

							A__	     =							model.state_dict()

							def to_tf_var_name(UpperCamelCase__				):
														for patt, repl in iter(_A				):
																					A__	     =							name.replace(_A       ,    _A				)
														return F'''bert/{name}'''

							def create_tf_var(UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__				):
														A__	     =							tf.dtypes.as_dtype(tensor.dtype				)
														A__	     =							tf.get_variable(dtype=_A       ,    shape=tensor.shape       ,    name=_A       ,    initializer=tf.zeros_initializer()				)
														session.run(tf.variables_initializer([tf_var]				)				)
														session.run(_A				)
														return tf_var

							tf.reset_default_graph()
							with tf.Session() as session:
														for var_name in state_dict:
																					A__	     =							to_tf_var_name(_A				)
																					A__	     =							state_dict[var_name].numpy()
																					if any(x in var_name for x in tensors_to_transpose				):
																												A__	     =							torch_tensor.T
																					A__	     =							create_tf_var(tensor=_A       ,    name=_A       ,    session=_A				)
																					tf.keras.backend.set_value(_A       ,    _A				)
																					A__	     =							session.run(_A				)
																					print(F'''Successfully created {tf_name}: {np.allclose(_A       ,    _A				)}'''				)

														A__	     =							tf.train.Saver(tf.trainable_variables()				)
														saver.save(_A       ,    os.path.join(_A       ,    model_name.replace('-'       ,    '_'				) + '.ckpt'				)				)






def   UpperCAmelCase					(	UpperCamelCase__=None				):






							"""simple docstring"""





							A__	     =							argparse.ArgumentParser()
							parser.add_argument('--model_name'       ,    type=_A       ,    required=_A       ,    help='model name e.g. bert-base-uncased'				)
							parser.add_argument(
							    '--cache_dir'       ,    type=_A       ,    default=_A       ,    required=_A       ,    help='Directory containing pytorch model'				)
							parser.add_argument('--pytorch_model_path'       ,    type=_A       ,    required=_A       ,    help='/path/to/<pytorch-model-name>.bin'				)
							parser.add_argument('--tf_cache_dir'       ,    type=_A       ,    required=_A       ,    help='Directory in which to save tensorflow model'				)
							A__	     =							parser.parse_args(_A				)

							A__	     =							BertModel.from_pretrained(
							    pretrained_model_name_or_path=args.model_name       ,    state_dict=torch.load(args.pytorch_model_path				)       ,    cache_dir=args.cache_dir       ,    )

							convert_pytorch_checkpoint_to_tf(model=_A       ,    ckpt_dir=args.tf_cache_dir       ,    model_name=args.model_name				)


if __name__ == "__main__":
				main()






"""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()





