
'''simple docstring'''
import argparse
import re

import numpy as np
import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    SamConfig,
    SamImageProcessor,
    SamModel,
    SamProcessor,
    SamVisionConfig,
)


lowerCAmelCase					:    Tuple           = {
    'iou_prediction_head.layers.0': 'iou_prediction_head.proj_in',
    'iou_prediction_head.layers.1': 'iou_prediction_head.layers.0',
    'iou_prediction_head.layers.2': 'iou_prediction_head.proj_out',
    'mask_decoder.output_upscaling.0': 'mask_decoder.upscale_conv1',
    'mask_decoder.output_upscaling.1': 'mask_decoder.upscale_layer_norm',
    'mask_decoder.output_upscaling.3': 'mask_decoder.upscale_conv2',
    'mask_downscaling.0': 'mask_embed.conv1',
    'mask_downscaling.1': 'mask_embed.layer_norm1',
    'mask_downscaling.3': 'mask_embed.conv2',
    'mask_downscaling.4': 'mask_embed.layer_norm2',
    'mask_downscaling.6': 'mask_embed.conv3',
    'point_embeddings': 'point_embed',
    'pe_layer.positional_encoding_gaussian_matrix': 'shared_embedding.positional_embedding',
    'image_encoder': 'vision_encoder',
    'neck.0': 'neck.conv1',
    'neck.1': 'neck.layer_norm1',
    'neck.2': 'neck.conv2',
    'neck.3': 'neck.layer_norm2',
    'patch_embed.proj': 'patch_embed.projection',
    '.norm': '.layer_norm',
    'blocks': 'layers',
}







def 		A_(     A						:		List[str]):
							UpperCamelCase   			=     {}
							state_dict.pop('pixel_mean'		,     A)
							state_dict.pop('pixel_std'		,     A)

							UpperCamelCase   			=     r'.*.output_hypernetworks_mlps.(\d+).layers.(\d+).*'

							for key, value in state_dict.items():
														for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items():
																					if key_to_modify in key:
																												UpperCamelCase   			=     key.replace(A		,     A)

														if re.match(A		,     A):
																					UpperCamelCase   			=     int(re.match(A		,     A).group(2))
																					if layer_nb == 0:
																												UpperCamelCase   			=     key.replace('layers.0'		,     'proj_in')
																					elif layer_nb == 1:
																												UpperCamelCase   			=     key.replace('layers.1'		,     'layers.0')
																					elif layer_nb == 2:
																												UpperCamelCase   			=     key.replace('layers.2'		,     'proj_out')

														UpperCamelCase   			=     value

							UpperCamelCase   			=     model_state_dict[
							    'prompt_encoder.shared_embedding.positional_embedding'
							]

							return model_state_dict







def 		A_(     A						:		Dict		,     A						:		List[Any]		,     A						:		List[Any]		,     A						:		List[str]="ybelkada/segment-anything"):
							UpperCamelCase   			=     hf_hub_download(A		,     f'''checkpoints/{model_name}.pth''')

							if "sam_vit_b" in model_name:
														UpperCamelCase   			=     SamConfig()
							elif "sam_vit_l" in model_name:
														UpperCamelCase   			=     SamVisionConfig(
														    hidden_size=1024		,     num_hidden_layers=24		,     num_attention_heads=16		,     global_attn_indexes=[5, 11, 17, 23]		,     )

														UpperCamelCase   			=     SamConfig(
														    vision_config=A		,     )
							elif "sam_vit_h" in model_name:
														UpperCamelCase   			=     SamVisionConfig(
														    hidden_size=1280		,     num_hidden_layers=32		,     num_attention_heads=16		,     global_attn_indexes=[7, 15, 23, 31]		,     )

														UpperCamelCase   			=     SamConfig(
														    vision_config=A		,     )

							UpperCamelCase   			=     torch.load(A		,     map_location='cpu')
							UpperCamelCase   			=     replace_keys(A)

							UpperCamelCase   			=     SamImageProcessor()

							UpperCamelCase   			=     SamProcessor(image_processor=A)
							UpperCamelCase   			=     SamModel(A)

							hf_model.load_state_dict(A)
							UpperCamelCase   			=     hf_model.to('cuda')

							UpperCamelCase   			=     'https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png'
							UpperCamelCase   			=     Image.open(requests.get(A		,     stream=A).raw).convert('RGB')

							UpperCamelCase   			=     [[[400, 650]]]
							UpperCamelCase   			=     [[1]]

							UpperCamelCase   			=     processor(images=np.array(A)		,     return_tensors='pt').to('cuda')

							with torch.no_grad():
														UpperCamelCase   			=     hf_model(**A)
							UpperCamelCase   			=     output.iou_scores.squeeze()

							if model_name == "sam_vit_h_4b8939":
														assert scores[-1].item() == 0.579_890_251_159_668

														UpperCamelCase   			=     processor(
														    images=np.array(A)		,     input_points=A		,     input_labels=A		,     return_tensors='pt').to('cuda')

														with torch.no_grad():
																					UpperCamelCase   			=     hf_model(**A)
														UpperCamelCase   			=     output.iou_scores.squeeze()

														assert scores[-1].item() == 0.9_712_603_092_193_604

														UpperCamelCase   			=     ((75, 275, 1725, 850),)

														UpperCamelCase   			=     processor(images=np.array(A)		,     input_boxes=A		,     return_tensors='pt').to('cuda')

														with torch.no_grad():
																					UpperCamelCase   			=     hf_model(**A)
														UpperCamelCase   			=     output.iou_scores.squeeze()

														assert scores[-1].item() == 0.8_686_015_605_926_514

														# Test with 2 points and 1 image.
														UpperCamelCase   			=     [[[400, 650], [800, 650]]]
														UpperCamelCase   			=     [[1, 1]]

														UpperCamelCase   			=     processor(
														    images=np.array(A)		,     input_points=A		,     input_labels=A		,     return_tensors='pt').to('cuda')

														with torch.no_grad():
																					UpperCamelCase   			=     hf_model(**A)
														UpperCamelCase   			=     output.iou_scores.squeeze()

														assert scores[-1].item() == 0.9_936_047_792_434_692


if __name__ == "__main__":
						lowerCAmelCase					:    Tuple           = argparse.ArgumentParser()
						lowerCAmelCase					:    List[str]           = ['sam_vit_b_01ec64', 'sam_vit_h_4b8939', 'sam_vit_l_0b3195']
						parser.add_argument(
						    '--model_name',
						    default='sam_vit_h_4b8939',
						    choices=choices,
						    type=str,
						    help='Path to hf config.json of model to convert',
						)
						parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
						parser.add_argument(
						    '--push_to_hub',
						    action='store_true',
						    help='Whether to push the model and processor to the hub after converting',
						)
						parser.add_argument(
						    '--model_hub_id',
						    default='ybelkada/segment-anything',
						    choices=choices,
						    type=str,
						    help='Path to hf config.json of model to convert',
						)

						lowerCAmelCase					:    Dict           = parser.parse_args()

						convert_sam_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub, args.model_hub_id)






'''simple docstring'''
import uuid
from typing import Any, Dict, List, Optional, Union

from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
						import tensorflow as tf

if is_torch_available():
						import torch


lowerCAmelCase					:    Optional[Any]           = logging.get_logger(__name__)


class 			SCREAMING_SNAKE_CASE__      :







				def __init__(       self						,				A_ = None						,				A_ = None						,				A_=None						,				A_=None     )->		Optional[Any]:







											'''simple docstring'''






											if not conversation_id:
																		UpperCamelCase   			=     uuid.uuida()
											if past_user_inputs is None:
																		UpperCamelCase   			=     []
											if generated_responses is None:
																		UpperCamelCase   			=     []

											UpperCamelCase   			=     conversation_id
											UpperCamelCase   			=     past_user_inputs
											UpperCamelCase   			=     generated_responses
											UpperCamelCase   			=     text







				def __eq__(       self						,				A_     )->		List[Any]:







											'''simple docstring'''






											if not isinstance(A_						,				A_     ):
																		return False
											if self.uuid == other.uuid:
																		return True
											return (
											    self.new_user_input == other.new_user_input
											    and self.past_user_inputs == other.past_user_inputs
											    and self.generated_responses == other.generated_responses
											)







				def 						UpperCAmelCase_    (       self						,				A_						,				A_ = False     )->		int:







											'''simple docstring'''






											if self.new_user_input:
																		if overwrite:
																									logger.warning(
																									    F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten '''
																									    F'''with: "{text}".'''     )
																									UpperCamelCase   			=     text
																		else:
																									logger.warning(
																									    F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input '''
																									    F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input'''     )
											else:
																		UpperCamelCase   			=     text







				def 						UpperCAmelCase_    (       self     )->		Any:







											'''simple docstring'''






											if self.new_user_input:
																		self.past_user_inputs.append(self.new_user_input     )
											UpperCamelCase   			=     None







				def 						UpperCAmelCase_    (       self						,				A_     )->		int:







											'''simple docstring'''






											self.generated_responses.append(A_     )







				def 						UpperCAmelCase_    (       self     )->		List[str]:







											'''simple docstring'''






											for user_input, generated_response in zip(self.past_user_inputs						,				self.generated_responses     ):
																		yield True, user_input
																		yield False, generated_response
											if self.new_user_input:
																		yield True, self.new_user_input







				def __repr__(       self     )->		Any:







											'''simple docstring'''






											UpperCamelCase   			=     F'''Conversation id: {self.uuid} \n'''
											for is_user, text in self.iter_texts():
																		UpperCamelCase   			=     'user' if is_user else 'bot'
																		output += F'''{name} >> {text} \n'''
											return output


@add_end_docstrings(
    snake_case_							,       R"""
        min_length_for_response (`int`, *optional*, defaults to 32):
            The minimum length (in number of tokens) for a response.
        minimum_tokens (`int`, *optional*, defaults to 10):
            The minimum length of tokens to leave for a response.
    """							,       )
class 			SCREAMING_SNAKE_CASE__      (     snake_case_):







				def __init__(       self						,				*A_						,				**A_     )->		Any:







											'''simple docstring'''






											super().__init__(*A_						,				**A_     )
											if self.tokenizer.pad_token_id is None:
																		UpperCamelCase   			=     self.tokenizer.eos_token







				def 						UpperCAmelCase_    (       self						,				A_=None						,				A_=None						,				A_=None						,				**A_     )->		Union[str, Any]:







											'''simple docstring'''






											UpperCamelCase   			=     {}
											UpperCamelCase   			=     {}
											UpperCamelCase   			=     {}

											if min_length_for_response is not None:
																		UpperCamelCase   			=     min_length_for_response
											if minimum_tokens is not None:
																		UpperCamelCase   			=     minimum_tokens

											if "max_length" in generate_kwargs:
																		UpperCamelCase   			=     generate_kwargs['max_length']
																		# self.max_length = generate_kwargs.get("max_length", self.model.config.max_length)
											if clean_up_tokenization_spaces is not None:
																		UpperCamelCase   			=     clean_up_tokenization_spaces

											if generate_kwargs:
																		forward_params.update(A_     )
											return preprocess_params, forward_params, postprocess_params







				def __call__(       self						,				A_						,				A_=0						,				**A_     )->		Any:







											'''simple docstring'''






											UpperCamelCase   			=     super().__call__(A_						,				num_workers=A_						,				**A_     )
											if isinstance(A_						,				A_     ) and len(A_     ) == 1:
																		return outputs[0]
											return outputs







				def 						UpperCAmelCase_    (       self						,				A_						,				A_=32     )->		Dict[str, Any]:







											'''simple docstring'''






											if not isinstance(A_						,				A_     ):
																		raise ValueError('ConversationalPipeline, expects Conversation as inputs'     )
											if conversation.new_user_input is None:
																		raise ValueError(
																		    F'''Conversation with UUID {type(conversation.uuid     )} does not contain new user input to process. '''
																		    'Add user inputs with the conversation\'s `add_user_input` method'     )
											if hasattr(self.tokenizer						,				'_build_conversation_input_ids'     ):
																		UpperCamelCase   			=     self.tokenizer._build_conversation_input_ids(A_     )
											else:
																		# If the tokenizer cannot handle conversations, we default to only the old version
																		UpperCamelCase   			=     self._legacy_parse_and_tokenize(A_     )

											if self.framework == "pt":
																		UpperCamelCase   			=     torch.LongTensor([input_ids]     )
											elif self.framework == "tf":
																		UpperCamelCase   			=     tf.constant([input_ids]     )
											return {"input_ids": input_ids, "conversation": conversation}







				def 						UpperCAmelCase_    (       self						,				A_						,				A_=10						,				**A_     )->		Optional[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     generate_kwargs.get('max_length'						,				self.model.config.max_length     )

											UpperCamelCase   			=     model_inputs['input_ids'].shape[1]
											if max_length - minimum_tokens < n:
																		logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})'''     )
																		UpperCamelCase   			=     max_length - minimum_tokens
																		UpperCamelCase   			=     model_inputs['input_ids'][:, -trim:]
																		if "attention_mask" in model_inputs:
																									UpperCamelCase   			=     model_inputs['attention_mask'][:, -trim:]
											UpperCamelCase   			=     model_inputs.pop('conversation'     )
											UpperCamelCase   			=     max_length
											UpperCamelCase   			=     self.model.generate(**A_						,				**A_     )
											if self.model.config.is_encoder_decoder:
																		UpperCamelCase   			=     1
											else:
																		UpperCamelCase   			=     n
											return {"output_ids": output_ids[:, start_position:], "conversation": conversation}







				def 						UpperCAmelCase_    (       self						,				A_						,				A_=True     )->		Tuple:







											'''simple docstring'''






											UpperCamelCase   			=     model_outputs['output_ids']
											UpperCamelCase   			=     self.tokenizer.decode(
											    output_ids[0]						,				skip_special_tokens=A_						,				clean_up_tokenization_spaces=A_						,				)
											UpperCamelCase   			=     model_outputs['conversation']
											conversation.mark_processed()
											conversation.append_response(A_     )
											return conversation







				def 						UpperCAmelCase_    (       self						,				A_     )->		Dict:







											'''simple docstring'''






											UpperCamelCase   			=     self.tokenizer.eos_token_id
											UpperCamelCase   			=     []
											for is_user, text in conversation.iter_texts():
																		if eos_token_id is not None:
																									input_ids.extend(self.tokenizer.encode(A_						,				add_special_tokens=A_     ) + [eos_token_id]     )
																		else:
																									input_ids.extend(self.tokenizer.encode(A_						,				add_special_tokens=A_     )     )

											if len(A_     ) > self.tokenizer.model_max_length:
																		UpperCamelCase   			=     input_ids[-self.tokenizer.model_max_length :]
											return input_ids




