Skip to content

unitorch.cli.models.clip¤

ClipProcessor¤

Tip

core/process/clip is the section for configuration of ClipProcessor.

Bases: ClipProcessor

Processor for the CLIP model.

Source code in src/unitorch/cli/models/clip/processing.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def __init__(
    self,
    vocab_path: str,
    merge_path: str,
    vision_config_path: str,
    max_seq_length: Optional[int] = 128,
    position_start_id: Optional[int] = 0,
):
    super().__init__(
        vocab_path=vocab_path,
        merge_path=merge_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/clip/processing.py
41
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
@classmethod
@config_defaults_init("core/process/clip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/process/clip")
    pretrained_name = config.getoption("pretrained_name", "clip-vit-base-patch16")
    vocab_path = config.getoption("vocab_path", None)
    vocab_path = pop_value(
        vocab_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "vocab"),
    )
    vocab_path = cached_path(vocab_path)

    merge_path = config.getoption("merge_path", None)
    merge_path = pop_value(
        merge_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "merge"),
    )
    merge_path = cached_path(merge_path)

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

    vision_config_path = cached_path(vision_config_path)

    return {
        "vocab_path": vocab_path,
        "merge_path": merge_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/clip/processing.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@register_process("core/process/clip/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/clip/processing.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
@register_process("core/process/clip/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/clip/processing.py
112
113
114
115
116
117
118
119
120
@register_process("core/process/clip/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)

ClipForPretrain¤

Tip

core/model/pretrain/clip is the section for configuration of ClipForPretrain.

Bases: ClipForPretrain

CLIP model for pretraining.

Source code in src/unitorch/cli/models/clip/modeling.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(
    self,
    config_path: str,
    projection_dim: Optional[int] = 512,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
    use_all_gather: Optional[bool] = True,
):
    super().__init__(
        config_path=config_path,
        projection_dim=projection_dim,
        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/clip/modeling.py
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
75
76
77
78
@classmethod
@config_defaults_init("core/model/pretrain/clip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/pretrain/clip")
    pretrained_name = config.getoption("pretrained_name", "clip-vit-base-patch16")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    projection_dim = config.getoption("projection_dim", 512)
    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,
        projection_dim=projection_dim,
        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_clip_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/clip/modeling.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@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)

ClipForClassification¤

Tip

core/model/classification/clip is the section for configuration of ClipForClassification.

Bases: ClipForClassification

CLIP model for classification.

Source code in src/unitorch/cli/models/clip/modeling.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def __init__(
    self,
    config_path: str,
    projection_dim: Optional[int] = 512,
    num_classes: Optional[int] = 1,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        projection_dim=projection_dim,
        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/clip/modeling.py
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
144
145
146
147
148
149
150
151
@classmethod
@config_defaults_init("core/model/classification/clip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/clip")
    pretrained_name = config.getoption("pretrained_name", "clip-vit-base-patch16")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    projection_dim = config.getoption("projection_dim", 512)
    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,
        projection_dim=projection_dim,
        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_clip_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/clip/modeling.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
@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)

ClipForTextClassification¤

Tip

core/model/classification/clip/text is the section for configuration of ClipForTextClassification.

Bases: ClipForTextClassification

CLIP model for text classification.

Source code in src/unitorch/cli/models/clip/modeling.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def __init__(
    self,
    config_path: str,
    projection_dim: Optional[int] = 512,
    num_classes: Optional[int] = 1,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        projection_dim=projection_dim,
        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/clip/modeling.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
@classmethod
@config_defaults_init("core/model/classification/clip/text")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/clip/text")
    pretrained_name = config.getoption("pretrained_name", "clip-vit-base-patch16")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    projection_dim = config.getoption("projection_dim", 512)
    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,
        projection_dim=projection_dim,
        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_clip_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/clip/modeling.py
226
227
228
229
230
231
232
233
234
235
236
237
238
@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)

ClipForImageClassification¤

Tip

core/model/classification/clip/image is the section for configuration of ClipForImageClassification.

Bases: ClipForImageClassification

CLIP model for image classification.

Source code in src/unitorch/cli/models/clip/modeling.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
def __init__(
    self,
    config_path: str,
    projection_dim: Optional[int] = 512,
    num_classes: Optional[int] = 1,
    freeze_base_model: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        projection_dim=projection_dim,
        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/clip/modeling.py
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
@classmethod
@config_defaults_init("core/model/classification/clip/image")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/clip/image")
    pretrained_name = config.getoption("pretrained_name", "clip-vit-base-patch16")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)

    projection_dim = config.getoption("projection_dim", 512)
    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,
        projection_dim=projection_dim,
        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_clip_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/clip/modeling.py
297
298
299
300
301
302
303
@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)