blip
mindnlp.transformers.models.blip.configuration_blip.BlipConfig
¶
Bases: PretrainedConfig
[BlipConfig
] is the configuration class to store the configuration of a [BlipModel
]. It is used to instantiate
a BLIP model according to the specified arguments, defining the text model and vision model configs. Instantiating
a configuration with the defaults will yield a similar configuration to that of the BLIP-base
Salesforce/blip-vqa-base architecture.
Configuration objects inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig
] for more information.
PARAMETER | DESCRIPTION |
---|---|
text_config |
Dictionary of configuration options used to initialize [
TYPE:
|
vision_config |
Dictionary of configuration options used to initialize [
TYPE:
|
projection_dim |
Dimentionality of text and vision projection layers.
TYPE:
|
logit_scale_init_value |
The inital value of the logit_scale paramter. Default is used as per the original BLIP implementation.
TYPE:
|
image_text_hidden_size |
Dimentionality of the hidden state of the image-text fusion layer.
TYPE:
|
label_smoothing |
A float in [0.0, 1.0]. Specifies the amount of smoothing when computing the loss, where 0.0 means no smoothing. The targets
become a mixture of the original ground truth and a uniform distribution as described in
TYPE:
|
kwargs |
Dictionary of keyword arguments.
TYPE:
|
Example
>>> from transformers import BlipConfig, BlipModel
...
>>> # Initializing a BlipConfig with Salesforce/blip-vqa-base style configuration
>>> configuration = BlipConfig()
...
>>> # Initializing a BlipPModel (with random weights) from the Salesforce/blip-vqa-base style configuration
>>> model = BlipModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
...
>>> # We can also initialize a BlipConfig from a BlipTextConfig and a BlipVisionConfig
...
>>> # Initializing a BLIPText and BLIPVision configuration
>>> config_text = BlipTextConfig()
>>> config_vision = BlipVisionConfig()
...
>>> config = BlipConfig.from_text_vision_configs(config_text, config_vision)
Source code in mindnlp/transformers/models/blip/configuration_blip.py
363 364 365 366 367 368 369 370 371 372 373 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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipConfig.__init__(text_config=None, vision_config=None, projection_dim=512, logit_scale_init_value=2.6592, image_text_hidden_size=256, label_smoothing=0.0, **kwargs)
¶
Initializes a BlipConfig object with the provided parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipConfig class.
TYPE:
|
text_config |
Configuration parameters for text. Defaults to None.
TYPE:
|
vision_config |
Configuration parameters for vision. Defaults to None.
TYPE:
|
projection_dim |
The dimension of the projection. Defaults to 512.
TYPE:
|
logit_scale_init_value |
The initial value for logit scaling. Defaults to 2.6592.
TYPE:
|
image_text_hidden_size |
The size of the hidden layer for image and text. Defaults to 256.
TYPE:
|
label_smoothing |
The amount of label smoothing. Defaults to 0.0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip/configuration_blip.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipConfig.from_text_vision_configs(text_config, vision_config, **kwargs)
classmethod
¶
Instantiate a [BlipConfig
] (or a derived class) from blip text model configuration and blip vision model
configuration.
RETURNS | DESCRIPTION |
---|---|
[ |
Source code in mindnlp/transformers/models/blip/configuration_blip.py
465 466 467 468 469 470 471 472 473 474 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipVisionConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [BlipVisionModel
]. It is used to instantiate a
BLIP vision model according to the specified arguments, defining the model architecture. Instantiating a
configuration defaults will yield a similar configuration to that of the Blip-base
Salesforce/blip-vqa-base architecture.
Configuration objects inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig
] for more information.
PARAMETER | DESCRIPTION |
---|---|
hidden_size |
Dimensionality of the encoder layers and the pooler layer.
TYPE:
|
intermediate_size |
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
TYPE:
|
num_hidden_layers |
Number of hidden layers in the Transformer encoder.
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
image_size |
The size (resolution) of each image.
TYPE:
|
patch_size |
The size (resolution) of each patch.
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the encoder and pooler. If string,
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
attention_dropout |
The dropout ratio for the attention probabilities.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
Example
>>> from transformers import BlipVisionConfig, BlipVisionModel
...
>>> # Initializing a BlipVisionConfig with Salesforce/blip-vqa-base style configuration
>>> configuration = BlipVisionConfig()
...
>>> # Initializing a BlipVisionModel (with random weights) from the Salesforce/blip-vqa-base style configuration
>>> model = BlipVisionModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/blip/configuration_blip.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 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 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 296 297 298 299 300 301 302 303 304 305 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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipVisionConfig.__init__(hidden_size=768, intermediate_size=3072, projection_dim=512, num_hidden_layers=12, num_attention_heads=12, image_size=384, patch_size=16, hidden_act='gelu', layer_norm_eps=1e-05, attention_dropout=0.0, initializer_range=1e-10, **kwargs)
¶
Initializes a BlipVisionConfig instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
hidden_size |
The size of the hidden layers. Defaults to 768.
TYPE:
|
intermediate_size |
The size of the intermediate layers. Defaults to 3072.
TYPE:
|
projection_dim |
The dimension of the projected output. Defaults to 512.
TYPE:
|
num_hidden_layers |
The number of hidden layers. Defaults to 12.
TYPE:
|
num_attention_heads |
The number of attention heads. Defaults to 12.
TYPE:
|
image_size |
The size of the input image. Defaults to 384.
TYPE:
|
patch_size |
The size of the image patch. Defaults to 16.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Defaults to 'gelu'.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization. Defaults to 1e-05.
TYPE:
|
attention_dropout |
The dropout rate for attention layers. Defaults to 0.0.
TYPE:
|
initializer_range |
The range for parameter initialization. Defaults to 1e-10.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip/configuration_blip.py
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipVisionConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
This method 'from_pretrained' in the class 'BlipVisionConfig' is used to create a new instance of the class by loading a pretrained model configuration.
PARAMETER | DESCRIPTION |
---|---|
cls |
The class object itself, automatically passed as the first argument.
TYPE:
|
pretrained_model_name_or_path |
A string representing the name or path of the pretrained model. It can be either a string or a PathLike object. This parameter is used to fetch the configuration dictionary for the pretrained model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
PretrainedConfig
|
An instance of the 'PretrainedConfig' class representing the configuration of the pretrained model. The method returns this configuration for further use.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
None
|
No specific exceptions are documented to be raised by this method based on the provided code snippet. However, potential exceptions might include:
|
Note
It is recommended to handle exceptions that may occur during the execution of this method to ensure proper error handling and flow control.
Source code in mindnlp/transformers/models/blip/configuration_blip.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipTextConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [BlipTextModel
]. It is used to instantiate a BLIP
text model according to the specified arguments, defining the model architecture. Instantiating a configuration
with the defaults will yield a similar configuration to that of the BlipText
used by the base
architectures.
Configuration objects inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig
] for more information.
PARAMETER | DESCRIPTION |
---|---|
vocab_size |
Vocabulary size of the
TYPE:
|
hidden_size |
Dimensionality of the encoder layers and the pooler layer.
TYPE:
|
encoder_hidden_size |
Dimensionality of the encoder layers from the vision model.
TYPE:
|
intermediate_size |
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
TYPE:
|
num_hidden_layers |
Number of hidden layers in the Transformer encoder.
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
max_position_embeddings |
The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the encoder and pooler. If string,
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
hidden_dropout_prob |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
attention_dropout |
The dropout ratio for the attention probabilities.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
bos_token_id |
The id of the
TYPE:
|
eos_token_id |
The id of the
TYPE:
|
pad_token_id |
The id of the
TYPE:
|
sep_token_id |
The id of the
TYPE:
|
is_decoder |
Whether the model is used as a decoder.
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models).
TYPE:
|
label_smoothing |
A float in [0.0, 1.0]. Specifies the amount of smoothing when computing the loss, where 0.0 means no smoothing. The targets
become a mixture of the original ground truth and a uniform distribution as described in
TYPE:
|
Example
>>> from transformers import BlipTextConfig, BlipTextModel
...
>>> # Initializing a BlipTextConfig with Salesforce/blip-vqa-base style configuration
>>> configuration = BlipTextConfig()
...
>>> # Initializing a BlipTextModel (with random weights) from the Salesforce/blip-vqa-base style configuration
>>> model = BlipTextModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/blip/configuration_blip.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 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 103 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 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 157 158 159 160 161 162 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 210 211 212 213 214 215 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipTextConfig.__init__(vocab_size=30524, hidden_size=768, encoder_hidden_size=768, intermediate_size=3072, projection_dim=768, num_hidden_layers=12, num_attention_heads=8, max_position_embeddings=512, hidden_act='gelu', layer_norm_eps=1e-12, hidden_dropout_prob=0.0, attention_probs_dropout_prob=0.0, initializer_range=0.02, bos_token_id=30522, eos_token_id=2, pad_token_id=0, sep_token_id=102, is_decoder=True, use_cache=True, label_smoothing=0.0, **kwargs)
¶
Initializes a BlipTextConfig object with the given parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The BlipTextConfig instance.
|
vocab_size |
The size of the vocabulary. Default is 30524.
TYPE:
|
hidden_size |
The size of the hidden layers. Default is 768.
TYPE:
|
encoder_hidden_size |
The size of the encoder hidden layers. Default is 768.
TYPE:
|
intermediate_size |
The size of the intermediate layers. Default is 3072.
TYPE:
|
projection_dim |
The projection dimension. Default is 768.
TYPE:
|
num_hidden_layers |
The number of hidden layers. Default is 12.
TYPE:
|
num_attention_heads |
The number of attention heads. Default is 8.
TYPE:
|
max_position_embeddings |
The maximum position embeddings. Default is 512.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Default is 'gelu'.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization. Default is 1e-12.
TYPE:
|
hidden_dropout_prob |
The dropout probability for the hidden layers. Default is 0.0.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability for attention probabilities. Default is 0.0.
TYPE:
|
initializer_range |
The range for weight initialization. Default is 0.02.
TYPE:
|
bos_token_id |
The ID of the beginning of sentence token. Default is 30522.
TYPE:
|
eos_token_id |
The ID of the end of sentence token. Default is 2.
TYPE:
|
pad_token_id |
The ID of the padding token. Default is 0.
TYPE:
|
sep_token_id |
The ID of the separator token. Default is 102.
TYPE:
|
is_decoder |
Whether the model is a decoder. Default is True.
TYPE:
|
use_cache |
Whether to use cache for faster decoding. Default is True.
TYPE:
|
label_smoothing |
The label smoothing factor. Default is 0.0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/blip/configuration_blip.py
99 100 101 102 103 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 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
mindnlp.transformers.models.blip.configuration_blip.BlipTextConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
This method 'from_pretrained' in the class 'BlipTextConfig' is used to instantiate a model configuration object based on a pretrained model name or path.
PARAMETER | DESCRIPTION |
---|---|
cls |
The class object itself.
TYPE:
|
pretrained_model_name_or_path |
A string representing the name of a pretrained model or a valid path to a pretrained model configuration file. This parameter is mandatory and required for initializing the configuration object.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
PretrainedConfig
|
An instance of 'PretrainedConfig' class representing the configuration settings of the pretrained model. The method returns the configuration object based on the provided pretrained model name or path.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided 'pretrained_model_name_or_path' is not a string or a valid path-like object. |
KeyError
|
If the 'model_type' key is missing from the configuration dictionary. |
Warning
|
If the model type in the configuration dictionary does not match the class model type, a warning message is logged as this may lead to errors during instantiation. |
Source code in mindnlp/transformers/models/blip/configuration_blip.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 213 214 215 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipModel
¶
Bases: BlipPreTrainedModel
BlipModel
BlipModel is a class that represents a multimodal model for processing both text and images. It inherits from BlipPreTrainedModel and includes methods for obtaining text and image features, as well as for forwarding the model output.
Example
>>> from transformers import AutoProcessor, BlipModel
>>> model = BlipModel.from_pretrained("Salesforce/blip-image-captioning-base")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")
>>> text_features = model.get_text_features(**inputs)
...
>>> from PIL import Image
>>> import requests
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> inputs = processor(images=image, return_tensors="pt")
>>> image_features = model.get_image_features(**inputs)
...
>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
>>> outputs = model(**inputs)
>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score
>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
Source code in mindnlp/transformers/models/blip/modeling_blip.py
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipModel.__init__(config)
¶
Initializes an instance of the BlipModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the BlipModel class.
|
config |
The configuration object for the BlipModel. It should contain the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If config.text_config is not of type BlipTextConfig. |
ValueError
|
If config.vision_config is not of type BlipVisionConfig. |
Note
This method initializes the BlipModel instance by setting the projection dimension, text embedding dimension, vision embedding dimension, text model, vision model, visual projection, text projection, and logit scale attributes based on the provided configuration. It also calls the post_init method.
Source code in mindnlp/transformers/models/blip/modeling_blip.py
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipModel.forward(input_ids=None, pixel_values=None, attention_mask=None, position_ids=None, return_loss=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BlipOutput]
|
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipModel
...
>>> model = BlipModel.from_pretrained("Salesforce/blip-image-captioning-base")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> inputs = processor(
... text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True
... )
...
>>> outputs = model(**inputs)
>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score
>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipModel.get_image_features(pixel_values=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
image_features
|
The image embeddings obtained by
applying the projection layer to the pooled output of [
TYPE:
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipModel
...
>>> model = BlipModel.from_pretrained("Salesforce/blip-image-captioning-base")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> inputs = processor(images=image, return_tensors="pt")
...
>>> image_features = model.get_image_features(**inputs)
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipModel.get_text_features(input_ids=None, attention_mask=None, position_ids=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
text_features
|
The text embeddings obtained by
TYPE:
|
Tensor
|
applying the projection layer to the pooled output of [ |
Example
>>> from transformers import AutoProcessor, BlipModel
...
>>> model = BlipModel.from_pretrained("Salesforce/blip-image-captioning-base")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
...
>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")
>>> text_features = model.get_text_features(**inputs)
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipPreTrainedModel
¶
Bases: PreTrainedModel
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
Source code in mindnlp/transformers/models/blip/modeling_blip.py
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForConditionalGeneration
¶
Bases: BlipPreTrainedModel
A class representing the BlipForConditionalGeneration model for image captioning.
This class extends the BlipPreTrainedModel class and provides methods for initializing the model, generating image captions, and forwarding the model's architecture.
ATTRIBUTE | DESCRIPTION |
---|---|
vision_model |
The vision model used for extracting image features.
TYPE:
|
text_decoder |
The text decoder model used for generating captions.
TYPE:
|
decoder_input_ids |
The token ID to start the decoder input sequence.
TYPE:
|
decoder_pad_token_id |
The token ID used for padding the decoder input sequence.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the BlipForConditionalGeneration model. |
get_input_embeddings |
Returns the input embeddings of the vision model. |
forward |
Constructs the model architecture and generates image captions. |
generate |
Generates image captions based on the input image. |
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipForConditionalGeneration
...
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
>>> model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> text = "A picture of"
...
>>> inputs = processor(images=image, text=text, return_tensors="pt")
...
>>> outputs = model(**inputs)
Overrides
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForConditionalGeneration.__init__(config)
¶
Initializes an instance of the BlipForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipForConditionalGeneration class. |
config |
An object representing the configuration settings for the Blip model. It contains the necessary configurations for the vision model and text decoder. It is expected that the config parameter is of type BlipConfig.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type BlipConfig. |
ValueError
|
If the config parameter is missing required configuration settings. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForConditionalGeneration.forward(pixel_values, input_ids=None, attention_mask=None, output_attentions=None, output_hidden_states=None, labels=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BlipForConditionalGenerationModelOutput]
|
Union[Tuple, BlipForConditionalGenerationModelOutput] |
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipForConditionalGeneration
...
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
>>> model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> text = "A picture of"
...
>>> inputs = processor(images=image, text=text, return_tensors="pt")
...
>>> outputs = model(**inputs)
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForConditionalGeneration.generate(pixel_values, input_ids=None, attention_mask=None, **generate_kwargs)
¶
Overrides generate function to be able to use the model as a conditional generator
PARAMETER | DESCRIPTION |
---|---|
pixel_values |
Input image to be processed
TYPE:
|
input_ids |
The sequence used as a prompt for the generation.
TYPE:
|
attention_mask |
Mask to avoid performing attention on padding token indices. Mask values selected in
TYPE:
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipForConditionalGeneration
...
>>> model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> inputs = processor(images=image, return_tensors="pt")
...
>>> outputs = model.generate(**inputs)
>>> print(processor.decode(outputs[0], skip_special_tokens=True))
two cats sleeping on a couch
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForConditionalGeneration.get_input_embeddings()
¶
This method returns the input embeddings for the BlipForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipForConditionalGeneration class. |
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: The input embeddings for the BlipForConditionalGeneration class. This is an instance of the nn.Module class. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForQuestionAnswering
¶
Bases: BlipPreTrainedModel
BlipForQuestionAnswering is a class that represents a model for question answering using both text and vision inputs. It is designed to be used with the BlipPreTrainedModel base class.
This class has the following attributes:
- vision_model: An instance of the BlipVisionModel class that handles the vision inputs.
- text_encoder: An instance of the BlipTextModel class that encodes the text inputs.
- text_decoder: An instance of the BlipTextLMHeadModel class that decodes the text inputs.
- decoder_pad_token_id: The ID of the padding token used in the decoder.
- decoder_start_token_id: The ID of the start token used in the decoder.
The BlipForQuestionAnswering class provides the following methods:
- init: Initializes the BlipForQuestionAnswering instance with the given configuration.
- get_input_embeddings: Returns the input embeddings of the vision model.
- forward: Constructs the model and performs the forward pass. Returns the model outputs.
- generate: Generates text outputs based on the given input IDs and pixel values.
Please refer to the code examples in the docstring for more information on how to use the BlipForQuestionAnswering class for training and inference.
Note
This documentation is auto-generated and may not capture all the intricacies of the class implementation. For more details, please refer to the source code.
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForQuestionAnswering.__init__(config)
¶
Initializes an instance of BlipForQuestionAnswering.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An instance of BlipConfig containing the configuration for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForQuestionAnswering.forward(input_ids, pixel_values, decoder_input_ids=None, decoder_attention_mask=None, attention_mask=None, output_attentions=None, output_hidden_states=None, labels=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BlipTextVisionModelOutput]
|
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipForQuestionAnswering
...
>>> model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> # training
>>> text = "How many cats are in the picture?"
>>> label = "2"
>>> inputs = processor(images=image, text=text, return_tensors="pt")
>>> labels = processor(text=label, return_tensors="pt").input_ids
...
>>> inputs["labels"] = labels
>>> outputs = model(**inputs)
>>> loss = outputs.loss
>>> loss.backward()
...
>>> # inference
>>> text = "How many cats are in the picture?"
>>> inputs = processor(images=image, text=text, return_tensors="pt")
>>> outputs = model.generate(**inputs)
>>> print(processor.decode(outputs[0], skip_special_tokens=True))
2
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForQuestionAnswering.generate(input_ids, pixel_values, attention_mask=None, **generate_kwargs)
¶
Overrides generate function to be able to use the model as a conditional generator
PARAMETER | DESCRIPTION |
---|---|
input_ids |
The sequence used as a prompt for the generation.
TYPE:
|
pixel_values |
Input image to be processed
TYPE:
|
attention_mask |
Mask to avoid performing attention on padding token indices. Mask values selected in
TYPE:
|
**generate_kwargs |
Additional arguments passed to the generate function of the decoder
DEFAULT:
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipForQuestionAnswering
...
>>> model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> text = "How many cats are in the picture?"
...
>>> inputs = processor(images=image, text=text, return_tensors="pt")
...
>>> outputs = model.generate(**inputs)
>>> print(processor.decode(outputs[0], skip_special_tokens=True))
2
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForQuestionAnswering.get_input_embeddings()
¶
This method returns the input embeddings from the vision model for question answering.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipForQuestionAnswering class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: The input embeddings from the vision model, which is of type nn.Module. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipVisionModel
¶
Bases: BlipPreTrainedModel
A class representing the BlipVisionModel for vision tasks.
This class inherits from the BlipPreTrainedModel and provides methods to forward the model, get input embeddings, and return the outputs.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration for the BlipVisionModel.
TYPE:
|
embeddings |
The embeddings layer for the BlipVisionModel.
TYPE:
|
encoder |
The encoder layer for the BlipVisionModel.
TYPE:
|
post_layernorm |
The post layer normalization layer for the BlipVisionModel.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the BlipVisionModel with the given configuration. |
forward |
Constructs the BlipVisionModel and returns the model outputs. |
get_input_embeddings |
Returns the input embeddings for the BlipVisionModel. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipVisionModel.__init__(config)
¶
Initializes a new instance of the BlipVisionModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
The configuration object that holds all the necessary parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipVisionModel.forward(pixel_values=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPooling]
|
Union[Tuple, BaseModelOutputWithPooling] |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipVisionModel.get_input_embeddings()
¶
Returns the input embeddings from the BlipVisionModel.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the BlipVisionModel class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
951 952 953 954 955 956 957 958 959 960 961 962 963 964 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipTextModel
¶
Bases: BlipTextPreTrainedModel
The model can behave as an encoder (with only self-attention) as well as a decoder, in which case a layer of
cross-attention is added between the self-attention layers, following the architecture described in Attention is
all you need by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit,
Llion Jones, Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin. argument and is_decoder
set to True
; an
encoder_hidden_states
is then expected as an input to the forward pass.
Source code in mindnlp/transformers/models/blip/modeling_blip_text.py
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipTextModel.__init__(config, add_pooling_layer=True)
¶
Initializes a BlipTextModel object.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance itself.
|
config |
A dictionary containing configuration parameters for the BlipTextModel.
TYPE:
|
add_pooling_layer |
A flag indicating whether to add a pooling layer to the model. Default is True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not provided or not of type dict. |
ValueError
|
If the config dictionary is missing required keys or has invalid values. |
RuntimeError
|
If an issue occurs during the initialization process. |
Source code in mindnlp/transformers/models/blip/modeling_blip_text.py
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipTextModel.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, past_key_values=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None, is_decoder=False)
¶
PARAMETER | DESCRIPTION |
---|---|
encoder_hidden_states |
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if the model is configured as a decoder.
TYPE:
|
encoder_attention_mask |
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
the cross-attention if the model is configured as a decoder. Mask values selected in
TYPE:
|
past_key_values |
Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.
If
TYPE:
|
use_cache |
If set to
TYPE:
|
Source code in mindnlp/transformers/models/blip/modeling_blip_text.py
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipTextModel.get_extended_attention_mask(attention_mask, input_shape, is_decoder)
¶
Makes broadcastable attention and causal masks so that future and masked tokens are ignored.
PARAMETER | DESCRIPTION |
---|---|
attention_mask |
Mask with ones indicating tokens to attend to, zeros for tokens to ignore.
TYPE:
|
input_shape |
The shape of the input to the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
|
Source code in mindnlp/transformers/models/blip/modeling_blip_text.py
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipTextModel.get_input_embeddings()
¶
This method returns the input embeddings from the BlipTextModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipTextModel class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns the input embeddings from the BlipTextModel. The input embeddings are retrieved from the word_embeddings attribute of the embeddings. |
Source code in mindnlp/transformers/models/blip/modeling_blip_text.py
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipTextModel.set_input_embeddings(value)
¶
Sets the input embeddings for the BlipTextModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipTextModel class.
TYPE:
|
value |
The input embeddings to be set for the BlipTextModel. It should be a tensor of shape (vocab_size, embedding_dim).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip/modeling_blip_text.py
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForImageTextRetrieval
¶
Bases: BlipPreTrainedModel
BlipForImageTextRetrieval is a class that implements a model for image-text retrieval tasks. It is designed to retrieve relevant text based on input images and vice versa. This class inherits from BlipPreTrainedModel.
The class's forwardor initializes the model with the provided configuration. It sets up the vision model, text encoder, projection layers, and other necessary components for image-text retrieval.
The 'get_input_embeddings' method returns the patch embeddings from the vision model.
The 'forward' method takes input image and text tensors and forwards the output based on the specified parameters. It utilizes the vision model to extract image features and the text encoder to process input text. Depending on the 'use_itm_head' parameter, the method either computes the similarity score between image and text features or uses the image and text projections for matching.
The method also handles optional parameters for controlling the output format and behavior. It provides examples on how to use the BlipForImageTextRetrieval class for image-text retrieval tasks.
Note
This docstring is a high-level overview and does not include method signatures or detailed implementation details.
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForImageTextRetrieval.__init__(config)
¶
Initializes an instance of the BlipForImageTextRetrieval class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class itself.
|
config |
The configuration object containing various settings for the BlipForImageTextRetrieval model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForImageTextRetrieval.forward(input_ids, pixel_values, use_itm_head=True, attention_mask=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BlipTextVisionModelOutput]
|
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, BlipForImageTextRetrieval
...
>>> model = BlipForImageTextRetrieval.from_pretrained("Salesforce/blip-itm-base-coco")
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-itm-base-coco")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> text = "an image of a cat"
...
>>> inputs = processor(images=image, text=text, return_tensors="pt")
>>> outputs = model(**inputs)
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 |
|
mindnlp.transformers.models.blip.modeling_blip.BlipForImageTextRetrieval.get_input_embeddings()
¶
Method to get the input embeddings from the vision model for image-text retrieval.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipForImageTextRetrieval class. This parameter is required to access the vision model and its embeddings. |
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: A neural network cell representing the input embeddings obtained from the vision model. These embeddings are used for matching image features with text features in the retrieval process. |
Source code in mindnlp/transformers/models/blip/modeling_blip.py
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 |
|
mindnlp.transformers.models.blip.image_processing_blip.BlipImageProcessor
¶
Bases: BaseImageProcessor
Constructs a BLIP image processor.
PARAMETER | DESCRIPTION |
---|---|
do_resize |
Whether to resize the image's (height, width) dimensions to the specified
TYPE:
|
size |
384, "width": 384}
TYPE:
|
resample |
Resampling filter to use if resizing the image. Only has an effect if
TYPE:
|
do_rescale |
Whether to rescale the image by the specified scale
TYPE:
|
rescale_factor |
Scale factor to use if rescaling the image. Only has an effect if
TYPE:
|
do_normalize |
Whether to normalize the image. Can be overridden by the
TYPE:
|
image_mean |
Mean to use if normalizing the image. This is a float or list of floats the length of the number of
channels in the image. Can be overridden by the
TYPE:
|
image_std |
Standard deviation to use if normalizing the image. This is a float or list of floats the length of the
number of channels in the image. Can be overridden by the
TYPE:
|
do_convert_rgb |
Whether to convert the image to RGB.
TYPE:
|
Source code in mindnlp/transformers/models/blip/image_processing_blip.py
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 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 157 158 159 160 161 162 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 210 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 238 239 240 241 242 243 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 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 296 297 298 299 300 301 302 303 304 305 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 337 338 339 340 341 342 |
|
mindnlp.transformers.models.blip.image_processing_blip.BlipImageProcessor.__init__(do_resize=True, size=None, resample=PILImageResampling.BICUBIC, do_rescale=True, rescale_factor=1 / 255, do_normalize=True, image_mean=None, image_std=None, do_convert_rgb=True, **kwargs)
¶
Initializes a BlipImageProcessor object.
PARAMETER | DESCRIPTION |
---|---|
self |
The BlipImageProcessor instance.
|
do_resize |
Specifies whether to resize the image. Defaults to True.
TYPE:
|
size |
Specifies the desired height and width of the image. Defaults to {'height': 384, 'width': 384}.
TYPE:
|
resample |
Specifies the resampling method for resizing the image. Defaults to PILImageResampling.BICUBIC.
TYPE:
|
do_rescale |
Specifies whether to rescale the image. Defaults to True.
TYPE:
|
rescale_factor |
Specifies the rescale factor for the image. Defaults to 1 / 255.
TYPE:
|
do_normalize |
Specifies whether to normalize the image. Defaults to True.
TYPE:
|
image_mean |
Specifies the mean value for image normalization. Defaults to None.
TYPE:
|
image_std |
Specifies the standard deviation value for image normalization. Defaults to None.
TYPE:
|
do_convert_rgb |
Specifies whether to convert the image to RGB format. Defaults to True.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/blip/image_processing_blip.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
mindnlp.transformers.models.blip.image_processing_blip.BlipImageProcessor.preprocess(images, do_resize=None, size=None, resample=None, do_rescale=None, rescale_factor=None, do_normalize=None, image_mean=None, image_std=None, return_tensors=None, do_convert_rgb=None, data_format=ChannelDimension.FIRST, input_data_format=None, **kwargs)
¶
Preprocess an image or batch of images.
PARAMETER | DESCRIPTION |
---|---|
images |
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set
TYPE:
|
do_resize |
Whether to resize the image.
TYPE:
|
size |
Controls the size of the image after
TYPE:
|
resample |
Resampling filter to use if resizing the image. Only has an effect if
TYPE:
|
do_rescale |
Whether to rescale the image values between [0 - 1].
TYPE:
|
rescale_factor |
Rescale factor to rescale the image by if
TYPE:
|
do_normalize |
Whether to normalize the image.
TYPE:
|
image_mean |
Image mean to normalize the image by if
TYPE:
|
image_std |
Image standard deviation to normalize the image by if
TYPE:
|
do_convert_rgb |
Whether to convert the image to RGB.
TYPE:
|
return_tensors |
The type of tensors to return. Can be one of:
TYPE:
|
data_format |
The channel dimension format for the output image. Can be one of:
TYPE:
|
input_data_format |
The channel dimension format for the input image. If unset, the channel dimension format is inferred from the input image. Can be one of:
TYPE:
|
Source code in mindnlp/transformers/models/blip/image_processing_blip.py
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 234 235 236 237 238 239 240 241 242 243 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 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 296 297 298 299 300 301 302 303 304 305 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 337 338 339 340 341 342 |
|
mindnlp.transformers.models.blip.image_processing_blip.BlipImageProcessor.resize(image, size, resample=PILImageResampling.BICUBIC, data_format=None, input_data_format=None, **kwargs)
¶
Resize an image to (size["height"], size["width"])
.
PARAMETER | DESCRIPTION |
---|---|
image |
Image to resize.
TYPE:
|
size |
Dictionary in the format
TYPE:
|
resample |
TYPE:
|
data_format |
The channel dimension format for the output image. If unset, the channel dimension format of the input image is used. Can be one of:
TYPE:
|
input_data_format |
The channel dimension format for the input image. If unset, the channel dimension format is inferred from the input image. Can be one of:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in mindnlp/transformers/models/blip/image_processing_blip.py
152 153 154 155 156 157 158 159 160 161 162 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 |
|
mindnlp.transformers.models.blip.processing_blip.BlipProcessor
¶
Bases: ProcessorMixin
Constructs a BLIP processor which wraps a BERT tokenizer and BLIP image processor into a single processor.
[BlipProcessor
] offers all the functionalities of [BlipImageProcessor
] and [BertTokenizerFast
]. See the
docstring of [~BlipProcessor.__call__
] and [~BlipProcessor.decode
] for more information.
PARAMETER | DESCRIPTION |
---|---|
image_processor |
An instance of [
TYPE:
|
tokenizer |
An instance of ['BertTokenizerFast`]. The tokenizer is a required input.
TYPE:
|
Source code in mindnlp/transformers/models/blip/processing_blip.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 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 103 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 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 157 158 159 160 161 162 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 |
|
mindnlp.transformers.models.blip.processing_blip.BlipProcessor.model_input_names
property
¶
This method, model_input_names, in the BlipProcessor class, retrieves the unique model input names from the tokenizer and image processor.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BlipProcessor class. This parameter is required to access the attributes of the BlipProcessor instance.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list
|
A list of unique model input names derived from the tokenizer and image processor. The list does not contain any duplicate names and is ordered as per the appearance in the combined tokenizer and image processor input names. |
mindnlp.transformers.models.blip.processing_blip.BlipProcessor.__call__(images=None, text=None, add_special_tokens=True, padding=False, truncation=None, max_length=None, stride=0, pad_to_multiple_of=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_token_type_ids=False, return_length=False, verbose=True, return_tensors=None, **kwargs)
¶
This method uses [BlipImageProcessor.__call__
] method to prepare image(s) for the model, and
[BertTokenizerFast.__call__
] to prepare text for the model.
Please refer to the docstring of the above two methods for more information.
Source code in mindnlp/transformers/models/blip/processing_blip.py
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 103 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
mindnlp.transformers.models.blip.processing_blip.BlipProcessor.__init__(image_processor, tokenizer)
¶
Initializes a BlipProcessor instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The BlipProcessor instance itself.
|
image_processor |
An object representing the image processor used for processing images.
|
tokenizer |
An object representing the tokenizer used for tokenization.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip/processing_blip.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 |
|
mindnlp.transformers.models.blip.processing_blip.BlipProcessor.batch_decode(*args, **kwargs)
¶
This method forwards all its arguments to BertTokenizerFast's [~PreTrainedTokenizer.batch_decode
]. Please
refer to the docstring of this method for more information.
Source code in mindnlp/transformers/models/blip/processing_blip.py
154 155 156 157 158 159 |
|
mindnlp.transformers.models.blip.processing_blip.BlipProcessor.decode(*args, **kwargs)
¶
This method forwards all its arguments to BertTokenizerFast's [~PreTrainedTokenizer.decode
]. Please refer to
the docstring of this method for more information.
Source code in mindnlp/transformers/models/blip/processing_blip.py
161 162 163 164 165 166 |
|