timesformer
mindnlp.transformers.models.timesformer.configuration_timesformer
¶
TimeSformer model configuration
mindnlp.transformers.models.timesformer.configuration_timesformer.TimesformerConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [TimesformerModel
]. It is used to instantiate a
TimeSformer 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 TimeSformer
facebook/timesformer-base-finetuned-k600
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:
|
patch_size |
The size (resolution) of each patch.
TYPE:
|
num_channels |
The number of input channels.
TYPE:
|
num_frames |
The number of frames in each video.
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" (i.e., 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:
|
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:
|
qkv_bias |
Whether to add a bias to the queries, keys and values.
TYPE:
|
attention_type |
The attention type to use. Must be one of
TYPE:
|
drop_path_rate |
The dropout ratio for stochastic depth.
TYPE:
|
Example
>>> from transformers import TimesformerConfig, TimesformerModel
...
>>> # Initializing a TimeSformer timesformer-base style configuration
>>> configuration = TimesformerConfig()
...
>>> # Initializing a model from the configuration
>>> model = TimesformerModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/timesformer/configuration_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.configuration_timesformer.TimesformerConfig.__init__(image_size=224, patch_size=16, num_channels=3, num_frames=8, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, hidden_act='gelu', hidden_dropout_prob=0.0, attention_probs_dropout_prob=0.0, initializer_range=0.02, layer_norm_eps=1e-06, qkv_bias=True, attention_type='divided_space_time', drop_path_rate=0, **kwargs)
¶
Initializes an instance of the TimesformerConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The TimesformerConfig object itself.
TYPE:
|
image_size |
The size of the input image. Defaults to 224.
TYPE:
|
patch_size |
The size of each patch in the input image. Defaults to 16.
TYPE:
|
num_channels |
The number of channels in the input image. Defaults to 3.
TYPE:
|
num_frames |
The number of frames in a video sequence. Defaults to 8.
TYPE:
|
hidden_size |
The size of the hidden state in the transformer layers. Defaults to 768.
TYPE:
|
num_hidden_layers |
The number of hidden layers in the transformer. Defaults to 12.
TYPE:
|
num_attention_heads |
The number of attention heads in the transformer. Defaults to 12.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the transformer. 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.0.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability for the attention layer. Defaults to 0.0.
TYPE:
|
initializer_range |
The range for the initializer. Defaults to 0.02.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization. Defaults to 1e-06.
TYPE:
|
qkv_bias |
Whether to include bias terms in the qkv layers. Defaults to True.
TYPE:
|
attention_type |
The type of attention used in the transformer. Defaults to 'divided_space_time'.
TYPE:
|
drop_path_rate |
The drop path rate for stochastic depth. Defaults to 0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/timesformer/configuration_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer
¶
MindSpore TimeSformer model.
mindnlp.transformers.models.timesformer.modeling_timesformer.TimeSformerAttention
¶
Bases: Module
The TimeSformerAttention class represents the self-attention mechanism for the TimeSformer model. It inherits from the nn.Module class and is responsible for performing self-attention and generating attention-based outputs.
The class contains the following methods:
- init: Initializes the TimeSformerAttention instance with the provided configuration.
- forward: Constructs the self-attention mechanism using the provided hidden states and optionally returns attention outputs.
This class is an essential component of the TimeSformer model, providing the functionality for self-attention computations and output generation.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimeSformerAttention.__init__(config)
¶
Initializes a new instance of the TimeSformerAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the TimeSformerAttention class.
|
config |
The configuration object specifying the settings for the TimeSformerAttention.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
This method initializes the TimeSformerAttention class by setting up the attention and output layers. The 'self' parameter refers to the current instance of the class, while the 'config' parameter is an instance of the TimesformerConfig class that specifies the configuration settings for the TimeSformerAttention.
The 'attention' attribute is assigned an instance of the TimesformerSelfAttention class, which handles the attention mechanism. The 'output' attribute is assigned an instance of the TimesformerSelfOutput class, which processes the attention output.
Example
>>> config = TimesformerConfig()
>>> time_sformer_attention = TimeSformerAttention(config)
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimeSformerAttention.forward(hidden_states, output_attentions=False)
¶
This method forwards the attention mechanism for the TimeSformer model.
PARAMETER | DESCRIPTION |
---|---|
self |
This parameter refers to the instance of the class itself.
|
hidden_states |
The input hidden states on which the attention mechanism is applied.
TYPE:
|
output_attentions |
A flag indicating whether to return the attention outputs. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor, Tensor], Tuple[Tensor]]
|
Union[Tuple[mindspore.Tensor, mindspore.Tensor], Tuple[mindspore.Tensor]]:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the hidden_states tensor is not of the expected format or shape. |
TypeError
|
If the input arguments are not of the expected types. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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.timesformer.modeling_timesformer.TimeSformerDropPath
¶
Bases: Module
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimeSformerDropPath.__init__(drop_prob=None)
¶
Initializes an instance of the TimeSformerDropPath class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
drop_prob |
The probability of dropping a path during training. Default is None. A floating-point number between 0 and 1 representing the probability of dropping a path.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimeSformerDropPath.extra_repr()
¶
Returns a string representation of the TimeSformerDropPath object.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the TimeSformerDropPath class.
|
RETURNS | DESCRIPTION |
---|---|
string
|
A string representing the TimeSformerDropPath object. The string is formatted as 'p={}' where '{}' is replaced with the value of the 'drop_prob' attribute of the current instance.
TYPE:
|
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimeSformerDropPath.forward(hidden_states)
¶
Method to apply drop path regularization to the hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TimeSformerDropPath class.
TYPE:
|
hidden_states |
The hidden states tensor to apply drop path regularization to.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The hidden states tensor after applying drop path regularization.
|
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerEmbeddings
¶
Bases: Module
Construct the patch and position embeddings.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerEmbeddings.__init__(config)
¶
Initialize the TimesformerEmbeddings instance with the given configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
An object containing the configuration parameters for the TimesformerEmbeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerEmbeddings.forward(pixel_values)
¶
Constructs the embeddings for the Timesformer model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TimesformerEmbeddings class.
|
pixel_values |
A tensor of shape (batch_size, num_frames, patch_height, patch_width) representing the input pixel values.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
torch.Tensor: A tensor of shape (batch_size, total_patches, embedding_dim) representing the forwarded embeddings. |
This method takes in a tensor of pixel values and forwards the embeddings for the Timesformer model.
The input tensor is expected to have dimensions (batch_size, num_frames, patch_height, patch_width).
The method first computes patch embeddings using the patch_embeddings
function. It then adds a special token
(cls_token
) to the embeddings. If the shape of the embeddings does not match the shape of the position
embeddings, the method adjusts the position embeddings to match the shape of the embeddings.
The method then adds the adjusted position embeddings to the embeddings. The embeddings are
then passed through a dropout layer (pos_drop
).
If the attention type is not 'space_only', the method separates the cls_token
from the embeddings, converts
the embeddings to the desired shape, and adds time embeddings to the embeddings. The time embeddings are
adjusted to match the number of frames in the input tensor. The adjusted time embeddings are added to the
embeddings. The embeddings are then passed through a dropout layer (time_drop
).
Finally, the embeddings are reshaped and the cls_token
is concatenated back to the embeddings.
The method returns the forwarded embeddings.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerEncoder
¶
Bases: Module
The TimesformerEncoder class represents a Timesformer encoder module that is used for encoding input sequences. It inherits from the nn.Module class.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object that specifies the hyperparameters of the Timesformer encoder.
TYPE:
|
layer |
A list of TimesformerLayer instances that make up the encoder's layers.
TYPE:
|
Note
- The TimesformerEncoder is composed of multiple TimesformerLayer instances.
- The hidden states and attention weights can be optionally returned for each layer.
- The output can be returned either as a tuple or as a BaseModelOutput dictionary.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerEncoder.__init__(config)
¶
Initializes the TimesformerEncoder object with the given configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TimesformerEncoder class.
TYPE:
|
config |
The configuration object containing the settings for the TimesformerEncoder.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerEncoder.forward(hidden_states, output_attentions=False, output_hidden_states=False, return_dict=True)
¶
Constructs the TimesformerEncoder.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TimesformerEncoder class.
TYPE:
|
hidden_states |
The input hidden states. Expected shape is (batch_size, sequence_length, hidden_size).
TYPE:
|
output_attentions |
Whether to output the attention weights of each layer. Defaults to False.
TYPE:
|
output_hidden_states |
Whether to output the hidden states of each layer. Defaults to False.
TYPE:
|
return_dict |
Whether to return the output as a dictionary. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[tuple, BaseModelOutput]
|
Union[tuple, BaseModelOutput]: The output of the TimesformerEncoder. If return_dict is True, returns a dictionary with the following keys:
|
Union[tuple, BaseModelOutput]
|
If return_dict is False, returns a tuple containing only the non-None values from the dictionary. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerForVideoClassification
¶
Bases: TimesformerPreTrainedModel
TimesformerForVideoClassification
This class is a video classification model based on the Timesformer architecture. It inherits from the TimesformerPreTrainedModel class.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
The number of labels for classification.
TYPE:
|
timesformer |
The Timesformer model for video classification.
TYPE:
|
classifier |
The classifier layer for the model.
TYPE:
|
config |
The configuration object for the model.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the TimesformerForVideoClassification instance. |
forward |
Constructs the model and computes the loss and output. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerForVideoClassification.__init__(config)
¶
Initializes a new instance of the TimesformerForVideoClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
config |
An object containing configuration parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
AttributeError
|
If the 'config' object does not contain the required attributes. |
TypeError
|
If the 'num_labels' or 'hidden_size' attributes in 'config' are not integers. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerForVideoClassification.forward(pixel_values=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the image classification/regression loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, ImageClassifierOutput]
|
Union[Tuple, ImageClassifierOutput] |
Example
>>> import av
>>> import mindspore
>>> import mindnlp
>>> import numpy as np
...
>>> from mindnlp.transformers import AutoImageProcessor, TimesformerForVideoClassification
>>> from huggingface_hub import hf_hub_download
...
>>> np.random.seed(0)
...
...
>>> def read_video_pyav(container, indices):
... '''
... Decode the video with PyAV decoder.
... Args:
... container (`av.container.input.InputContainer`): PyAV container.
... indices (`List[int]`): List of frame indices to decode.
... Returns:
... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
... '''
... frames = []
... container.seek(0)
... start_index = indices[0]
... end_index = indices[-1]
... for i, frame in enumerate(container.decode(video=0)):
... if i > end_index:
... break
... if i >= start_index and i in indices:
... frames.append(frame)
... return np.stack([x.to_ndarray(format="rgb24") for x in frames])
...
...
>>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len):
... '''
... Sample a given number of frame indices from the video.
... Args:
... clip_len (`int`): Total number of frames to sample.
... frame_sample_rate (`int`): Sample every n-th frame.
... seg_len (`int`): Maximum allowed index of sample's last frame.
... Returns:
... indices (`List[int]`): List of sampled frame indices
... '''
... converted_len = int(clip_len * frame_sample_rate)
... end_idx = np.random.randint(converted_len, seg_len)
... start_idx = end_idx - converted_len
... indices = np.linspace(start_idx, end_idx, num=clip_len)
... indices = np.clip(indices, start_idx, end_idx - 1).astype(np.int64)
... return indices
...
...
>>> # video clip consists of 300 frames (10 seconds at 30 FPS)
>>> file_path = hf_hub_download(
... repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
... )
>>> container = av.open(file_path)
...
>>> # sample 8 frames
>>> indices = sample_frame_indices(clip_len=8, frame_sample_rate=1, seg_len=container.streams.video[0].frames)
>>> video = read_video_pyav(container, indices)
...
>>> image_processor = AutoImageProcessor.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics")
>>> model = TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400")
...
>>> inputs = image_processor(list(video), return_tensors="ms")
...
>>> outputs = model(**inputs)
>>> logits = outputs.logits
...
>>> # model predicts one of the 400 Kinetics-400 classes
>>> predicted_label = logits.argmax(-1).item()
>>> print(model.config.id2label[predicted_label])
eating spaghetti
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerIntermediate
¶
Bases: Module
The TimesformerIntermediate class represents a component of the Timesformer model that performs intermediate computations on the input hidden states. This class inherits from the nn.Module class.
ATTRIBUTE | DESCRIPTION |
---|---|
dense |
A dense layer used for transforming the input hidden states.
TYPE:
|
dropout |
A dropout layer with a dropout probability specified in the configuration.
TYPE:
|
intermediate_act_fn |
The activation function applied to the intermediate hidden states.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the TimesformerIntermediate instance with the provided configuration. |
forward |
Performs intermediate computations on the input hidden states and returns the result. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerIntermediate.__init__(config)
¶
Initializes a new instance of the TimesformerIntermediate class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TimesformerIntermediate class.
|
config |
An instance of the TimesformerConfig class containing configuration parameters for the TimesformerIntermediate class. It specifies the hidden size, intermediate size, and hidden dropout probability.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type TimesformerConfig. |
KeyError
|
If the config.hidden_act is not a valid string key in the ACT2FN dictionary. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerIntermediate.forward(hidden_states)
¶
This method forwards the intermediate representation for the Timesformer model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TimesformerIntermediate class.
|
hidden_states |
The input hidden states to be processed. It should be a Tensor object containing the hidden states data required for intermediate representation forwardion.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: Returns a Tensor object representing the intermediate representation forwarded using the input hidden states data. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input hidden_states is not a valid mindspore.Tensor object. |
RuntimeError
|
If any error occurs during the intermediate representation forwardion process. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerLayer
¶
Bases: Module
This class represents a Timesformer layer, which is a component of the Timesformer model. The Timesformer layer includes various operations such as attention, intermediate, and output layers. It supports different attention types, including divided space-time, space only, and joint space-time.
The TimesformerLayer class inherits from the nn.Module class.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object for the Timesformer model.
TYPE:
|
attention_type |
The type of attention used in the layer. Valid values are 'divided_space_time', 'space_only', and 'joint_space_time'.
TYPE:
|
drop_path |
The dropout layer used for drop path regularization.
TYPE:
|
attention |
The attention module used in the layer.
TYPE:
|
intermediate |
The intermediate module used in the layer.
TYPE:
|
output |
The output module used in the layer.
TYPE:
|
layernorm_before |
The layer normalization module applied before the attention operation.
TYPE:
|
layernorm_after |
The layer normalization module applied after the attention operation.
TYPE:
|
temporal_layernorm |
The layer normalization module applied to temporal embeddings in case of 'divided_space_time' attention type.
TYPE:
|
temporal_attention |
The attention module applied to temporal embeddings in case of 'divided_space_time' attention type.
TYPE:
|
temporal_dense |
The dense layer applied to temporal embeddings in case of 'divided_space_time' attention type.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the TimesformerLayer object with the given configuration and layer index. Sets up the various modules and attributes of the layer. |
forward |
Constructs the TimesformerLayer by applying the attention and intermediate operations to the given hidden states. Returns the output hidden states and optionally the attention outputs. |
Note
- The TimesformerLayer class assumes that the following modules are defined: TimeSformerDropPath, TimeSformerAttention, TimesformerIntermediate, and TimesformerOutput.
- The forward method assumes that the following operations are defined: mindspore.Tensor.shape, ops.linspace, ops.cat, ops.mean, ops.reshape, ops.tile, and ops.permute.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided attention type is not one of the valid options. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerLayer.__init__(config, layer_index)
¶
Initializes a TimesformerLayer instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The TimesformerLayer instance.
|
config |
The configuration object for the Timesformer model.
TYPE:
|
layer_index |
The index of the layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the attention_type in the config is not one of ['divided_space_time', 'space_only', 'joint_space_time']. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerLayer.forward(hidden_states, output_attentions=False)
¶
Construct a Timesformer layer.
PARAMETER | DESCRIPTION |
---|---|
self |
The TimesformerLayer instance.
TYPE:
|
hidden_states |
The input hidden states tensor of shape (batch_size, sequence_length, hidden_size).
TYPE:
|
output_attentions |
Whether to output attentions. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the attention_type is not one of ['space_only', 'joint_space_time', 'divided_space_time']. |
ValueError
|
If the shape of the hidden_states tensor is not as expected. |
ValueError
|
If the shape of the temporal_embedding tensor is not as expected. |
ValueError
|
If the shape of the spatial_embedding tensor is not as expected. |
ValueError
|
If the shape of the cls_token tensor is not as expected. |
ValueError
|
If the shape of the residual_spatial tensor is not as expected. |
ValueError
|
If the shape of the hidden_states tensor after operations is not as expected. |
ValueError
|
If the shape of the layer_output tensor is not as expected. |
ValueError
|
If the shape of the outputs tuple is not as expected. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerModel
¶
Bases: TimesformerPreTrainedModel
Represents a Timesformer model.
This class inherits from TimesformerPreTrainedModel and implements the Timesformer model architecture for processing video data. It includes methods for initializing the model, getting input embeddings, pruning heads, and forwarding the model output.
The init method initializes the TimesformerModel with the provided configuration. The get_input_embeddings method returns the patch embeddings from the model's embeddings. The _prune_heads method prunes heads of the model based on the provided heads_to_prune dictionary. The forward method processes the input pixel values and returns the model output, with options to include attentions and hidden states in the returned dictionary.
The class also contains additional methods and attributes inherited from the base class TimesformerPreTrainedModel.
Example usage and explanations are provided within the docstring for reference and clarity.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerModel.__init__(config)
¶
Initializes a new instance of the TimesformerModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TimesformerModel class.
|
config |
A dictionary containing configuration parameters for the model. This dictionary should include the necessary settings for configuring the model. Example fields include 'hidden_size', 'layer_norm_eps', etc.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not provided or is not of type dict. |
AttributeError
|
If any required field is missing in the config dictionary. |
ValueError
|
If the provided configuration settings are invalid or inconsistent. |
RuntimeError
|
If there are issues during the initialization process of the model components. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerModel.forward(pixel_values, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], BaseModelOutput]
|
Union[Tuple[mindspore.Tensor], BaseModelOutput]: |
Example
>>> import av
>>> import numpy as np
...
>>> from transformers import AutoImageProcessor, TimesformerModel
>>> from huggingface_hub import hf_hub_download
...
>>> np.random.seed(0)
...
>>> def read_video_pyav(container, indices):
... '''
... Decode the video with PyAV decoder.
... Args:
... container (`av.container.input.InputContainer`): PyAV container.
... indices (`List[int]`): List of frame indices to decode.
... Returns:
... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
... '''
... frames = []
... container.seek(0)
... start_index = indices[0]
... end_index = indices[-1]
... for i, frame in enumerate(container.decode(video=0)):
... if i > end_index:
... break
... if i >= start_index and i in indices:
... frames.append(frame)
... return np.stack([x.to_ndarray(format="rgb24") for x in frames])
...
...
>>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len):
... '''
... Sample a given number of frame indices from the video.
... Args:
... clip_len (`int`): Total number of frames to sample.
... frame_sample_rate (`int`): Sample every n-th frame.
... seg_len (`int`): Maximum allowed index of sample's last frame.
... Returns:
... indices (`List[int]`): List of sampled frame indices
... '''
... converted_len = int(clip_len * frame_sample_rate)
... end_idx = np.random.randint(converted_len, seg_len)
... start_idx = end_idx - converted_len
... indices = np.linspace(start_idx, end_idx, num=clip_len)
... indices = np.clip(indices, start_idx, end_idx - 1).astype(np.int64)
... return indices
...
...
>>> # video clip consists of 300 frames (10 seconds at 30 FPS)
>>> file_path = hf_hub_download(
... repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
... )
>>> container = av.open(file_path)
...
>>> # sample 8 frames
>>> indices = sample_frame_indices(clip_len=8, frame_sample_rate=4, seg_len=container.streams.video[0].frames)
>>> video = read_video_pyav(container, indices)
...
>>> image_processor = AutoImageProcessor.from_pretrained("MCG-NJU/videomae-base")
>>> model = TimesformerModel.from_pretrained("facebook/timesformer-base-finetuned-k400")
...
>>> # prepare video for the model
>>> inputs = image_processor(list(video), return_tensors="pt")
...
>>> # forward pass
>>> outputs = model(**inputs)
>>> last_hidden_states = outputs.last_hidden_state
>>> list(last_hidden_states.shape)
[1, 1569, 768]
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerModel.get_input_embeddings()
¶
Retrieve the input embeddings for the TimesformerModel.
PARAMETER | DESCRIPTION |
---|---|
self |
TimesformerModel The instance of the TimesformerModel class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerOutput
¶
Bases: Module
TimesformerOutput represents the output of the Timesformer model, containing methods for processing hidden states.
This class inherits from nn.Module and provides functionality for processing hidden states through dense and dropout layers.
ATTRIBUTE | DESCRIPTION |
---|---|
dense |
A dense layer used for transforming hidden states.
TYPE:
|
dropout |
A dropout layer used for regularization.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the TimesformerOutput object with the provided configuration. |
forward |
Applies dense and dropout layers to the input hidden states. |
Example
>>> config = TimesformerConfig(intermediate_size=1024, hidden_size=512, hidden_dropout_prob=0.1)
>>> output = TimesformerOutput(config)
>>> processed_states = output.forward(hidden_states)
Note
The TimesformerOutput class is designed to work in conjunction with the Timesformer model for processing hidden states efficiently.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerOutput.__init__(config)
¶
Initializes a new instance of TimesformerOutput.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object for the Timesformer model, specifying the model's parameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerOutput.forward(hidden_states)
¶
This method 'forward' is defined within the class 'TimesformerOutput' and is used to process the input 'hidden_states' through a series of operations and return the resulting tensor.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TimesformerOutput class.
TYPE:
|
hidden_states |
The input tensor containing the hidden states. It is expected to be of type mindspore.Tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The processed tensor after applying the dense and dropout operations. |
RAISES | DESCRIPTION |
---|---|
None
|
This method does not explicitly raise any exceptions. However, it is important to note that the operations performed within this method may raise exceptions related to tensor manipulation in the mindspore library. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerPatchEmbeddings
¶
Bases: Module
Image to Patch Embedding
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerPatchEmbeddings.__init__(config)
¶
Initializes an instance of the TimesformerPatchEmbeddings class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An object containing configuration parameters for TimesformerPatchEmbeddings.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerPatchEmbeddings.forward(pixel_values)
¶
forward method in TimesformerPatchEmbeddings class.
This method forwards patch embeddings from the input pixel_values.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TimesformerPatchEmbeddings class. |
pixel_values |
A 5-dimensional tensor representing the pixel values of the input images, with dimensions (batch_size, num_frames, num_channels, height, width).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the following values:
|
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerPreTrainedModel
¶
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/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerSelfAttention
¶
Bases: Module
This class represents the self-attention mechanism used in the Timesformer model. It is a subclass of nn.Module.
ATTRIBUTE | DESCRIPTION |
---|---|
num_heads |
The number of attention heads.
TYPE:
|
scale |
The scaling factor applied to the attention scores.
TYPE:
|
qkv |
The fully connected layer used to compute the query, key, and value representations.
TYPE:
|
attn_drop |
The dropout layer applied to the attention scores.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the TimesformerSelfAttention instance. |
forward |
Applies self-attention mechanism to the input hidden states. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerSelfAttention.__init__(config)
¶
Initializes a new instance of the TimesformerSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current object instance.
|
config |
The configuration object for Timesformer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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.timesformer.modeling_timesformer.TimesformerSelfAttention.forward(hidden_states, output_attentions=False)
¶
Constructs the self-attention mechanism within a Timesformer model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TimesformerSelfAttention class.
TYPE:
|
hidden_states |
The input hidden states to be processed by the self-attention mechanism. Expected shape is (batch_size, hidden_size, num_channels).
TYPE:
|
output_attentions |
Flag indicating whether to output attention probabilities. Default is False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the context layer tensor and optionally the attention probabilities tensor.
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input hidden_states tensor does not have the expected shape. |
RuntimeError
|
If an error occurs during the Softmax calculation or reshaping operations. |
AttributeError
|
If an attribute error is encountered while accessing class properties. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerSelfOutput
¶
Bases: Module
The residual connection is defined in TimesformerLayer instead of here (as is the case with other models), due to the layernorm applied before each block.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
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 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerSelfOutput.__init__(config)
¶
Initializes a new instance of the TimesformerSelfOutput class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TimesformerSelfOutput class.
|
config |
A configuration object containing parameters for the self output layer. It specifies the hidden size and dropout probability for the layer. It must be an instance of the TimesformerConfig class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type TimesformerConfig. |
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.TimesformerSelfOutput.forward(hidden_states)
¶
Constructs the self output of the Timesformer model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TimesformerSelfOutput class.
TYPE:
|
hidden_states |
The hidden states tensor representing the input to the self output layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
hidden_states
|
The output tensor after applying the self output layer operations.
TYPE:
|
This method takes the hidden states tensor and applies the self output layer operations to it. It first applies a dense layer to transform the hidden states tensor. Then, it applies dropout to the transformed tensor to prevent overfitting. Finally, it returns the output tensor after the self output layer operations have been applied.
Source code in mindnlp/transformers/models/timesformer/modeling_timesformer.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
mindnlp.transformers.models.timesformer.modeling_timesformer.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/timesformer/modeling_timesformer.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
mindnlp.transformers.models.timesformer.image_processing_videomae
¶
Image processor class for VideoMAE model, and reused by Timesformer model.
mindnlp.transformers.models.timesformer.image_processing_videomae.VideoMAEImageProcessor
¶
Bases: BaseImageProcessor
Constructs a VideoMAE image processor.
PARAMETER | DESCRIPTION |
---|---|
do_resize |
Whether to resize the image's (height, width) dimensions to the specified
TYPE:
|
size |
224}
TYPE:
|
resample |
Resampling filter to use if resizing the image. Can be overridden by the
TYPE:
|
do_center_crop |
Whether to center crop the image to the specified
TYPE:
|
crop_size |
224, "width": 224}
TYPE:
|
do_rescale |
Whether to rescale the image by the specified scale
TYPE:
|
rescale_factor |
Defines the scale factor to use if rescaling the image. Can be overridden by the
TYPE:
|
do_normalize |
Whether to normalize the image. Can be overridden by the
TYPE:
|
image_mean |
Mean to use if normalizing the image. This is a float or list of floats the length of the number of
channels in the image. Can be overridden by the
TYPE:
|
image_std |
Standard deviation to use if normalizing the image. This is a float or list of floats the length of the
number of channels in the image. Can be overridden by the
TYPE:
|
Source code in mindnlp/transformers/models/timesformer/image_processing_videomae.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
|
mindnlp.transformers.models.timesformer.image_processing_videomae.VideoMAEImageProcessor.__init__(do_resize=True, size=None, resample=PILImageResampling.BILINEAR, do_center_crop=True, crop_size=None, do_rescale=True, rescale_factor=1 / 255, do_normalize=True, image_mean=None, image_std=None, **kwargs)
¶
Initializes an instance of the VideoMAEImageProcessor class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
do_resize |
Indicates whether resizing should be performed on the input image. Defaults to True.
TYPE:
|
size |
Specifies the desired size of the image after resizing. Defaults to None.
TYPE:
|
resample |
The resampling method to be used during resizing. Defaults to PILImageResampling.BILINEAR.
TYPE:
|
do_center_crop |
Indicates whether center cropping should be performed on the resized image. Defaults to True.
TYPE:
|
crop_size |
Specifies the size of the cropped image. Defaults to None.
TYPE:
|
do_rescale |
Indicates whether rescaling should be performed on the image. Defaults to True.
TYPE:
|
rescale_factor |
The factor by which the image should be rescaled. Defaults to 1/255.
TYPE:
|
do_normalize |
Indicates whether normalization should be performed on the image. Defaults to True.
TYPE:
|
image_mean |
The mean values used for normalization. Defaults to None.
TYPE:
|
image_std |
The standard deviation values used for normalization. Defaults to None.
TYPE:
|
**kwargs |
Additional keyword arguments that can be passed to the super class.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/timesformer/image_processing_videomae.py
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 |
|
mindnlp.transformers.models.timesformer.image_processing_videomae.VideoMAEImageProcessor.preprocess(videos, do_resize=None, size=None, resample=None, do_center_crop=None, crop_size=None, do_rescale=None, rescale_factor=None, do_normalize=None, image_mean=None, image_std=None, return_tensors=None, data_format=ChannelDimension.FIRST, input_data_format=None, **kwargs)
¶
Preprocess an image or batch of images.
PARAMETER | DESCRIPTION |
---|---|
images |
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set
TYPE:
|
do_resize |
Whether to resize the image.
TYPE:
|
size |
Size of the image after applying resize.
TYPE:
|
resample |
Resampling filter to use if resizing the image. This can be one of the enum
TYPE:
|
do_center_crop |
Whether to centre crop the image.
TYPE:
|
crop_size |
Size of the image after applying the centre crop.
TYPE:
|
do_rescale |
Whether to rescale the image values between [0 - 1].
TYPE:
|
rescale_factor |
Rescale factor to rescale the image by if
TYPE:
|
do_normalize |
Whether to normalize the image.
TYPE:
|
image_mean |
Image mean.
TYPE:
|
image_std |
Image standard deviation.
TYPE:
|
return_tensors |
The type of tensors to return. Can be one of:
TYPE:
|
data_format |
The channel dimension format for the output image. Can be one of:
TYPE:
|
input_data_format |
The channel dimension format for the input image. If unset, the channel dimension format is inferred from the input image. Can be one of:
TYPE:
|
Source code in mindnlp/transformers/models/timesformer/image_processing_videomae.py
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 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 |
|
mindnlp.transformers.models.timesformer.image_processing_videomae.VideoMAEImageProcessor.resize(image, size, resample=PILImageResampling.BILINEAR, data_format=None, input_data_format=None, **kwargs)
¶
Resize an image.
PARAMETER | DESCRIPTION |
---|---|
image |
Image to resize.
TYPE:
|
size |
Size of the output image. If
TYPE:
|
resample |
Resampling filter to use when resiizing the image.
TYPE:
|
data_format |
The channel dimension format of the image. If not provided, it will be the same as the input image.
TYPE:
|
input_data_format |
The channel dimension format of the input image. If not provided, it will be inferred.
TYPE:
|
Source code in mindnlp/transformers/models/timesformer/image_processing_videomae.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
mindnlp.transformers.models.timesformer.image_processing_videomae.make_batched(videos)
¶
PARAMETER | DESCRIPTION |
---|---|
videos |
A list or tuple of image inputs or a single image input.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[List[ImageInput]]
|
List[List[ImageInput]]: A batched list of image inputs. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input videos cannot be batched into a valid format. |
Source code in mindnlp/transformers/models/timesformer/image_processing_videomae.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|