unitorch.cli.models.chinese_clip
ChineseClipProcessor
Tip
core/process/chinese_clip is the section for configuration of ChineseClipProcessor.
Bases: ChineseClipProcessor
Processor for the Chinese CLIP model.
Source code in src/unitorch/cli/models/chinese_clip/processing.py
25
26
27
28
29
30
31
32
33
34
35
36
37 | 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/chinese_clip/processing.py
39
40
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 | @classmethod
@config_defaults_init("core/process/chinese_clip")
def from_config(cls, config, **kwargs):
config.set_default_section("core/process/chinese_clip")
pretrained_name = config.getoption(
"pretrained_name", "chinese-clip-vit-base-patch16"
)
vocab_path = config.getoption("vocab_path", None)
vocab_path = pop_value(
vocab_path,
nested_dict_value(pretrained_chinese_clip_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_chinese_clip_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/chinese_clip/processing.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 | @register_process("core/process/chinese_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,
token_type_ids=outputs.token_type_ids,
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/chinese_clip/processing.py
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 | @register_process("core/process/chinese_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,
token_type_ids=outputs.token_type_ids,
position_ids=outputs.position_ids,
)
|
_image_classification
_image_classification(image: Union[Image, str])
Source code in src/unitorch/cli/models/chinese_clip/processing.py
108
109
110
111
112
113
114
115
116 | @register_process("core/process/chinese_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)
|
ChineseClipForPretrain
Tip
core/model/pretrain/chinese_clip is the section for configuration of ChineseClipForPretrain.
Bases: ChineseClipForPretrain
Chinese CLIP model for pretraining.
Source code in src/unitorch/cli/models/chinese_clip/modeling.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | 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/chinese_clip/modeling.py
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
75
76
77
78
79 | @classmethod
@config_defaults_init("core/model/pretrain/chinese_clip")
def from_config(cls, config, **kwargs):
config.set_default_section("core/model/pretrain/chinese_clip")
pretrained_name = config.getoption(
"pretrained_name", "chinese-clip-vit-base-patch16"
)
config_path = config.getoption("config_path", None)
config_path = pop_value(
config_path,
nested_dict_value(pretrained_chinese_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_chinese_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,
token_type_ids: Optional[Tensor] = None,
position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/chinese_clip/modeling.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 | @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,
token_type_ids: 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,
token_type_ids=token_type_ids,
position_ids=position_ids,
)
return LossOutputs(loss=outputs)
|
ChineseClipForClassification
Tip
core/model/classification/chinese_clip is the section for configuration of ChineseClipForClassification.
Bases: ChineseClipForClassification
Chinese CLIP model for classification.
Source code in src/unitorch/cli/models/chinese_clip/modeling.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 | 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/chinese_clip/modeling.py
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
152
153
154
155
156 | @classmethod
@config_defaults_init("core/model/classification/chinese_clip")
def from_config(cls, config, **kwargs):
config.set_default_section("core/model/classification/chinese_clip")
pretrained_name = config.getoption(
"pretrained_name", "chinese-clip-vit-base-patch16"
)
config_path = config.getoption("config_path", None)
config_path = pop_value(
config_path,
nested_dict_value(pretrained_chinese_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_chinese_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,
token_type_ids: Optional[Tensor] = None,
position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/chinese_clip/modeling.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 | @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,
token_type_ids: 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,
token_type_ids=token_type_ids,
position_ids=position_ids,
)
return ClassificationOutputs(outputs=outputs)
|
ChineseClipForTextClassification
Tip
core/model/classification/chinese_clip/text is the section for configuration of ChineseClipForTextClassification.
Bases: ChineseClipForTextClassification
Chinese CLIP model for text classification.
Source code in src/unitorch/cli/models/chinese_clip/modeling.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 | 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/chinese_clip/modeling.py
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
225
226
227
228
229
230
231
232
233 | @classmethod
@config_defaults_init("core/model/classification/chinese_clip/text")
def from_config(cls, config, **kwargs):
config.set_default_section("core/model/classification/chinese_clip/text")
pretrained_name = config.getoption(
"pretrained_name", "chinese-clip-vit-base-patch16"
)
config_path = config.getoption("config_path", None)
config_path = pop_value(
config_path,
nested_dict_value(pretrained_chinese_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_chinese_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,
token_type_ids: Optional[Tensor] = None,
position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/chinese_clip/modeling.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 | @autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
self,
input_ids=None,
attention_mask: Optional[torch.Tensor] = None,
token_type_ids: Optional[torch.Tensor] = None,
position_ids: Optional[torch.Tensor] = None,
):
outputs = super().forward(
input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
)
return ClassificationOutputs(outputs=outputs)
|
ChineseClipForImageClassification
Tip
core/model/classification/chinese_clip/image is the section for configuration of ChineseClipForImageClassification.
Bases: ChineseClipForImageClassification
Chinese CLIP model for image classification.
Source code in src/unitorch/cli/models/chinese_clip/modeling.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 | 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/chinese_clip/modeling.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 | @classmethod
@config_defaults_init("core/model/classification/chinese_clip/image")
def from_config(cls, config, **kwargs):
config.set_default_section("core/model/classification/chinese_clip/image")
pretrained_name = config.getoption(
"pretrained_name", "chinese-clip-vit-base-patch16"
)
config_path = config.getoption("config_path", None)
config_path = pop_value(
config_path,
nested_dict_value(pretrained_chinese_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_chinese_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/chinese_clip/modeling.py
310
311
312
313
314
315
316 | @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)
|