codingutf8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license imagetext processor class for clipseg attributes imageprocessor tokenizer imageprocessorclass vitimageprocessor tokenizerclass cliptokenizer cliptokenizerfast def initself imageprocessornone tokenizernone kwargs featureextractor none if featureextractor in kwargs warnings warn the featureextractor argument is deprecated and will be removed in v5 use imageprocessor instead futurewarning featureextractor kwargs popfeatureextractor imageprocessor imageprocessor if imageprocessor is not none else featureextractor if imageprocessor is none raise valueerroryou need to specify an imageprocessor if tokenizer is none raise valueerroryou need to specify a tokenizer super initimageprocessor tokenizer def callself textnone imagesnone visualpromptnone returntensorsnone kwargs if text is none and visualprompt is none and images is none raise valueerroryou have to specify either text visual prompt or images if text is not none and visualprompt is not none raise valueerroryou have to specify exactly one type of prompt either text or visual prompt if text is not none encoding self tokenizertext returntensorsreturntensors kwargs if visualprompt is not none promptfeatures self imageprocessorvisualprompt returntensorsreturntensors kwargs if images is not none imagefeatures self imageprocessorimages returntensorsreturntensors kwargs if visualprompt is not none and images is not none encoding pixelvalues imagefeatures pixelvalues conditionalpixelvalues promptfeatures pixelvalues return encoding elif text is not none and images is not none encodingpixelvalues imagefeatures pixelvalues return encoding elif text is not none return encoding elif visualprompt is not none encoding conditionalpixelvalues promptfeatures pixelvalues return encoding else return batchencodingdatadictimagefeatures tensortypereturntensors def batchdecodeself args kwargs return self tokenizer batchdecodeargs kwargs def decodeself args kwargs return self tokenizer decodeargs kwargs property def featureextractorclassself warnings warn featureextractorclass is deprecated and will be removed in v5 use imageprocessorclass instead futurewarning return self imageprocessorclass property def featureextractorself warnings warn featureextractor is deprecated and will be removed in v5 use imageprocessor instead futurewarning return self imageprocessor coding utf 8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license image text processor class for clipseg constructs a clipseg processor which wraps a clipseg image processor and a clip tokenizer into a single processor clipsegprocessor offers all the functionalities of vitimageprocessor and cliptokenizerfast see the clipsegprocessor __call__ and clipsegprocessor decode for more information args image_processor vitimageprocessor optional the image processor is a required input tokenizer cliptokenizerfast optional the tokenizer is a required input main method to prepare for the model one or several sequences s and image s this method forwards the text and kwargs arguments to cliptokenizerfast s cliptokenizerfast __call__ if text is not none to encode the text to prepare the image s this method forwards the images and kwrags arguments to vitimageprocessor s vitimageprocessor __call__ if images is not none please refer to the doctsring of the above two methods for more information args text str list str list list str the sequence or batch of sequences to be encoded each sequence can be a string or a list of strings pretokenized string if the sequences are provided as list of strings pretokenized you must set is_split_into_words true to lift the ambiguity with a batch of sequences images pil image image np ndarray torch tensor list pil image image list np ndarray list torch tensor the image or batch of images to be prepared each image can be a pil image numpy array or pytorch tensor in case of a numpy array pytorch tensor each image should be of shape c h w where c is a number of channels h and w are image height and width visual_prompt pil image image np ndarray torch tensor list pil image image list np ndarray list torch tensor the visual prompt image or batch of images to be prepared each visual prompt image can be a pil image numpy array or pytorch tensor in case of a numpy array pytorch tensor each image should be of shape c h w where c is a number of channels h and w are image height and width return_tensors str or utils tensortype optional if set will return tensors of a particular framework acceptable values are tf return tensorflow tf constant objects pt return pytorch torch tensor objects np return numpy np ndarray objects jax return jax jnp ndarray objects returns batchencoding a batchencoding with the following fields input_ids list of token ids to be fed to a model returned when text is not none attention_mask list of indices specifying which tokens should be attended to by the model when return_attention_mask true or if attention_mask is in self model_input_names and if text is not none pixel_values pixel values to be fed to a model returned when images is not none this method forwards all its arguments to cliptokenizerfast s pretrainedtokenizer batch_decode please refer to the docstring of this method for more information this method forwards all its arguments to cliptokenizerfast s pretrainedtokenizer decode please refer to the docstring of this method for more information
import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class CLIPSegProcessor(ProcessorMixin):
    r

    attributes = ["image_processor", "tokenizer"]
    image_processor_class = "ViTImageProcessor"
    tokenizer_class = ("CLIPTokenizer", "CLIPTokenizerFast")

    def __init__(self, image_processor=None, tokenizer=None, **kwargs):
        feature_extractor = None
        if "feature_extractor" in kwargs:
            warnings.warn(
                "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                " instead.",
                FutureWarning,
            )
            feature_extractor = kwargs.pop("feature_extractor")

        image_processor = image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
            raise ValueError("You need to specify an `image_processor`.")
        if tokenizer is None:
            raise ValueError("You need to specify a `tokenizer`.")

        super().__init__(image_processor, tokenizer)

    def __call__(self, text=None, images=None, visual_prompt=None, return_tensors=None, **kwargs):
        
        if text is None and visual_prompt is None and images is None:
            raise ValueError("You have to specify either text, visual prompt or images.")

        if text is not None and visual_prompt is not None:
            raise ValueError("You have to specify exactly one type of prompt. Either text or visual prompt.")

        if text is not None:
            encoding = self.tokenizer(text, return_tensors=return_tensors, **kwargs)

        if visual_prompt is not None:
            prompt_features = self.image_processor(visual_prompt, return_tensors=return_tensors, **kwargs)

        if images is not None:
            image_features = self.image_processor(images, return_tensors=return_tensors, **kwargs)

        if visual_prompt is not None and images is not None:
            encoding = {
                "pixel_values": image_features.pixel_values,
                "conditional_pixel_values": prompt_features.pixel_values,
            }
            return encoding
        elif text is not None and images is not None:
            encoding["pixel_values"] = image_features.pixel_values
            return encoding
        elif text is not None:
            return encoding
        elif visual_prompt is not None:
            encoding = {
                "conditional_pixel_values": prompt_features.pixel_values,
            }
            return encoding
        else:
            return BatchEncoding(data=dict(**image_features), tensor_type=return_tensors)

    def batch_decode(self, *args, **kwargs):
        
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        
        return self.tokenizer.decode(*args, **kwargs)

    @property
    def feature_extractor_class(self):
        warnings.warn(
            "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.",
            FutureWarning,
        )
        return self.image_processor_class

    @property
    def feature_extractor(self):
        warnings.warn(
            "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.",
            FutureWarning,
        )
        return self.image_processor