segformer
mindnlp.transformers.models.segformer.configuration_segformer
¶
SegFormer model configuration
mindnlp.transformers.models.segformer.configuration_segformer.SegformerConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [SegformerModel
]. It is used to instantiate an
SegFormer 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 SegFormer
nvidia/segformer-b0-finetuned-ade-512-512
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:
|
num_encoder_blocks |
The number of encoder blocks (i.e. stages in the Mix Transformer encoder).
TYPE:
|
depths |
The number of layers in each encoder block.
TYPE:
|
sr_ratios |
Sequence reduction ratios in each encoder block.
TYPE:
|
hidden_sizes |
Dimension of each of the encoder blocks.
TYPE:
|
patch_sizes |
Patch size before each encoder block.
TYPE:
|
strides |
Stride before each encoder block.
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in each block of the Transformer encoder.
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:
|
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:
|
classifier_dropout_prob |
The dropout probability before the classification head.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
drop_path_rate |
The dropout probability for stochastic depth, used in the blocks of the Transformer encoder.
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
decoder_hidden_size |
The dimension of the all-MLP decode head.
TYPE:
|
semantic_loss_ignore_index |
The index that is ignored by the loss function of the semantic segmentation model.
TYPE:
|
Example
>>> from transformers import SegformerModel, SegformerConfig
...
>>> # Initializing a SegFormer nvidia/segformer-b0-finetuned-ade-512-512 style configuration
>>> configuration = SegformerConfig()
...
>>> # Initializing a model from the nvidia/segformer-b0-finetuned-ade-512-512 style configuration
>>> model = SegformerModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/segformer/configuration_segformer.py
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 |
|
mindnlp.transformers.models.segformer.configuration_segformer.SegformerConfig.__init__(num_channels=3, num_encoder_blocks=4, depths=[2, 2, 2, 2], sr_ratios=[8, 4, 2, 1], hidden_sizes=[32, 64, 160, 256], patch_sizes=[7, 3, 3, 3], strides=[4, 2, 2, 2], num_attention_heads=[1, 2, 5, 8], mlp_ratios=[4, 4, 4, 4], hidden_act='gelu', hidden_dropout_prob=0.0, attention_probs_dropout_prob=0.0, classifier_dropout_prob=0.1, initializer_range=0.02, drop_path_rate=0.1, layer_norm_eps=1e-06, decoder_hidden_size=256, semantic_loss_ignore_index=255, **kwargs)
¶
Initializes a new instance of SegformerConfig.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerConfig class.
|
num_channels |
The number of input channels. Defaults to 3.
TYPE:
|
num_encoder_blocks |
The number of encoder blocks. Defaults to 4.
TYPE:
|
depths |
The depths of each stage in the encoder block.
TYPE:
|
sr_ratios |
The spatial reduction ratios for each stage in the encoder block.
TYPE:
|
hidden_sizes |
The hidden sizes for each stage in the encoder block.
TYPE:
|
patch_sizes |
The patch sizes for each stage in the encoder block.
TYPE:
|
strides |
The strides for each stage in the encoder block.
TYPE:
|
num_attention_heads |
The number of attention heads for each stage in the encoder block.
TYPE:
|
mlp_ratios |
The ratio of mlp hidden size to the input size for each stage in the encoder block.
TYPE:
|
hidden_act |
The activation function for the hidden layer. 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 probabilities. Defaults to 0.0.
TYPE:
|
classifier_dropout_prob |
The dropout probability for the classifier. Defaults to 0.1.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices. Defaults to 0.02.
TYPE:
|
drop_path_rate |
The dropout rate for stochastic depth. Defaults to 0.1.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization. Defaults to 1e-06.
TYPE:
|
decoder_hidden_size |
The hidden size of the decoder.
TYPE:
|
semantic_loss_ignore_index |
The index to ignore in the semantic loss calculation.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
FutureWarning
|
If 'reshape_last_stage' is set to False in the kwargs, a warning about the deprecation of this argument will be raised. |
Source code in mindnlp/transformers/models/segformer/configuration_segformer.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 |
|
mindnlp.transformers.models.segformer.image_processing_segformer
¶
Image processor class for Segformer.
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor
¶
Bases: BaseImageProcessor
Constructs a Segformer image processor.
PARAMETER | DESCRIPTION |
---|---|
do_resize |
Whether to resize the image's (height, width) dimensions to the specified
TYPE:
|
size |
512, "width": 512}
TYPE:
|
resample |
Resampling filter to use if resizing the image. Can be overridden by the
TYPE:
|
do_rescale |
Whether to rescale the image by the specified scale
TYPE:
|
rescale_factor |
Whether to normalize 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:
|
do_reduce_labels |
Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is
used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). The
background label will be replaced by 255. Can be overridden by the
TYPE:
|
Source code in mindnlp/transformers/models/segformer/image_processing_segformer.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 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 |
|
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor.__call__(images, segmentation_maps=None, **kwargs)
¶
Preprocesses a batch of images and optionally segmentation maps.
Overrides the __call__
method of the Preprocessor
class so that both images and segmentation maps can be
passed in as positional arguments.
Source code in mindnlp/transformers/models/segformer/image_processing_segformer.py
383 384 385 386 387 388 389 390 |
|
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor.__init__(do_resize=True, size=None, resample=PILImageResampling.BILINEAR, do_rescale=True, rescale_factor=1 / 255, do_normalize=True, image_mean=None, image_std=None, do_reduce_labels=False, **kwargs)
¶
Initialize the SegformerImageProcessor.
This method initializes the SegformerImageProcessor object with the provided parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The SegformerImageProcessor object.
|
do_resize |
Whether to resize the image. Defaults to True.
TYPE:
|
size |
The desired height and width of the image. Defaults to {'height': 512, 'width': 512}.
TYPE:
|
resample |
The resampling method to use during resizing. Defaults to PILImageResampling.BILINEAR.
TYPE:
|
do_rescale |
Whether to rescale the image. Defaults to True.
TYPE:
|
rescale_factor |
The rescale factor to apply to the image. Defaults to 1 / 255.
TYPE:
|
do_normalize |
Whether to normalize the image. Defaults to True.
TYPE:
|
image_mean |
The mean values used for image normalization. Defaults to None, which uses IMAGENET_DEFAULT_MEAN.
TYPE:
|
image_std |
The standard deviation values used for image normalization. Defaults to None, which uses IMAGENET_DEFAULT_STD.
TYPE:
|
do_reduce_labels |
Whether to reduce the number of labels. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
RAISES | DESCRIPTION |
---|---|
FutureWarning
|
If the 'reduce_labels' parameter is used. This parameter is deprecated and will be removed in a future version. Please use 'do_reduce_labels' instead. |
Source code in mindnlp/transformers/models/segformer/image_processing_segformer.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor.from_dict(image_processor_dict, **kwargs)
classmethod
¶
Overrides the from_dict
method from the base class to make sure do_reduce_labels
is updated if image
processor is created using from_dict and kwargs e.g. SegformerImageProcessor.from_pretrained(checkpoint,
reduce_labels=True)
Source code in mindnlp/transformers/models/segformer/image_processing_segformer.py
165 166 167 168 169 170 171 172 173 174 175 |
|
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor.post_process_semantic_segmentation(outputs, target_sizes=None)
¶
Converts the output of [SegformerForSemanticSegmentation
] into semantic segmentation maps.
Only supports PyTorch.
PARAMETER | DESCRIPTION |
---|---|
outputs |
Raw outputs of the model.
TYPE:
|
target_sizes |
List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
semantic_segmentation
|
|
Source code in mindnlp/transformers/models/segformer/image_processing_segformer.py
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 |
|
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor.preprocess(images, segmentation_maps=None, do_resize=None, size=None, resample=None, do_rescale=None, rescale_factor=None, do_normalize=None, image_mean=None, image_std=None, do_reduce_labels=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:
|
segmentation_maps |
Segmentation map to preprocess.
TYPE:
|
do_resize |
Whether to resize the image.
TYPE:
|
size |
Size of the image after
TYPE:
|
resample |
Resampling filter to use if resizing the image. This can be one of the enum
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:
|
do_reduce_labels |
Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.
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/segformer/image_processing_segformer.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 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 |
|
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor.reduce_label(label)
¶
Reduces the label values in the input image for Segformer image processing.
PARAMETER | DESCRIPTION |
---|---|
self |
Instance of the SegformerImageProcessor class.
|
label |
Input label image to be processed. It should be in a compatible format for processing.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
np.ndarray: A NumPy array representing the processed label image with reduced values. |
Source code in mindnlp/transformers/models/segformer/image_processing_segformer.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
mindnlp.transformers.models.segformer.image_processing_segformer.SegformerImageProcessor.resize(image, size, resample=PILImageResampling.BILINEAR, data_format=None, input_data_format=None, **kwargs)
¶
Resize an image to (size["height"], size["width"])
.
PARAMETER | DESCRIPTION |
---|---|
image |
Image to resize.
TYPE:
|
size |
Dictionary in the format
TYPE:
|
resample |
TYPE:
|
data_format |
The channel dimension format for the output image. If unset, the channel dimension format of the input image is used. 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:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in mindnlp/transformers/models/segformer/image_processing_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer
¶
MindSpore SegFormer model.
mindnlp.transformers.models.segformer.modeling_segformer.SegFormerImageClassifierOutput
dataclass
¶
Bases: ImageClassifierOutput
Base class for outputs of image classification models.
PARAMETER | DESCRIPTION |
---|---|
loss |
Classification (or regression if config.num_labels==1) loss.
TYPE:
|
logits |
Classification (or regression if config.num_labels==1) scores (before SoftMax).
TYPE:
|
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerAttention
¶
Bases: Module
This class represents the attention mechanism used in the Segformer model. It inherits from the nn.Module
class.
ATTRIBUTE | DESCRIPTION |
---|---|
self |
Instance of the SegformerEfficientSelfAttention class that handles self-attention computations. |
output |
Instance of the SegformerSelfOutput class that computes the final attention output.
TYPE:
|
pruned_heads |
A set that stores the indices of pruned attention heads.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the SegformerAttention class. Args:
|
prune_heads |
Prunes the specified attention heads from the model. Args:
|
forward |
Constructs the attention mechanism. Args:
Returns:
|
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerAttention.__init__(config, hidden_size, num_attention_heads, sequence_reduction_ratio)
¶
Initializes the SegformerAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
A configuration object containing various parameters for the attention mechanism. - Type: object - Purpose: It provides the configuration settings for the attention mechanism. - Restrictions: Must be a valid configuration object.
|
hidden_size |
The size of the hidden layers in the attention mechanism. - Type: int - Purpose: It defines the dimensionality of the hidden layers. - Restrictions: Must be a positive integer.
|
num_attention_heads |
The number of attention heads to be used in the attention mechanism. - Type: int - Purpose: It determines the parallel attention computations. - Restrictions: Must be a positive integer.
|
sequence_reduction_ratio |
The ratio by which the input sequence length is reduced in the attention mechanism. - Type: int - Purpose: It controls the reduction of the input sequence length. - Restrictions: Must be a positive integer.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerAttention.forward(hidden_states, height, width, output_attentions=False)
¶
Construct the attention output of the SegformerAttention module.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerAttention class.
TYPE:
|
hidden_states |
The input hidden states tensor of shape (batch_size, sequence_length, hidden_size).
TYPE:
|
height |
The height of the attention output.
TYPE:
|
width |
The width of the attention output.
TYPE:
|
output_attentions |
Whether to output attentions. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the attention output tensor of shape (batch_size, sequence_length, hidden_size), and any additional outputs as returned by the self.self() method. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerAttention.prune_heads(heads)
¶
This method 'prune_heads' is defined in the class 'SegformerAttention' and is used to prune the attention heads and corresponding linear layers based on the provided 'heads' input.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the 'SegformerAttention' class.
TYPE:
|
heads |
A list containing the indices of attention heads to be pruned. The indices should be within the valid range of attention heads for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the length of the 'heads' list is 0, indicating no heads to be pruned. |
TypeError
|
If the 'heads' parameter is not provided as a list. |
IndexError
|
If the indices in the 'heads' list are out of range for the attention heads in the model. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDWConv
¶
Bases: Module
The SegformerDWConv class represents a depthwise separable convolutional layer for segmentation tasks. This class inherits from the nn.Module module.
ATTRIBUTE | DESCRIPTION |
---|---|
dim |
The dimensionality of the input and output channels for the depthwise separable convolution.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the SegformerDWConv object with a specified dimensionality for input and output channels. |
forward |
Applies the depthwise separable convolution to the input hidden_states and returns the processed output. |
Example
>>> # Create a SegformerDWConv object with default dimensionality
>>> seg_dwconv = SegformerDWConv()
...
>>> # Apply the depthwise separable convolution to a set of hidden states
>>> output = seg_dwconv.forward(hidden_states, height, width)
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDWConv.__init__(dim=768)
¶
Initializes a SegformerDWConv instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerDWConv class.
|
dim |
The dimension of the input and output channels. Defaults to 768.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided dimension is not a positive integer. |
TypeError
|
If the provided dimension is not an integer. |
RuntimeError
|
If an error occurs during the initialization process. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDWConv.forward(hidden_states, height, width)
¶
Constructs the SegformerDWConv.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the SegformerDWConv class.
TYPE:
|
hidden_states |
A tensor of shape (batch_size, seq_len, num_channels) representing the hidden states.
TYPE:
|
height |
The desired height of the hidden states after transformation.
TYPE:
|
width |
The desired width of the hidden states after transformation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDecodeHead
¶
Bases: SegformerPreTrainedModel
The SegformerDecodeHead
class is a subclass of SegformerPreTrainedModel
and represents the decoding head
component of the Segformer model.
This class contains methods for forwarding the decoding head and generating logits for semantic segmentation.
ATTRIBUTE | DESCRIPTION |
---|---|
linear_c |
A list of MLP (Multi-Layer Perceptron) modules for each encoder block.
TYPE:
|
linear_fuse |
A convolutional layer used for fusing the hidden states of all encoder blocks.
TYPE:
|
batch_norm |
A batch normalization layer applied to the fused hidden states.
TYPE:
|
activation |
An activation function applied to the hidden states.
TYPE:
|
dropout |
A dropout layer applied to the hidden states.
TYPE:
|
classifier |
A convolutional layer for generating the final logits.
TYPE:
|
config |
The configuration object containing hyperparameters and settings for the SegformerDecodeHead.
|
METHOD | DESCRIPTION |
---|---|
forward |
mindspore.Tensor) -> mindspore.Tensor: Constructs the decoding head and generates logits for semantic segmentation based on the given encoder hidden states. Args:
Returns:
|
Note
- The
SegformerDecodeHead
class requires an instance ofSegformerPreTrainedModel
as its parent class. - The decoding head consists of multiple MLP modules, a fusion layer, batch normalization, activation, dropout, and a final classifier.
- The
forward
method takes the encoder hidden states as input and performs the necessary computations to generate the logits. - The
SegformerDecodeHead
class is designed to be used in conjunction with the Segformer model for semantic segmentation tasks.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDecodeHead.__init__(config)
¶
Initializes the SegformerDecodeHead class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerDecodeHead class.
|
config |
A dictionary containing the configuration parameters for the SegformerDecodeHead, including the following keys:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDecodeHead.forward(encoder_hidden_states)
¶
Constructs the decode head for Segformer.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerDecodeHead class.
TYPE:
|
encoder_hidden_states |
The hidden states from the encoder. It is a tensor representing the hidden states from the encoder with shape (N, C, H, W). N represents the batch size, C represents the number of channels, H represents the height, and W represents the width.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: A tensor representing the logits for the segmentation task with shape (N, C', H', W'). N represents the batch size, C' represents the number of classes, H' represents the height, and W' represents the width. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the reshape_last_stage configuration is False and the encoder_hidden_state has 3 dimensions. |
RuntimeError
|
If there is an issue with the linear fusion operation. |
RuntimeError
|
If there is an issue with the batch normalization operation. |
RuntimeError
|
If there is an issue with the activation operation. |
RuntimeError
|
If there is an issue with the dropout operation. |
RuntimeError
|
If there is an issue with the classifier operation. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDropPath
¶
Bases: Module
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDropPath.__init__(drop_prob=None)
¶
Initialize an instance of SegformerDropPath.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerDropPath class.
|
drop_prob |
The probability of dropping a connection during training. If None, no connections are dropped. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDropPath.extra_repr()
¶
This method returns a string representation of the drop probability for a SegformerDropPath instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of SegformerDropPath for which the drop probability is being represented.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
A string representing the drop probability of the SegformerDropPath instance.
TYPE:
|
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerDropPath.forward(hidden_states)
¶
Constructs a drop path operation on the hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the SegformerDropPath class.
TYPE:
|
hidden_states |
The input hidden states to apply drop path on.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The modified hidden states after applying drop path operation. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the input hidden_states is not a mindspore.Tensor object. |
ValueError
|
If the drop_prob is not a valid probability value. |
Note
Drop path is a regularization technique used in training deep neural networks. It randomly sets a fraction of the hidden states to zero during training, which helps in reducing overfitting.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerEfficientSelfAttention
¶
Bases: Module
SegFormer's efficient self-attention mechanism. Employs the sequence reduction process introduced in the PvT paper.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerEfficientSelfAttention.__init__(config, hidden_size, num_attention_heads, sequence_reduction_ratio)
¶
Initializes an instance of the SegformerEfficientSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
Configuration object containing various settings.
|
hidden_size |
The size of the hidden states.
TYPE:
|
num_attention_heads |
The number of attention heads.
TYPE:
|
sequence_reduction_ratio |
The ratio by which the sequence length is reduced.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the hidden_size is not a multiple of the num_attention_heads. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerEfficientSelfAttention.forward(hidden_states, height, width, output_attentions=False)
¶
Constructs the self-attention mechanism for the SegformerEfficientSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerEfficientSelfAttention class.
TYPE:
|
hidden_states |
The input tensor representing the hidden states. Shape (batch_size, seq_len, num_channels).
TYPE:
|
height |
The height of the input tensor.
TYPE:
|
width |
The width of the input tensor.
TYPE:
|
output_attentions |
Flag indicating whether to output attentions. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the context layer tensor and attention probabilities tensor if output_attentions is True, otherwise only the context layer tensor. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the shape of the hidden_states tensor is not compatible. |
TypeError
|
If the input parameters are not of the expected types. |
RuntimeError
|
If an error occurs during the computation. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerEfficientSelfAttention.swapaxes_for_scores(hidden_states)
¶
Swaps axes and reshapes the input tensor for calculating attention scores in the SegformerEfficientSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the SegformerEfficientSelfAttention class. |
hidden_states |
A tensor representing the hidden states. It should have a shape of (batch_size, sequence_length, hidden_size).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
torch.Tensor: A tensor representing the reshaped hidden states. The shape of the tensor will be (batch_size, num_attention_heads, sequence_length, attention_head_size). |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerEncoder
¶
Bases: Module
SegformerEncoder is a neural network module that represents the encoder of the Segformer model. It takes input pixel values and produces a sequence of hidden states that can be used for various downstream tasks.
Inherits from
nn.Module
PARAMETER | DESCRIPTION |
---|---|
config |
An instance of SegformerConfig that contains various hyperparameters for the encoder.
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input config is not an instance of SegformerConfig. |
Example
>>> config = SegformerConfig()
>>> encoder = SegformerEncoder(config)
>>> pixel_values = mindspore.Tensor(np.zeros((1, 3, 224, 224)), mindspore.float32)
>>> outputs = encoder(pixel_values)
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerEncoder.__init__(config)
¶
This method initializes a SegformerEncoder instance with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The SegformerEncoder instance.
TYPE:
|
config |
A configuration object containing various parameters for the SegformerEncoder. It should include the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided configuration is invalid or incomplete. |
TypeError
|
If the provided configuration is of an unexpected type. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.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 889 890 891 892 893 894 895 896 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerEncoder.forward(pixel_values, output_attentions=False, output_hidden_states=False, return_dict=True)
¶
Method to forward the SegformerEncoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerEncoder class.
|
pixel_values |
The input pixel values as a Tensor.
TYPE:
|
output_attentions |
Whether to output attentions. Defaults to False.
TYPE:
|
output_hidden_states |
Whether to output hidden states. 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 value which can be either a Tuple or BaseModelOutput.
|
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerForImageClassification
¶
Bases: SegformerPreTrainedModel
This class represents a Segformer model for image classification. It is a subclass of SegformerPreTrainedModel.
The SegformerForImageClassification class initializes and forwards a Segformer model for image classification. It takes in a configuration object as a parameter, which includes the number of labels for classification.
The forwardor (init) initializes the SegformerForImageClassification object by calling the superclass's forwardor with the provided configuration. It sets the number of labels and creates instances of the SegformerModel and nn.Linear classes. The post_init method is then called.
The forward method forwards the Segformer model for image classification. It takes in several optional parameters, including pixel_values (input image tensor), labels (classification labels), output_attentions (whether to output attention weights), output_hidden_states (whether to output hidden states), and return_dict (whether to return results as a dictionary). It returns a tuple or a SegFormerImageClassifierOutput object.
The labels parameter is an optional tensor that represents the classification labels for computing the image classification/regression loss. The indices in the labels tensor should be in the range of [0, ..., config.num_labels - 1]. If config.num_labels == 1, a regression loss (Mean-Square loss) is computed. If config.num_labels > 1, a classification loss (Cross-Entropy) is computed.
The method first calls the SegformerModel's forward method with the provided inputs and optional parameters. The output of the forward pass is stored in the sequence_output variable. If the reshape_last_stage configuration option is enabled, the sequence_output tensor is permuted and reshaped. Then, the mean of the sequence_output tensor is calculated along the second axis.
The logits tensor is obtained by passing the sequence_output tensor through the classifier module. The loss variable is initially set to None.
If the labels tensor is provided, the problem_type configuration option is checked to determine the type of loss calculation. If the problem_type is not set, it is inferred based on the number of labels and the data type of the labels tensor. For regression problems with a single label, the problem_type is set to 'regression'. For single-label classification problems, the problem_type is set to 'single_label_classification'. For multi-label classification problems, the problem_type is set to 'multi_label_classification'.
The loss is calculated based on the problem_type. For regression problems with a single label, the mean squared error (MSE) loss is computed. For single-label classification problems, the cross-entropy loss is computed. For multi-label classification problems, the binary cross-entropy with logits loss is computed.
Finally, the method returns the computed loss and other outputs depending on the value of the return_dict parameter. If return_dict is False, the method returns a tuple containing the logits and other outputs. If loss is None, the output tuple does not include the loss. If return_dict is True, the method returns a SegFormerImageClassifierOutput object containing the loss, logits, hidden states, and attentions.
Note
This docstring does not include the function signatures or any other code.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerForImageClassification.__init__(config)
¶
Initializes a new SegformerForImageClassification instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerForImageClassification class.
|
config |
An object containing configuration settings for the model. It should include the following attributes:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of the expected type. |
ValueError
|
If the config parameter is missing required attributes. |
RuntimeError
|
If there is an issue during the initialization process. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerForImageClassification.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:
|
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerForSemanticSegmentation
¶
Bases: SegformerPreTrainedModel
This class represents a Segformer model for semantic segmentation, specifically designed for image processing tasks. It is a subclass of SegformerPreTrainedModel.
The SegformerForSemanticSegmentation class includes methods for model initialization and forwardion. It utilizes the SegformerModel and SegformerDecodeHead classes for the main processing steps.
METHOD | DESCRIPTION |
---|---|
`__init__` |
Initializes the SegformerForSemanticSegmentation instance with a given configuration.
|
`forward` |
Constructs the semantic segmentation output based on the input pixel values. Parameters:
Returns:
|
Example
>>> from transformers import AutoImageProcessor, SegformerForSemanticSegmentation
>>> from PIL import Image
>>> import requests
...
>>> image_processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
>>> model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> inputs = image_processor(images=image, return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
>>> list(logits.shape)
[1, 150, 128, 128]
Source code in mindnlp/transformers/models/segformer/modeling_segformer.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 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 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerForSemanticSegmentation.__init__(config)
¶
Initializes an instance of SegformerForSemanticSegmentation.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerForSemanticSegmentation class.
|
config |
A dictionary containing configuration parameters for the Segformer model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not a dictionary. |
ValueError
|
If the config parameter does not contain the required configuration parameters. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerForSemanticSegmentation.forward(pixel_values, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Ground truth semantic segmentation maps for computing the loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, SemanticSegmenterOutput]
|
Union[Tuple, SemanticSegmenterOutput] |
Example
>>> from transformers import AutoImageProcessor, SegformerForSemanticSegmentation
>>> from PIL import Image
>>> import requests
...
>>> image_processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
>>> model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
...
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
...
>>> inputs = image_processor(images=image, return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
>>> list(logits.shape)
[1, 150, 128, 128]
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerLayer
¶
Bases: Module
This corresponds to the Block class in the original implementation.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerLayer.__init__(config, hidden_size, num_attention_heads, drop_path, sequence_reduction_ratio, mlp_ratio)
¶
Initializes a new instance of the SegformerLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
A configuration object specifying the settings for the SegformerLayer.
|
hidden_size |
The size of the hidden layer.
TYPE:
|
num_attention_heads |
The number of attention heads.
TYPE:
|
drop_path |
The probability of dropping a path during training. Must be between 0.0 and 1.0.
TYPE:
|
sequence_reduction_ratio |
The ratio by which the sequence length is reduced.
TYPE:
|
mlp_ratio |
The ratio by which the hidden size of the Multi-Layer Perceptron (MLP) is computed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerLayer.forward(hidden_states, height, width, output_attentions=False)
¶
This method forwards a Segformer layer by performing self-attention and multi-layer perceptron (mlp) operations.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerLayer class.
TYPE:
|
hidden_states |
The input tensor representing the hidden states of the layer.
TYPE:
|
height |
The height dimension of the input tensor.
TYPE:
|
width |
The width dimension of the input tensor.
TYPE:
|
output_attentions |
Flag indicating whether to output attentions. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the output layer and any additional outputs from the layer. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerMLP
¶
Bases: Module
Linear Embedding.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerMLP.__init__(config, input_dim)
¶
Initializes the SegformerMLP class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerMLP class.
TYPE:
|
config |
An instance of SegformerConfig containing configuration settings.
TYPE:
|
input_dim |
The dimensionality of the input data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the input arguments are not of the expected types. |
ValueError
|
If the input_dim is less than or equal to 0. |
RuntimeError
|
If there is an issue during the initialization process. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerMLP.forward(hidden_states)
¶
Constructs the SegformerMLP.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the SegformerMLP class.
TYPE:
|
hidden_states |
A tensor containing the hidden states. It should have a shape of (batch_size, sequence_length, hidden_size).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerMixFFN
¶
Bases: Module
The SegformerMixFFN class represents a feed-forward neural network (FFN) module for the Segformer architecture. It is designed to process input features and generate output features using dense layers, depthwise convolution, activation functions, and dropout regularization. The class inherits from nn.Module and provides methods for initializing the module and forwarding the FFN computation graph.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object containing parameters for the FFN module.
TYPE:
|
in_features |
The number of input features.
TYPE:
|
hidden_features |
The number of hidden features. If not provided, defaults to None.
TYPE:
|
out_features |
The number of output features. If not provided, defaults to None.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the SegformerMixFFN module with the provided configuration and feature dimensions. |
forward |
Constructs the computation graph for the FFN module using the given input hidden_states and spatial dimensions (height and width). |
The forwardion of the computation graph involves passing the input through dense layers, depthwise convolution, activation functions, and dropout layers to generate the output hidden states.
Note
This docstring is a representation of the class attributes and methods. Please refer to the source code for the most accurate and up-to-date information.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerMixFFN.__init__(config, in_features, hidden_features=None, out_features=None)
¶
Initializes an instance of the SegformerMixFFN class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
The configuration object containing various settings.
TYPE:
|
in_features |
The number of input features.
TYPE:
|
hidden_features |
The number of hidden features. Defaults to None.
TYPE:
|
out_features |
The number of output features. If not provided, it will be set equal to in_features.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerMixFFN.forward(hidden_states, height, width)
¶
This method forwards the feed-forward network for the SegformerMixFFN class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerMixFFN class.
TYPE:
|
hidden_states |
The input hidden states for the feed-forward network.
TYPE:
|
height |
The height of the input feature map.
TYPE:
|
width |
The width of the input feature map.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerModel
¶
Bases: SegformerPreTrainedModel
A Python class representing a SegformerModel.
This class is a SegformerModel that inherits from SegformerPreTrainedModel. It is used for performing semantic segmentation tasks using the Segformer architecture.
The SegformerModel class provides methods for initializing the model, pruning model heads, and forwarding the model with input pixel values. It also allows for customization of the output, including attention maps and hidden states.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the SegformerModel instance with the provided configuration. |
_prune_heads |
Prunes specific heads of the model based on the provided dictionary. |
forward |
Constructs the model with the given pixel values and returns the output. Customization of output options is available. |
Note
This class assumes the presence of the SegformerPreTrainedModel class.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerModel.__init__(config)
¶
Initializes an instance of the SegformerModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerModel class.
|
config |
A dictionary containing configuration parameters for initializing the SegformerModel. It should include the necessary configuration settings for the model. Required keys and their datatypes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
many exceptions
|
Any exceptions that may be raised during the initialization process should be documented here:
|
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerModel.forward(pixel_values, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Constructs the SegformerModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the SegformerModel class.
|
pixel_values |
The input tensor containing pixel values. Shape: (batch_size, num_channels, image_height, image_width).
TYPE:
|
output_attentions |
Whether to include attention weights in the output. If not provided, it defaults to the value specified in the model's configuration. Defaults to None.
TYPE:
|
output_hidden_states |
Whether to include hidden states in the output. If not provided, it defaults to the value specified in the model's configuration. Defaults to None.
TYPE:
|
return_dict |
Whether to return outputs as a BaseModelOutput dictionary. If not provided, it defaults to the value specified in the model's configuration. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutput]
|
Union[Tuple, BaseModelOutput]: The output of the SegformerModel. If
|
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerOverlapPatchEmbeddings
¶
Bases: Module
Construct the overlapping patch embeddings.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerOverlapPatchEmbeddings.__init__(patch_size, stride, num_channels, hidden_size)
¶
Initialize the SegformerOverlapPatchEmbeddings class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
patch_size |
The size of the patches used for the convolutional layer.
TYPE:
|
stride |
The stride value for the convolutional layer.
TYPE:
|
num_channels |
The number of input channels.
TYPE:
|
hidden_size |
The number of output channels.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerOverlapPatchEmbeddings.forward(pixel_values)
¶
Constructs the overlap patch embeddings for the input pixel values.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the SegformerOverlapPatchEmbeddings class. |
pixel_values |
A tensor representing the input pixel values. The shape of the tensor should be (batch_size, channels, height, width).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the following elements:
|
Note
- The 'proj' method referred to in the code should be a method defined in the SegformerOverlapPatchEmbeddings class.
- The 'layer_norm' method referred to in the code should be a method defined in the SegformerOverlapPatchEmbeddings class.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
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 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerPreTrainedModel
¶
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/segformer/modeling_segformer.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerSelfOutput
¶
Bases: Module
This class represents the self-output of a segmenter transformer model (Segformer) in a neural network architecture. It inherits from the nn.Module class.
ATTRIBUTE | DESCRIPTION |
---|---|
dense |
A fully connected layer that applies linear transformation to the input hidden states.
TYPE:
|
dropout |
A dropout layer that randomly zeros some of the elements of the input tensor.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes an instance of the SegformerSelfOutput class. |
forward |
Constructs the self-output of the Segformer model. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerSelfOutput.__init__(config, hidden_size)
¶
Initializes an instance of the SegformerSelfOutput class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing various settings.
TYPE:
|
hidden_size |
The size of the hidden layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Description
This method is called when a new instance of the SegformerSelfOutput class is created. It initializes the instance by setting up the necessary components for self-attention and output computation.
The 'config' parameter is an object that stores various settings and configurations for the model. It is used to access the hidden dropout probability, which is used in the dropout layer. The 'hidden_size' parameter specifies the size of the hidden layer in the model.
Inside the method, the 'super().init()' statement calls the init() method of the parent class to ensure proper initialization.
The 'self.dense' attribute is an instance of the nn.Linear class, which represents a fully connected layer. It takes the 'hidden_size' as both the input and output size. This layer is used for self-attention computation.
The 'self.dropout' attribute is an instance of the nn.Dropout class. It takes the 'config.hidden_dropout_prob' as the dropout probability. This layer is used for regularization during training to prevent overfitting.
Note that this method does not perform any computations and is solely responsible for setting up the necessary components for the SegformerSelfOutput class.
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
mindnlp.transformers.models.segformer.modeling_segformer.SegformerSelfOutput.forward(hidden_states, input_tensor)
¶
Constructs the output of the SegformerSelfOutput class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the SegformerSelfOutput class.
TYPE:
|
hidden_states |
The hidden states of the self-attention mechanism. These states are passed through a dense layer and a dropout layer.
TYPE:
|
input_tensor |
The input tensor to the self-attention mechanism.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/segformer/modeling_segformer.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
mindnlp.transformers.models.segformer.modeling_segformer.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/segformer/modeling_segformer.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|