cvt
mindnlp.transformers.models.cvt.configuration_cvt
¶
CvT model configuration
mindnlp.transformers.models.cvt.configuration_cvt.CvtConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [CvtModel
]. It is used to instantiate a CvT 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 CvT
microsoft/cvt-13 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 |
---|---|
num_channels |
The number of input channels.
TYPE:
|
patch_sizes |
The kernel size of each encoder's patch embedding.
TYPE:
|
patch_stride |
The stride size of each encoder's patch embedding.
TYPE:
|
patch_padding |
The padding size of each encoder's patch embedding.
TYPE:
|
embed_dim |
Dimension of each of the encoder blocks.
TYPE:
|
num_heads |
Number of attention heads for each attention layer in each block of the Transformer encoder.
TYPE:
|
depth |
The number of layers in each encoder block.
TYPE:
|
mlp_ratios |
Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the encoder blocks.
TYPE:
|
attention_drop_rate |
The dropout ratio for the attention probabilities.
TYPE:
|
drop_rate |
The dropout ratio for the patch embeddings probabilities.
TYPE:
|
drop_path_rate |
The dropout probability for stochastic depth, used in the blocks of the Transformer encoder.
TYPE:
|
qkv_bias |
The bias bool for query, key and value in attentions
TYPE:
|
cls_token |
Whether or not to add a classification token to the output of each of the last 3 stages.
TYPE:
|
qkv_projection_method |
The projection method for query, key and value Default is depth-wise convolutions with batch norm. For Linear projection use "avg".
TYPE:
|
kernel_qkv |
The kernel size for query, key and value in attention layer
TYPE:
|
padding_kv |
The padding size for key and value in attention layer
TYPE:
|
stride_kv |
The stride size for key and value in attention layer
TYPE:
|
padding_q |
The padding size for query in attention layer
TYPE:
|
stride_q |
The stride size for query in attention layer
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:
|
Example
>>> from transformers import CvtConfig, CvtModel
...
>>> # Initializing a Cvt msft/cvt style configuration
>>> configuration = CvtConfig()
...
>>> # Initializing a model (with random weights) from the msft/cvt style configuration
>>> model = CvtModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/cvt/configuration_cvt.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 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 |
|
mindnlp.transformers.models.cvt.configuration_cvt.CvtConfig.__init__(num_channels=3, patch_sizes=[7, 3, 3], patch_stride=[4, 2, 2], patch_padding=[2, 1, 1], embed_dim=[64, 192, 384], num_heads=[1, 3, 6], depth=[1, 2, 10], mlp_ratio=[4.0, 4.0, 4.0], attention_drop_rate=[0.0, 0.0, 0.0], drop_rate=[0.0, 0.0, 0.0], drop_path_rate=[0.0, 0.0, 0.1], qkv_bias=[True, True, True], cls_token=[False, False, True], qkv_projection_method=['dw_bn', 'dw_bn', 'dw_bn'], kernel_qkv=[3, 3, 3], padding_kv=[1, 1, 1], stride_kv=[2, 2, 2], padding_q=[1, 1, 1], stride_q=[1, 1, 1], initializer_range=0.02, layer_norm_eps=1e-12, **kwargs)
¶
This method initializes an instance of the CvtConfig class with the provided parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
num_channels |
Number of input channels. Defaults to 3.
TYPE:
|
patch_sizes |
List of patch sizes for each layer. Defaults to [7, 3, 3].
TYPE:
|
patch_stride |
List of patch strides for each layer. Defaults to [4, 2, 2].
TYPE:
|
patch_padding |
List of patch paddings for each layer. Defaults to [2, 1, 1].
TYPE:
|
embed_dim |
List of embedding dimensions for each layer. Defaults to [64, 192, 384].
TYPE:
|
num_heads |
List of the number of attention heads for each layer. Defaults to [1, 3, 6].
TYPE:
|
depth |
List of the depths for each layer. Defaults to [1, 2, 10].
TYPE:
|
mlp_ratio |
List of the MLP ratio for each layer. Defaults to [4.0, 4.0, 4.0].
TYPE:
|
attention_drop_rate |
List of attention dropout rates for each layer. Defaults to [0.0, 0.0, 0.0].
TYPE:
|
drop_rate |
List of dropout rates for each layer. Defaults to [0.0, 0.0, 0.0].
TYPE:
|
drop_path_rate |
List of drop path rates for each layer. Defaults to [0.0, 0.0, 0.1].
TYPE:
|
qkv_bias |
List of booleans indicating whether to include bias for query, key, and value projections for each layer. Defaults to [True, True, True].
TYPE:
|
cls_token |
List of booleans indicating whether to include a class token for each layer. Defaults to [False, False, True].
TYPE:
|
qkv_projection_method |
List of methods for query, key, and value projections for each layer. Defaults to ['dw_bn', 'dw_bn', 'dw_bn'].
TYPE:
|
kernel_qkv |
List of kernel sizes for query, key, and value projections for each layer. Defaults to [3, 3, 3].
TYPE:
|
padding_kv |
List of paddings for key and value projections for each layer. Defaults to [1, 1, 1].
TYPE:
|
stride_kv |
List of strides for key and value projections for each layer. Defaults to [2, 2, 2].
TYPE:
|
padding_q |
List of paddings for query projection for each layer. Defaults to [1, 1, 1].
TYPE:
|
stride_q |
List of strides for query projection for each layer. Defaults to [1, 1, 1].
TYPE:
|
initializer_range |
The range of the initializer. Defaults to 0.02.
TYPE:
|
layer_norm_eps |
Epsilon value for layer normalization. Defaults to 1e-12.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/configuration_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt
¶
MindSpore CvT model.
mindnlp.transformers.models.cvt.modeling_cvt.BaseModelOutputWithCLSToken
dataclass
¶
Bases: ModelOutput
Base class for model's outputs, with potential hidden states and attentions.
PARAMETER | DESCRIPTION |
---|---|
last_hidden_state |
Sequence of hidden-states at the output of the last layer of the model.
TYPE:
|
cls_token_value |
Classification token at the output of the last layer of the model.
TYPE:
|
hidden_states |
Tuple of
TYPE:
|
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtAttention
¶
Bases: Module
This class represents an attention mechanism for the Cvt model. It includes methods for initializing the attention mechanism, pruning specific attention heads, and forwarding the attention output.
ATTRIBUTE | DESCRIPTION |
---|---|
num_heads |
Number of attention heads.
TYPE:
|
embed_dim |
Dimension of the input embeddings.
TYPE:
|
kernel_size |
Size of the convolutional kernel.
TYPE:
|
padding_q |
Padding size for query tensor.
TYPE:
|
padding_kv |
Padding size for key and value tensors.
TYPE:
|
stride_q |
Stride size for query tensor.
TYPE:
|
stride_kv |
Stride size for key and value tensors.
TYPE:
|
qkv_projection_method |
Method for projecting query, key, and value tensors.
TYPE:
|
qkv_bias |
Whether to include bias in query, key, and value projections.
TYPE:
|
attention_drop_rate |
Dropout rate for attention scores.
TYPE:
|
drop_rate |
Dropout rate for output.
TYPE:
|
with_cls_token |
Whether to include a classification token in the input.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the attention mechanism with the given parameters. |
prune_heads |
Prunes specified attention heads based on the provided indices. |
forward |
Constructs the attention output using the input hidden state and spatial dimensions. |
Inherits from
nn.Module
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 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 812 813 814 815 816 817 818 819 820 821 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtAttention.__init__(num_heads, embed_dim, kernel_size, padding_q, padding_kv, stride_q, stride_kv, qkv_projection_method, qkv_bias, attention_drop_rate, drop_rate, with_cls_token=True)
¶
Initializes a CvtAttention instance with the specified parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the CvtAttention class.
TYPE:
|
num_heads |
The number of attention heads to use.
TYPE:
|
embed_dim |
The dimension of the input embeddings.
TYPE:
|
kernel_size |
The size of the convolutional kernel.
TYPE:
|
padding_q |
Padding size for query tensor.
TYPE:
|
padding_kv |
Padding size for key and value tensors.
TYPE:
|
stride_q |
Stride size for query tensor.
TYPE:
|
stride_kv |
Stride size for key and value tensors.
TYPE:
|
qkv_projection_method |
The method used for query, key, value projection.
TYPE:
|
qkv_bias |
Flag indicating whether to include bias in query, key, value projection.
TYPE:
|
attention_drop_rate |
The dropout rate applied to attention weights.
TYPE:
|
drop_rate |
The dropout rate applied to the output.
TYPE:
|
with_cls_token |
Flag indicating whether to include a classification token.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If num_heads is not a positive integer. |
ValueError
|
If embed_dim is not a positive integer. |
ValueError
|
If kernel_size is not a positive integer. |
ValueError
|
If padding_q is not a non-negative integer. |
ValueError
|
If padding_kv is not a non-negative integer. |
ValueError
|
If stride_q is not a positive integer. |
ValueError
|
If stride_kv is not a positive integer. |
ValueError
|
If qkv_projection_method is not a string. |
ValueError
|
If attention_drop_rate is not in the range [0, 1]. |
ValueError
|
If drop_rate is not in the range [0, 1]. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtAttention.forward(hidden_state, height, width)
¶
Constructs an attention output based on the given hidden state, height, and width.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CvtAttention class.
TYPE:
|
hidden_state |
The hidden state used for attention computation.
|
height |
The height of the attention output.
TYPE:
|
width |
The width of the attention output.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtAttention.prune_heads(heads)
¶
This method 'prune_heads' is defined within the class 'CvtAttention' and is used to prune the attention heads based on the provided 'heads' parameter.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the 'CvtAttention' class.
TYPE:
|
heads |
A list containing the indices of attention heads to be pruned. If the list is empty, no pruning is performed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the length of the 'heads' list is invalid or if any of the provided indices are out of range. |
TypeError
|
If the 'heads' parameter is not a list or if any of the internal operations encounter unexpected data types. |
RuntimeError
|
If an unexpected error occurs during the pruning process. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtConvEmbeddings
¶
Bases: Module
Image to Conv Embedding.
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtConvEmbeddings.__init__(patch_size, num_channels, embed_dim, stride, padding)
¶
init
Initializes the CvtConvEmbeddings class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
patch_size |
The size of the patch or kernel used for convolution. If an int is provided, the patch will be square. If a tuple is provided, it should contain two integers representing the height and width of the patch.
TYPE:
|
num_channels |
The number of input channels for the convolutional layer.
TYPE:
|
embed_dim |
The dimensionality of the output embedding.
TYPE:
|
stride |
The stride of the convolution operation. If an int is provided, the same stride is used in both dimensions. If a tuple is provided, it should contain two integers representing the stride in the height and width dimensions.
TYPE:
|
padding |
The amount of padding to be added to the input data for the convolution operation. If an int is provided, the same padding is added to both dimensions. If a tuple is provided, it should contain two integers representing the padding in the height and width dimensions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtConvEmbeddings.forward(pixel_values)
¶
Constructs the pixel embeddings for a given set of pixel values.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CvtConvEmbeddings class.
TYPE:
|
pixel_values |
A tensor containing the pixel values of the image. It should have the shape (batch_size, num_channels, height, width).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method modifies the pixel_values tensor in-place. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtDropPath
¶
Bases: Module
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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.cvt.modeling_cvt.CvtDropPath.__init__(drop_prob=None)
¶
Initializes an instance of the CvtDropPath class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
drop_prob |
The probability of dropping a connection during training. Defaults to None. Must be a float value between 0 and 1, inclusive.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtDropPath.extra_repr()
¶
This method provides a string representation for the CvtDropPath class.
PARAMETER | DESCRIPTION |
---|---|
self |
CvtDropPath instance. Represents the current instance of the CvtDropPath class.
|
RETURNS | DESCRIPTION |
---|---|
str
|
A string representing the drop probability of the CvtDropPath instance.
TYPE:
|
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtDropPath.forward(hidden_states)
¶
This method forwards a modified version of the input hidden_states tensor using the drop_path operation.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtDropPath class.
TYPE:
|
hidden_states |
The input tensor representing hidden states. It should be a tensor of arbitrary shape and type.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: A tensor of the same shape and type as the input hidden_states tensor, but with the drop_path operation applied. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input hidden_states tensor is not a valid mindspore.Tensor object. |
RuntimeError
|
If an error occurs during the drop_path operation. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtEmbeddings
¶
Bases: Module
Construct the CvT embeddings.
Source code in mindnlp/transformers/models/cvt/modeling_cvt.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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtEmbeddings.__init__(patch_size, num_channels, embed_dim, stride, padding, dropout_rate)
¶
Initializes an instance of the CvtEmbeddings class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
patch_size |
The size of the patches to be extracted from the input image.
TYPE:
|
num_channels |
The number of input channels in the image.
TYPE:
|
embed_dim |
The dimension of the embedded representation.
TYPE:
|
stride |
The stride of the convolution operation.
TYPE:
|
padding |
The amount of padding to be added to the input image.
TYPE:
|
dropout_rate |
The dropout rate to be applied to the convolutional embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtEmbeddings.forward(pixel_values)
¶
Constructs the hidden state using convolutional embeddings.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtEmbeddings class.
TYPE:
|
pixel_values |
An array-like object containing pixel values for image data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
numpy.ndarray: The hidden state forwarded using convolutional embeddings. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the pixel_values parameter is empty or not valid. |
TypeError
|
If the pixel_values parameter is not array-like. |
RuntimeError
|
If an unexpected error occurs during the forwardion process. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtEncoder
¶
Bases: Module
This class represents a converter encoder used for converting pixel values to hidden states. It is a subclass of nn.Module.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object for the CvtEncoder.
TYPE:
|
stages |
A list of CvtStage instances representing the stages of the converter encoder.
TYPE:
|
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtEncoder.__init__(config)
¶
Initializes an instance of the CvtEncoder class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object that holds the parameters for the encoder. This object is used to configure the behavior of the encoder. It must be an instance of the Config class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtEncoder.forward(pixel_values, output_hidden_states=False, return_dict=True)
¶
Constructs the CvTEncoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtEncoder class.
TYPE:
|
pixel_values |
The input pixel values.
TYPE:
|
output_hidden_states |
Whether to output hidden states or not. Defaults to False.
TYPE:
|
return_dict |
Whether to return the result as a dictionary or not. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtForImageClassification
¶
Bases: CvtPreTrainedModel
CvtForImageClassification is a class that represents a model for image classification utilizing the Cvt architecture. It inherits from the CvtPreTrainedModel class and provides methods for forwarding the model and computing image classification/regression loss.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
Number of labels for classification
TYPE:
|
cvt |
CvtModel instance used for image processing
TYPE:
|
layernorm |
Layer normalization module
TYPE:
|
classifier |
Classifier module for final predictions
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the CvtForImageClassification model with the provided configuration. |
forward |
Constructs the model and computes loss for image classification. Parameters:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, ImageClassifierOutputWithNoAttention]: Tuple containing loss and output if return_dict is False. Otherwise, returns an ImageClassifierOutputWithNoAttention instance. |
Notes
- The 'forward' method handles the processing of input pixel values, computation of logits, and determination of loss based on the configuration settings.
- The loss calculation depends on the problem type (regression, single_label_classification, or multi_label_classification) and the number of labels.
- The final output includes logits and optionally hidden states depending on the return_dict flag.
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtForImageClassification.__init__(config)
¶
Initializes a new instance of the CvtForImageClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An instance of the class Config containing the configuration settings.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtForImageClassification.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/cvt/modeling_cvt.py
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtIntermediate
¶
Bases: Module
Represents an intermediate layer in a Convolutional Vision Transformer (CVT) network.
This class defines an intermediate layer in a CVT network that consists of a dense layer followed by a GELU activation function. The intermediate layer is used to process the hidden states in the network.
ATTRIBUTE | DESCRIPTION |
---|---|
embed_dim |
The dimension of the input embeddings.
TYPE:
|
mlp_ratio |
The ratio used to determine the size of the hidden layer in the dense layer.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the CvtIntermediate object with the specified embedding dimension and MLP ratio. |
forward |
Constructs the intermediate layer by applying a dense layer and GELU activation function to the input hidden state. |
Inherits from
nn.Module
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtIntermediate.__init__(embed_dim, mlp_ratio)
¶
Initializes an instance of the CvtIntermediate class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
embed_dim |
The dimension of the embedding.
TYPE:
|
mlp_ratio |
The ratio used to calculate the hidden dimension of the MLP.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtIntermediate.forward(hidden_state)
¶
Constructs the hidden state of the CvtIntermediate class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CvtIntermediate class.
TYPE:
|
hidden_state |
The hidden state to be processed. It should be a tensor or array-like object.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method modifies the hidden state in-place. |
This method takes in the 'hidden_state' and applies transformations to it in order to forward the hidden state of the CvtIntermediate class. The 'hidden_state' is first passed through a dense layer using the 'self.dense' function. Then, the resulting tensor is passed through the activation function specified by the 'self.activation' attribute. The modified hidden state is returned as the output of this method.
Note that this method modifies the hidden state in-place and does not create a new object.
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtLayer
¶
Bases: Module
CvtLayer composed by attention layers, normalization and multi-layer perceptrons (mlps).
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtLayer.__init__(num_heads, embed_dim, kernel_size, padding_q, padding_kv, stride_q, stride_kv, qkv_projection_method, qkv_bias, attention_drop_rate, drop_rate, mlp_ratio, drop_path_rate, with_cls_token=True)
¶
Initializes an instance of the CvtLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
num_heads |
The number of attention heads.
TYPE:
|
embed_dim |
The dimensionality of the embedding.
TYPE:
|
kernel_size |
The kernel size for the attention computation.
TYPE:
|
padding_q |
The padding size for queries.
TYPE:
|
padding_kv |
The padding size for key and value.
TYPE:
|
stride_q |
The stride size for queries.
TYPE:
|
stride_kv |
The stride size for key and value.
TYPE:
|
qkv_projection_method |
The method used for query, key, and value projection.
TYPE:
|
qkv_bias |
Whether to include bias in query, key, and value projection.
TYPE:
|
attention_drop_rate |
The dropout rate for attention weights.
TYPE:
|
drop_rate |
The dropout rate for the output tensor.
TYPE:
|
mlp_ratio |
The ratio of the hidden size to the input size in the intermediate layer.
TYPE:
|
drop_path_rate |
The dropout rate for the residual connection.
TYPE:
|
with_cls_token |
Whether to include a classification token.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtLayer.forward(hidden_state, height, width)
¶
This method forwards a layer in the CvtLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtLayer class.
TYPE:
|
hidden_state |
The hidden state of the layer.
TYPE:
|
height |
The height of the input tensor.
TYPE:
|
width |
The width of the input tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the hidden_state is not a valid tensor. |
TypeError
|
If height and width are not integer values. |
RuntimeError
|
If an unexpected error occurs during the execution of the method. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtModel
¶
Bases: CvtPreTrainedModel
CvtModel is a model class that represents a Convolutional Vision Transformer (Cvt) model for processing visual data. This class inherits from CvtPreTrainedModel and provides functionalities for initializing the model, pruning heads, and forwarding the model output.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object for the model.
TYPE:
|
encoder |
The encoder component of the CvtModel responsible for processing input data.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the CvtModel instance with the provided configuration. |
_prune_heads |
Prunes specified heads of the model based on the provided dictionary of layer numbers and heads to prune. |
forward |
Constructs the model output by processing the input pixel values and returning the output hidden states. If pixel_values is not provided, a ValueError is raised. The output format is determined based on the return_dict flag and the model configuration. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtModel.__init__(config, add_pooling_layer=True)
¶
Initializes a new instance of the CvtModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtModel class.
TYPE:
|
config |
The configuration object containing model settings and parameters.
TYPE:
|
add_pooling_layer |
A flag indicating whether to add a pooling layer. Default is True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided config is invalid or missing required parameters. |
TypeError
|
If the provided config is not of the expected type. |
RuntimeError
|
If an error occurs during initialization. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtModel.forward(pixel_values=None, output_hidden_states=None, return_dict=None)
¶
Constructs the CvtModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtModel class.
TYPE:
|
pixel_values |
The pixel values of the input image. Default is None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Default is None.
TYPE:
|
return_dict |
Whether to return a dictionary output. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithCLSToken]
|
Union[Tuple, BaseModelOutputWithCLSToken]: The forwarded model output.
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtOutput
¶
Bases: Module
The 'CvtOutput' class represents a conversion output module that is used in neural network models.
This class inherits from the 'nn.Module' class, which is a base class for all neural network cells in the MindSpore framework.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a new instance of the 'CvtOutput' class. Args:
|
forward |
Constructs the conversion output module by applying operations to the input tensors. Args:
Returns:
|
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtOutput.__init__(embed_dim, mlp_ratio, drop_rate)
¶
Initialize the CvtOutput class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
embed_dim |
The dimension of the embedding.
TYPE:
|
mlp_ratio |
The ratio used to determine the hidden layer size in the MLP.
TYPE:
|
drop_rate |
The dropout rate applied to the output.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtOutput.forward(hidden_state, input_tensor)
¶
Constructs the output of the CvtOutput class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CvtOutput class.
TYPE:
|
hidden_state |
The hidden state tensor. This tensor represents the current state of the model and is used as input for further processing. It should have a shape compatible with the dense layer.
TYPE:
|
input_tensor |
The input tensor. This tensor represents the input data and is added to the hidden state tensor. It should have the same shape as the hidden state tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtPreTrainedModel
¶
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/cvt/modeling_cvt.py
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttention
¶
Bases: Module
This class represents a Convolutional Self-Attention layer for a neural network model. It inherits from the nn.Module class.
ATTRIBUTE | DESCRIPTION |
---|---|
num_heads |
The number of attention heads.
TYPE:
|
embed_dim |
The dimension of the input embeddings.
TYPE:
|
kernel_size |
The size of the convolutional kernel.
TYPE:
|
padding_q |
The amount of padding for the query projection convolution.
TYPE:
|
padding_kv |
The amount of padding for the key and value projection convolutions.
TYPE:
|
stride_q |
The stride for the query projection convolution.
TYPE:
|
stride_kv |
The stride for the key and value projection convolutions.
TYPE:
|
qkv_projection_method |
The projection method used for the query, key, and value projections.
TYPE:
|
qkv_bias |
Indicates whether bias is added to the query, key, and value projections.
TYPE:
|
attention_drop_rate |
The dropout rate for the attention scores.
TYPE:
|
with_cls_token |
Indicates whether a classification token is included in the input.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the CvtSelfAttention instance. |
rearrange_for_multi_head_attention |
Rearranges the input hidden state for multi-head attention computations. |
forward |
Constructs the CvtSelfAttention layer by performing convolutional projections, multi-head attention calculations, and output rearrangement. |
Note
- The CvtSelfAttention layer assumes that the input hidden state is a 4D tensor with shape (batch_size, hidden_size, height, width).
- The attention_score and attention_probs computations make use of the Einstein summation convention (einsum).
- The context output is a 3D tensor with shape (batch_size, hidden_size, num_heads * head_dim).
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttention.__init__(num_heads, embed_dim, kernel_size, padding_q, padding_kv, stride_q, stride_kv, qkv_projection_method, qkv_bias, attention_drop_rate, with_cls_token=True, **kwargs)
¶
init
Initializes the CvtSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
num_heads |
The number of attention heads.
TYPE:
|
embed_dim |
The dimension of the input embeddings.
TYPE:
|
kernel_size |
The size of the convolutional kernel.
TYPE:
|
padding_q |
The padding size for the query projection.
TYPE:
|
padding_kv |
The padding size for the key and value projections.
TYPE:
|
stride_q |
The stride for the query projection.
TYPE:
|
stride_kv |
The stride for the key and value projections.
TYPE:
|
qkv_projection_method |
The method used for query, key, and value projections. Can be 'avg' or any other specific projection method.
TYPE:
|
qkv_bias |
Indicates whether bias is applied to the query, key, and value projections.
TYPE:
|
attention_drop_rate |
The dropout rate for attention weights.
TYPE:
|
with_cls_token |
Indicates whether the class token is included. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If embed_dim is not a positive integer. |
ValueError
|
If num_heads is not a positive integer. |
ValueError
|
If kernel_size, padding_q, padding_kv, stride_q, or stride_kv is not a positive integer. |
ValueError
|
If qkv_projection_method is not 'avg' or a valid specific projection method. |
ValueError
|
If attention_drop_rate is not in the range [0, 1]. |
TypeError
|
If with_cls_token is not a boolean value. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttention.forward(hidden_state, height, width)
¶
Constructs the self-attention context for the CvtSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CvtSelfAttention class.
|
hidden_state |
The hidden state tensor of shape (batch_size, hidden_size, num_channels). It represents the input features.
TYPE:
|
height |
The height of the hidden state tensor.
TYPE:
|
width |
The width of the hidden state tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
The context tensor of shape (batch_size, hidden_size, num_heads * head_dim). It represents the output context after applying self-attention mechanism. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttention.rearrange_for_multi_head_attention(hidden_state)
¶
In the class CvtSelfAttention, this method rearranges the hidden state tensor for multi-head attention computation.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtSelfAttention class. This parameter is required for accessing the attributes and methods of the class.
TYPE:
|
hidden_state |
The input hidden state tensor of shape (batch_size, hidden_size, _).
This tensor represents the input hidden state that needs to be rearranged for multi-head attention computation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. It rearranges the hidden state tensor in place and does not create a new tensor. |
RAISES | DESCRIPTION |
---|---|
None
|
This method does not explicitly raise any exceptions. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionConvProjection
¶
Bases: Module
CvtSelfAttentionConvProjection represents a class for performing convolution and normalization operations on input data. This class inherits from nn.Module and provides methods for initializing the convolution and normalization layers, as well as for forwarding the output from the input hidden state.
ATTRIBUTE | DESCRIPTION |
---|---|
embed_dim |
The dimension of the input embedding.
TYPE:
|
kernel_size |
The size of the convolutional kernel.
TYPE:
|
padding |
The amount of padding to apply to the input data.
TYPE:
|
stride |
The stride of the convolution operation.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the CvtSelfAttentionConvProjection class with the specified parameters. |
forward |
Constructs the output from the input hidden state by applying convolution and normalization operations. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionConvProjection.__init__(embed_dim, kernel_size, padding, stride)
¶
Initializes a new instance of the CvtSelfAttentionConvProjection class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself. |
embed_dim |
The number of channels in the input and output tensors.
TYPE:
|
kernel_size |
The size of the convolving kernel.
TYPE:
|
padding |
The amount of padding added to the input.
TYPE:
|
stride |
The stride of the convolution.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionConvProjection.forward(hidden_state)
¶
Constructs a hidden state using convolution, normalization, and projection in the CvtSelfAttentionConvProjection class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CvtSelfAttentionConvProjection class. |
hidden_state |
The input hidden state.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionLinearProjection
¶
Bases: Module
The 'CvtSelfAttentionLinearProjection' class is a Python class that inherits from the 'nn.Module' class. It represents a linear projection operation applied to hidden states in a self-attention mechanism.
METHOD | DESCRIPTION |
---|---|
forward |
Applies a linear projection to the input hidden state. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionLinearProjection.forward(hidden_state)
¶
Constructs a linear projection of hidden state for self-attention in the CvtSelfAttentionLinearProjection class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtSelfAttentionLinearProjection class. |
hidden_state |
The hidden state tensor with shape (batch_size, num_channels, height, width), where batch_size is the number of samples in the batch, num_channels is the number of channels, height is the height of the hidden state tensor, and width is the width of the hidden state tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
torch.Tensor: The linearly projected hidden state tensor with shape (batch_size, hidden_size, num_channels), where batch_size is the number of samples in the batch, hidden_size is the product of height and width of the hidden state tensor, and num_channels is the number of channels. The tensor is permuted to have the dimensions (batch_size, hidden_size, num_channels). |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionProjection
¶
Bases: Module
A class representing the projection layer for self-attention in a Convolutional Transformer network.
This class is responsible for projecting the input hidden state using convolutional and linear projections. It provides methods to initialize the projections and apply them sequentially to the input hidden state.
ATTRIBUTE | DESCRIPTION |
---|---|
embed_dim |
The dimensionality of the input embeddings.
TYPE:
|
kernel_size |
The size of the convolutional kernel.
TYPE:
|
padding |
The amount of padding to apply during convolution.
TYPE:
|
stride |
The stride of the convolution operation.
TYPE:
|
projection_method |
The method used for projection, default is 'dw_bn' (depthwise batch normalization).
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the projection layer with the specified parameters. |
forward |
Applies the convolutional projection followed by the linear projection to the input hidden state. Returns the projected hidden state. |
Note
This class inherits from nn.Module and is designed to be used within a Convolutional Transformer network.
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionProjection.__init__(embed_dim, kernel_size, padding, stride, projection_method='dw_bn')
¶
Initializes an instance of the CvtSelfAttentionProjection class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class. |
embed_dim |
The dimensionality of the input embeddings.
TYPE:
|
kernel_size |
The size of the convolutional kernel.
TYPE:
|
padding |
The amount of padding to be added to the input.
TYPE:
|
stride |
The stride value for the convolution operation.
TYPE:
|
projection_method |
The method used for projection. Defaults to 'dw_bn'.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfAttentionProjection.forward(hidden_state)
¶
Constructs the self-attention projection for the CvtSelfAttentionProjection class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtSelfAttentionProjection class. |
hidden_state |
The hidden state tensor to be projected.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
The method modifies the hidden_state in-place after applying convolution and linear projections. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfOutput
¶
Bases: Module
The residual connection is defined in CvtLayer instead of here (as is the case with other models), due to the layernorm applied before each block.
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
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 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfOutput.__init__(embed_dim, drop_rate)
¶
Initializes an instance of the CvtSelfOutput class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
embed_dim |
The dimension of the embedding.
TYPE:
|
drop_rate |
The dropout rate to be applied.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtSelfOutput.forward(hidden_state, input_tensor)
¶
Constructs the output of the CvtSelfOutput class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CvtSelfOutput class.
TYPE:
|
hidden_state |
The hidden state to be processed. This tensor represents the current state of the model and is expected to have shape (batch_size, hidden_size). It serves as input to the dense layer and will be transformed.
TYPE:
|
input_tensor |
The input tensor to the method. This tensor represents additional input to the forward method and can be of any shape.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtStage
¶
Bases: Module
The CvtStage class represents a stage in the Cross Vision Transformer (Cvt) model. It inherits from nn.Module and is designed to handle the processing and transformation of input data within a specific stage of the Cvt model.
This class includes methods for initializing the stage with configuration and stage information, as well as forwarding the hidden state through a series of operations involving embeddings, layer processing, and token manipulation.
The class supports the configuration of parameters such as patch size, stride, number of channels, embedding dimensions, padding, dropout rates, depth, number of heads, kernel size, attention and multi-layer perceptron (MLP) settings, and the inclusion of a classification (cls) token.
The forward method is responsible for processing the hidden state by applying the configured embeddings, manipulating the hidden state based on the existence of a cls token, and iterating through the layers to transform the hidden state. Additionally, it handles the splitting and reshaping of the hidden state before returning the updated hidden state and cls token.
Overall, the CvtStage class provides a structured and configurable framework for managing the transformation of data within a specific stage of the Cvt model.
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtStage.__init__(config, stage)
¶
This method initializes an instance of the CvtStage class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtStage class.
|
config |
The configuration object containing various parameters such as patch size, stride, number of channels, embedding dimensions, padding, dropout rate, depth, number of heads, kernel size, padding for query, key, and value, stride for key and value, stride for query, method for QKV projection, QKV bias, attention dropout rate, drop rate, drop path rate, MLP ratio, and presence of a classification token.
TYPE:
|
stage |
The stage of the CvtStage.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the config.cls_token[self.stage] does not exist or is not a valid value. |
TypeError
|
If the config.drop_path_rate[self.stage] is not a valid type. |
IndexError
|
If the drop_path_rates[self.stage] does not exist or is not a valid index. |
TypeError
|
If any of the parameters in the CvtLayer instantiation are of an invalid type. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 |
|
mindnlp.transformers.models.cvt.modeling_cvt.CvtStage.forward(hidden_state)
¶
Constructs the hidden state for the CvtStage class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CvtStage class.
TYPE:
|
hidden_state |
The hidden state input for forwarding the hidden state. It should be a tensor.
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the forwarded hidden state and cls_token. The hidden state is a tensor with dimensions (batch_size, num_channels, height, width), representing the forwarded hidden state. The cls_token is a tensor with dimensions (batch_size, 1, num_channels), representing the cls_token if it exists, otherwise it is None. |
Source code in mindnlp/transformers/models/cvt/modeling_cvt.py
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 |
|
mindnlp.transformers.models.cvt.modeling_cvt.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/cvt/modeling_cvt.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|