align
mindnlp.transformers.models.align.configuration_align.ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP = {'kakaobrain/align-base': 'https://hf-mirror.com/kakaobrain/align-base/resolve/main/config.json'}
module-attribute
¶
mindnlp.transformers.models.align.configuration_align.AlignConfig
¶
Bases: PretrainedConfig
[AlignConfig
] is the configuration class to store the configuration of a [AlignModel
]. It is used to
instantiate a ALIGN 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 ALIGN
kakaobrain/align-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:
|
temperature_init_value |
The inital value of the temperature paramter. Default is used as per the original ALIGN implementation.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
kwargs |
Dictionary of keyword arguments.
TYPE:
|
Example
>>> from transformers import AlignConfig, AlignModel
...
>>> # Initializing a AlignConfig with kakaobrain/align-base style configuration
>>> configuration = AlignConfig()
...
>>> # Initializing a AlignModel (with random weights) from the kakaobrain/align-base style configuration
>>> model = AlignModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
...
>>> # We can also initialize a AlignConfig from a AlignTextConfig and a AlignVisionConfig
>>> from transformers import AlignTextConfig, AlignVisionConfig
...
>>> # Initializing ALIGN Text and Vision configurations
>>> config_text = AlignTextConfig()
>>> config_vision = AlignVisionConfig()
...
>>> config = AlignConfig.from_text_vision_configs(config_text, config_vision)
Source code in mindnlp/transformers/models/align/configuration_align.py
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 475 476 477 478 479 480 481 482 483 |
|
mindnlp.transformers.models.align.configuration_align.AlignConfig.__init__(text_config=None, vision_config=None, projection_dim=640, temperature_init_value=1.0, initializer_range=0.02, **kwargs)
¶
Initializes an instance of the AlignConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the AlignConfig class.
|
text_config |
A dictionary containing configurations for text alignment. Defaults to None.
TYPE:
|
vision_config |
A dictionary containing configurations for vision alignment. Defaults to None.
TYPE:
|
projection_dim |
The dimension of the projection. Defaults to 640.
TYPE:
|
temperature_init_value |
The initial value for temperature. Defaults to 1.0.
TYPE:
|
initializer_range |
The range for initializing variables. Defaults to 0.02.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/align/configuration_align.py
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 |
|
mindnlp.transformers.models.align.configuration_align.AlignConfig.from_text_vision_configs(text_config, vision_config, **kwargs)
classmethod
¶
Instantiate a [AlignConfig
] (or a derived class) from align text model configuration and align vision model
configuration.
RETURNS | DESCRIPTION |
---|---|
[ |
Source code in mindnlp/transformers/models/align/configuration_align.py
474 475 476 477 478 479 480 481 482 483 |
|
mindnlp.transformers.models.align.configuration_align.AlignTextConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [AlignTextModel
]. It is used to instantiate a
ALIGN text encoder according to the specified arguments, defining the model architecture. Instantiating a
configuration with the defaults will yield a similar configuration to that of the text encoder of the ALIGN
kakaobrain/align-base architecture. The default values here are
copied from BERT.
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 Align Text 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:
|
type_vocab_size |
The vocabulary size of the
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:
|
pad_token_id |
Padding token id.
TYPE:
|
position_embedding_type |
Type of position embedding. Choose one of
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if
TYPE:
|
Example
>>> from transformers import AlignTextConfig, AlignTextModel
...
>>> # Initializing a AlignTextConfig with kakaobrain/align-base style configuration
>>> configuration = AlignTextConfig()
...
>>> # Initializing a AlignTextModel (with random weights) from the kakaobrain/align-base style configuration
>>> model = AlignTextModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/align/configuration_align.py
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 |
|
mindnlp.transformers.models.align.configuration_align.AlignTextConfig.__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, type_vocab_size=2, initializer_range=0.02, layer_norm_eps=1e-12, pad_token_id=0, position_embedding_type='absolute', use_cache=True, **kwargs)
¶
Initializes a new instance of the AlignTextConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Defaults to 30522.
TYPE:
|
hidden_size |
The size of the hidden layers. Defaults to 768.
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:
|
intermediate_size |
The size of the intermediate layer in the transformer encoder. Defaults to 3072.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Defaults to 'gelu'.
TYPE:
|
hidden_dropout_prob |
The dropout probability for the hidden layers. Defaults to 0.1.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability for the attention probabilities. Defaults to 0.1.
TYPE:
|
max_position_embeddings |
The maximum number of positions for the positional embeddings. Defaults to 512.
TYPE:
|
type_vocab_size |
The size of the type vocabulary. Defaults to 2.
TYPE:
|
initializer_range |
The range for the weight initializers. Defaults to 0.02.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization. Defaults to 1e-12.
TYPE:
|
pad_token_id |
The token id for padding. Defaults to 0.
TYPE:
|
position_embedding_type |
The type of position embedding to use (e.g., 'absolute'). Defaults to 'absolute'.
TYPE:
|
use_cache |
Whether to use cache for the transformer encoder. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the provided parameters are not of the expected type or value range. |
Source code in mindnlp/transformers/models/align/configuration_align.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 |
|
mindnlp.transformers.models.align.configuration_align.AlignTextConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
Loads a pretrained model configuration from a given model name or file path.
PARAMETER | DESCRIPTION |
---|---|
cls |
The class object itself.
TYPE:
|
pretrained_model_name_or_path |
The name or path of the pretrained model. It can be either a string representing the model name or an os.PathLike object representing the file path. Note that the model should be of type 'align' according to the configuration. Using a model of different type may cause errors in some configurations.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
PretrainedConfig
|
The loaded pretrained model configuration.
TYPE:
|
Source code in mindnlp/transformers/models/align/configuration_align.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
mindnlp.transformers.models.align.configuration_align.AlignVisionConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [AlignVisionModel
]. It is used to instantiate a
ALIGN vision encoder according to the specified arguments, defining the model architecture. Instantiating a
configuration with the defaults will yield a similar configuration to that of the vision encoder of the ALIGN
kakaobrain/align-base architecture. The default values are copied
from EfficientNet (efficientnet-b7)
Configuration objects inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig
] for more information.
PARAMETER | DESCRIPTION |
---|---|
num_channels |
The number of input channels.
TYPE:
|
image_size |
The input image size.
TYPE:
|
width_coefficient |
Scaling coefficient for network width at each stage.
TYPE:
|
depth_coefficient |
Scaling coefficient for network depth at each stage.
TYPE:
|
depth_divisor |
A unit of network width.
TYPE:
|
kernel_sizes |
List of kernel sizes to be used in each block.
TYPE:
|
in_channels |
List of input channel sizes to be used in each block for convolutional layers.
TYPE:
|
out_channels |
List of output channel sizes to be used in each block for convolutional layers.
TYPE:
|
depthwise_padding |
List of block indices with square padding.
TYPE:
|
strides |
List of stride sizes to be used in each block for convolutional layers.
TYPE:
|
num_block_repeats |
List of the number of times each block is to repeated.
TYPE:
|
expand_ratios |
List of scaling coefficient of each block.
TYPE:
|
squeeze_expansion_ratio |
Squeeze expansion ratio.
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in each block. If string,
TYPE:
|
hiddem_dim |
The hidden dimension of the layer before the classification head.
TYPE:
|
pooling_type |
Type of final pooling to be applied before the dense classification head. Available options are [
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
batch_norm_eps |
The epsilon used by the batch normalization layers.
TYPE:
|
batch_norm_momentum |
The momentum used by the batch normalization layers.
TYPE:
|
drop_connect_rate |
The drop rate for skip connections.
TYPE:
|
Example
>>> from transformers import AlignVisionConfig, AlignVisionModel
...
>>> # Initializing a AlignVisionConfig with kakaobrain/align-base style configuration
>>> configuration = AlignVisionConfig()
...
>>> # Initializing a AlignVisionModel (with random weights) from the kakaobrain/align-base style configuration
>>> model = AlignVisionModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/align/configuration_align.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
mindnlp.transformers.models.align.configuration_align.AlignVisionConfig.__init__(num_channels=3, image_size=600, width_coefficient=2.0, depth_coefficient=3.1, depth_divisor=8, kernel_sizes=[3, 3, 5, 3, 5, 5, 3], in_channels=[32, 16, 24, 40, 80, 112, 192], out_channels=[16, 24, 40, 80, 112, 192, 320], depthwise_padding=[], strides=[1, 2, 2, 2, 1, 2, 1], num_block_repeats=[1, 2, 2, 3, 3, 4, 1], expand_ratios=[1, 6, 6, 6, 6, 6, 6], squeeze_expansion_ratio=0.25, hidden_act='swish', hidden_dim=2560, pooling_type='mean', initializer_range=0.02, batch_norm_eps=0.001, batch_norm_momentum=0.99, drop_connect_rate=0.2, **kwargs)
¶
Initializes an instance of the AlignVisionConfig
class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class itself.
|
num_channels |
The number of channels in the input image. Default is 3.
TYPE:
|
image_size |
The size of the input image. Default is 600.
TYPE:
|
width_coefficient |
The width coefficient for scaling the number of channels in each layer. Default is 2.0.
TYPE:
|
depth_coefficient |
The depth coefficient for scaling the number of layers. Default is 3.1.
TYPE:
|
depth_divisor |
The divisor for computing the number of output channels in each layer. Default is 8.
TYPE:
|
kernel_sizes |
The list of kernel sizes for each layer. Default is [3, 3, 5, 3, 5, 5, 3].
TYPE:
|
in_channels |
The list of input channels for each layer. Default is [32, 16, 24, 40, 80, 112, 192].
TYPE:
|
out_channels |
The list of output channels for each layer. Default is [16, 24, 40, 80, 112, 192, 320].
TYPE:
|
depthwise_padding |
The list of padding values for depthwise convolution layers. Default is [].
TYPE:
|
strides |
The list of stride values for each layer. Default is [1, 2, 2, 2, 1, 2, 1].
TYPE:
|
num_block_repeats |
The list of repeat counts for each block. Default is [1, 2, 2, 3, 3, 4, 1].
TYPE:
|
expand_ratios |
The list of expansion ratios for each block. Default is [1, 6, 6, 6, 6, 6, 6].
TYPE:
|
squeeze_expansion_ratio |
The expansion ratio for the squeeze layer. Default is 0.25.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Default is 'swish'.
TYPE:
|
hidden_dim |
The dimension of the hidden layers. Default is 2560.
TYPE:
|
pooling_type |
The type of pooling to use. Default is 'mean'.
TYPE:
|
initializer_range |
The range of the initializer. Default is 0.02.
TYPE:
|
batch_norm_eps |
The epsilon value for batch normalization. Default is 0.001.
TYPE:
|
batch_norm_momentum |
The momentum value for batch normalization. Default is 0.99.
TYPE:
|
drop_connect_rate |
The rate at which to drop connections. Default is 0.2.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/align/configuration_align.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 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 |
|
mindnlp.transformers.models.align.configuration_align.AlignVisionConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
This method creates an instance of AlignVisionConfig from a pretrained model configuration.
PARAMETER | DESCRIPTION |
---|---|
cls |
The class object itself.
TYPE:
|
pretrained_model_name_or_path |
The name or path of the pretrained model configuration. It can be either a string or a path-like object.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
PretrainedConfig
|
An instance of PretrainedConfig class representing the configuration of the pretrained model.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
Warning
|
If the model type specified in the configuration is different from the model type of the class, a warning is issued because using a different model type may lead to errors in some configurations of models. |
Source code in mindnlp/transformers/models/align/configuration_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST = ['kakaobrain/align-base']
module-attribute
¶
mindnlp.transformers.models.align.modeling_align.AlignModel
¶
Bases: AlignPreTrainedModel
The AlignModel
class is a model for aligning text and image embeddings.
It is designed to compute image-text similarity scores using pre-trained text and vision models.
The class inherits from the AlignPreTrainedModel
class.
ATTRIBUTE | DESCRIPTION |
---|---|
`projection_dim` |
The dimension of the projection layer.
|
`text_embed_dim` |
The dimension of the text embeddings.
|
`text_model` |
An instance of the
|
`vision_model` |
An instance of the
|
`text_projection` |
A dense layer for projecting the text embeddings.
|
`temperature` |
A parameter for scaling the similarity scores.
|
METHOD | DESCRIPTION |
---|---|
`__init__` |
Initializes the |
`get_text_features` |
Computes the text embeddings. |
`get_image_features` |
Computes the image embeddings. |
`forward` |
Constructs the model and computes the image-text similarity scores. |
Please see the code examples in the docstrings of each method for usage details.
Source code in mindnlp/transformers/models/align/modeling_align.py
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 2468 2469 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 |
|
mindnlp.transformers.models.align.modeling_align.AlignModel.__init__(config)
¶
Initializes the AlignModel with the specified configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the AlignModel class.
|
config |
An object containing the configuration settings for the AlignModel.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the config.text_config is not of type AlignTextConfig. |
ValueError
|
If the config.vision_config is not of type AlignVisionConfig. |
Source code in mindnlp/transformers/models/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignModel.forward(input_ids=None, pixel_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, return_loss=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, AlignOutput]
|
Union[Tuple, AlignOutput] |
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, AlignModel
...
>>> model = AlignModel.from_pretrained("kakaobrain/align-base")
>>> processor = AutoProcessor.from_pretrained("kakaobrain/align-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/align/modeling_align.py
2466 2467 2468 2469 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 |
|
mindnlp.transformers.models.align.modeling_align.AlignModel.get_image_features(pixel_values=None, output_hidden_states=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, AlignModel
...
>>> model = AlignModel.from_pretrained("kakaobrain/align-base")
>>> processor = AutoProcessor.from_pretrained("kakaobrain/align-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/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignModel.get_text_features(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
text_features
|
The text embeddings obtained by
applying the projection layer to the pooled output of [
TYPE:
|
Example
>>> from transformers import AutoTokenizer, AlignModel
...
>>> model = AlignModel.from_pretrained("kakaobrain/align-base")
>>> tokenizer = AutoTokenizer.from_pretrained("kakaobrain/align-base")
...
>>> inputs = tokenizer(["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/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignPreTrainedModel
¶
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/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignTextModel
¶
Bases: AlignPreTrainedModel
The AlignTextModel
class represents a model for aligning text.
It includes methods for initializing the model, getting and setting input embeddings, and forwarding the model for inference.
The __init__
method initializes the model with the provided configuration and sets up
the embeddings, encoder, and pooler layers based on the configuration parameters.
The get_input_embeddings
method retrieves the word embeddings used as input to the model.
The set_input_embeddings
method allows for setting custom word embeddings as input to the model.
The forward
method forwards the model for inference based on the input parameters such as
input tokens, attention mask, token type ids, etc.
It returns the model outputs including the last hidden state and pooled output.
The class also includes examples of how to use the model for text alignment tasks.
This class inherits from AlignPreTrainedModel
.
Source code in mindnlp/transformers/models/align/modeling_align.py
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 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 |
|
mindnlp.transformers.models.align.modeling_align.AlignTextModel.__init__(config, add_pooling_layer=True)
¶
Initializes an instance of AlignTextModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the AlignTextModel class.
|
config |
An instance of AlignTextConfig containing configuration parameters.
TYPE:
|
add_pooling_layer |
A flag indicating whether to add a pooling layer. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignTextModel.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions]
|
Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions] |
Example
>>> from transformers import AutoTokenizer, AlignTextModel
...
>>> model = AlignTextModel.from_pretrained("kakaobrain/align-base")
>>> tokenizer = AutoTokenizer.from_pretrained("kakaobrain/align-base")
...
>>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")
...
>>> outputs = model(**inputs)
>>> last_hidden_state = outputs.last_hidden_state
>>> pooled_output = outputs.pooler_output # pooled (EOS token) states
Source code in mindnlp/transformers/models/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignTextModel.get_input_embeddings()
¶
This method retrieves the input embeddings from the AlignTextModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the AlignTextModel class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None as it retrieves the input embeddings without any transformations. |
Source code in mindnlp/transformers/models/align/modeling_align.py
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 |
|
mindnlp.transformers.models.align.modeling_align.AlignTextModel.set_input_embeddings(value)
¶
Sets the input embeddings for the AlignTextModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the AlignTextModel class.
TYPE:
|
value |
The input embeddings value to be set for the model. It can be of any type.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/align/modeling_align.py
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 |
|
mindnlp.transformers.models.align.modeling_align.AlignVisionModel
¶
Bases: AlignPreTrainedModel
This class represents an AlignVision model for vision tasks, which includes functionalities for processing images and generating embeddings using a vision encoder.
The model supports different pooling strategies for extracting features from the encoded image representations.
It inherits from AlignPreTrainedModel and provides methods for initializing the model, accessing input embeddings, and forwarding the model output.
The model's forwardor takes an AlignVisionConfig object as a parameter to configure the model's behavior. It initializes the model's components including embeddings and encoder based on the provided configuration, and sets up the pooling strategy based on the specified pooling type in the configuration.
The 'get_input_embeddings' method returns the input embeddings generated by the model's convolutional layers for further processing.
The 'forward' method processes input pixel values to generate embeddings using the model's embeddings and encoder components. It then applies the pooling strategy to extract features from the encoded image representations. The method returns the last hidden state, pooled output, and additional encoder outputs based on the specified return format.
The class provides examples in the docstring to demonstrate how to use the model for image processing tasks, including loading an image, processing it with the model, and accessing the output hidden states and pooled output for further analysis.
Source code in mindnlp/transformers/models/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignVisionModel.__init__(config)
¶
Initializes an instance of the AlignVisionModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object containing configuration parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the 'pooling_type' in the config is not one of ['mean', 'max']. |
Description
This method initializes an instance of the AlignVisionModel class. It takes in a config object which contains the configuration parameters for the model. The 'config' parameter is of type AlignVisionConfig.
Inside the method, the superclass's init method is called with the 'config' parameter. The 'config' is then assigned to the 'self.config' attribute.
The method also initializes the 'embeddings' attribute with an instance of AlignVisionEmbeddings, passing in the 'config' parameter. Similarly, the 'encoder' attribute is initialized with an instance of AlignVisionEncoder, passing in the 'config' parameter.
The 'pooler' attribute is dynamically set based on the value of the 'pooling_type' in the 'config'.
- If 'pooling_type' is set to 'mean', the 'pooler' attribute is set to a partial function 'ops.mean' with the specified axis and keep_dims parameters.
- If 'pooling_type' is set to 'max', the 'pooler' attribute is set to an instance of nn.MaxPool2d with the specified 'hidden_dim' and 'ceil_mode' parameters.
- If the 'pooling_type' in the 'config' is not one of ['mean', 'max'], a ValueError is raised.
Finally, the 'post_init' method is called.
This method does not return any value.
Source code in mindnlp/transformers/models/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignVisionModel.forward(pixel_values=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]
|
Union[Tuple, BaseModelOutputWithPoolingAndNoAttention] |
Example
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, AlignVisionModel
...
>>> model = AlignVisionModel.from_pretrained("kakaobrain/align-base")
>>> processor = AutoProcessor.from_pretrained("kakaobrain/align-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(**inputs)
>>> last_hidden_state = outputs.last_hidden_state
>>> pooled_output = outputs.pooler_output # pooled CLS states
Source code in mindnlp/transformers/models/align/modeling_align.py
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 |
|
mindnlp.transformers.models.align.modeling_align.AlignVisionModel.get_input_embeddings()
¶
Retrieve the input embeddings from the AlignVisionModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the AlignVisionModel class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: The input embeddings extracted from the vision model's convolution layer. |
Source code in mindnlp/transformers/models/align/modeling_align.py
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 |
|
mindnlp.transformers.models.align.processing_align.AlignProcessor
¶
Bases: ProcessorMixin
Constructs an ALIGN processor which wraps [EfficientNetImageProcessor
] and
[BertTokenizer
]/[BertTokenizerFast
] into a single processor that interits both the image processor and
tokenizer functionalities. See the [~AlignProcessor.__call__
] and [~OwlViTProcessor.decode
] for more
information.
PARAMETER | DESCRIPTION |
---|---|
image_processor |
The image processor is a required input.
TYPE:
|
tokenizer |
The tokenizer is a required input.
TYPE:
|
Source code in mindnlp/transformers/models/align/processing_align.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 |
|
mindnlp.transformers.models.align.processing_align.AlignProcessor.model_input_names
property
¶
This method retrieves the input names required for the model from the tokenizer and image processor.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the AlignProcessor class.
|
RETURNS | DESCRIPTION |
---|---|
list
|
A list of unique input names required for the model, which are obtained by combining the input names from the tokenizer and image processor. |
mindnlp.transformers.models.align.processing_align.AlignProcessor.__call__(text=None, images=None, padding='max_length', max_length=64, return_tensors=None, **kwargs)
¶
Main method to prepare text(s) and image(s) to be fed as input to the model. This method forwards the text
and kwargs
arguments to BertTokenizerFast's [~BertTokenizerFast.__call__
] if text
is not None
to encode
the text. To prepare the image(s), this method forwards the images
and kwargs
arguments to
EfficientNetImageProcessor's [~EfficientNetImageProcessor.__call__
] if images
is not None
. Please refer
to the doctsring of the above two methods for more information.
PARAMETER | DESCRIPTION |
---|---|
text |
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
TYPE:
|
images |
The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width.
TYPE:
|
padding |
Activates and controls padding for tokenization of input text. Choose between [
TYPE:
|
max_length |
Maximum padding value to use to pad the input text during tokenization.
TYPE:
|
return_tensors |
If set, will return tensors of a particular framework. Acceptable values are:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
[
|
Source code in mindnlp/transformers/models/align/processing_align.py
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 |
|
mindnlp.transformers.models.align.processing_align.AlignProcessor.__init__(image_processor, tokenizer)
¶
Initializes an AlignProcessor object.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
image_processor |
An object of the image processor class that handles image processing.
TYPE:
|
tokenizer |
An object of the tokenizer class that handles text tokenization.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/align/processing_align.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
mindnlp.transformers.models.align.processing_align.AlignProcessor.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/align/processing_align.py
117 118 119 120 121 122 |
|
mindnlp.transformers.models.align.processing_align.AlignProcessor.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/align/processing_align.py
124 125 126 127 128 129 |
|