van
mindnlp.transformers.models.van.configuration_van
¶
VAN model configuration
mindnlp.transformers.models.van.configuration_van.VanConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [VanModel
]. It is used to instantiate a VAN 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 VAN
Visual-Attention-Network/van-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 |
---|---|
image_size |
The size (resolution) of each image.
TYPE:
|
num_channels |
The number of input channels.
TYPE:
|
patch_sizes |
Patch size to use in each stage's embedding layer.
TYPE:
|
strides |
Stride size to use in each stage's embedding layer to downsample the input.
TYPE:
|
hidden_sizes |
Dimensionality (hidden size) at each stage.
TYPE:
|
depths |
Depth (number of layers) for each stage.
TYPE:
|
mlp_ratios |
The expansion ratio for mlp layer at each stage.
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in each layer. If string,
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:
|
layer_scale_init_value |
The initial value for layer scaling.
TYPE:
|
drop_path_rate |
The dropout probability for stochastic depth.
TYPE:
|
dropout_rate |
The dropout probability for dropout.
TYPE:
|
Example
>>> from transformers import VanModel, VanConfig
...
>>> # Initializing a VAN van-base style configuration
>>> configuration = VanConfig()
>>> # Initializing a model from the van-base style configuration
>>> model = VanModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/van/configuration_van.py
23 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 |
|
mindnlp.transformers.models.van.configuration_van.VanConfig.__init__(image_size=224, num_channels=3, patch_sizes=[7, 3, 3, 3], strides=[4, 2, 2, 2], hidden_sizes=[64, 128, 320, 512], depths=[3, 3, 12, 3], mlp_ratios=[8, 8, 4, 4], hidden_act='gelu', initializer_range=0.02, layer_norm_eps=1e-06, layer_scale_init_value=0.01, drop_path_rate=0.0, dropout_rate=0.0, **kwargs)
¶
Initializes a new instance of the VanConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
image_size |
The size of the input image (default is 224).
TYPE:
|
num_channels |
The number of channels in the input image (default is 3).
TYPE:
|
patch_sizes |
List of patch sizes for each layer in the model (default is [7, 3, 3, 3]).
TYPE:
|
strides |
List of stride values for each layer in the model (default is [4, 2, 2, 2]).
TYPE:
|
hidden_sizes |
List of hidden layer sizes for each layer in the model (default is [64, 128, 320, 512]).
TYPE:
|
depths |
List of depths for each layer in the model (default is [3, 3, 12, 3]).
TYPE:
|
mlp_ratios |
List of MLP ratio values for each layer in the model (default is [8, 8, 4, 4]).
TYPE:
|
hidden_act |
The activation function to be used in hidden layers (default is 'gelu').
TYPE:
|
initializer_range |
The range for weight initialization (default is 0.02).
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization (default is 1e-06).
TYPE:
|
layer_scale_init_value |
The initial value for layer scale (default is 0.01).
TYPE:
|
drop_path_rate |
The rate for drop path regularization (default is 0.0).
TYPE:
|
dropout_rate |
The dropout rate (default is 0.0).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/van/configuration_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van
¶
MindSpore Visual Attention Network (VAN) model.
mindnlp.transformers.models.van.modeling_van.VanDropPath
¶
Bases: Module
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
Source code in mindnlp/transformers/models/van/modeling_van.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
mindnlp.transformers.models.van.modeling_van.VanDropPath.__init__(drop_prob=None)
¶
Initialize a new instance of the VanDropPath class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VanDropPath class.
|
drop_prob |
The probability of dropping a path during training. If set to None, no paths will be dropped. Should be a float value between 0 and 1, inclusive.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/van/modeling_van.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
mindnlp.transformers.models.van.modeling_van.VanDropPath.extra_repr()
¶
Return a string representation of the probability of dropping nodes during training.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the VanDropPath class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
A string representation of the probability of dropping nodes during training.
TYPE:
|
This method returns a formatted string representation of the drop probability of the VanDropPath instance.
The drop probability is obtained from the drop_prob
attribute of the instance. The returned string is of the
form 'p={}', where '{}' is replaced by the actual drop probability value.
Example
If the drop_prob
attribute of the instance is 0.3, the method will return the string "p=0.3".
Source code in mindnlp/transformers/models/van/modeling_van.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
mindnlp.transformers.models.van.modeling_van.VanDropPath.forward(hidden_states)
¶
Constructs a new tensor by applying drop_path operation to the given hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the VanDropPath class.
TYPE:
|
hidden_states |
A tensor containing the hidden states.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: A tensor representing the output of the drop_path operation. |
Note
The drop_path operation randomly sets a fraction of the hidden states to zero during training. This helps in regularizing the model and preventing overfitting. The drop probability is controlled by the 'drop_prob' attribute of the VanDropPath class.
Example
>>> drop_path = VanDropPath()
>>> hidden_states = mindspore.Tensor([[1, 2, 3], [4, 5, 6]], mindspore.float32)
>>> output = drop_path.forward(hidden_states)
>>> print(output)
[[1, 0, 3], [4, 0, 6]]
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanEncoder
¶
Bases: Module
VanEncoder, consisting of multiple stages.
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanEncoder.__init__(config)
¶
Initializes a VanEncoder object.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object containing configuration parameters for the VanEncoder. It includes the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the config parameter is not of type VanConfig. |
TypeError
|
If any of the config attributes are not of the expected types. |
ValueError
|
If the drop_path_rate value is out of range or invalid. |
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanEncoder.forward(hidden_state, output_hidden_states=False, return_dict=True)
¶
Construct method in the VanEncoder class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
hidden_state |
The input hidden state tensor.
TYPE:
|
output_hidden_states |
A flag indicating whether to output hidden states. Defaults to False.
TYPE:
|
return_dict |
A flag indicating whether to return the output as a dictionary. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithNoAttention]
|
Union[Tuple, BaseModelOutputWithNoAttention]: The forwarded output, which is either a tuple of hidden state and all hidden states or an instance of BaseModelOutputWithNoAttention. |
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanForImageClassification
¶
Bases: VanPreTrainedModel
VanForImageClassification is a class that represents a model for image classification using a pre-trained VanModel for feature extraction and a classifier for final prediction. It inherits from VanPreTrainedModel and implements methods for model initialization and inference.
ATTRIBUTE | DESCRIPTION |
---|---|
van |
The VanModel instance used for feature extraction.
TYPE:
|
classifier |
The classifier module for predicting the final output based on the extracted features.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the VanForImageClassification model with the given configuration. |
forward |
Constructs the model for image classification. Args:
Returns:
|
Example
>>> model = VanForImageClassification(config)
>>> output = model.forward(pixel_values, labels, output_hidden_states, return_dict)
Note
The forward method computes the loss based on the labels and the model's prediction, and returns the output based on the configured settings.
Source code in mindnlp/transformers/models/van/modeling_van.py
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
|
mindnlp.transformers.models.van.modeling_van.VanForImageClassification.__init__(config)
¶
init
Initializes an instance of the VanForImageClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
A configuration object containing parameters for the van model and classification. This parameter is of type 'config' and is used to configure the van model and classifier. It should be an instance of the configuration class and must be provided.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/van/modeling_van.py
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
|
mindnlp.transformers.models.van.modeling_van.VanForImageClassification.forward(pixel_values=None, labels=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the image classification/regression loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/van/modeling_van.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
|
mindnlp.transformers.models.van.modeling_van.VanLargeKernelAttention
¶
Bases: Module
Basic Large Kernel Attention (LKA).
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanLargeKernelAttention.__init__(hidden_size)
¶
Initializes an instance of the VanLargeKernelAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
hidden_size |
The size of the hidden layer. Specifies the number of hidden units in the neural network. It is used to define the dimensions of the convolutional layers within the attention mechanism.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/van/modeling_van.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
mindnlp.transformers.models.van.modeling_van.VanLargeKernelAttention.forward(hidden_state)
¶
Constructs the attention mechanism in the VanLargeKernelAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the VanLargeKernelAttention class.
TYPE:
|
hidden_state |
The hidden state tensor representing the input data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The transformed hidden state tensor after passing through the attention mechanism. |
Source code in mindnlp/transformers/models/van/modeling_van.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
mindnlp.transformers.models.van.modeling_van.VanLargeKernelAttentionLayer
¶
Bases: Module
Computes attention using Large Kernel Attention (LKA) and attends the input.
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanLargeKernelAttentionLayer.__init__(hidden_size)
¶
Initializes a VanLargeKernelAttentionLayer instance with the specified hidden size.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VanLargeKernelAttentionLayer class.
|
hidden_size |
The size of the hidden state, representing the dimensionality of the input feature space. It must be a positive integer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/van/modeling_van.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
mindnlp.transformers.models.van.modeling_van.VanLargeKernelAttentionLayer.forward(hidden_state)
¶
This method forwards an attention mechanism in the VanLargeKernelAttentionLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VanLargeKernelAttentionLayer class.
|
hidden_state |
The hidden state tensor on which the attention mechanism is applied.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The attended tensor resulting from applying attention to the hidden state. |
Source code in mindnlp/transformers/models/van/modeling_van.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
mindnlp.transformers.models.van.modeling_van.VanLayer
¶
Bases: Module
Van layer composed by normalization layers, large kernel attention (LKA) and a multi layer perceptron (MLP).
Source code in mindnlp/transformers/models/van/modeling_van.py
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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
|
mindnlp.transformers.models.van.modeling_van.VanLayer.__init__(config, hidden_size, mlp_ratio=4, drop_path_rate=0.5)
¶
Initializes an instance of the VanLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An object containing configuration settings for the layer.
TYPE:
|
hidden_size |
The size of the hidden layer.
TYPE:
|
mlp_ratio |
The ratio of the hidden size to the output size of the MLP layer. Defaults to 4.
TYPE:
|
drop_path_rate |
The rate at which to apply drop path regularization. Defaults to 0.5.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/van/modeling_van.py
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 484 485 486 487 488 489 490 |
|
mindnlp.transformers.models.van.modeling_van.VanLayer.forward(hidden_state)
¶
Construct method in the VanLayer class.
This method forwards the output tensor by applying a series of operations to the input hidden state.
PARAMETER | DESCRIPTION |
---|---|
self |
Instance of the VanLayer class.
|
hidden_state |
The input hidden state tensor on which the operations are performed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The output tensor after applying the operations on the input hidden state. |
Source code in mindnlp/transformers/models/van/modeling_van.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
|
mindnlp.transformers.models.van.modeling_van.VanLayerScaling
¶
Bases: Module
Scales the inputs by a learnable parameter initialized by initial_value
.
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanLayerScaling.__init__(hidden_size, initial_value=0.01)
¶
Initializes a new instance of the VanLayerScaling class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
hidden_size |
The size of the hidden layer.
TYPE:
|
initial_value |
The initial value for the weight parameter. Default is 0.01.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/van/modeling_van.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
mindnlp.transformers.models.van.modeling_van.VanLayerScaling.forward(hidden_state)
¶
This method is part of the VanLayerScaling class and is used to perform scaling on the hidden_state tensor.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VanLayerScaling class.
TYPE:
|
hidden_state |
The input tensor representing the hidden state. It is expected to be a tensor of type mindspore.Tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: Returns a tensor of type mindspore.Tensor which is the result of scaling the input hidden_state tensor. |
Source code in mindnlp/transformers/models/van/modeling_van.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
mindnlp.transformers.models.van.modeling_van.VanMlpLayer
¶
Bases: Module
MLP with depth-wise convolution, from PVTv2: Improved Baselines with Pyramid Vision Transformer.
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanMlpLayer.__init__(in_channels, hidden_size, out_channels, hidden_act='gelu', dropout_rate=0.5)
¶
Initializes an instance of the VanMlpLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
in_channels |
The number of input channels. This specifies the number of channels in the input tensor.
TYPE:
|
hidden_size |
The size of the hidden layer. This determines the number of output channels of the first convolutional layer.
TYPE:
|
out_channels |
The number of output channels. This specifies the number of channels in the output tensor.
TYPE:
|
hidden_act |
The activation function for the hidden layer. Defaults to 'gelu'. This specifies the activation function to be used in the hidden layer. Supported options are 'gelu', 'relu', 'sigmoid', 'tanh', 'softmax', 'softplus', 'softsign', 'leaky_relu'.
TYPE:
|
dropout_rate |
The dropout rate. Defaults to 0.5. This specifies the probability of an element to be zeroed in the dropout layers.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanMlpLayer.forward(hidden_state)
¶
This method forwards a multi-layer perceptron (MLP) layer in the VanMlpLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VanMlpLayer class.
TYPE:
|
hidden_state |
The input hidden state tensor to be processed by the MLP layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The output tensor after processing through the MLP layer. |
Source code in mindnlp/transformers/models/van/modeling_van.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
mindnlp.transformers.models.van.modeling_van.VanModel
¶
Bases: VanPreTrainedModel
The VanModel class represents a model for processing pixel values using the VanEncoder and providing various output representations. It inherits from the VanPreTrainedModel class and includes methods for initialization and forwarding the model's output. The forwardor initializes the model with the provided configuration, while the forward method processes the pixel values and returns the output representation. The class provides flexibility for handling hidden states and returning output in the form of BaseModelOutputWithPoolingAndNoAttention.
Source code in mindnlp/transformers/models/van/modeling_van.py
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
|
mindnlp.transformers.models.van.modeling_van.VanModel.__init__(config)
¶
Initializes a new instance of the VanModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
The configuration object that contains various settings for the model. This object should have the following attributes:
The config object is required for the proper initialization of the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/van/modeling_van.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 |
|
mindnlp.transformers.models.van.modeling_van.VanModel.forward(pixel_values, output_hidden_states=None, return_dict=None)
¶
Constructs the encoder outputs and pooled output from the given pixel values.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VanModel class.
TYPE:
|
pixel_values |
The input pixel values. If provided, it should be a Tensor.
TYPE:
|
output_hidden_states |
Whether to output hidden states. If None, the value is taken from self.config.output_hidden_states.
TYPE:
|
return_dict |
Whether to return the output as a dictionary. If None, the value is taken from self.config.use_return_dict.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]
|
Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]: A tuple containing the last hidden state and the pooled output, along with the encoder hidden states if return_dict is False. Otherwise, it returns a BaseModelOutputWithPoolingAndNoAttention object. |
Source code in mindnlp/transformers/models/van/modeling_van.py
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
|
mindnlp.transformers.models.van.modeling_van.VanOverlappingPatchEmbedder
¶
Bases: Module
Downsamples the input using a patchify operation with a stride
of 4 by default making adjacent windows overlap by
half of the area. From PVTv2: Improved Baselines with Pyramid Vision
Transformer.
Source code in mindnlp/transformers/models/van/modeling_van.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
mindnlp.transformers.models.van.modeling_van.VanOverlappingPatchEmbedder.__init__(in_channels, hidden_size, patch_size=7, stride=4)
¶
Initializes a VanOverlappingPatchEmbedder object.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
in_channels |
Number of input channels for the convolutional layer.
TYPE:
|
hidden_size |
Number of output channels from the convolutional layer.
TYPE:
|
patch_size |
Size of the patch/kernel for the convolutional layer. Default is 7.
TYPE:
|
stride |
Stride value for the convolution operation. Default is 4.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/van/modeling_van.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
mindnlp.transformers.models.van.modeling_van.VanOverlappingPatchEmbedder.forward(input)
¶
Constructs a hidden state tensor using the provided input tensor.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the VanOverlappingPatchEmbedder class. |
input |
The input tensor to be processed. It should have shape (batch_size, channels, height, width).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The hidden state tensor obtained from the input tensor after applying convolution and normalization. It has the same shape as the input tensor. |
Note
- The 'convolution' method is applied to the input tensor to obtain an intermediate hidden state tensor.
- The 'normalization' method is then applied to the intermediate hidden state tensor to obtain the final hidden state tensor.
Source code in mindnlp/transformers/models/van/modeling_van.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
mindnlp.transformers.models.van.modeling_van.VanPreTrainedModel
¶
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/van/modeling_van.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
|
mindnlp.transformers.models.van.modeling_van.VanSpatialAttentionLayer
¶
Bases: Module
Van spatial attention layer composed by projection (via conv) -> act -> Large Kernel Attention (LKA) attention -> projection (via conv) + residual connection.
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanSpatialAttentionLayer.__init__(hidden_size, hidden_act='gelu')
¶
Initializes an instance of the VanSpatialAttentionLayer class.
PARAMETER | DESCRIPTION |
---|---|
hidden_size |
The size of the hidden layer.
TYPE:
|
hidden_act |
The activation function to be used in the pre_projection layer. Defaults to 'gelu'.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/van/modeling_van.py
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 |
|
mindnlp.transformers.models.van.modeling_van.VanSpatialAttentionLayer.forward(hidden_state)
¶
This method forwards a spatial attention layer in the VanSpatialAttentionLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VanSpatialAttentionLayer class.
|
hidden_state |
The input hidden state tensor to be processed. It represents the feature map of the input data and should be a tensor of shape [batch_size, channels, height, width].
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The processed hidden state tensor after applying the spatial attention mechanism. It has the same shape as the input hidden_state tensor. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input hidden_state tensor is not a valid mindspore.Tensor. |
RuntimeError
|
If an error occurs during the processing of the spatial attention mechanism. |
Source code in mindnlp/transformers/models/van/modeling_van.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
mindnlp.transformers.models.van.modeling_van.VanStage
¶
Bases: Module
VanStage, consisting of multiple layers.
Source code in mindnlp/transformers/models/van/modeling_van.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
mindnlp.transformers.models.van.modeling_van.VanStage.__init__(config, in_channels, hidden_size, patch_size, stride, depth, mlp_ratio=4, drop_path_rate=0.0)
¶
init
Initializes a new instance of the VanStage class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current object instance.
|
config |
An instance of VanConfig class containing configuration parameters.
TYPE:
|
in_channels |
The number of input channels.
TYPE:
|
hidden_size |
The size of the hidden layer.
TYPE:
|
patch_size |
The size of the patch.
TYPE:
|
stride |
The stride for patching.
TYPE:
|
depth |
The depth of the network.
TYPE:
|
mlp_ratio |
The ratio for the multi-layer perceptron. Defaults to 4.
TYPE:
|
drop_path_rate |
The rate for drop path regularization. Defaults to 0.0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If any of the input arguments does not match the expected type. |
ValueError
|
If any of the input arguments does not meet the specified restrictions. |
Source code in mindnlp/transformers/models/van/modeling_van.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
|
mindnlp.transformers.models.van.modeling_van.VanStage.forward(hidden_state)
¶
Constructs the hidden state tensor for the VanStage class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the VanStage class.
|
hidden_state |
A tensor representing the hidden state. It should have a shape of (batch_size, hidden_size, height, width).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: A tensor representing the forwarded hidden state. It has a shape of (batch_size, hidden_size, height, width). |
Source code in mindnlp/transformers/models/van/modeling_van.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
mindnlp.transformers.models.van.modeling_van.drop_path(input, drop_prob=0.0, training=False)
¶
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
Comment by Ross Wightman: This is the same as the DropConnect impl I created for EfficientNet, etc networks, however, the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper... See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use 'survival rate' as the argument.
Source code in mindnlp/transformers/models/van/modeling_van.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|