code: 
  
  
  
  
  """simple docstring"""
  
  import argparse
  
  import requests
  import torch
  from PIL import Image
  
  from transformers import (
      CLIPSegConfig,
      CLIPSegForImageSegmentation,
      CLIPSegProcessor,
      CLIPSegTextConfig,
      CLIPSegVisionConfig,
      CLIPTokenizer,
      ViTImageProcessor,
  )
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Union[str, Any]   ):
  		snake_case		:						Optional[Any]								=				CLIPSegTextConfig()
  		snake_case		:						Any								=				CLIPSegVisionConfig(patch_size=16   )
  
  		snake_case		:						Tuple								=				True if "refined" in model_name else False
  		snake_case		:						Optional[int]								=				16 if "rd16" in model_name else 64
  
  		snake_case		:						Any								=				CLIPSegConfig.from_text_vision_configs(
  		    lowercase__       ,     lowercase__       ,     use_complex_transposed_convolution=lowercase__       ,     reduce_dim=lowercase__       ,     )
  		return config
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Optional[Any]   ):
  		# update prefixes
  		if "clip_model" in name:
  				snake_case		:						str								=				name.replace("clip_model"       ,     "clip"   )
  		if "transformer" in name:
  				if "visual" in name:
  						snake_case		:						List[str]								=				name.replace("visual.transformer"       ,     "vision_model"   )
  				else:
  						snake_case		:						Union[str, Any]								=				name.replace("transformer"       ,     "text_model"   )
  		if "resblocks" in name:
  				snake_case		:						int								=				name.replace("resblocks"       ,     "encoder.layers"   )
  		if "ln_1" in name:
  				snake_case		:						Dict								=				name.replace("ln_1"       ,     "layer_norm1"   )
  		if "ln_2" in name:
  				snake_case		:						Tuple								=				name.replace("ln_2"       ,     "layer_norm2"   )
  		if "c_fc" in name:
  				snake_case		:						Union[str, Any]								=				name.replace("c_fc"       ,     "fc1"   )
  		if "c_proj" in name:
  				snake_case		:						int								=				name.replace("c_proj"       ,     "fc2"   )
  		if "attn" in name and "self" not in name:
  				snake_case		:						str								=				name.replace("attn"       ,     "self_attn"   )
  		# text encoder
  		if "token_embedding" in name:
  				snake_case		:						Optional[int]								=				name.replace("token_embedding"       ,     "text_model.embeddings.token_embedding"   )
  		if "positional_embedding" in name and "visual" not in name:
  				snake_case		:						Optional[int]								=				name.replace("positional_embedding"       ,     "text_model.embeddings.position_embedding.weight"   )
  		if "ln_final" in name:
  				snake_case		:						str								=				name.replace("ln_final"       ,     "text_model.final_layer_norm"   )
  		# vision encoder
  		if "visual.class_embedding" in name:
  				snake_case		:						Union[str, Any]								=				name.replace("visual.class_embedding"       ,     "vision_model.embeddings.class_embedding"   )
  		if "visual.conv1" in name:
  				snake_case		:						int								=				name.replace("visual.conv1"       ,     "vision_model.embeddings.patch_embedding"   )
  		if "visual.positional_embedding" in name:
  				snake_case		:						List[Any]								=				name.replace("visual.positional_embedding"       ,     "vision_model.embeddings.position_embedding.weight"   )
  		if "visual.ln_pre" in name:
  				snake_case		:						Tuple								=				name.replace("visual.ln_pre"       ,     "vision_model.pre_layrnorm"   )
  		if "visual.ln_post" in name:
  				snake_case		:						Optional[int]								=				name.replace("visual.ln_post"       ,     "vision_model.post_layernorm"   )
  		# projection layers
  		if "visual.proj" in name:
  				snake_case		:						int								=				name.replace("visual.proj"       ,     "visual_projection.weight"   )
  		if "text_projection" in name:
  				snake_case		:						str								=				name.replace("text_projection"       ,     "text_projection.weight"   )
  		# decoder
  		if "trans_conv" in name:
  				snake_case		:						Any								=				name.replace("trans_conv"       ,     "transposed_convolution"   )
  		if "film_mul" in name or "film_add" in name or "reduce" in name or "transposed_convolution" in name:
  				snake_case		:						Optional[int]								=				"decoder." + name
  		if "blocks" in name:
  				snake_case		:						Optional[Any]								=				name.replace("blocks"       ,     "decoder.layers"   )
  		if "linear1" in name:
  				snake_case		:						Dict								=				name.replace("linear1"       ,     "mlp.fc1"   )
  		if "linear2" in name:
  				snake_case		:						Tuple								=				name.replace("linear2"       ,     "mlp.fc2"   )
  		if "norm1" in name and "layer_" not in name:
  				snake_case		:						List[str]								=				name.replace("norm1"       ,     "layer_norm1"   )
  		if "norm2" in name and "layer_" not in name:
  				snake_case		:						str								=				name.replace("norm2"       ,     "layer_norm2"   )
  
  		return name
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     str       ,     lowercase__  :     List[str]   ):
  		for key in orig_state_dict.copy().keys():
  				snake_case		:						Dict								=				orig_state_dict.pop(lowercase__   )
  
  				if key.startswith("clip_model"   ) and "attn.in_proj" in key:
  						snake_case		:						Union[str, Any]								=				key.split("."   )
  						if "visual" in key:
  								snake_case		:						List[Any]								=				int(key_split[4]   )
  								snake_case		:						Optional[Any]								=				config.vision_config.hidden_size
  								snake_case		:						Dict								=				"vision_model"
  						else:
  								snake_case		:						Dict								=				int(key_split[3]   )
  								snake_case		:						Optional[int]								=				config.text_config.hidden_size
  								snake_case		:						List[Any]								=				"text_model"
  
  						if "weight" in key:
  								snake_case		:						Optional[Any]								=				val[:dim, :]
  								snake_case		:						Optional[int]								=				val[
  								    dim : dim * 2, :
  								]
  								snake_case		:						Optional[int]								=				val[-dim:, :]
  						else:
  								snake_case		:						Any								=				val[:dim]
  								snake_case		:						Optional[Any]								=				val[dim : dim * 2]
  								snake_case		:						Optional[Any]								=				val[-dim:]
  				elif "self_attn" in key and "out_proj" not in key:
  						snake_case		:						Tuple								=				key.split("."   )
  						snake_case		:						Optional[int]								=				int(key_split[1]   )
  						snake_case		:						Optional[int]								=				config.reduce_dim
  						if "weight" in key:
  								snake_case		:						Dict								=				val[:dim, :]
  								snake_case		:						Tuple								=				val[dim : dim * 2, :]
  								snake_case		:						str								=				val[-dim:, :]
  						else:
  								snake_case		:						List[str]								=				val[:dim]
  								snake_case		:						Dict								=				val[dim : dim * 2]
  								snake_case		:						Tuple								=				val[-dim:]
  				else:
  						snake_case		:						Union[str, Any]								=				rename_key(lowercase__   )
  						if "visual_projection" in new_name or "text_projection" in new_name:
  								snake_case		:						List[str]								=				val.T
  						snake_case		:						Dict								=				val
  
  		return orig_state_dict
  
  
  
  def   UpperCamelCase__     (		):
  		snake_case		:						Any								=				"http://images.cocodataset.org/val2017/000000039769.jpg"
  		snake_case		:						List[str]								=				Image.open(requests.get(lowercase__       ,     stream=lowercase__   ).raw   )
  		return image
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Dict       ,     lowercase__  :     Tuple       ,     lowercase__  :     str       ,     lowercase__  :     Tuple   ):
  		snake_case		:						str								=				get_clipseg_config(lowercase__   )
  		snake_case		:						str								=				CLIPSegForImageSegmentation(lowercase__   )
  		model.eval()
  
  		snake_case		:						List[Any]								=				torch.load(lowercase__       ,     map_location="cpu"   )
  
  		# remove some keys
  		for key in state_dict.copy().keys():
  				if key.startswith("model"   ):
  						state_dict.pop(lowercase__       ,     lowercase__   )
  
      # rename some keys
  		snake_case		:						Tuple								=				convert_state_dict(lowercase__       ,     lowercase__   )
  		snake_case			,				snake_case		:						List[Any]								=				model.load_state_dict(lowercase__       ,     strict=lowercase__   )
  
  		if missing_keys != ["clip.text_model.embeddings.position_ids", "clip.vision_model.embeddings.position_ids"]:
  				raise ValueError("Missing keys that are not expected: {}".format(lowercase__   )   )
  		if unexpected_keys != ["decoder.reduce.weight", "decoder.reduce.bias"]:
  				raise ValueError(F'''Unexpected keys: {unexpected_keys}'''   )
  
  		snake_case		:						str								=				ViTImageProcessor(size=352   )
  		snake_case		:						Union[str, Any]								=				CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32"   )
  		snake_case		:						List[str]								=				CLIPSegProcessor(image_processor=lowercase__       ,     tokenizer=lowercase__   )
  
  		snake_case		:						List[Any]								=				prepare_img()
  		snake_case		:						Union[str, Any]								=				["a glass", "something to fill", "wood", "a jar"]
  
  		snake_case		:						Dict								=				processor(text=lowercase__       ,     images=[image] * len(lowercase__   )       ,     padding="max_length"       ,     return_tensors="pt"   )
  
  		with torch.no_grad():
  				snake_case		:						Optional[int]								=				model(**lowercase__   )
  
  		# verify values
  		snake_case		:						int								=				torch.tensor([0.11_10, -0.18_82, 0.16_45]   )
  		snake_case		:						List[Any]								=				torch.tensor([0.26_92, -0.71_97, -0.13_28]   )
  		if model_name == "clipseg-rd64-refined":
  				snake_case		:						Tuple								=				torch.tensor(
  				    [[-10.04_07, -9.94_31, -10.26_46], [-9.97_51, -9.70_64, -9.95_86], [-9.68_91, -9.56_45, -9.96_18]]   )
  		elif model_name == "clipseg-rd64":
  				snake_case		:						str								=				torch.tensor(
  				    [[-7.28_77, -7.27_11, -7.24_63], [-7.26_52, -7.27_80, -7.25_20], [-7.22_39, -7.22_04, -7.20_01]]   )
  		elif model_name == "clipseg-rd16":
  				snake_case		:						Any								=				torch.tensor(
  				    [[-6.39_55, -6.40_55, -6.41_51], [-6.39_11, -6.40_33, -6.41_00], [-6.34_74, -6.37_02, -6.37_62]]   )
  		else:
  				raise ValueError(F'''Model name {model_name} not supported.'''   )
  
  		assert torch.allclose(outputs.logits[0, :3, :3]       ,     lowercase__       ,     atol=1E-3   )
  		assert torch.allclose(outputs.conditional_embeddings[0, :3]       ,     lowercase__       ,     atol=1E-3   )
  		assert torch.allclose(outputs.pooled_output[0, :3]       ,     lowercase__       ,     atol=1E-3   )
  		print("Looks ok!"   )
  
  		if pytorch_dump_folder_path is not None:
  				print(F'''Saving model and processor to {pytorch_dump_folder_path}'''   )
  				model.save_pretrained(lowercase__   )
  				processor.save_pretrained(lowercase__   )
  
  		if push_to_hub:
  				print(F'''Pushing model and processor for {model_name} to the hub'''   )
  				model.push_to_hub(F'''CIDAS/{model_name}'''   )
  				processor.push_to_hub(F'''CIDAS/{model_name}'''   )
  
  
  if __name__ == "__main__":
  							__A 		=			argparse.ArgumentParser()
  							# Required parameters
  							parser.add_argument(
  							    "--model_name",
  							    default="clipseg-rd64",
  							    type=str,
  							    choices=["clipseg-rd16", "clipseg-rd64", "clipseg-rd64-refined"],
  							    help=(
  							        "Name of the model. Supported models are: clipseg-rd64, clipseg-rd16 and clipseg-rd64-refined (rd meaning"
  							        " reduce dimension)"
  							    ),
  							)
  							parser.add_argument(
  							    "--checkpoint_path",
  							    default="/Users/nielsrogge/Documents/CLIPSeg/clip_plus_rd64-uni.pth",
  							    type=str,
  							    help=(
  							        "Path to the original checkpoint. Note that the script assumes that the checkpoint includes both CLIP and"
  							        " the decoder weights."
  							    ),
  							)
  							parser.add_argument(
  							    "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
  							)
  							parser.add_argument(
  							    "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub."
  							)
  
  							__A 		=			parser.parse_args()
  							convert_clipseg_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
  
  
style_context: 
  
  
  
  
  """simple docstring"""
  
  import textwrap
  
  import pyarrow as pa
  import pytest
  
  from datasets import Features, Image
  from datasets.packaged_modules.text.text import Text
  
  from ..utils import require_pil
  
  
  
  @pytest.fixture
  def   UpperCamelCase__     (		lowercase__  :     str   ):
  		snake_case		:						List[str]								=				tmp_path / "text.txt"
  		snake_case		:						Tuple								=				textwrap.dedent(
  		    "\\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n        Second paragraph:\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n        "   )
  		with open(lowercase__       ,     "w"       ,     encoding="utf-8"   ) as f:
  				f.write(lowercase__   )
  		return str(lowercase__   )
  
  
  
  @pytest.fixture
  def   UpperCamelCase__     (		lowercase__  :     str       ,     lowercase__  :     Dict   ):
  		snake_case		:						Any								=				tmp_path / "text_with_image.txt"
  		with open(lowercase__       ,     "w"       ,     encoding="utf-8"   ) as f:
  				f.write(lowercase__   )
  		return str(lowercase__   )
  
  
  
  @pytest.mark.parametrize("keep_linebreaks"       ,     [True, False]   )
  def   UpperCamelCase__     (		lowercase__  :     Union[str, Any]       ,     lowercase__  :     Tuple   ):
  		with open(lowercase__       ,     encoding="utf-8"   ) as f:
  				snake_case		:						List[Any]								=				f.read().splitlines(keepends=lowercase__   )
  		snake_case		:						List[Any]								=				Text(keep_linebreaks=lowercase__       ,     encoding="utf-8"   )
  		snake_case		:						Optional[int]								=				text._generate_tables([[text_file]]   )
  		snake_case		:						int								=				pa.concat_tables([table for _, table in generator]   ).to_pydict()["text"]
  		assert generated_content == expected_content
  
  
  
  @require_pil
  def   UpperCamelCase__     (		lowercase__  :     Optional[Any]   ):
  		with open(lowercase__       ,     encoding="utf-8"   ) as f:
  				snake_case		:						Union[str, Any]								=				f.read().splitlines()[0]
  		snake_case		:						List[Any]								=				Text(encoding="utf-8"       ,     features=Features({"image": Image()}   )   )
  		snake_case		:						Tuple								=				text._generate_tables([[text_file_with_image]]   )
  		snake_case		:						List[str]								=				pa.concat_tables([table for _, table in generator]   )
  		assert pa_table.schema.field("image"   ).type == Image()()
  		snake_case		:						Optional[Any]								=				pa_table.to_pydict()["image"]
  		assert generated_content == [{"path": image_file, "bytes": None}]
  
  
  
  @pytest.mark.parametrize("sample_by"       ,     ["line", "paragraph", "document"]   )
  def   UpperCamelCase__     (		lowercase__  :     Union[str, Any]       ,     lowercase__  :     List[str]   ):
  		with open(lowercase__       ,     encoding="utf-8"   ) as f:
  				snake_case		:						Tuple								=				f.read()
  		if sample_by == "line":
  				snake_case		:						Optional[int]								=				expected_content.splitlines()
  		elif sample_by == "paragraph":
  				snake_case		:						str								=				expected_content.split("\n\n"   )
  		elif sample_by == "document":
  				snake_case		:						List[Any]								=				[expected_content]
  		snake_case		:						str								=				Text(sample_by=lowercase__       ,     encoding="utf-8"       ,     chunksize=100   )
  		snake_case		:						Optional[int]								=				text._generate_tables([[text_file]]   )
  		snake_case		:						Optional[Any]								=				pa.concat_tables([table for _, table in generator]   ).to_pydict()["text"]
  		assert generated_content == expected_content
  
  