blip_2
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2Config
¶
Bases: PretrainedConfig
[Blip2Config
] is the configuration class to store the configuration of a [Blip2ForConditionalGeneration
]. It is
used to instantiate a BLIP-2 model according to the specified arguments, defining the vision model, Q-Former model
and language model configs. Instantiating a configuration with the defaults will yield a similar configuration to
that of the BLIP-2 Salesforce/blip2-opt-2.7b 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 |
---|---|
vision_config |
Dictionary of configuration options used to initialize [
TYPE:
|
qformer_config |
Dictionary of configuration options used to initialize [
TYPE:
|
text_config |
Dictionary of configuration options used to initialize any [
TYPE:
|
num_query_tokens |
The number of query tokens passed through the Transformer.
TYPE:
|
kwargs |
Dictionary of keyword arguments.
TYPE:
|
Example
>>> from transformers import (
... Blip2VisionConfig,
... Blip2QFormerConfig,
... OPTConfig,
... Blip2Config,
... Blip2ForConditionalGeneration,
... )
...
>>> # Initializing a Blip2Config with Salesforce/blip2-opt-2.7b style configuration
>>> configuration = Blip2Config()
...
>>> # Initializing a Blip2ForConditionalGeneration (with random weights) from the Salesforce/blip2-opt-2.7b style configuration
>>> model = Blip2ForConditionalGeneration(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
...
>>> # We can also initialize a Blip2Config from a Blip2VisionConfig, Blip2QFormerConfig and any PretrainedConfig
...
>>> # Initializing BLIP-2 vision, BLIP-2 Q-Former and language model configurations
>>> vision_config = Blip2VisionConfig()
>>> qformer_config = Blip2QFormerConfig()
>>> text_config = OPTConfig()
...
>>> config = Blip2Config.from_text_vision_configs(vision_config, qformer_config, text_config)
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 361 362 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 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2Config.__init__(vision_config=None, qformer_config=None, text_config=None, num_query_tokens=32, **kwargs)
¶
This method initializes an instance of the Blip2Config class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vision_config |
Configuration for vision. Defaults to None.
TYPE:
|
qformer_config |
Configuration for qformer. Defaults to None.
TYPE:
|
text_config |
Configuration for text. Defaults to None.
TYPE:
|
num_query_tokens |
The number of query tokens. Defaults to 32.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2Config.from_vision_qformer_text_configs(vision_config, qformer_config, text_config, **kwargs)
classmethod
¶
Instantiate a [Blip2Config
] (or a derived class) from a BLIP-2 vision model, Q-Former and language model
configurations.
RETURNS | DESCRIPTION |
---|---|
[ |
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2QFormerConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [Blip2QFormerModel
]. It is used to instantiate a
BLIP-2 Querying Transformer (Q-Former) 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 BLIP-2
Salesforce/blip2-opt-2.7b architecture. Configuration objects
inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the documentation from
[PretrainedConfig
] for more information.
Note that [Blip2QFormerModel
] is very similar to [BertLMHeadModel
] with interleaved cross-attention.
PARAMETER | DESCRIPTION |
---|---|
vocab_size |
Vocabulary size of the Q-Former model. Defines the number of different tokens that can be represented by
the
TYPE:
|
hidden_size |
Dimensionality of the encoder layers and the pooler layer.
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:
|
intermediate_size |
Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the encoder and pooler. If string,
TYPE:
|
hidden_dropout_prob |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
attention_probs_dropout_prob |
The dropout ratio for the attention probabilities.
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:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
position_embedding_type |
Type of position embedding. Choose one of
TYPE:
|
cross_attention_frequency |
The frequency of adding cross-attention to the Transformer layers.
TYPE:
|
encoder_hidden_size |
The hidden size of the hidden states for cross-attention.
TYPE:
|
Example
>>> from transformers import Blip2QFormerConfig, Blip2QFormerModel
...
>>> # Initializing a BLIP-2 Salesforce/blip2-opt-2.7b style configuration
>>> configuration = Blip2QFormerConfig()
...
>>> # Initializing a model (with random weights) from the Salesforce/blip2-opt-2.7b style configuration
>>> model = Blip2QFormerModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2QFormerConfig.__init__(vocab_size=30522, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, hidden_act='gelu', hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, initializer_range=0.02, layer_norm_eps=1e-12, pad_token_id=0, position_embedding_type='absolute', cross_attention_frequency=2, encoder_hidden_size=1408, **kwargs)
¶
Initialize a Blip2QFormerConfig object with the specified parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
vocab_size |
The size of the vocabulary.
TYPE:
|
hidden_size |
The size of the hidden layers.
TYPE:
|
num_hidden_layers |
The number of hidden layers in the model.
TYPE:
|
num_attention_heads |
The number of attention heads in the model.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the model.
TYPE:
|
hidden_act |
The activation function for the hidden layers.
TYPE:
|
hidden_dropout_prob |
The dropout probability for the hidden layers.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability for attention probabilities.
TYPE:
|
max_position_embeddings |
The maximum position embeddings allowed.
TYPE:
|
initializer_range |
The range for weight initializations.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization.
TYPE:
|
pad_token_id |
The token ID for padding.
TYPE:
|
position_embedding_type |
The type of position embedding used (e.g., absolute).
TYPE:
|
cross_attention_frequency |
The frequency of cross-attention layers in the model.
TYPE:
|
encoder_hidden_size |
The size of the encoder hidden layers.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2QFormerConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
This method creates a 'Blip2QFormerConfig' instance from a pretrained model.
PARAMETER | DESCRIPTION |
---|---|
cls |
The class type of 'Blip2QFormerConfig'.
TYPE:
|
pretrained_model_name_or_path |
The name or path of the pretrained model. It accepts a string or os.PathLike object.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
PretrainedConfig
|
Returns a 'PretrainedConfig' object representing the configuration of the pretrained model.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the 'config_dict' does not contain the required keys or values. |
TypeError
|
If the input parameters are of incorrect types. |
Warning
|
If the instantiated model type differs from the provided model type, a warning is issued. |
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2VisionConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [Blip2VisionModel
]. It is used to instantiate a
BLIP-2 vision encoder according to the specified arguments, defining the model architecture. Instantiating a
configuration defaults will yield a similar configuration to that of the BLIP-2
Salesforce/blip2-opt-2.7b 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:
|
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:
|
qkv_bias |
Whether to add a bias to the queries and values in the self-attention layers.
TYPE:
|
Example
>>> from transformers import Blip2VisionConfig, Blip2VisionModel
...
>>> # Initializing a Blip2VisionConfig with Salesforce/blip2-opt-2.7b style configuration
>>> configuration = Blip2VisionConfig()
...
>>> # Initializing a Blip2VisionModel (with random weights) from the Salesforce/blip2-opt-2.7b style configuration
>>> model = Blip2VisionModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2VisionConfig.__init__(hidden_size=1408, intermediate_size=6144, num_hidden_layers=39, num_attention_heads=16, image_size=224, patch_size=14, hidden_act='gelu', layer_norm_eps=1e-06, attention_dropout=0.0, initializer_range=1e-10, qkv_bias=True, **kwargs)
¶
Initializes an instance of the Blip2VisionConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
hidden_size |
The size of the hidden layers. Defaults to 1408.
TYPE:
|
intermediate_size |
The size of the intermediate layers. Defaults to 6144.
TYPE:
|
num_hidden_layers |
The number of hidden layers. Defaults to 39.
TYPE:
|
num_attention_heads |
The number of attention heads. Defaults to 16.
TYPE:
|
image_size |
The size of the image. Defaults to 224.
TYPE:
|
patch_size |
The size of the patches. Defaults to 14.
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-06.
TYPE:
|
attention_dropout |
The dropout rate for attention layers. Defaults to 0.0.
TYPE:
|
initializer_range |
The range for weight initialization. Defaults to 1e-10.
TYPE:
|
qkv_bias |
Whether to include bias in the query, key, value layers. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.configuration_blip_2.Blip2VisionConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
Method to create a Blip2VisionConfig instance from a pretrained model.
PARAMETER | DESCRIPTION |
---|---|
cls |
The class object itself.
TYPE:
|
pretrained_model_name_or_path |
A string representing the name or path of the pretrained model to load.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
PretrainedConfig
|
An instance of the Blip2VisionConfig class initialized with the configuration obtained from the pretrained model.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the 'model_type' key is not found in the configuration dictionary. |
AttributeError
|
If the 'model_type' key is present in the configuration dictionary but the class does not have a 'model_type' attribute. |
Warning
|
If the model type in the configuration dictionary does not match the class's model type, a warning is logged to alert the user. |
Source code in mindnlp/transformers/models/blip_2/configuration_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model
¶
Bases: Blip2PreTrainedModel
A class representing the Blip2Model for multimodal language understanding tasks.
Blip2Model is a multimodal transformer model that combines vision and text inputs to generate high-level representations and perform tasks such as image captioning, visual question answering, and multimodal language generation.
This class extends the Blip2PreTrainedModel, which is the base class for all models in the Blip2 project.
ATTRIBUTE | DESCRIPTION |
---|---|
vision_model |
The vision model that processes the image inputs.
TYPE:
|
query_tokens |
Query tokens used in the QFormer model.
TYPE:
|
qformer |
The QFormer model that processes the query tokens and image embeddings.
TYPE:
|
language_projection |
Projection layer that maps the QFormer output to the input size of the language model.
TYPE:
|
language_model |
The language model used for text processing.
TYPE:
|
_tied_weights_keys |
List of tied weights keys for the language model.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the Blip2Model with the given configuration. |
get_input_embeddings |
Returns the input embeddings of the language model. |
set_input_embeddings |
Sets the input embeddings of the language model. |
set_output_embeddings |
Sets the output embeddings of the language model. |
get_output_embeddings |
Returns the output embeddings of the language model. |
get_encoder |
Returns the encoder of the language model. |
get_decoder |
Returns the decoder of the language model. |
_tie_weights |
Ties the weights of the encoder and decoder in the language model. |
get_text_features |
Retrieves the text features from the language model. |
get_image_features |
Retrieves the image features from the vision model. |
get_qformer_features |
Retrieves the query transformer (QFormer) features from the vision model. |
forward |
Constructs the Blip2Model with the given inputs and returns the model outputs. |
Example
>>> import torch
>>> from PIL import Image
>>> import requests
>>> from transformers import Blip2Processor, Blip2Model
...
>>> processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
>>> model = Blip2Model.from_pretrained("Salesforce/blip2-opt-2.7b")
...
>>> 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.get_image_features(**inputs)
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.__init__(config)
¶
Initialize the Blip2Model with the specified configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2Model class.
|
config |
An instance of Blip2Config containing the configuration parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.forward(pixel_values, input_ids, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, output_attentions=None, output_hidden_states=None, labels=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, Blip2ForConditionalGenerationModelOutput]
|
|
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import Blip2Processor, Blip2Model
>>> import torch
...
...
>>> processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
>>> model = Blip2Model.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> prompt = "Question: how many cats are there? Answer:"
>>> inputs = processor(images=image, text=prompt, return_tensors="pt").to(torch.float16)
...
>>> outputs = model(**inputs)
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.get_decoder()
¶
This method returns the decoder from the language model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2Model class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns the decoder obtained from the language model. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.get_encoder()
¶
Method to retrieve the encoder from the language model within the Blip2Model class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2Model class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None as it retrieves the encoder from the language model. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.get_image_features(pixel_values=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
vision_outputs
|
The vision model outputs. If
TYPE:
|
Example
>>> import torch
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, Blip2Model
...
>>> model = Blip2Model.from_pretrained("Salesforce/blip2-opt-2.7b")
...
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
>>> 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_outputs = model.get_image_features(**inputs)
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.get_input_embeddings()
¶
Get the input embeddings from the Blip2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the Blip2Model class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.get_output_embeddings()
¶
Returns the output embeddings of the Blip2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
Blip2Model - The instance of the Blip2Model class.
|
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: The output embeddings of the Blip2Model language model. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.get_qformer_features(pixel_values=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
vision_outputs
|
The vision model outputs. If
TYPE:
|
Example
>>> import torch
>>> from PIL import Image
>>> import requests
>>> from transformers import Blip2Processor, Blip2Model
...
>>> processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
>>> model = Blip2Model.from_pretrained("Salesforce/blip2-opt-2.7b")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> inputs = processor(images=image, return_tensors="pt")
>>> qformer_outputs = model.get_qformer_features(**inputs)
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.get_text_features(input_ids=None, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
text_outputs
|
The language model outputs. If
TYPE:
|
Example
>>> import torch
>>> from transformers import AutoTokenizer, Blip2Model
...
>>> model = Blip2Model.from_pretrained("Salesforce/blip2-opt-2.7b")
...
>>> tokenizer = AutoTokenizer.from_pretrained("Salesforce/blip2-opt-2.7b")
>>> inputs = tokenizer(["a photo of a cat"], padding=True, return_tensors="pt")
>>> text_features = model.get_text_features(**inputs)
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.set_input_embeddings(value)
¶
Set the input embeddings for the Blip2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2Model class.
TYPE:
|
value |
The input embeddings to be set for the language model. It should be of type torch.Tensor or any compatible type.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the value provided is not of the expected type. |
ValueError
|
If the value provided is invalid or cannot be used for setting input embeddings. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2Model.set_output_embeddings(new_embeddings)
¶
This method sets the output embeddings for the Blip2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2Model class.
TYPE:
|
new_embeddings |
The new output embeddings to be set for the language model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2QFormerModel
¶
Bases: Blip2PreTrainedModel
Querying Transformer (Q-Former), used in BLIP-2.
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
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 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2QFormerModel.__init__(config)
¶
Initializes a new instance of the Blip2QFormerModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing the model settings. It should be an instance of Blip2QFormerConfig class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Description
This method initializes the Blip2QFormerModel instance with the provided configuration. It sets the instance variables as follows:
- self.config: The provided config object is stored as an instance variable.
- self.layernorm: A LayerNorm module is created with the hidden size specified in the config. The epsilon value for LayerNorm is set to the value specified in the config.
- self.dropout: A Dropout module is created with the dropout probability specified in the config.
- self.encoder: An instance of Blip2QFormerEncoder class is created with the provided config.
Note
After the initialization, self.post_init() method is called to perform any additional post-initialization steps.
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.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 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2QFormerModel.forward(query_embeds, attention_mask=None, head_mask=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)
¶
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 |
shape
TYPE:
|
use_cache |
If set to
TYPE:
|
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2QFormerModel.get_extended_attention_mask(attention_mask, input_shape, has_query=False)
¶
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_2/modeling_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2QFormerModel.get_input_embeddings()
¶
This method retrieves the input embeddings for the Blip2QFormerModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The reference to the current instance of the Blip2QFormerModel class.
|
RETURNS | DESCRIPTION |
---|---|
word_embeddings
|
The method returns the word embeddings for the input. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2QFormerModel.set_input_embeddings(value)
¶
Set the input embeddings for the Blip2QFormerModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2QFormerModel class.
TYPE:
|
value |
The input embeddings to be set. It can be of any valid type.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2PreTrainedModel
¶
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_2/modeling_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration
¶
Bases: Blip2PreTrainedModel
The Blip2ForConditionalGeneration
class is a model for image captioning and visual question answering. It is a conditional generator that takes as input an image and generates captions or answers to
questions based on the image.
The class inherits from the Blip2PreTrainedModel
class.
Example
>>> from transformers import Blip2ForConditionalGeneration
...
>>>model = Blip2ForConditionalGeneration()
METHOD | DESCRIPTION |
---|---|
`__init__` |
Initializes the Blip2ForConditionalGeneration model with the given configuration. |
`get_input_embeddings` |
Returns the input embeddings of the language model. |
`set_input_embeddings` |
Sets the input embeddings of the language model to the given value. |
`set_output_embeddings` |
Sets the output embeddings of the language model to the given new embeddings. |
`get_output_embeddings` |
Returns the output embeddings of the language model. |
`get_encoder` |
Returns the encoder of the language model. |
`get_decoder` |
Returns the decoder of the language model. |
`_tie_weights` |
Ties the weights of the encoder and decoder if the model is not using a decoder-only language model. |
`forward` |
Constructs the Blip2ForConditionalGeneration model with the given inputs and returns the output. |
`generate` |
Generates captions or answers based on the given image and optionally the input sequence and attention mask. |
Please refer to the docstrings of each method for more detailed information.
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.__init__(config)
¶
This method initializes an instance of the Blip2ForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2ForConditionalGeneration class.
|
config |
An object containing the configuration settings for the Blip2 model. It is used to initialize the various components of the model, such as vision model, query tokens, qformer, language projection, and language model. The config parameter is required and must be of type Blip2Config.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.forward(pixel_values, input_ids, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, output_attentions=None, output_hidden_states=None, labels=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, Blip2ForConditionalGenerationModelOutput]
|
Union[Tuple, Blip2ForConditionalGenerationModelOutput] |
Example
Prepare processor, model and image input
>>> from PIL import Image
>>> import requests
>>> from transformers import Blip2Processor, Blip2ForConditionalGeneration
>>> import torch
...
...
>>> processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
>>> model = Blip2ForConditionalGeneration.from_pretrained(
... "Salesforce/blip2-opt-2.7b", load_in_8bit=True, torch_dtype=torch.float16
... ) # doctest: +IGNORE_RESULT
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
Image captioning (without providing a text prompt):
>>> inputs = processor(images=image, return_tensors="pt").to(torch.float16)
...
>>> generated_ids = model.generate(**inputs)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
>>> print(generated_text)
two cats laying on a couch
Visual question answering (prompt = question):
>>> prompt = "Question: how many cats are there? Answer:"
>>> inputs = processor(images=image, text=prompt, return_tensors="pt").to(dtype=torch.float16)
...
>>> generated_ids = model.generate(**inputs)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
>>> print(generated_text)
two
Note that int8 inference is also supported through bitsandbytes. This greatly reduces the amount of memory used by the model while maintaining the same performance.
>>> model = Blip2ForConditionalGeneration.from_pretrained(
... "Salesforce/blip2-opt-2.7b", load_in_8bit=True, torch_dtype=torch.bfloat16
... ) # doctest: +IGNORE_RESULT
...
>>> inputs = processor(images=image, text=prompt, return_tensors="pt").to(dtype=torch.bfloat16)
...
>>> generated_ids = model.generate(**inputs)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
>>> print(generated_text)
two
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.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 images 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
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
captions
|
A list of strings of length batch_size * num_captions.
TYPE:
|
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.get_decoder()
¶
This method returns the decoder from the language model associated with the Blip2ForConditionalGeneration instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2ForConditionalGeneration class. It is used to access the language model and retrieve the decoder. |
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value directly. It retrieves the decoder from the language model associated with Blip2ForConditionalGeneration. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.get_encoder()
¶
Method to retrieve the encoder from the language model for Blip2ForConditionalGeneration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2ForConditionalGeneration class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
The method returns None as it retrieves the encoder from the language model and does not return any specific value.
|
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.get_input_embeddings()
¶
Retrieves the input embeddings from the Blip2 language model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the Blip2ForConditionalGeneration class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method retrieves the input embeddings from the underlying language model used in Blip2ForConditionalGeneration. It returns None since it directly calls the 'get_input_embeddings' method of the language model and does not modify or process the embeddings further.
Please note that this method takes only one parameter, which is the instance of the Blip2ForConditionalGeneration class itself (self).
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.get_output_embeddings()
¶
Get the output embeddings of the Blip2ForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2ForConditionalGeneration class. |
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: The output embeddings of the language model. |
This method returns the output embeddings of the language model used in the Blip2ForConditionalGeneration class. The output embeddings are retrieved by calling the 'get_output_embeddings()' method of the language model.
Note
The output embeddings are typically used to map the hidden states of the language model to the vocabulary space. They can be further processed or used in downstream tasks.
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.set_input_embeddings(value)
¶
Set the input embeddings for the Blip2ForConditionalGeneration model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2ForConditionalGeneration class. |
value |
The input embeddings to be set for the language model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method sets the input embeddings for the underlying language model of Blip2ForConditionalGeneration. The input embeddings are used to represent the input tokens during the model's forward pass. The 'value' parameter should be a tensor of shape (vocab_size, embedding_dim), where 'vocab_size' represents the size of the vocabulary and 'embedding_dim' represents the dimensionality of the embedding space. The method updates the input embeddings of the language model with the provided 'value'.
Example
>>> model = Blip2ForConditionalGeneration()
>>> embeddings = torch.randn(10000, 300)
>>> model.set_input_embeddings(embeddings)
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings for the Blip2ForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2ForConditionalGeneration class. |
new_embeddings |
The new embeddings to be set for the language model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2VisionModel
¶
Bases: Blip2PreTrainedModel
This class represents a Blip2VisionModel which is designed for vision tasks using Blip2 models. The Blip2VisionModel class inherits from the Blip2PreTrainedModel class and provides functionality for processing pixel values, forwarding embeddings, and generating output for vision-related tasks.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
An instance of Blip2VisionConfig containing configuration settings for the model.
|
embeddings |
An instance of Blip2VisionEmbeddings for creating embeddings from pixel values.
|
encoder |
An instance of Blip2Encoder for encoding input embeddings.
|
post_layernorm |
A LayerNorm module for applying layer normalization to the output.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the Blip2VisionModel with the provided configuration. |
forward |
Constructs the model by processing pixel values, generating embeddings, and producing output for vision tasks. |
get_input_embeddings |
Retrieves the embeddings module used by the model for processing input pixel values. |
The Blip2VisionModel class provides a comprehensive solution for vision tasks by leveraging the Blip2 architecture and incorporating advanced features such as layer normalization and configurable output options.
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2VisionModel.__init__(config)
¶
Initializes a new instance of the Blip2VisionModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
The configuration object containing various settings for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2VisionModel.forward(pixel_values=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPooling]
|
|
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
|
mindnlp.transformers.models.blip_2.modeling_blip_2.Blip2VisionModel.get_input_embeddings()
¶
This method retrieves the input embeddings from the Blip2VisionModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Blip2VisionModel class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
embeddings
|
This method returns the input embeddings. |
Source code in mindnlp/transformers/models/blip_2/modeling_blip_2.py
698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
|
mindnlp.transformers.models.blip_2.processing_blip_2.Blip2Processor
¶
Bases: ProcessorMixin
Constructs a BLIP-2 processor which wraps a BLIP image processor and an OPT/T5 tokenizer into a single processor.
[BlipProcessor
] offers all the functionalities of [BlipImageProcessor
] and [AutoTokenizer
]. See the docstring
of [~BlipProcessor.__call__
] and [~BlipProcessor.decode
] for more information.
PARAMETER | DESCRIPTION |
---|---|
image_processor |
An instance of [
TYPE:
|
tokenizer |
An instance of ['PreTrainedTokenizer`]. The tokenizer is a required input.
TYPE:
|
Source code in mindnlp/transformers/models/blip_2/processing_blip_2.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 |
|
mindnlp.transformers.models.blip_2.processing_blip_2.Blip2Processor.model_input_names
property
¶
Retrieves the names of the model inputs for the 'Blip2Processor' class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the 'Blip2Processor' class.
|
RETURNS | DESCRIPTION |
---|---|
A list of strings containing the names of the model inputs. |
mindnlp.transformers.models.blip_2.processing_blip_2.Blip2Processor.__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_2/processing_blip_2.py
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 |
|
mindnlp.transformers.models.blip_2.processing_blip_2.Blip2Processor.__init__(image_processor, tokenizer)
¶
Initializes a new instance of the Blip2Processor class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
image_processor |
An object representing the image processor to be used. It should have the necessary methods and attributes required for image processing.
|
tokenizer |
An object representing the tokenizer to be used. t should have the necessary methods and attributes required for tokenization. The 'return_token_type_ids' attribute of the tokenizer will be set to False.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/blip_2/processing_blip_2.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
mindnlp.transformers.models.blip_2.processing_blip_2.Blip2Processor.batch_decode(*args, **kwargs)
¶
This method forwards all its arguments to PreTrainedTokenizer's [~PreTrainedTokenizer.batch_decode
]. Please
refer to the docstring of this method for more information.
Source code in mindnlp/transformers/models/blip_2/processing_blip_2.py
151 152 153 154 155 156 |
|
mindnlp.transformers.models.blip_2.processing_blip_2.Blip2Processor.decode(*args, **kwargs)
¶
This method forwards all its arguments to PreTrainedTokenizer's [~PreTrainedTokenizer.decode
]. Please refer to
the docstring of this method for more information.
Source code in mindnlp/transformers/models/blip_2/processing_blip_2.py
159 160 161 162 163 164 |
|