
"""simple docstring"""







import argparse

import requests
import torch
from PIL import Image

from transformers import CLIPProcessor, GroupViTConfig, GroupViTModel
def    __lowerCAmelCase    (_UpperCamelCase		):
	# vision encoder
	if "img_encoder.pos_embed" in name:
		__lowerCAmelCase      :						int						      =				name.replace('img_encoder.pos_embed'	,  'vision_model.embeddings.position_embeddings'		)
	if "img_encoder.patch_embed.proj" in name:
		__lowerCAmelCase      :						List[Any]						      =				name.replace('img_encoder.patch_embed.proj'	,  'vision_model.embeddings.patch_embeddings.projection'		)
	if "img_encoder.patch_embed.norm" in name:
		__lowerCAmelCase      :						List[Any]						      =				name.replace('img_encoder.patch_embed.norm'	,  'vision_model.embeddings.layernorm'		)
	if "img_encoder.layers" in name:
		__lowerCAmelCase      :						List[str]						      =				name.replace('img_encoder.layers'	,  'vision_model.encoder.stages'		)
	if "blocks" in name and "res" not in name:
		__lowerCAmelCase      :						List[Any]						      =				name.replace('blocks'	,  'layers'		)
	if "attn" in name and "pre_assign" not in name:
		__lowerCAmelCase      :						str						      =				name.replace('attn'	,  'self_attn'		)
	if "proj" in name and "self_attn" in name and "text" not in name:
		__lowerCAmelCase      :						List[Any]						      =				name.replace('proj'	,  'out_proj'		)
	if "pre_assign_attn.attn.proj" in name:
		__lowerCAmelCase      :						Union[str, Any]						      =				name.replace('pre_assign_attn.attn.proj'	,  'pre_assign_attn.attn.out_proj'		)
	if "norm1" in name:
		__lowerCAmelCase      :						Union[str, Any]						      =				name.replace('norm1'	,  'layer_norm1'		)
	if "norm2" in name and "pre_assign" not in name:
		__lowerCAmelCase      :						Optional[Any]						      =				name.replace('norm2'	,  'layer_norm2'		)
	if "img_encoder.norm" in name:
		__lowerCAmelCase      :						Union[str, Any]						      =				name.replace('img_encoder.norm'	,  'vision_model.layernorm'		)
	# text encoder
	if "text_encoder.token_embedding" in name:
		__lowerCAmelCase      :						List[str]						      =				name.replace('text_encoder.token_embedding'	,  'text_model.embeddings.token_embedding'		)
	if "text_encoder.positional_embedding" in name:
		__lowerCAmelCase      :						List[str]						      =				name.replace('text_encoder.positional_embedding'	,  'text_model.embeddings.position_embedding.weight'		)
	if "text_encoder.transformer.resblocks." in name:
		__lowerCAmelCase      :						Optional[int]						      =				name.replace('text_encoder.transformer.resblocks.'	,  'text_model.encoder.layers.'		)
	if "ln_1" in name:
		__lowerCAmelCase      :						Tuple						      =				name.replace('ln_1'	,  'layer_norm1'		)
	if "ln_2" in name:
		__lowerCAmelCase      :						Tuple						      =				name.replace('ln_2'	,  'layer_norm2'		)
	if "c_fc" in name:
		__lowerCAmelCase      :						Optional[Any]						      =				name.replace('c_fc'	,  'fc1'		)
	if "c_proj" in name:
		__lowerCAmelCase      :						int						      =				name.replace('c_proj'	,  'fc2'		)
	if "text_encoder" in name:
		__lowerCAmelCase      :						Optional[int]						      =				name.replace('text_encoder'	,  'text_model'		)
	if "ln_final" in name:
		__lowerCAmelCase      :						List[str]						      =				name.replace('ln_final'	,  'final_layer_norm'		)
	# projection layers
	if "img_projector.linear_hidden." in name:
		__lowerCAmelCase      :						str						      =				name.replace('img_projector.linear_hidden.'	,  'visual_projection.'		)
	if "img_projector.linear_out." in name:
		__lowerCAmelCase      :						Any						      =				name.replace('img_projector.linear_out.'	,  'visual_projection.3.'		)
	if "text_projector.linear_hidden" in name:
		__lowerCAmelCase      :						str						      =				name.replace('text_projector.linear_hidden'	,  'text_projection'		)
	if "text_projector.linear_out" in name:
		__lowerCAmelCase      :						Optional[Any]						      =				name.replace('text_projector.linear_out'	,  'text_projection.3'		)

	return name
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase		):
	for key in orig_state_dict.copy().keys():
		__lowerCAmelCase      :						List[str]						      =				orig_state_dict.pop(_UpperCamelCase		)

		if "qkv" in key:
			# weights and biases of the key, value and query projections of vision encoder's attention layers require special treatment:
			# we need to split them up into separate matrices/vectors
			__lowerCAmelCase      :						Dict						      =				key.split('.'		)
			__lowerCAmelCase      ,       __lowerCAmelCase      :						str						      =				int(key_split[2]		), int(key_split[4]		)
			__lowerCAmelCase      :						Any						      =				config.vision_config.hidden_size
			if "weight" in key:
				__lowerCAmelCase      :						Any						      =				val[:dim, :]
				__lowerCAmelCase      :						str						      =				val[dim : dim * 2, :]
				__lowerCAmelCase      :						List[Any]						      =				val[-dim:, :]
			else:
				__lowerCAmelCase      :						Optional[Any]						      =				val[:dim]
				__lowerCAmelCase      :						List[Any]						      =				val[dim : dim * 2]
				__lowerCAmelCase      :						Tuple						      =				val[-dim:]
		elif "in_proj" in key:
			# weights and biases of the key, value and query projections of text encoder's attention layers require special treatment:
			# we need to split them up into separate matrices/vectors
			__lowerCAmelCase      :						Tuple						      =				key.split('.'		)
			__lowerCAmelCase      :						Any						      =				int(key_split[3]		)
			__lowerCAmelCase      :						str						      =				config.text_config.hidden_size
			if "weight" in key:
				__lowerCAmelCase      :						Dict						      =				val[:dim, :]
				__lowerCAmelCase      :						Tuple						      =				val[
				    dim : dim * 2, :
				]
				__lowerCAmelCase      :						Any						      =				val[-dim:, :]
			else:
				__lowerCAmelCase      :						Tuple						      =				val[:dim]
				__lowerCAmelCase      :						Dict						      =				val[dim : dim * 2]
				__lowerCAmelCase      :						Any						      =				val[-dim:]
		else:
			__lowerCAmelCase      :						Any						      =				rename_key(_UpperCamelCase		)
			# squeeze if necessary
			if (
			    "text_projection.0" in new_name
			    or "text_projection.3" in new_name
			    or "visual_projection.0" in new_name
			    or "visual_projection.3" in new_name
			):
				__lowerCAmelCase      :						Dict						      =				val.squeeze_()
			else:
				__lowerCAmelCase      :						Optional[int]						      =				val

	return orig_state_dict
def    __lowerCAmelCase    ():
	__lowerCAmelCase      :						Optional[Any]						      =				'http://images.cocodataset.org/val2017/000000039769.jpg'
	__lowerCAmelCase      :						Tuple						      =				Image.open(requests.get(_UpperCamelCase	,  stream=_UpperCamelCase		).raw		)
	return im
@torch.no_grad()
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase="groupvit-gcc-yfcc"	,  _UpperCamelCase=False		):
	__lowerCAmelCase      :						List[str]						      =				GroupViTConfig()
	__lowerCAmelCase      :						Dict						      =				GroupViTModel(_UpperCamelCase		).eval()

	__lowerCAmelCase      :						List[Any]						      =				torch.load(_UpperCamelCase	,  map_location='cpu'		)['model']
	__lowerCAmelCase      :						Optional[int]						      =				convert_state_dict(_UpperCamelCase	,  _UpperCamelCase		)
	__lowerCAmelCase      ,       __lowerCAmelCase      :						Optional[Any]						      =				model.load_state_dict(_UpperCamelCase	,  strict=_UpperCamelCase		)
	assert missing_keys == ["text_model.embeddings.position_ids"]
	assert (unexpected_keys == ["multi_label_logit_scale"]) or (len(_UpperCamelCase		) == 0)

	# verify result
	__lowerCAmelCase      :						int						      =				CLIPProcessor.from_pretrained('openai/clip-vit-base-patch32'		)
	__lowerCAmelCase      :						int						      =				prepare_img()
	__lowerCAmelCase      :						Any						      =				processor(text=['a photo of a cat', 'a photo of a dog']	,  images=_UpperCamelCase	,  padding=_UpperCamelCase	,  return_tensors='pt'		)

	with torch.no_grad():
		__lowerCAmelCase      :						List[str]						      =				model(**_UpperCamelCase		)

	if model_name == "groupvit-gcc-yfcc":
		__lowerCAmelCase      :						Tuple						      =				torch.tensor([[13.3523, 6.3629]]		)
	elif model_name == "groupvit-gcc-redcaps":
		__lowerCAmelCase      :						List[Any]						      =				torch.tensor([[16.1873, 8.6230]]		)
	else:
		raise ValueError(F"Model name {model_name} not supported."		)
	assert torch.allclose(outputs.logits_per_image	,  _UpperCamelCase	,  atol=1e-3		)

	processor.save_pretrained(_UpperCamelCase		)
	model.save_pretrained(_UpperCamelCase		)
	print('Successfully saved processor and model to'	,  _UpperCamelCase		)

	if push_to_hub:
		print('Pushing to the hub...'		)
		processor.push_to_hub(_UpperCamelCase	,  organization='nielsr'		)
		model.push_to_hub(_UpperCamelCase	,  organization='nielsr'		)


if __name__ == "__main__":
							lowerCamelCase__        =		argparse.ArgumentParser()
							parser.add_argument(
							    """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to dump the processor and PyTorch model."""
							)
							parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to GroupViT checkpoint""")
							parser.add_argument(
							    """--model_name""",
							    default="""groupvit-gccy-fcc""",
							    type=str,
							    help="""Name of the model. Expecting either 'groupvit-gcc-yfcc' or 'groupvit-gcc-redcaps'""",
							)
							parser.add_argument(
							    """--push_to_hub""",
							    action="""store_true""",
							    help="""Whether or not to push the converted model and processor to the 🤗 hub using the provided `model_name`.""",
							)
							lowerCamelCase__        =		parser.parse_args()

							convert_groupvit_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)

"""simple docstring"""







import argparse
import datetime
def    __lowerCAmelCase    (_UpperCamelCase		):
	__lowerCAmelCase      :						Optional[Any]						      =				{
	    '0': 'Sunday',
	    '1': 'Monday',
	    '2': 'Tuesday',
	    '3': 'Wednesday',
	    '4': 'Thursday',
	    '5': 'Friday',
	    '6': 'Saturday',
	}

	__lowerCAmelCase      :						Optional[Any]						      =				{0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0}

	# Validate
	if not 0 < len(_UpperCamelCase		) < 11:
		raise ValueError('Must be 10 characters long'		)

	# Get month
	__lowerCAmelCase      :						int						      =				int(date_input[0] + date_input[1]		)
	# Validate
	if not 0 < m < 13:
		raise ValueError('Month must be between 1 - 12'		)

	__lowerCAmelCase      :						str						      =				date_input[2]
	# Validate
	if sep_a not in ["-", "/"]:
		raise ValueError('Date separator must be \'-\' or \'/\''		)

	# Get day
	__lowerCAmelCase      :						int						      =				int(date_input[3] + date_input[4]		)
	# Validate
	if not 0 < d < 32:
		raise ValueError('Date must be between 1 - 31'		)

	# Get second separator
	__lowerCAmelCase      :						str						      =				date_input[5]
	# Validate
	if sep_a not in ["-", "/"]:
		raise ValueError('Date separator must be \'-\' or \'/\''		)

	# Get year
	__lowerCAmelCase      :						int						      =				int(date_input[6] + date_input[7] + date_input[8] + date_input[9]		)
	# Arbitrary year range
	if not 45 < y < 8500:
		raise ValueError(
		    'Year out of range. There has to be some sort of limit...right?'		)

	# Get datetime obj for validation
	__lowerCAmelCase      :						Tuple						      =				datetime.date(int(_UpperCamelCase		)	,  int(_UpperCamelCase		)	,  int(_UpperCamelCase		)		)

	# Start math
	if m <= 2:
		__lowerCAmelCase      :						int						      =				y - 1
		__lowerCAmelCase      :						Tuple						      =				m + 12
	# maths var
	__lowerCAmelCase      :						int						      =				int(str(_UpperCamelCase		)[:2]		)
	__lowerCAmelCase      :						int						      =				int(str(_UpperCamelCase		)[2:]		)
	__lowerCAmelCase      :						int						      =				int(2.6 * m - 5.39		)
	__lowerCAmelCase      :						int						      =				int(c / 4		)
	__lowerCAmelCase      :						int						      =				int(k / 4		)
	__lowerCAmelCase      :						int						      =				int(d + k		)
	__lowerCAmelCase      :						int						      =				int(t + u + v + x		)
	__lowerCAmelCase      :						int						      =				int(z - (2 * c)		)
	__lowerCAmelCase      :						int						      =				round(w % 7		)
	# End math

	# Validate math
	if f != convert_datetime_days[dt_ck.weekday()]:
		raise AssertionError('The date was evaluated incorrectly. Contact developer.'		)

	# Response
	__lowerCAmelCase      :						str						      =				F"Your date {date_input}, is a {days[str(_UpperCamelCase		)]}!"
	return response


if __name__ == "__main__":
							import doctest

							doctest.testmod()
							lowerCamelCase__        =		argparse.ArgumentParser(
							    description=(
							        """Find out what day of the week nearly any date is or was. Enter """
							        """date as a string in the mm-dd-yyyy or mm/dd/yyyy format"""
							    )
							)
							parser.add_argument(
							    """date_input""", type=str, help="""Date as a string (mm-dd-yyyy or mm/dd/yyyy)"""
							)
							lowerCamelCase__        =		parser.parse_args()
							zeller(args.date_input)