Skip to content

unitorch.cli.models.blip¤

BlipProcessor¤

Tip

core/process/blip is the section for configuration of BlipProcessor.

Bases: BlipProcessor

Processor for the BLIP model.

Initialize BlipProcessor.

Parameters:

Name Type Description Default
vocab_path str

The path to the vocabulary file.

required
vision_config_path str

The path to the vision configuration file.

required
max_seq_length int

The maximum sequence length. Defaults to 128.

128
max_gen_seq_length int

The maximum generated sequence length. Defaults to 48.

48
position_start_id int

The start position ID. Defaults to 0.

0
Source code in src/unitorch/cli/models/blip/processing.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def __init__(
    self,
    vocab_path: str,
    vision_config_path: str,
    max_seq_length: Optional[int] = 128,
    max_gen_seq_length: Optional[int] = 48,
    position_start_id: Optional[int] = 0,
):
    """
    Initialize BlipProcessor.

    Args:
        vocab_path (str): The path to the vocabulary file.
        vision_config_path (str): The path to the vision configuration file.
        max_seq_length (int, optional): The maximum sequence length. Defaults to 128.
        max_gen_seq_length (int, optional): The maximum generated sequence length. Defaults to 48.
        position_start_id (int, optional): The start position ID. Defaults to 0.
    """
    super().__init__(
        vocab_path=vocab_path,
        vision_config_path=vision_config_path,
        max_seq_length=max_seq_length,
        max_gen_seq_length=max_gen_seq_length,
        position_start_id=position_start_id,
    )

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BlipProcessor from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
dict

A dictionary containing the processor parameters.

Source code in src/unitorch/cli/models/blip/processing.py
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
80
81
82
83
84
85
86
87
@classmethod
@add_default_section_for_init("core/process/blip")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BlipProcessor from a core configuration.

    Args:
        config: The core configuration.
        **kwargs: Additional keyword arguments.

    Returns:
        dict: A dictionary containing the processor parameters.
    """
    config.set_default_section("core/process/blip")
    pretrained_name = config.getoption(
        "pretrained_name", "blip-image-captioning-base"
    )

    vocab_path = config.getoption("vocab_path", None)
    vocab_path = pop_value(
        vocab_path,
        nested_dict_value(pretrained_blip_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_blip_infos, pretrained_name, "vision_config"),
    )
    vision_config_path = cached_path(vision_config_path)

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

BlipForPretrain¤

Tip

core/model/pretrain/blip is the section for configuration of BlipForPretrain.

Bases: BlipForPretrain

BLIP model for pretraining.

Initialize BlipForPretrain.

Parameters:

Name Type Description Default
config_path str

The path to the model configuration file.

required
projection_dim int

The dimension of the projection head. Defaults to 512.

512
freeze_base_model bool

Whether to freeze the base model parameters. Defaults to True.

True
gradient_checkpointing bool

Whether to use gradient checkpointing. Defaults to False.

False
use_all_gather bool

Whether to use all_gather operation. Defaults to True.

True
Source code in src/unitorch/cli/models/blip/modeling.py
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
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,
):
    """
    Initialize BlipForPretrain.

    Args:
        config_path (str): The path to the model configuration file.
        projection_dim (int, optional): The dimension of the projection head. Defaults to 512.
        freeze_base_model (bool, optional): Whether to freeze the base model parameters. Defaults to True.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing. Defaults to False.
        use_all_gather (bool, optional): Whether to use all_gather operation. Defaults to 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,
    )

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Tensor = None,
    position_ids: Tensor = None,
)

Forward pass of the BlipForPretrain model.

Parameters:

Name Type Description Default
input_ids Tensor

The input token IDs.

required
pixel_values Tensor

The pixel values of the images.

required
attention_mask Tensor

The attention mask. Defaults to None.

None
position_ids Tensor

The position IDs. Defaults to None.

None

Returns:

Name Type Description
LossOutputs

The loss outputs.

Source code in src/unitorch/cli/models/blip/modeling.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: torch.Tensor = None,
    position_ids: torch.Tensor = None,
):
    """
    Forward pass of the BlipForPretrain model.

    Args:
        input_ids (torch.Tensor): The input token IDs.
        pixel_values (torch.Tensor): The pixel values of the images.
        attention_mask (torch.Tensor, optional): The attention mask. Defaults to None.
        position_ids (torch.Tensor, optional): The position IDs. Defaults to None.

    Returns:
        LossOutputs: The loss outputs.
    """
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return LossOutputs(loss=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BlipForPretrain from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
BlipForPretrain

An instance of BlipForPretrain.

Source code in src/unitorch/cli/models/blip/modeling.py
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@classmethod
@add_default_section_for_init("core/model/pretrain/blip")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BlipForPretrain from a core configuration.

    Args:
        config: The core configuration.
        **kwargs: Additional keyword arguments.

    Returns:
        BlipForPretrain: An instance of BlipForPretrain.
    """
    config.set_default_section("core/model/pretrain/blip")
    pretrained_name = config.getoption(
        "pretrained_name", "blip-image-captioning-base"
    )

    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_blip_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_blip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

BlipForClassification¤

Tip

core/model/classification/blip is the section for configuration of BlipForClassification.

Bases: BlipForClassification

BLIP model for classification.

Initialize BlipForClassification.

Parameters:

Name Type Description Default
config_path str

The path to the model configuration file.

required
projection_dim int

The dimension of the projection head. Defaults to 512.

512
num_classes int

The number of classes for classification. Defaults to 1.

1
freeze_base_model bool

Whether to freeze the base model parameters. Defaults to True.

True
gradient_checkpointing bool

Whether to use gradient checkpointing. Defaults to False.

False
Source code in src/unitorch/cli/models/blip/modeling.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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,
):
    """
    Initialize BlipForClassification.

    Args:
        config_path (str): The path to the model configuration file.
        projection_dim (int, optional): The dimension of the projection head. Defaults to 512.
        num_classes (int, optional): The number of classes for classification. Defaults to 1.
        freeze_base_model (bool, optional): Whether to freeze the base model parameters. Defaults to True.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing. Defaults to 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,
    )

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Tensor = None,
    position_ids: Tensor = None,
)

Forward pass of the BlipForClassification model.

Parameters:

Name Type Description Default
input_ids Tensor

The input token IDs.

required
pixel_values Tensor

The pixel values of the images.

required
attention_mask Tensor

The attention mask. Defaults to None.

None
position_ids Tensor

The position IDs. Defaults to None.

None

Returns:

Name Type Description
ClassificationOutputs

The classification outputs.

Source code in src/unitorch/cli/models/blip/modeling.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: torch.Tensor = None,
    position_ids: torch.Tensor = None,
):
    """
    Forward pass of the BlipForClassification model.

    Args:
        input_ids (torch.Tensor): The input token IDs.
        pixel_values (torch.Tensor): The pixel values of the images.
        attention_mask (torch.Tensor, optional): The attention mask. Defaults to None.
        position_ids (torch.Tensor, optional): The position IDs. Defaults to None.

    Returns:
        ClassificationOutputs: The classification outputs.
    """
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BlipForClassification from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
BlipForClassification

An instance of BlipForClassification.

Source code in src/unitorch/cli/models/blip/modeling.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
@classmethod
@add_default_section_for_init("core/model/classification/blip")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BlipForClassification from a core configuration.

    Args:
        config: The core configuration.
        **kwargs: Additional keyword arguments.

    Returns:
        BlipForClassification: An instance of BlipForClassification.
    """
    config.set_default_section("core/model/classification/blip")
    pretrained_name = config.getoption(
        "pretrained_name", "blip-image-captioning-base"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_blip_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_blip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

BlipForTextClassification¤

Tip

core/model/classification/blip/text is the section for configuration of BlipForTextClassification.

Bases: BlipForTextClassification

BLIP model for text classification.

Initialize BlipForTextClassification.

Parameters:

Name Type Description Default
config_path str

The path to the model configuration file.

required
projection_dim int

The dimension of the projection head. Defaults to 512.

512
num_classes int

The number of classes for classification. Defaults to 1.

1
freeze_base_model bool

Whether to freeze the base model parameters. Defaults to True.

True
gradient_checkpointing bool

Whether to use gradient checkpointing. Defaults to False.

False
Source code in src/unitorch/cli/models/blip/modeling.py
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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,
):
    """
    Initialize BlipForTextClassification.

    Args:
        config_path (str): The path to the model configuration file.
        projection_dim (int, optional): The dimension of the projection head. Defaults to 512.
        num_classes (int, optional): The number of classes for classification. Defaults to 1.
        freeze_base_model (bool, optional): Whether to freeze the base model parameters. Defaults to True.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing. Defaults to 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,
    )

forward ¤

forward(
    input_ids=None,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)

Forward pass of the BlipForTextClassification model.

Parameters:

Name Type Description Default
input_ids Tensor

The input token IDs. Defaults to None.

None
attention_mask Tensor

The attention mask. Defaults to None.

None
position_ids Tensor

The position IDs. Defaults to None.

None

Returns:

Name Type Description
ClassificationOutputs

The classification outputs.

Source code in src/unitorch/cli/models/blip/modeling.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
@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,
):
    """
    Forward pass of the BlipForTextClassification model.

    Args:
        input_ids (torch.Tensor, optional): The input token IDs. Defaults to None.
        attention_mask (torch.Tensor, optional): The attention mask. Defaults to None.
        position_ids (torch.Tensor, optional): The position IDs. Defaults to None.

    Returns:
        ClassificationOutputs: The classification outputs.
    """
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BlipForTextClassification from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
BlipForTextClassification

An instance of BlipForTextClassification.

Source code in src/unitorch/cli/models/blip/modeling.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
@classmethod
@add_default_section_for_init("core/model/classification/blip/text")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BlipForTextClassification from a core configuration.

    Args:
        config: The core configuration.
        **kwargs: Additional keyword arguments.

    Returns:
        BlipForTextClassification: An instance of BlipForTextClassification.
    """
    config.set_default_section("core/model/classification/blip/text")
    pretrained_name = config.getoption(
        "pretrained_name", "blip-image-captioning-base"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_blip_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_blip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

BlipForImageClassification¤

Tip

core/model/classification/blip/image is the section for configuration of BlipForImageClassification.

Bases: BlipForImageClassification

BLIP model for image classification.

Initialize BlipForImageClassification.

Parameters:

Name Type Description Default
config_path str

The path to the model configuration file.

required
projection_dim int

The dimension of the projection head. Defaults to 512.

512
num_classes int

The number of classes for classification. Defaults to 1.

1
freeze_base_model bool

Whether to freeze the base model parameters. Defaults to True.

True
gradient_checkpointing bool

Whether to use gradient checkpointing. Defaults to False.

False
Source code in src/unitorch/cli/models/blip/modeling.py
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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,
):
    """
    Initialize BlipForImageClassification.

    Args:
        config_path (str): The path to the model configuration file.
        projection_dim (int, optional): The dimension of the projection head. Defaults to 512.
        num_classes (int, optional): The number of classes for classification. Defaults to 1.
        freeze_base_model (bool, optional): Whether to freeze the base model parameters. Defaults to True.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing. Defaults to 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,
    )

forward ¤

forward(pixel_values: Tensor)

Forward pass of the BlipForImageClassification model.

Parameters:

Name Type Description Default
pixel_values Tensor

The pixel values of the images.

required

Returns:

Name Type Description
ClassificationOutputs

The classification outputs.

Source code in src/unitorch/cli/models/blip/modeling.py
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    pixel_values: torch.Tensor,
):
    """
    Forward pass of the BlipForImageClassification model.

    Args:
        pixel_values (torch.Tensor): The pixel values of the images.

    Returns:
        ClassificationOutputs: The classification outputs.
    """
    outputs = super().forward(pixel_values=pixel_values)
    return ClassificationOutputs(outputs=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BlipForImageClassification from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
BlipForImageClassification

An instance of BlipForImageClassification.

Source code in src/unitorch/cli/models/blip/modeling.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
@classmethod
@add_default_section_for_init("core/model/classification/blip/image")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BlipForImageClassification from a core configuration.

    Args:
        config: The core configuration.
        **kwargs: Additional keyword arguments.

    Returns:
        BlipForImageClassification: An instance of BlipForImageClassification.
    """
    config.set_default_section("core/model/classification/blip/image")
    pretrained_name = config.getoption(
        "pretrained_name", "blip-image-captioning-base"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_blip_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_blip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

BlipForImageCaption¤

Tip

core/model/caption/blip is the section for configuration of BlipForImageCaption.

Bases: BlipForImageCaption

BLIP model for image captioning.

Initialize BlipForImageCaption.

Parameters:

Name Type Description Default
config_path str

The path to the model configuration file.

required
gradient_checkpointing bool

Whether to use gradient checkpointing. Defaults to False.

False
Source code in src/unitorch/cli/models/blip/modeling.py
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
def __init__(
    self,
    config_path: str,
    gradient_checkpointing: Optional[bool] = False,
):
    """
    Initialize BlipForImageCaption.

    Args:
        config_path (str): The path to the model configuration file.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing. Defaults to False.
    """
    super().__init__(
        config_path=config_path,
        gradient_checkpointing=gradient_checkpointing,
    )

forward ¤

forward(
    pixel_values: Tensor,
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
)

Forward pass of the BlipForImageCaption model.

Parameters:

Name Type Description Default
pixel_values Tensor

The pixel values of the images.

required
input_ids Tensor

The input captions.

required

Returns:

Name Type Description
GenerationOutputs

The generation outputs.

Source code in src/unitorch/cli/models/blip/modeling.py
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    pixel_values: torch.Tensor,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
):
    """
    Forward pass of the BlipForImageCaption model.

    Args:
        pixel_values (torch.Tensor): The pixel values of the images.
        input_ids (torch.Tensor): The input captions.

    Returns:
        GenerationOutputs: The generation outputs.
    """
    outputs = super().forward(
        pixel_values=pixel_values,
        input_ids=input_ids,
        attention_mask=attention_mask,
    )
    return GenerationOutputs(sequences=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BlipForImageCaption from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
BlipForImageCaption

An instance of BlipForImageCaption.

Source code in src/unitorch/cli/models/blip/modeling.py
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
@classmethod
@add_default_section_for_init("core/model/caption/blip")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BlipForImageCaption from a core configuration.

    Args:
        config: The core configuration.
        **kwargs: Additional keyword arguments.

    Returns:
        BlipForImageCaption: An instance of BlipForImageCaption.
    """
    config.set_default_section("core/model/caption/blip")
    pretrained_name = config.getoption(
        "pretrained_name", "blip-image-captioning-base"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_blip_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

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

    return inst

generate ¤

generate(
    pixel_values: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 101,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = 102,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 48,
    repetition_penalty: Optional[float] = 1.0,
    no_repeat_ngram_size: Optional[int] = 0,
    early_stopping: Optional[bool] = True,
    length_penalty: Optional[float] = 1.0,
    num_beam_groups: Optional[int] = 1,
    diversity_penalty: Optional[float] = 0.0,
    do_sample: Optional[bool] = False,
    temperature: Optional[float] = 1.0,
    top_k: Optional[int] = 50,
    top_p: Optional[float] = 1.0,
)

Generate captions using the BlipForImageCaption model.

Parameters:

Name Type Description Default
pixel_values Tensor

The pixel values of the images.

required
num_beams int

The number of beams for beam search. Defaults to 5.

5
decoder_start_token_id int

The start token ID for the decoder. Defaults to 30522.

101
decoder_end_token_id int or List[int]

The end token ID for the decoder. Defaults to 2.

102
num_return_sequences int

The number of sequences to return. Defaults to 1.

1
min_gen_seq_length int

The minimum length of generated sequences. Defaults to 0.

0
max_gen_seq_length int

The maximum length of generated sequences. Defaults to 48.

48
repetition_penalty float

The repetition penalty. Defaults to 1.0.

1.0
no_repeat_ngram_size int

The size of n-grams to avoid repeating. Defaults to 0.

0
early_stopping bool

Whether to perform early stopping. Defaults to True.

True
length_penalty float

The length penalty. Defaults to 1.0.

1.0
num_beam_groups int

The number of beam groups. Defaults to 1.

1
diversity_penalty float

The diversity penalty. Defaults to 0.0.

0.0
do_sample bool

Whether to use sampling for generation. Defaults to False.

False
temperature float

The temperature value for sampling. Defaults to 1.0.

1.0
top_k int

The top-k value for sampling. Defaults to 50.

50
top_p float

The top-p value for sampling. Defaults to 1.0.

1.0

Returns:

Name Type Description
GenerationOutputs

The generation outputs.

Source code in src/unitorch/cli/models/blip/modeling.py
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
@torch.no_grad()
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def generate(
    self,
    pixel_values: torch.Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 101,
    decoder_end_token_id: Optional[Union[int, List[int]]] = 102,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 48,
    repetition_penalty: Optional[float] = 1.0,
    no_repeat_ngram_size: Optional[int] = 0,
    early_stopping: Optional[bool] = True,
    length_penalty: Optional[float] = 1.0,
    num_beam_groups: Optional[int] = 1,
    diversity_penalty: Optional[float] = 0.0,
    do_sample: Optional[bool] = False,
    temperature: Optional[float] = 1.0,
    top_k: Optional[int] = 50,
    top_p: Optional[float] = 1.0,
):
    """
    Generate captions using the BlipForImageCaption model.

    Args:
        pixel_values (torch.Tensor): The pixel values of the images.
        num_beams (int, optional): The number of beams for beam search. Defaults to 5.
        decoder_start_token_id (int, optional): The start token ID for the decoder. Defaults to 30522.
        decoder_end_token_id (int or List[int], optional): The end token ID for the decoder. Defaults to 2.
        num_return_sequences (int, optional): The number of sequences to return. Defaults to 1.
        min_gen_seq_length (int, optional): The minimum length of generated sequences. Defaults to 0.
        max_gen_seq_length (int, optional): The maximum length of generated sequences. Defaults to 48.
        repetition_penalty (float, optional): The repetition penalty. Defaults to 1.0.
        no_repeat_ngram_size (int, optional): The size of n-grams to avoid repeating. Defaults to 0.
        early_stopping (bool, optional): Whether to perform early stopping. Defaults to True.
        length_penalty (float, optional): The length penalty. Defaults to 1.0.
        num_beam_groups (int, optional): The number of beam groups. Defaults to 1.
        diversity_penalty (float, optional): The diversity penalty. Defaults to 0.0.
        do_sample (bool, optional): Whether to use sampling for generation. Defaults to False.
        temperature (float, optional): The temperature value for sampling. Defaults to 1.0.
        top_k (int, optional): The top-k value for sampling. Defaults to 50.
        top_p (float, optional): The top-p value for sampling. Defaults to 1.0.

    Returns:
        GenerationOutputs: The generation outputs.
    """
    outputs = super().generate(
        pixel_values=pixel_values,
        num_beams=num_beams,
        decoder_start_token_id=decoder_start_token_id,
        decoder_end_token_id=decoder_end_token_id,
        num_return_sequences=num_return_sequences,
        min_gen_seq_length=min_gen_seq_length,
        max_gen_seq_length=max_gen_seq_length,
        repetition_penalty=repetition_penalty,
        no_repeat_ngram_size=no_repeat_ngram_size,
        early_stopping=early_stopping,
        length_penalty=length_penalty,
        num_beam_groups=num_beam_groups,
        diversity_penalty=diversity_penalty,
        do_sample=do_sample,
        temperature=temperature,
        top_k=top_k,
        top_p=top_p,
    )

    return GenerationOutputs(
        sequences=outputs.sequences,
        sequences_scores=outputs.sequences_scores,
    )