Skip to content

unitorch.cli.models.siglip¤

SiglipProcessor¤

Tip

core/process/siglip is the section for configuration of SiglipProcessor.

Bases: SiglipProcessor

Processor for Siglip models.

Source code in src/unitorch/cli/models/siglip/processing.py
20
21
22
23
24
25
26
27
28
29
30
31
32
def __init__(
    self,
    vocab_path: str,
    vision_config_path: str,
    max_seq_length: Optional[int] = 128,
    position_start_id: Optional[int] = 0,
):
    super().__init__(
        vocab_path=vocab_path,
        vision_config_path=vision_config_path,
        max_seq_length=max_seq_length,
        position_start_id=position_start_id,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/siglip/processing.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@classmethod
@config_defaults_init("core/process/siglip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/process/siglip")
    pretrained_name = config.getoption("pretrained_name", "siglip-base-patch16-224")
    vocab_path = config.getoption("vocab_path", None)
    vocab_path = pop_value(
        vocab_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "vocab"),
    )
    vocab_path = cached_path(vocab_path)

    vision_config_path = config.getoption("vision_config_path", None)
    vision_config_path = pop_value(
        vision_config_path,
        nested_dict_value(
            pretrained_siglip_infos, pretrained_name, "vision_config"
        ),
    )

    vision_config_path = cached_path(vision_config_path)

    return {
        "vocab_path": vocab_path,
        "vision_config_path": vision_config_path,
    }

_classification ¤

_classification(
    text: str,
    image: Union[Image, str],
    max_seq_length: Optional[int] = None,
)
Source code in src/unitorch/cli/models/siglip/processing.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@register_process("core/process/siglip/classification")
def _classification(
    self,
    text: str,
    image: Union[Image.Image, str],
    max_seq_length: Optional[int] = None,
):
    if isinstance(image, str):
        image = Image.open(image)

    outputs = super().classification(
        text=text,
        image=image,
        max_seq_length=max_seq_length,
    )
    return TensorInputs(
        input_ids=outputs.input_ids,
        attention_mask=outputs.attention_mask,
        position_ids=outputs.position_ids,
        pixel_values=outputs.pixel_values,
    )

_text_classification ¤

_text_classification(
    text: str, max_seq_length: Optional[int] = None
)
Source code in src/unitorch/cli/models/siglip/processing.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@register_process("core/process/siglip/text_classification")
def _text_classification(
    self,
    text: str,
    max_seq_length: Optional[int] = None,
):
    outputs = super().text_classification(
        text=text,
        max_seq_length=max_seq_length,
    )
    return TensorInputs(
        input_ids=outputs.input_ids,
        attention_mask=outputs.attention_mask,
        position_ids=outputs.position_ids,
    )

_image_classification ¤

_image_classification(image: Union[Image, str])
Source code in src/unitorch/cli/models/siglip/processing.py
 99
100
101
102
103
104
105
106
107
@register_process("core/process/siglip/image_classification")
def _image_classification(
    self,
    image: Union[Image.Image, str],
):
    if isinstance(image, str):
        image = Image.open(image)
    outputs = super().image_classification(image=image)
    return TensorInputs(pixel_values=outputs.pixel_values)

SiglipForPretrain¤

Tip

core/model/pretrain/siglip is the section for configuration of SiglipForPretrain.

Bases: SiglipForPretrain

Siglip model for pretraining.

Source code in src/unitorch/cli/models/siglip/modeling.py
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(
    self,
    config_path: str,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
    use_all_gather: Optional[bool] = True,
):
    super().__init__(
        config_path=config_path,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
        use_all_gather=use_all_gather,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/siglip/modeling.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@classmethod
@config_defaults_init("core/model/pretrain/siglip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/pretrain/siglip")
    pretrained_name = config.getoption("pretrained_name", "siglip-base-patch16-224")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    freeze_base_model = config.getoption("freeze_base_model", True)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)
    use_all_gather = config.getoption("use_all_gather", True)

    inst = cls(
        config_path=config_path,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
        use_all_gather=use_all_gather,
    )
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/siglip/modeling.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return LossOutputs(loss=outputs)

SiglipForClassification¤

Tip

core/model/classification/siglip is the section for configuration of SiglipForClassification.

Bases: SiglipForClassification

Siglip model for multimodal classification.

Source code in src/unitorch/cli/models/siglip/modeling.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def __init__(
    self,
    config_path: str,
    num_classes: Optional[int] = 1,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        num_classes=num_classes,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/siglip/modeling.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@classmethod
@config_defaults_init("core/model/classification/siglip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/siglip")
    pretrained_name = config.getoption("pretrained_name", "siglip-base-patch16-224")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    num_classes = config.getoption("num_classes", 1)
    freeze_base_model = config.getoption("freeze_base_model", True)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

    inst = cls(
        config_path=config_path,
        num_classes=num_classes,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/siglip/modeling.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

SiglipForTextClassification¤

Tip

core/model/classification/siglip/text is the section for configuration of SiglipForTextClassification.

Bases: SiglipForTextClassification

Siglip model for text classification.

Source code in src/unitorch/cli/models/siglip/modeling.py
166
167
168
169
170
171
172
173
174
175
176
177
178
def __init__(
    self,
    config_path: str,
    num_classes: Optional[int] = 1,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        num_classes=num_classes,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/siglip/modeling.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
@classmethod
@config_defaults_init("core/model/classification/siglip/text")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/siglip/text")
    pretrained_name = config.getoption("pretrained_name", "siglip-base-patch16-224")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    num_classes = config.getoption("num_classes", 1)
    freeze_base_model = config.getoption("freeze_base_Truemodel", True)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

    inst = cls(
        config_path=config_path,
        num_classes=num_classes,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

forward ¤

forward(
    input_ids=None,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/siglip/modeling.py
214
215
216
217
218
219
220
221
222
223
224
225
226
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids=None,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

SiglipForImageClassification¤

Tip

core/model/classification/siglip/image is the section for configuration of SiglipForImageClassification.

Bases: SiglipForImageClassification

Siglip model for image-only classification.

Source code in src/unitorch/cli/models/siglip/modeling.py
233
234
235
236
237
238
239
240
241
242
243
244
245
def __init__(
    self,
    config_path: str,
    num_classes: Optional[int] = 1,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        num_classes=num_classes,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/siglip/modeling.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
@classmethod
@config_defaults_init("core/model/classification/siglip/image")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/siglip/image")
    pretrained_name = config.getoption("pretrained_name", "siglip-base-patch16-224")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    num_classes = config.getoption("num_classes", 1)
    freeze_base_model = config.getoption("freeze_base_model", True)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

    inst = cls(
        config_path=config_path,
        num_classes=num_classes,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

forward ¤

forward(pixel_values: Tensor)
Source code in src/unitorch/cli/models/siglip/modeling.py
281
282
283
284
285
286
287
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    pixel_values: torch.Tensor,
):
    outputs = super().forward(pixel_values=pixel_values)
    return ClassificationOutputs(outputs=outputs)

SiglipForMatching¤

Tip

core/model/matching/siglip is the section for configuration of SiglipForMatching.

Bases: SiglipForMatching

Siglip model for image-text matching.

Source code in src/unitorch/cli/models/siglip/modeling.py
294
295
296
297
298
299
300
301
302
303
304
def __init__(
    self,
    config_path: str,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/siglip/modeling.py
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
@classmethod
@config_defaults_init("core/model/matching/siglip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/matching/siglip")
    pretrained_name = config.getoption("pretrained_name", "siglip-base-patch16-224")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    freeze_base_model = config.getoption("freeze_base_model", True)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

    inst = cls(
        config_path=config_path,
        freeze_base_model=freeze_base_model,
        gradient_checkpointing=gradient_checkpointing,
    )
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_siglip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/siglip/modeling.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)