




import json
import os
import re
import unicodedata
from typing import Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace
from ...utils import logging


UpperCAmelCase__						:       Union[str, Any]							=    logging.get_logger(__name__)

UpperCAmelCase__						:       Dict							=    {
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
}

UpperCAmelCase__						:       List[Any]							=    {
    'vocab_file': {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/vocab.json'},
    'merges_file': {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/merges.txt'},
}

UpperCAmelCase__						:       Union[str, Any]							=    {
    'openai-gpt': 512,
}


def 		_lowerCamelCase				(					UpperCamelCase							)			->		int:





  """simple docstring"""





  _lowercase: Optional[Any]						     =    text.strip()
  if not text:
    return []
  _lowercase: Optional[Any]						     =    text.split()
  return tokens

class      UpperCAmelCase						(			a_   ):






   def __init__(	self   :       str						,					UpperCamelCase_   :       Union[str, Any]=True						,					UpperCamelCase_   :       Optional[Any]=None						,					UpperCamelCase_   :       Dict=True						,					UpperCamelCase_   :       List[Any]=None						,					UpperCamelCase_   :       Optional[Any]=True						,					)->			Optional[Any]:
     if never_split is None:
       _lowercase: Union[str, Any]						     =    []
     _lowercase: List[Any]						     =    do_lower_case
     _lowercase: List[Any]						     =    set(UpperCamelCase_     )
     _lowercase: int						     =    tokenize_chinese_chars
     _lowercase: Dict						     =    strip_accents
     _lowercase: List[Any]						     =    do_split_on_punc






   def  _a						(	self   :       int						,					UpperCamelCase_   :       List[str]						,					UpperCamelCase_   :       List[Any]=None     )->			List[str]:
     _lowercase: Any						     =    self.never_split.union(set(UpperCamelCase_     )     ) if never_split else self.never_split
     _lowercase: int						     =    self._clean_text(UpperCamelCase_     )

     # This was added on November 1st, 2018 for the multilingual and Chinese
     # models. This is also applied to the English models now, but it doesn't
     # matter since the English models were not trained on any Chinese data
     # and generally don't have any Chinese data in them (there are Chinese
     # characters in the vocabulary because Wikipedia does have some Chinese
     # words in the English Wikipedia.).
     if self.tokenize_chinese_chars:
       _lowercase: Optional[int]						     =    self._tokenize_chinese_chars(UpperCamelCase_     )
     # prevents treating the same character with different unicode codepoints as different characters
     _lowercase: int						     =    unicodedata.normalize('NFC'						,					UpperCamelCase_     )
     _lowercase: Tuple						     =    whitespace_tokenize(UpperCamelCase_     )
     _lowercase: Dict						     =    []
     for token in orig_tokens:
       if token not in never_split:
         if self.do_lower_case:
           _lowercase: Tuple						     =    token.lower()
           if self.strip_accents is not False:
             _lowercase: List[Any]						     =    self._run_strip_accents(UpperCamelCase_     )
         elif self.strip_accents:
           _lowercase: List[str]						     =    self._run_strip_accents(UpperCamelCase_     )
       split_tokens.extend(self._run_split_on_punc(UpperCamelCase_						,					UpperCamelCase_     )     )

     _lowercase: str						     =    whitespace_tokenize(' '.join(UpperCamelCase_     )     )
     return output_tokens






   def  _a						(	self   :       List[str]						,					UpperCamelCase_   :       Union[str, Any]     )->			str:
     _lowercase: Optional[Any]						     =    unicodedata.normalize('NFD'						,					UpperCamelCase_     )
     _lowercase: Dict						     =    []
     for char in text:
       _lowercase: Tuple						     =    unicodedata.category(UpperCamelCase_     )
       if cat == "Mn":
         continue
       output.append(UpperCamelCase_     )
     return "".join(UpperCamelCase_     )






   def  _a						(	self   :       List[str]						,					UpperCamelCase_   :       List[Any]						,					UpperCamelCase_   :       int=None     )->			List[Any]:
     if not self.do_split_on_punc or (never_split is not None and text in never_split):
       return [text]
     _lowercase: Optional[int]						     =    list(UpperCamelCase_     )
     _lowercase: Dict						     =    0
     _lowercase: Union[str, Any]						     =    True
     _lowercase: int						     =    []
     while i < len(UpperCamelCase_     ):
       _lowercase: List[str]						     =    chars[i]
       if _is_punctuation(UpperCamelCase_     ):
         output.append([char]     )
         _lowercase: str						     =    True
       else:
         if start_new_word:
           output.append([]     )
         _lowercase: List[str]						     =    False
         output[-1].append(UpperCamelCase_     )
       i += 1

     return ["".join(UpperCamelCase_     ) for x in output]






   def  _a						(	self   :       List[str]						,					UpperCamelCase_   :       int     )->			Optional[Any]:
     _lowercase: Dict						     =    []
     for char in text:
       _lowercase: Optional[int]						     =    ord(UpperCamelCase_     )
       if self._is_chinese_char(UpperCamelCase_     ):
         output.append(' '     )
         output.append(UpperCamelCase_     )
         output.append(' '     )
       else:
         output.append(UpperCamelCase_     )
     return "".join(UpperCamelCase_     )






   def  _a						(	self   :       Union[str, Any]						,					UpperCamelCase_   :       int     )->			Optional[Any]:
     # This defines a "chinese character" as anything in the CJK Unicode block:
     #   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
     #
     # Note that the CJK Unicode block is NOT all Japanese and Korean characters,
     # despite its name. The modern Korean Hangul alphabet is a different block,
     # as is Japanese Hiragana and Katakana. Those alphabets are used to write
     # space-separated words, so they are not treated specially and handled
     # like the all of the other languages.
     if (
         (cp >= 0x4_e_0_0 and cp <= 0x9_f_f_f)
         or (cp >= 0x3_4_0_0 and cp <= 0x4_d_b_f)  #
         or (cp >= 0x2_0_0_0_0 and cp <= 0x2_a_6_d_f)  #
         or (cp >= 0x2_a_7_0_0 and cp <= 0x2_b_7_3_f)  #
         or (cp >= 0x2_b_7_4_0 and cp <= 0x2_b_8_1_f)  #
         or (cp >= 0x2_b_8_2_0 and cp <= 0x2_c_e_a_f)  #
         or (cp >= 0xf_9_0_0 and cp <= 0xf_a_f_f)
         or (cp >= 0x2_f_8_0_0 and cp <= 0x2_f_a_1_f)  #
     ):  #
       return True

     return False






   def  _a						(	self   :       Any						,					UpperCamelCase_   :       int     )->			Dict:
     _lowercase: int						     =    []
     for char in text:
       _lowercase: str						     =    ord(UpperCamelCase_     )
       if cp == 0 or cp == 0xf_f_f_d or _is_control(UpperCamelCase_     ):
         continue
       if _is_whitespace(UpperCamelCase_     ):
         output.append(' '     )
       else:
         output.append(UpperCamelCase_     )
     return "".join(UpperCamelCase_     )


def 		_lowerCamelCase				(					UpperCamelCase							)			->		Union[str, Any]:





  """simple docstring"""





  _lowercase: List[Any]						     =    set()
  _lowercase: Tuple						     =    word[0]
  for char in word[1:]:
    pairs.add((prev_char, char)							)
    _lowercase: Tuple						     =    char
  return pairs


def 		_lowerCamelCase				(					UpperCamelCase							)			->		Any:





  """simple docstring"""





  _lowercase: Union[str, Any]						     =    text.replace('—'					,     '-'							)
  _lowercase: int						     =    text.replace('–'					,     '-'							)
  _lowercase: Dict						     =    text.replace('―'					,     '-'							)
  _lowercase: Any						     =    text.replace('…'					,     '...'							)
  _lowercase: Dict						     =    text.replace('´'					,     '\''							)
  _lowercase: Union[str, Any]						     =    re.sub(r'(-+|~+|!+|"+|;+|\?+|\++|,+|\)+|\(+|\\+|\/+|\*+|\[+|\]+|}+|{+|\|+|_+)'					,     r' \1 '					,     lowercase_							)
  _lowercase: Dict						     =    re.sub(r'\s*\n\s*'					,     ' \n '					,     lowercase_							)
  _lowercase: Union[str, Any]						     =    re.sub(r'[^\S\n]+'					,     ' '					,     lowercase_							)
  return text.strip()





class      UpperCAmelCase						(			a_   ):
   __SCREAMING_SNAKE_CASE		:							int							=					VOCAB_FILES_NAMES
   __SCREAMING_SNAKE_CASE		:							Any							=					PRETRAINED_VOCAB_FILES_MAP
   __SCREAMING_SNAKE_CASE		:							Optional[int]							=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __SCREAMING_SNAKE_CASE		:							List[str]							=					['input_ids', 'attention_mask']






   def __init__(	self   :       str						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       Tuple						,					UpperCamelCase_   :       Union[str, Any]="<unk>"						,					**UpperCamelCase_   :       Any     )->			Dict:
     super().__init__(unk_token=UpperCamelCase_						,					**UpperCamelCase_     )

     try:
       import ftfy
       from spacy.lang.en import English

       _lowercase: Any						     =    English()
       _lowercase: Any						     =    _nlp.tokenizer
       _lowercase: Dict						     =    ftfy.fix_text
     except ImportError:
       logger.warning('ftfy or spacy is not installed using BERT BasicTokenizer instead of SpaCy & ftfy.'     )
       _lowercase: int						     =    BasicTokenizer(do_lower_case=UpperCamelCase_     )
       _lowercase: List[Any]						     =    None

     with open(UpperCamelCase_						,					encoding='utf-8'     ) as vocab_handle:
       _lowercase: Tuple						     =    json.load(UpperCamelCase_     )
     _lowercase: List[Any]						     =    {v: k for k, v in self.encoder.items()}
     with open(UpperCamelCase_						,					encoding='utf-8'     ) as merges_handle:
       _lowercase: List[str]						     =    merges_handle.read().split('\n'     )[1:-1]
     _lowercase: Optional[int]						     =    [tuple(merge.split()     ) for merge in merges]
     _lowercase: Dict						     =    dict(zip(UpperCamelCase_						,					range(len(UpperCamelCase_     )     )     )     )
     _lowercase: Optional[Any]						     =    {}






   @property
   def  _a						(	self   :       str     )->			int:
     return True






   @property
   def  _a						(	self   :       int     )->			Dict:
     return len(self.encoder     )






   def  _a						(	self   :       Optional[Any]     )->			Union[str, Any]:
     return dict(self.encoder						,					**self.added_tokens_encoder     )






   def  _a						(	self   :       Union[str, Any]						,					UpperCamelCase_   :       Optional[int]     )->			Union[str, Any]:
     _lowercase: int						     =    tuple(token[:-1]     ) + (token[-1] + '</w>',)
     if token in self.cache:
       return self.cache[token]
     _lowercase: Dict						     =    get_pairs(UpperCamelCase_     )

     if not pairs:
       return token + "</w>"

     while True:
       _lowercase: List[str]						     =    min(UpperCamelCase_						,					key=lambda UpperCamelCase_     : self.bpe_ranks.get(UpperCamelCase_						,					float('inf'     )     )     )
       if bigram not in self.bpe_ranks:
         break
       _lowercase: Any						     =    bigram
       _lowercase: Optional[Any]						     =    []
       _lowercase: Optional[int]						     =    0
       while i < len(UpperCamelCase_     ):
         try:
           _lowercase: int						     =    word.index(UpperCamelCase_						,					UpperCamelCase_     )
         except ValueError:
           new_word.extend(word[i:]     )
           break
         else:
           new_word.extend(word[i:j]     )
           _lowercase: Union[str, Any]						     =    j

         if word[i] == first and i < len(UpperCamelCase_     ) - 1 and word[i + 1] == second:
           new_word.append(first + second     )
           i += 2
         else:
           new_word.append(word[i]     )
           i += 1
       _lowercase: str						     =    tuple(UpperCamelCase_     )
       _lowercase: Optional[int]						     =    new_word
       if len(UpperCamelCase_     ) == 1:
         break
       else:
         _lowercase: List[Any]						     =    get_pairs(UpperCamelCase_     )
     _lowercase: Any						     =    ' '.join(UpperCamelCase_     )
     if word == "\n  </w>":
       _lowercase: Union[str, Any]						     =    '\n</w>'
     _lowercase: Tuple						     =    word
     return word






   def  _a						(	self   :       List[str]						,					UpperCamelCase_   :       Optional[Any]     )->			List[Any]:
     _lowercase: Optional[int]						     =    []
     if self.fix_text is None:
       # Using BERT's BasicTokenizer
       _lowercase: int						     =    self.nlp.tokenize(UpperCamelCase_     )
       for token in text:
         split_tokens.extend(list(self.bpe(UpperCamelCase_     ).split(' '     )     )     )
     else:
       # Using SpaCy & ftfy (original tokenization process of OpenAI GPT)
       _lowercase: Optional[Any]						     =    self.nlp(text_standardize(self.fix_text(UpperCamelCase_     )     )     )
       for token in text:
         split_tokens.extend(list(self.bpe(token.text.lower()     ).split(' '     )     )     )
     return split_tokens






   def  _a						(	self   :       int						,					UpperCamelCase_   :       Optional[int]     )->			int:
     return self.encoder.get(UpperCamelCase_						,					self.encoder.get(self.unk_token     )     )






   def  _a						(	self   :       Dict						,					UpperCamelCase_   :       List[str]     )->			str:
     return self.decoder.get(UpperCamelCase_						,					self.unk_token     )






   def  _a						(	self   :       Union[str, Any]						,					UpperCamelCase_   :       Dict     )->			Tuple:
     _lowercase: str						     =    ''.join(UpperCamelCase_     ).replace('</w>'						,					' '     ).strip()
     return out_string






   def  _a						(	self   :       List[str]						,					UpperCamelCase_   :       str						,					UpperCamelCase_   :       Optional[str] = None     )->			Tuple[str]:
     if not os.path.isdir(UpperCamelCase_     ):
       logger.error(f'Vocabulary path ({save_directory}) should be a directory'     )
       return
     _lowercase: str						     =    os.path.join(
         UpperCamelCase_						,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']     )
     _lowercase: List[str]						     =    os.path.join(
         UpperCamelCase_						,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']     )

     with open(UpperCamelCase_						,					'w'						,					encoding='utf-8'     ) as f:
       f.write(json.dumps(self.encoder						,					indent=2						,					sort_keys=UpperCamelCase_						,					ensure_ascii=UpperCamelCase_     ) + '\n'     )

     _lowercase: List[Any]						     =    0
     with open(UpperCamelCase_						,					'w'						,					encoding='utf-8'     ) as writer:
       writer.write('#version: 0.2\n'     )
       for bpe_tokens, token_index in sorted(self.bpe_ranks.items()						,					key=lambda UpperCamelCase_     : kv[1]     ):
         if index != token_index:
           logger.warning(
               f'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.'
               ' Please check that the tokenizer is not corrupted!'     )
           _lowercase: str						     =    token_index
         writer.write(' '.join(UpperCamelCase_     ) + '\n'     )
         index += 1

     return vocab_file, merge_file










import argparse

import requests
import torch
from PIL import Image

from transformers import (
    CLIPSegConfig,
    CLIPSegForImageSegmentation,
    CLIPSegProcessor,
    CLIPSegTextConfig,
    CLIPSegVisionConfig,
    CLIPTokenizer,
    ViTImageProcessor,
)


def 		_lowerCamelCase				(					UpperCamelCase							)			->		str:





  """simple docstring"""





  _lowercase: Any						     =    CLIPSegTextConfig()
  _lowercase: Optional[Any]						     =    CLIPSegVisionConfig(patch_size=16							)

  _lowercase: Union[str, Any]						     =    True if 'refined' in model_name else False
  _lowercase: str						     =    16 if 'rd16' in model_name else 64

  _lowercase: Any						     =    CLIPSegConfig.from_text_vision_configs(
      UpperCamelCase					,     UpperCamelCase					,     use_complex_transposed_convolution=UpperCamelCase					,     reduce_dim=UpperCamelCase					,     )
  return config


def 		_lowerCamelCase				(					UpperCamelCase							)			->		Any:





  """simple docstring"""





  if "clip_model" in name:
    _lowercase: Tuple						     =    name.replace('clip_model'					,     'clip'							)
  if "transformer" in name:
    if "visual" in name:
      _lowercase: Union[str, Any]						     =    name.replace('visual.transformer'					,     'vision_model'							)
    else:
      _lowercase: Optional[int]						     =    name.replace('transformer'					,     'text_model'							)
  if "resblocks" in name:
    _lowercase: List[Any]						     =    name.replace('resblocks'					,     'encoder.layers'							)
  if "ln_1" in name:
    _lowercase: Union[str, Any]						     =    name.replace('ln_1'					,     'layer_norm1'							)
  if "ln_2" in name:
    _lowercase: Union[str, Any]						     =    name.replace('ln_2'					,     'layer_norm2'							)
  if "c_fc" in name:
    _lowercase: str						     =    name.replace('c_fc'					,     'fc1'							)
  if "c_proj" in name:
    _lowercase: List[Any]						     =    name.replace('c_proj'					,     'fc2'							)
  if "attn" in name and "self" not in name:
    _lowercase: Dict						     =    name.replace('attn'					,     'self_attn'							)
  # text encoder
  if "token_embedding" in name:
    _lowercase: Optional[int]						     =    name.replace('token_embedding'					,     'text_model.embeddings.token_embedding'							)
  if "positional_embedding" in name and "visual" not in name:
    _lowercase: int						     =    name.replace('positional_embedding'					,     'text_model.embeddings.position_embedding.weight'							)
  if "ln_final" in name:
    _lowercase: Optional[Any]						     =    name.replace('ln_final'					,     'text_model.final_layer_norm'							)
  # vision encoder
  if "visual.class_embedding" in name:
    _lowercase: Union[str, Any]						     =    name.replace('visual.class_embedding'					,     'vision_model.embeddings.class_embedding'							)
  if "visual.conv1" in name:
    _lowercase: int						     =    name.replace('visual.conv1'					,     'vision_model.embeddings.patch_embedding'							)
  if "visual.positional_embedding" in name:
    _lowercase: List[Any]						     =    name.replace('visual.positional_embedding'					,     'vision_model.embeddings.position_embedding.weight'							)
  if "visual.ln_pre" in name:
    _lowercase: Dict						     =    name.replace('visual.ln_pre'					,     'vision_model.pre_layrnorm'							)
  if "visual.ln_post" in name:
    _lowercase: Dict						     =    name.replace('visual.ln_post'					,     'vision_model.post_layernorm'							)
  # projection layers
  if "visual.proj" in name:
    _lowercase: List[str]						     =    name.replace('visual.proj'					,     'visual_projection.weight'							)
  if "text_projection" in name:
    _lowercase: Any						     =    name.replace('text_projection'					,     'text_projection.weight'							)
  # decoder
  if "trans_conv" in name:
    _lowercase: List[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:
    _lowercase: Any						     =    'decoder.' + name
  if "blocks" in name:
    _lowercase: Optional[int]						     =    name.replace('blocks'					,     'decoder.layers'							)
  if "linear1" in name:
    _lowercase: Optional[Any]						     =    name.replace('linear1'					,     'mlp.fc1'							)
  if "linear2" in name:
    _lowercase: int						     =    name.replace('linear2'					,     'mlp.fc2'							)
  if "norm1" in name and "layer_" not in name:
    _lowercase: Optional[int]						     =    name.replace('norm1'					,     'layer_norm1'							)
  if "norm2" in name and "layer_" not in name:
    _lowercase: Tuple						     =    name.replace('norm2'					,     'layer_norm2'							)

  return name


def 		_lowerCamelCase				(					UpperCamelCase					,     UpperCamelCase							)			->		Optional[Any]:





  """simple docstring"""





  for key in orig_state_dict.copy().keys():
    _lowercase: int						     =    orig_state_dict.pop(UpperCamelCase							)

    if key.startswith('clip_model'							) and "attn.in_proj" in key:
      _lowercase: Union[str, Any]						     =    key.split('.'							)
      if "visual" in key:
        _lowercase: List[str]						     =    int(key_split[4]							)
        _lowercase: Dict						     =    config.vision_config.hidden_size
        _lowercase: Union[str, Any]						     =    'vision_model'
      else:
        _lowercase: str						     =    int(key_split[3]							)
        _lowercase: Optional[Any]						     =    config.text_config.hidden_size
        _lowercase: List[str]						     =    'text_model'

      if "weight" in key:
        _lowercase: Optional[int]						     =    val[:dim, :]
        _lowercase: List[Any]						     =    val[
            dim : dim * 2, :
        ]
        _lowercase: Tuple						     =    val[-dim:, :]
      else:
        _lowercase: Any						     =    val[:dim]
        _lowercase: List[str]						     =    val[dim : dim * 2]
        _lowercase: Optional[int]						     =    val[-dim:]
    elif "self_attn" in key and "out_proj" not in key:
      _lowercase: str						     =    key.split('.'							)
      _lowercase: Union[str, Any]						     =    int(key_split[1]							)
      _lowercase: List[str]						     =    config.reduce_dim
      if "weight" in key:
        _lowercase: Union[str, Any]						     =    val[:dim, :]
        _lowercase: Tuple						     =    val[dim : dim * 2, :]
        _lowercase: Union[str, Any]						     =    val[-dim:, :]
      else:
        _lowercase: Union[str, Any]						     =    val[:dim]
        _lowercase: Dict						     =    val[dim : dim * 2]
        _lowercase: Any						     =    val[-dim:]
    else:
      _lowercase: Dict						     =    rename_key(UpperCamelCase							)
      if "visual_projection" in new_name or "text_projection" in new_name:
        _lowercase: str						     =    val.T
      _lowercase: Tuple						     =    val

  return orig_state_dict


def 		_lowerCamelCase				(					)			->		Optional[Any]:





  """simple docstring"""





  _lowercase: Any						     =    'http://images.cocodataset.org/val2017/000000039769.jpg'
  _lowercase: str						     =    Image.open(requests.get(UpperCamelCase					,     stream=UpperCamelCase							).raw							)
  return image


def 		_lowerCamelCase				(					UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase							)			->		Optional[int]:





  """simple docstring"""





  _lowercase: Optional[Any]						     =    get_clipseg_config(UpperCamelCase							)
  _lowercase: Optional[Any]						     =    CLIPSegForImageSegmentation(UpperCamelCase							)
  model.eval()

  _lowercase: Dict						     =    torch.load(UpperCamelCase					,     map_location='cpu'							)

  # remove some keys
  for key in state_dict.copy().keys():
    if key.startswith('model'							):
      state_dict.pop(UpperCamelCase					,     UpperCamelCase							)

    # rename some keys
  _lowercase: List[str]						     =    convert_state_dict(UpperCamelCase					,     UpperCamelCase							)
  _lowercase     ,					_lowercase: List[Any]						     =    model.load_state_dict(UpperCamelCase					,     strict=UpperCamelCase							)

  if missing_keys != ["clip.text_model.embeddings.position_ids", "clip.vision_model.embeddings.position_ids"]:
    raise ValueError('Missing keys that are not expected: {}'.format(UpperCamelCase							)							)
  if unexpected_keys != ["decoder.reduce.weight", "decoder.reduce.bias"]:
    raise ValueError(F'Unexpected keys: {unexpected_keys}'							)

  _lowercase: Tuple						     =    ViTImageProcessor(size=352							)
  _lowercase: Union[str, Any]						     =    CLIPTokenizer.from_pretrained('openai/clip-vit-base-patch32'							)
  _lowercase: Optional[Any]						     =    CLIPSegProcessor(image_processor=UpperCamelCase					,     tokenizer=UpperCamelCase							)

  _lowercase: Union[str, Any]						     =    prepare_img()
  _lowercase: List[Any]						     =    ['a glass', 'something to fill', 'wood', 'a jar']

  _lowercase: str						     =    processor(text=UpperCamelCase					,     images=[image] * len(UpperCamelCase							)					,     padding='max_length'					,     return_tensors='pt'							)

  with torch.no_grad():
    _lowercase: Optional[Any]						     =    model(**UpperCamelCase							)

  # verify values
  _lowercase: int						     =    torch.tensor([0.1_110, -0.1_882, 0.1_645]							)
  _lowercase: Tuple						     =    torch.tensor([0.2_692, -0.7_197, -0.1_328]							)
  if model_name == "clipseg-rd64-refined":
    _lowercase: str						     =    torch.tensor(
        [[-10.0_407, -9.9_431, -10.2_646], [-9.9_751, -9.7_064, -9.9_586], [-9.6_891, -9.5_645, -9.9_618]]							)
  elif model_name == "clipseg-rd64":
    _lowercase: Dict						     =    torch.tensor(
        [[-7.2_877, -7.2_711, -7.2_463], [-7.2_652, -7.2_780, -7.2_520], [-7.2_239, -7.2_204, -7.2_001]]							)
  elif model_name == "clipseg-rd16":
    _lowercase: Optional[int]						     =    torch.tensor(
        [[-6.3_955, -6.4_055, -6.4_151], [-6.3_911, -6.4_033, -6.4_100], [-6.3_474, -6.3_702, -6.3_762]]							)
  else:
    raise ValueError(F'Model name {model_name} not supported.'							)

  assert torch.allclose(outputs.logits[0, :3, :3]					,     UpperCamelCase					,     atol=1E-3							)
  assert torch.allclose(outputs.conditional_embeddings[0, :3]					,     UpperCamelCase					,     atol=1E-3							)
  assert torch.allclose(outputs.pooled_output[0, :3]					,     UpperCamelCase					,     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(UpperCamelCase							)
    processor.save_pretrained(UpperCamelCase							)

  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__":
   UpperCAmelCase__						:       int							=    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.'
   )

   UpperCAmelCase__						:       str							=    parser.parse_args()
   convert_clipseg_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
