Bases: HfImageClassificationProcessor, HfTextClassificationProcessor
Multimodal processor for CLIP models.
Source code in src/unitorch/models/clip/processing.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 | def __init__(
self,
vocab_path: Optional[str] = None,
merge_path: Optional[str] = None,
vision_config_path: Optional[str] = None,
max_seq_length: int = 128,
position_start_id: int = 0,
) -> None:
vision_processor = (
CLIPImageProcessor.from_json_file(vision_config_path)
if vision_config_path is not None
else CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch32")
)
HfImageClassificationProcessor.__init__(self, vision_processor=vision_processor)
tokenizer = (
self._build_tokenizer(vocab_path=vocab_path, merge_path=merge_path)
if vocab_path is not None and merge_path is not None
else CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32")
)
tokenizer.cls_token = tokenizer.bos_token
tokenizer.sep_token = tokenizer.eos_token
HfTextClassificationProcessor.__init__(
self,
tokenizer=tokenizer,
max_seq_length=max_seq_length,
source_type_id=0,
target_type_id=0,
position_start_id=position_start_id,
)
|
_build_tokenizer
staticmethod
_build_tokenizer(
vocab_path: str, merge_path: str
) -> CLIPTokenizer
Source code in src/unitorch/models/clip/processing.py
| @staticmethod
def _build_tokenizer(vocab_path: str, merge_path: str) -> CLIPTokenizer:
init_params = inspect.signature(CLIPTokenizer.__init__).parameters
if "vocab_file" in init_params and "merges_file" in init_params:
return CLIPTokenizer(vocab_file=vocab_path, merges_file=merge_path)
return CLIPTokenizer(vocab=vocab_path, merges=merge_path)
|
text_classification
text_classification(
text: str, max_seq_length: Optional[int] = None
) -> GenericOutputs
Tokenise text for text classification.
Source code in src/unitorch/models/clip/processing.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71 | def text_classification(
self,
text: str,
max_seq_length: Optional[int] = None,
) -> GenericOutputs:
"""Tokenise *text* for text classification."""
outputs = HfTextClassificationProcessor.classification(
self, text=text, max_seq_length=max_seq_length
)
return GenericOutputs(
input_ids=outputs.input_ids,
attention_mask=outputs.attention_mask,
position_ids=outputs.position_ids,
)
|
image_classification
image_classification(
image: Union[Image, str],
) -> GenericOutputs
Preprocess image for image classification.
Source code in src/unitorch/models/clip/processing.py
| def image_classification(self, image: Union[Image.Image, str]) -> GenericOutputs:
"""Preprocess *image* for image classification."""
return GenericOutputs(
pixel_values=HfImageClassificationProcessor.classification(
self, image=image
).pixel_values,
)
|
classification
classification(
text: str,
image: Union[Image, str],
max_seq_length: Optional[int] = None,
) -> GenericOutputs
Preprocess a text-image pair for multimodal classification.
Source code in src/unitorch/models/clip/processing.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 | def classification(
self,
text: str,
image: Union[Image.Image, str],
max_seq_length: Optional[int] = None,
) -> GenericOutputs:
"""Preprocess a text-image pair for multimodal classification."""
text_out = self.text_classification(text=text, max_seq_length=max_seq_length)
pixel_out = self.image_classification(image=image)
return GenericOutputs(
input_ids=text_out.input_ids,
attention_mask=text_out.attention_mask,
position_ids=text_out.position_ids,
pixel_values=pixel_out.pixel_values,
)
|