


from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import TFResNetForImageClassification, TFResNetModel
		from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor

class 		__a				:
		def __init__(      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=3      ,   _SCREAMING_SNAKE_CASE=32      ,   _SCREAMING_SNAKE_CASE=3      ,   _SCREAMING_SNAKE_CASE=10      ,   _SCREAMING_SNAKE_CASE=[10, 20, 30, 40]      ,   _SCREAMING_SNAKE_CASE=[1, 1, 2, 1]      ,   _SCREAMING_SNAKE_CASE=True      ,   _SCREAMING_SNAKE_CASE=True      ,   _SCREAMING_SNAKE_CASE="relu"      ,   _SCREAMING_SNAKE_CASE=3      ,   _SCREAMING_SNAKE_CASE=None      ,   )			->	Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=      parent
									_UpperCAmelCase					=      batch_size
									_UpperCAmelCase					=      image_size
									_UpperCAmelCase					=      num_channels
									_UpperCAmelCase					=      embeddings_size
									_UpperCAmelCase					=      hidden_sizes
									_UpperCAmelCase					=      depths
									_UpperCAmelCase					=      is_training
									_UpperCAmelCase					=      use_labels
									_UpperCAmelCase					=      hidden_act
									_UpperCAmelCase					=      num_labels
									_UpperCAmelCase					=      scope
									_UpperCAmelCase					=      len(_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Any:




									"""simple docstring"""





									_UpperCAmelCase					=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

									_UpperCAmelCase					=      None
									if self.use_labels:
																_UpperCAmelCase					=      ids_tensor([self.batch_size]      ,   self.num_labels						)

									_UpperCAmelCase					=      self.get_config()

									return config, pixel_values, labels
		def      UpperCAmelCase__    (      self						)			->	List[Any]:




									"""simple docstring"""





									return ResNetConfig(
									    num_channels=self.num_channels      ,   embeddings_size=self.embeddings_size      ,   hidden_sizes=self.hidden_sizes      ,   depths=self.depths      ,   hidden_act=self.hidden_act      ,   num_labels=self.num_labels      ,   image_size=self.image_size      ,   )
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)			->	Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase					=      TFResNetModel(config=_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      model(_SCREAMING_SNAKE_CASE						)
									# expected last hidden states: B, C, H // 32, W // 32
									self.parent.assertEqual(
									    result.last_hidden_state.shape      ,   (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)      ,   )
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)			->	List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      self.num_labels
									_UpperCAmelCase					=      TFResNetForImageClassification(_SCREAMING_SNAKE_CASE						)
									_UpperCAmelCase					=      model(_SCREAMING_SNAKE_CASE      ,   labels=_SCREAMING_SNAKE_CASE						)
									self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.num_labels)						)






		def      UpperCAmelCase__    (      self						)			->	int:




									"""simple docstring"""





									_UpperCAmelCase					=      self.prepare_config_and_inputs()
									_UpperCAmelCase		,	_UpperCAmelCase		,	_UpperCAmelCase					=      config_and_inputs
									_UpperCAmelCase					=      {'pixel_values': pixel_values}
									return config, inputs_dict



@require_tf
class 		__a				(    UpperCAmelCase   ,				UpperCAmelCase   ,				unittest.TestCase      ):
		_a     :  Optional[int]           =     (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
		_a     :  Optional[int]           =     (
		    {'feature-extraction': TFResNetModel, 'image-classification': TFResNetForImageClassification}
		    if is_tf_available()
		    else {}
		)

		_a     :  Optional[int]           =     False
		_a     :  Optional[Any]           =     False
		_a     :  int           =     False
		_a     :  Union[str, Any]           =     False
		_a     :  str           =     False
		def      UpperCAmelCase__    (      self						)			->	Any:




									"""simple docstring"""





									_UpperCAmelCase					=      TFResNetModelTester(self						)
									_UpperCAmelCase					=      ConfigTester(self      ,   config_class=_SCREAMING_SNAKE_CASE      ,   has_text_modality=_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	str:




									"""simple docstring"""





									self.create_and_test_config_common_properties()
									self.config_tester.create_and_test_config_to_json_string()
									self.config_tester.create_and_test_config_to_json_file()
									self.config_tester.create_and_test_config_from_and_save_pretrained()
									self.config_tester.create_and_test_config_with_num_labels()
									self.config_tester.check_config_can_be_init_without_params()
									self.config_tester.check_config_arguments_init()
		def      UpperCAmelCase__    (      self						)			->	Any:




									"""simple docstring"""





									return
		@unittest.skip(reason='ResNet does not use inputs_embeds'						)
		def      UpperCAmelCase__    (      self						)			->	Union[str, Any]:




									"""simple docstring"""





									pass
		@unittest.skip(reason='ResNet does not support input and output embeddings'						)
		def      UpperCAmelCase__    (      self						)			->	Union[str, Any]:




									"""simple docstring"""





									pass
		def      UpperCAmelCase__    (      self						)			->	List[str]:




									"""simple docstring"""





									_UpperCAmelCase		,	_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																_UpperCAmelCase					=      model_class(_SCREAMING_SNAKE_CASE						)
																_UpperCAmelCase					=      inspect.signature(model.call						)
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																_UpperCAmelCase					=      [*signature.parameters.keys()]

																_UpperCAmelCase					=      ['pixel_values']
																self.assertListEqual(arg_names[:1]      ,   _SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Optional[Any]:




									"""simple docstring"""





									def check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						):
																_UpperCAmelCase					=      model_class(_SCREAMING_SNAKE_CASE						)
																_UpperCAmelCase					=      model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)						)

																_UpperCAmelCase					=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																_UpperCAmelCase					=      self.model_tester.num_stages
																self.assertEqual(len(_SCREAMING_SNAKE_CASE						)      ,   expected_num_stages + 1						)

																# ResNet's feature maps are of shape (batch_size, num_channels, height, width)
																self.assertListEqual(
																    list(hidden_states[0].shape[-2:]						)      ,   [self.model_tester.image_size // 4, self.model_tester.image_size // 4]      ,   )

									_UpperCAmelCase		,	_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs_for_common()
									_UpperCAmelCase					=      ['basic', 'bottleneck']
									for model_class in self.all_model_classes:
																for layer_type in layers_type:
																							_UpperCAmelCase					=      layer_type
																							_UpperCAmelCase					=      True
																							check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)

																							# check that output_hidden_states also work using config
																							del inputs_dict["output_hidden_states"]
																							_UpperCAmelCase					=      True

																							check_hidden_states_output(_SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE						)
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE						)






		@slow
		def      UpperCAmelCase__    (      self						)			->	List[str]:




									"""simple docstring"""





									for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCAmelCase					=      TFResNetModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
																self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)



def 	lowerCAmelCase__			(      )  ->		str:


							'''simple docstring'''





							_UpperCAmelCase					=      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
							return image







@require_tf
@require_vision
class 		__a				(    unittest.TestCase      ):
		@cached_property
		def      UpperCAmelCase__    (      self						)			->	Tuple:




									"""simple docstring"""





									return (
									    AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)
									    if is_vision_available()
									    else None
									)






		@slow
		def      UpperCAmelCase__    (      self						)			->	Dict:




									"""simple docstring"""





									_UpperCAmelCase					=      TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)

									_UpperCAmelCase					=      self.default_image_processor
									_UpperCAmelCase					=      prepare_img()
									_UpperCAmelCase					=      image_processor(images=_SCREAMING_SNAKE_CASE      ,   return_tensors='tf'						)

									# forward pass
									_UpperCAmelCase					=      model(**_SCREAMING_SNAKE_CASE						)

									# verify the logits
									_UpperCAmelCase					=      tf.TensorShape((1, 1000)						)
									self.assertEqual(outputs.logits.shape      ,   _SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      tf.constant([-11.1069, -9.7877, -8.3777]						)

									self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()      ,   _SCREAMING_SNAKE_CASE      ,   atol=1e-4						)						)




import argparse
import re

import requests
import torch

# git clone https://github.com/salesforce/BLIP.git
from models.blip import blip_decoder
from models.blip_itm import blip_itm
from models.blip_vqa import blip_vqa
from PIL import Image
from torchvision import transforms
from torchvision.transforms.functional import InterpolationMode

from transformers import (
    BertTokenizer,
    BlipConfig,
    BlipForConditionalGeneration,
    BlipForImageTextRetrieval,
    BlipForQuestionAnswering,
)

def 	lowerCAmelCase__			(      a__:		str      ,  a__:		List[Any]	)  ->		Optional[int]:


							'''simple docstring'''





							_UpperCAmelCase					=      'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
							_UpperCAmelCase					=      Image.open(requests.get(a__      ,  stream=a__	).raw	).convert('RGB'	)

							_UpperCAmelCase					=      transforms.Compose(
							    [
							        transforms.Resize((image_size, image_size)      ,  interpolation=InterpolationMode.BICUBIC	),
							        transforms.ToTensor(),
							        transforms.Normalize((0.48_145_466, 0.4_578_275, 0.40_821_073)      ,  (0.26_862_954, 0.26_130_258, 0.27_577_711)	),
							    ]	)
							_UpperCAmelCase					=      transform(a__	).unsqueeze(0	).to(a__	)
							return image

def 	lowerCAmelCase__			(      a__:		Optional[int]	)  ->		int:


							'''simple docstring'''





							if "visual_encoder" in key:
														_UpperCAmelCase					=      re.sub('visual_encoder*'      ,  'vision_model.encoder'      ,  a__	)
							if "blocks" in key:
														_UpperCAmelCase					=      re.sub(R'blocks'      ,  'layers'      ,  a__	)
							if "attn" in key:
														_UpperCAmelCase					=      re.sub(R'attn'      ,  'self_attn'      ,  a__	)
							if "norm1" in key:
														_UpperCAmelCase					=      re.sub(R'norm1'      ,  'layer_norm1'      ,  a__	)
							if "norm2" in key:
														_UpperCAmelCase					=      re.sub(R'norm2'      ,  'layer_norm2'      ,  a__	)
							if "encoder.norm" in key:
														_UpperCAmelCase					=      re.sub(R'encoder.norm'      ,  'post_layernorm'      ,  a__	)
							if "encoder.patch_embed.proj" in key:
														_UpperCAmelCase					=      re.sub(R'encoder.patch_embed.proj'      ,  'embeddings.patch_embedding'      ,  a__	)

							if "encoder.pos_embed" in key:
														_UpperCAmelCase					=      re.sub(R'encoder.pos_embed'      ,  'embeddings.position_embedding'      ,  a__	)
							if "encoder.cls_token" in key:
														_UpperCAmelCase					=      re.sub(R'encoder.cls_token'      ,  'embeddings.class_embedding'      ,  a__	)

							if "self_attn" in key:
														_UpperCAmelCase					=      re.sub(R'self_attn.proj'      ,  'self_attn.projection'      ,  a__	)

							return key

@torch.no_grad()
def 	lowerCAmelCase__			(      a__:		Optional[Any]      ,  a__:		List[str]=None	)  ->		Optional[Any]:


							'''simple docstring'''





							if config_path is not None:
														_UpperCAmelCase					=      BlipConfig.from_pretrained(a__	)
							else:
														_UpperCAmelCase					=      BlipConfig(projection_dim=5_1_2      ,  text_config={}      ,  vision_config={}	)

							_UpperCAmelCase					=      BlipForConditionalGeneration(a__	).eval()

							_UpperCAmelCase					=      'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth'

							_UpperCAmelCase					=      blip_decoder(pretrained=a__      ,  image_size=3_8_4      ,  vit='base'	)
							_UpperCAmelCase					=      pt_model.eval()

							_UpperCAmelCase					=      pt_model.state_dict()
							for key in modified_state_dict.copy():
														_UpperCAmelCase					=      modified_state_dict.pop(a__	)
														_UpperCAmelCase					=      rename_key(a__	)
														_UpperCAmelCase					=      value

							hf_model.load_state_dict(a__	)

							_UpperCAmelCase					=      3_8_4
							_UpperCAmelCase					=      load_demo_image(image_size=a__      ,  device='cpu'	)
							_UpperCAmelCase					=      BertTokenizer.from_pretrained('bert-base-uncased'	)
							_UpperCAmelCase					=      tokenizer(['a picture of']	).input_ids

							_UpperCAmelCase					=      hf_model.generate(a__      ,  a__	)

							assert out[0].tolist() == [3_0_5_2_2, 1_0_3_7, 3_8_6_1, 1_9_9_7, 1_0_3_7, 2_4_5_0, 3_5_6_4, 2_0_0_6, 1_9_9_6, 3_5_0_9, 2_0_0_7, 2_0_1_4, 3_8_9_9, 1_0_2]

							_UpperCAmelCase					=      hf_model.generate(a__	)

							assert out[0].tolist() == [3_0_5_2_2, 1_0_3_7, 2_4_5_0, 3_5_6_4, 2_0_0_6, 1_9_9_6, 3_5_0_9, 2_0_0_7, 2_0_1_4, 3_8_9_9, 1_0_2]

							if pytorch_dump_folder_path is not None:
														hf_model.save_pretrained(a__	)

							# model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth'
							_UpperCAmelCase					=      (
							    'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth'
							)

							_UpperCAmelCase					=      blip_vqa(pretrained=a__      ,  image_size=a__      ,  vit='base'	)
							vqa_model.eval()

							_UpperCAmelCase					=      vqa_model.state_dict()
							for key in modified_state_dict.copy():
														_UpperCAmelCase					=      modified_state_dict.pop(a__	)
														_UpperCAmelCase					=      rename_key(a__	)
														_UpperCAmelCase					=      value

							_UpperCAmelCase					=      BlipForQuestionAnswering(a__	)

							hf_vqa_model.load_state_dict(a__	)

							_UpperCAmelCase					=      ['How many dogs are in this image?']
							_UpperCAmelCase					=      tokenizer(a__      ,  return_tensors='pt'	).input_ids

							_UpperCAmelCase					=      hf_vqa_model.generate(a__      ,  a__	)
							print(tokenizer.decode(answer[0]	)	)

							assert tokenizer.decode(answer[0]	) == "[UNK] 1 [SEP]"
							if pytorch_dump_folder_path is not None:
														hf_vqa_model.save_pretrained(pytorch_dump_folder_path + '_vqa'	)

							_UpperCAmelCase					=      'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth'

							_UpperCAmelCase					=      blip_itm(pretrained=a__      ,  image_size=a__      ,  vit='base'	)
							itm_model.eval()

							_UpperCAmelCase					=      itm_model.state_dict()
							for key in modified_state_dict.copy():
														_UpperCAmelCase					=      modified_state_dict.pop(a__	)
														_UpperCAmelCase					=      rename_key(a__	)
														_UpperCAmelCase					=      value

							_UpperCAmelCase					=      BlipForImageTextRetrieval(a__	)

							_UpperCAmelCase					=      ['A picture of a woman with a dog sitting in a beach']
							_UpperCAmelCase					=      tokenizer(
							    a__      ,  return_tensors='pt'      ,  padding='max_length'      ,  truncation=a__      ,  max_length=3_5      ,  ).input_ids

							hf_itm_model.load_state_dict(a__	)
							hf_itm_model.eval()

							_UpperCAmelCase					=      hf_itm_model(a__      ,  a__      ,  use_itm_head=a__	)
							_UpperCAmelCase					=      hf_itm_model(a__      ,  a__      ,  use_itm_head=a__	)

							assert out[0].item() == 0.2_110_687_494_277_954
							assert torch.nn.functional.softmax(out_itm[0]      ,  dim=1	)[:, 1].item() == 0.45_698_845_386_505_127

							if pytorch_dump_folder_path is not None:
														hf_itm_model.save_pretrained(pytorch_dump_folder_path + '_itm'	)


if __name__ == "__main__":
		lowerCAmelCase__  :int             =							argparse.ArgumentParser()
		parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
		parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''')
		lowerCAmelCase__  :List[Any]             =							parser.parse_args()

		convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
