sam
mindnlp.transformers.models.sam.configuration_sam
¶
SAM model configuration
mindnlp.transformers.models.sam.configuration_sam.SamConfig
¶
Bases: PretrainedConfig
[SamConfig] is the configuration class to store the configuration of a [SamModel]. It is used to instantiate a
SAM model according to the specified arguments, defining the vision model, prompt-encoder model and mask decoder
configs. Instantiating a configuration with the defaults will yield a similar configuration to that of the
SAM-ViT-H facebook/sam-vit-huge 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 |
|---|---|
vision_config
|
Dictionary of configuration options used to initialize [
TYPE:
|
prompt_encoder_config
|
Dictionary of configuration options used to initialize [
TYPE:
|
mask_decoder_config
|
Dictionary of configuration options used to initialize [
TYPE:
|
kwargs
|
Dictionary of keyword arguments.
TYPE:
|
Example
>>> from transformers import (
... SamVisionConfig,
... SamPromptEncoderConfig,
... SamMaskDecoderConfig,
... SamModel,
... )
...
>>> # Initializing a SamConfig with `"facebook/sam-vit-huge"` style configuration
>>> configuration = SamConfig()
...
>>> # Initializing a SamModel (with random weights) from the `"facebook/sam-vit-huge"` style configuration
>>> model = SamModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
...
>>> # We can also initialize a SamConfig from a SamVisionConfig, SamPromptEncoderConfig, and SamMaskDecoderConfig
...
>>> # Initializing SAM vision, SAM Q-Former and language model configurations
>>> vision_config = SamVisionConfig()
>>> prompt_encoder_config = SamPromptEncoderConfig()
>>> mask_decoder_config = SamMaskDecoderConfig()
>>> config = SamConfig(vision_config, prompt_encoder_config, mask_decoder_config)
Source code in mindnlp\transformers\models\sam\configuration_sam.py
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 | |
mindnlp.transformers.models.sam.configuration_sam.SamConfig.__init__(vision_config=None, prompt_encoder_config=None, mask_decoder_config=None, initializer_range=0.02, **kwargs)
¶
Initializes a new instance of the SamConfig class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The current instance of the SamConfig class.
|
vision_config
|
The configuration for vision. If provided, it should be an instance of SamVisionConfig. Defaults to None.
TYPE:
|
prompt_encoder_config
|
The configuration for prompt encoder. If provided, it should be an instance of SamPromptEncoderConfig. Defaults to None.
TYPE:
|
mask_decoder_config
|
The configuration for mask decoder. If provided, it should be an instance of SamMaskDecoderConfig. Defaults to None.
TYPE:
|
initializer_range
|
The range for weight initialization. Defaults to 0.02.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\sam\configuration_sam.py
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 | |
mindnlp.transformers.models.sam.configuration_sam.SamMaskDecoderConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [SamMaskDecoder]. It is used to instantiate a SAM
mask decoder to the specified arguments, defining the model architecture. Instantiating a configuration defaults
will yield a similar configuration to that of the SAM-vit-h
facebook/sam-vit-huge 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 |
|---|---|
hidden_size
|
Dimensionality of the hidden states.
TYPE:
|
hidden_act
|
The non-linear activation function used inside the
TYPE:
|
mlp_dim
|
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
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:
|
attention_downsample_rate
|
The downsampling rate of the attention layer.
TYPE:
|
num_multimask_outputs
|
The number of outputs from the
TYPE:
|
iou_head_depth
|
The number of layers in the IoU head module.
TYPE:
|
iou_head_hidden_dim
|
The dimensionality of the hidden states in the IoU head module.
TYPE:
|
layer_norm_eps
|
The epsilon used by the layer normalization layers.
TYPE:
|
Source code in mindnlp\transformers\models\sam\configuration_sam.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 | |
mindnlp.transformers.models.sam.configuration_sam.SamMaskDecoderConfig.__init__(hidden_size=256, hidden_act='relu', mlp_dim=2048, num_hidden_layers=2, num_attention_heads=8, attention_downsample_rate=2, num_multimask_outputs=3, iou_head_depth=3, iou_head_hidden_dim=256, layer_norm_eps=1e-06, **kwargs)
¶
Initializes a new instance of the SamMaskDecoderConfig class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The object itself.
|
hidden_size
|
The size of the hidden layer. Default is 256.
TYPE:
|
hidden_act
|
The activation function to be used in the hidden layer. Default is 'relu'.
TYPE:
|
mlp_dim
|
The dimension of the Multi-Layer Perceptron (MLP). Default is 2048.
TYPE:
|
num_hidden_layers
|
The number of hidden layers. Default is 2.
TYPE:
|
num_attention_heads
|
The number of attention heads. Default is 8.
TYPE:
|
attention_downsample_rate
|
The downsample rate for attention. Default is 2.
TYPE:
|
num_multimask_outputs
|
The number of outputs for multimask. Default is 3.
TYPE:
|
iou_head_depth
|
The depth of the Intersection over Union (IoU) head. Default is 3.
TYPE:
|
iou_head_hidden_dim
|
The hidden dimension of the IoU head. Default is 256.
TYPE:
|
layer_norm_eps
|
The epsilon value for layer normalization. Default is 1e-06.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp\transformers\models\sam\configuration_sam.py
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 | |
mindnlp.transformers.models.sam.configuration_sam.SamPromptEncoderConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [SamPromptEncoder]. The [SamPromptEncoder]
module is used to encode the input 2D points and bounding boxes. Instantiating a configuration defaults will yield
a similar configuration to that of the SAM-vit-h
facebook/sam-vit-huge 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 |
|---|---|
hidden_size
|
Dimensionality of the hidden states.
TYPE:
|
image_size
|
The expected output resolution of the image.
TYPE:
|
patch_size
|
The size (resolution) of each patch.
TYPE:
|
mask_input_channels
|
The number of channels to be fed to the
TYPE:
|
num_point_embeddings
|
The number of point embeddings to be used.
TYPE:
|
hidden_act
|
The non-linear activation function in the encoder and pooler.
TYPE:
|
Source code in mindnlp\transformers\models\sam\configuration_sam.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 | |
mindnlp.transformers.models.sam.configuration_sam.SamPromptEncoderConfig.__init__(hidden_size=256, image_size=1024, patch_size=16, mask_input_channels=16, num_point_embeddings=4, hidden_act='gelu', layer_norm_eps=1e-06, **kwargs)
¶
Initializes an instance of the SamPromptEncoderConfig class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class itself.
TYPE:
|
hidden_size
|
The size of the hidden state. Defaults to 256.
TYPE:
|
image_size
|
The size of the input image. Defaults to 1024.
TYPE:
|
patch_size
|
The size of each image patch. Defaults to 16.
TYPE:
|
mask_input_channels
|
The number of input channels for masking. Defaults to 16.
TYPE:
|
num_point_embeddings
|
The number of point embeddings. Defaults to 4.
TYPE:
|
hidden_act
|
The activation function for the hidden layers. Defaults to 'gelu'.
TYPE:
|
layer_norm_eps
|
The epsilon value for layer normalization. Defaults to 1e-06.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp\transformers\models\sam\configuration_sam.py
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 | |
mindnlp.transformers.models.sam.configuration_sam.SamVisionConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [SamVisionModel]. It is used to instantiate a SAM
vision encoder according to the specified arguments, defining the model architecture. Instantiating a configuration
defaults will yield a similar configuration to that of the SAM ViT-h
facebook/sam-vit-huge 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 |
|---|---|
hidden_size
|
Dimensionality of the encoder layers and the pooler layer.
TYPE:
|
output_channels
|
Dimensionality of the output channels in the Patch Encoder.
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:
|
num_channels
|
Number of channels in the input image.
TYPE:
|
image_size
|
Expected resolution. Target size of the resized input image.
TYPE:
|
patch_size
|
Size of the patches to be extracted from the input image.
TYPE:
|
hidden_act
|
The non-linear activation function (function or string)
TYPE:
|
layer_norm_eps
|
The epsilon used by the layer normalization layers.
TYPE:
|
attention_dropout
|
The dropout ratio for the attention probabilities.
TYPE:
|
initializer_range
|
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
qkv_bias
|
Whether to add a bias to query, key, value projections.
TYPE:
|
mlp_ratio
|
Ratio of mlp hidden dim to embedding dim.
TYPE:
|
use_abs_pos
|
Whether to use absolute position embedding.
TYPE:
|
use_rel_pos
|
Whether to use relative position embedding.
TYPE:
|
window_size
|
Window size for relative position.
TYPE:
|
global_attn_indexes
|
The indexes of the global attention layers.
TYPE:
|
num_pos_feats
|
The dimensionality of the position embedding.
TYPE:
|
mlp_dim
|
The dimensionality of the MLP layer in the Transformer encoder. If
TYPE:
|
Source code in mindnlp\transformers\models\sam\configuration_sam.py
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 | |
mindnlp.transformers.models.sam.configuration_sam.SamVisionConfig.__init__(hidden_size=768, output_channels=256, num_hidden_layers=12, num_attention_heads=12, num_channels=3, image_size=1024, patch_size=16, hidden_act='gelu', layer_norm_eps=1e-06, attention_dropout=0.0, initializer_range=1e-10, qkv_bias=True, mlp_ratio=4.0, use_abs_pos=True, use_rel_pos=True, window_size=14, global_attn_indexes=[2, 5, 8, 11], num_pos_feats=128, mlp_dim=None, **kwargs)
¶
Initializes an instance of the SamVisionConfig class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The object instance.
|
hidden_size
|
The size of the hidden state. Defaults to 768.
TYPE:
|
output_channels
|
The number of output channels. Defaults to 256.
TYPE:
|
num_hidden_layers
|
The number of hidden layers. Defaults to 12.
TYPE:
|
num_attention_heads
|
The number of attention heads. Defaults to 12.
TYPE:
|
num_channels
|
The number of input channels. Defaults to 3.
TYPE:
|
image_size
|
The size of the input image. Defaults to 1024.
TYPE:
|
patch_size
|
The size of each patch in the image. Defaults to 16.
TYPE:
|
hidden_act
|
The activation function for the hidden layers. Defaults to 'gelu'.
TYPE:
|
layer_norm_eps
|
The epsilon value for layer normalization. Defaults to 1e-06.
TYPE:
|
attention_dropout
|
The dropout rate for the attention mechanism. Defaults to 0.0.
TYPE:
|
initializer_range
|
The range for parameter initialization. Defaults to 1e-10.
TYPE:
|
qkv_bias
|
Whether to include bias in the query, key, and value projections. Defaults to True.
TYPE:
|
mlp_ratio
|
The ratio of the hidden size to the feed-forward network size. Defaults to 4.0.
TYPE:
|
use_abs_pos
|
Whether to use absolute position embeddings. Defaults to True.
TYPE:
|
use_rel_pos
|
Whether to use relative position embeddings. Defaults to True.
TYPE:
|
window_size
|
The size of the attention window. Defaults to 14.
TYPE:
|
global_attn_indexes
|
The list of indexes for global attention. Defaults to [2, 5, 8, 11].
TYPE:
|
num_pos_feats
|
The number of positional features. Defaults to 128.
TYPE:
|
mlp_dim
|
The size of the hidden layer in the feed-forward network. If not provided, it is calculated as int(hidden_size * mlp_ratio).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\sam\configuration_sam.py
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 | |
mindnlp.transformers.models.sam.image_processing_sam
¶
Image processor class for SAM.
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor
¶
Bases: BaseImageProcessor
Constructs a SAM image processor.
| PARAMETER | DESCRIPTION |
|---|---|
do_resize
|
Whether to resize the image's (height, width) dimensions to the specified
TYPE:
|
size
|
Size of the output image after resizing. Resizes the longest edge of the image to match
TYPE:
|
mask_size
|
Size of the output segmentation map after resizing. Resizes the longest edge of the image to match
TYPE:
|
resample
|
Resampling filter to use if resizing the image. Can be overridden by the
TYPE:
|
do_rescale
|
Wwhether to rescale the image by the specified scale
TYPE:
|
rescale_factor
|
Scale factor to use if rescaling the image. Only has an effect if
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_pad
|
Whether to pad the image to the specified
TYPE:
|
pad_size
|
Size of the output image after padding. Can be overridden by the
TYPE:
|
mask_pad_size
|
Size of the output segmentation map after padding. Can be overridden by the
TYPE:
|
do_convert_rgb
|
Whether to convert the image to RGB.
TYPE:
|
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 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 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 960 961 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 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 | |
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor.__init__(do_resize=True, size=None, mask_size=None, resample=PILImageResampling.BILINEAR, do_rescale=True, rescale_factor=1 / 255, do_normalize=True, image_mean=None, image_std=None, do_pad=True, pad_size=None, mask_pad_size=None, do_convert_rgb=True, **kwargs)
¶
Initializes an instance of the SamImageProcessor class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
do_resize
|
Determines whether resizing of images should be performed. Defaults to True.
TYPE:
|
size
|
The desired size of the images. Defaults to {'longest_edge': 1024}. The size can be specified as a dictionary with keys 'longest_edge' or 'height' and 'width'. If not provided as a dictionary, it is converted to a dictionary with the 'longest_edge' key.
TYPE:
|
mask_size
|
The desired size of the segmentation masks. Defaults to {'longest_edge': 256}. The size can be specified as a dictionary with keys 'longest_edge' or 'height' and 'width'. If not provided as a dictionary, it is converted to a dictionary with the 'longest_edge' key.
TYPE:
|
resample
|
The resampling method to use during image resizing. Defaults to PILImageResampling.BILINEAR.
TYPE:
|
do_rescale
|
Determines whether rescaling of pixel values should be performed. Defaults to True.
TYPE:
|
rescale_factor
|
The factor to divide pixel values by during rescaling. Defaults to 1 / 255.
TYPE:
|
do_normalize
|
Determines whether normalization of pixel values should be performed. Defaults to True.
TYPE:
|
image_mean
|
The mean values to subtract from pixel values during normalization. Defaults to None, which uses the IMAGENET_DEFAULT_MEAN.
TYPE:
|
image_std
|
The standard deviation values to divide pixel values by during normalization. Defaults to None, which uses the IMAGENET_DEFAULT_STD.
TYPE:
|
do_pad
|
Determines whether padding of images should be performed. Defaults to True.
TYPE:
|
pad_size
|
The desired size of the padded images. Defaults to None, which uses {'height': 1024, 'width': 1024}. The size can be specified as a single integer, representing both height and width.
TYPE:
|
mask_pad_size
|
The desired size of the padded segmentation masks. Defaults to None, which uses {'height': 256, 'width': 256}. The size can be specified as a single integer, representing both height and width.
TYPE:
|
do_convert_rgb
|
Determines whether conversion to RGB color space should be performed. Defaults to True.
TYPE:
|
**kwargs
|
Additional keyword arguments to be passed to the parent class forwardor.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None. |
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
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 | |
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor.filter_masks(masks, iou_scores, original_size, cropped_box_image, pred_iou_thresh=0.88, stability_score_thresh=0.95, mask_threshold=0, stability_score_offset=1, return_tensors='ms')
¶
Filters the predicted masks by selecting only the ones that meets several criteria. The first criterion being
that the iou scores needs to be greater than pred_iou_thresh. The second criterion is that the stability
score needs to be greater than stability_score_thresh. The method also converts the predicted masks to
bounding boxes and pad the predicted masks if necessary.
| PARAMETER | DESCRIPTION |
|---|---|
masks
|
Input masks.
TYPE:
|
iou_scores
|
List of IoU scores.
TYPE:
|
original_size
|
Size of the orginal image.
TYPE:
|
cropped_box_image
|
The cropped image.
TYPE:
|
pred_iou_thresh
|
The threshold for the iou scores.
TYPE:
|
stability_score_thresh
|
The threshold for the stability score.
TYPE:
|
mask_threshold
|
The threshold for the predicted masks.
TYPE:
|
stability_score_offset
|
The offset for the stability score used in the
TYPE:
|
return_tensors
|
If
TYPE:
|
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
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 | |
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor.generate_crop_boxes(image, target_size, crop_n_layers=0, overlap_ratio=512 / 1500, points_per_crop=32, crop_n_points_downscale_factor=1, input_data_format=None, return_tensors='ms')
¶
Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer.
| PARAMETER | DESCRIPTION |
|---|---|
image
|
Input original image
TYPE:
|
target_size
|
Target size of the resized image
TYPE:
|
crop_n_layers
|
If >0, mask prediction will be run again on crops of the image. Sets the number of layers to run, where each layer has 2**i_layer number of image crops.
TYPE:
|
overlap_ratio
|
Sets the degree to which crops overlap. In the first crop layer, crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap.
TYPE:
|
points_per_crop
|
Number of points to sample from each crop.
TYPE:
|
crop_n_points_downscale_factor
|
The number of points-per-side sampled in layer n is scaled down by crop_n_points_downscale_factor**n.
TYPE:
|
input_data_format
|
The channel dimension format of the input image. If not provided, it will be inferred.
TYPE:
|
return_tensors
|
If
TYPE:
|
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
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.sam.image_processing_sam.SamImageProcessor.pad_image(image, pad_size, data_format=None, input_data_format=None, **kwargs)
¶
Pad an image to (pad_size["height"], pad_size["width"]) with zeros to the right and bottom.
| PARAMETER | DESCRIPTION |
|---|---|
image
|
Image to pad.
TYPE:
|
pad_size
|
Size of the output image after padding.
TYPE:
|
data_format
|
The data format of the image. Can be either "channels_first" or "channels_last". If
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\sam\image_processing_sam.py
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 | |
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor.post_process_for_mask_generation(all_masks, all_scores, all_boxes, crops_nms_thresh, return_tensors='ms')
¶
Post processes mask that are generated by calling the Non Maximum Suppression algorithm on the predicted masks.
| PARAMETER | DESCRIPTION |
|---|---|
all_masks
|
List of all predicted segmentation masks
TYPE:
|
all_scores
|
List of all predicted iou scores
TYPE:
|
all_boxes
|
List of all bounding boxes of the predicted masks
TYPE:
|
crops_nms_thresh
|
Threshold for NMS (Non Maximum Suppression) algorithm.
TYPE:
|
return_tensors
|
If
TYPE:
|
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | |
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor.post_process_masks(masks, original_sizes, reshaped_input_sizes, mask_threshold=0.0, binarize=True, pad_size=None, return_tensors='ms')
¶
Remove padding and upscale masks to the original image size.
| PARAMETER | DESCRIPTION |
|---|---|
masks
|
Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format.
TYPE:
|
original_sizes
|
The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format.
TYPE:
|
reshaped_input_sizes
|
The size of each image as it is fed to the model, in (height, width) format. Used to remove padding.
TYPE:
|
mask_threshold
|
The threshold to use for binarizing the masks.
TYPE:
|
binarize
|
Whether to binarize the masks.
TYPE:
|
pad_size
|
The target size the images were padded to before being passed to the model. If None, the target size is
assumed to be the processor's
TYPE:
|
return_tensors
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
`Union[mindspore.Tensor, tf.Tensor]`
|
Batched masks in batch_size, num_channels, height, width) format, where |
|
(height, width) is given by original_size. |
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
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 | |
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor.preprocess(images, segmentation_maps=None, do_resize=None, size=None, mask_size=None, resample=None, do_rescale=None, rescale_factor=None, do_normalize=None, image_mean=None, image_std=None, do_pad=None, pad_size=None, mask_pad_size=None, do_convert_rgb=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
|
Controls the size of the image after
TYPE:
|
mask_size
|
Controls the size of the segmentation map after
TYPE:
|
resample
|
TYPE:
|
do_rescale
|
Whether to rescale the image pixel values by rescaling factor.
TYPE:
|
rescale_factor
|
Rescale factor to apply to the image pixel values.
TYPE:
|
do_normalize
|
Whether to normalize the image.
TYPE:
|
image_mean
|
Image mean to normalize the image by if
TYPE:
|
image_std
|
Image standard deviation to normalize the image by if
TYPE:
|
do_pad
|
Whether to pad the image.
TYPE:
|
pad_size
|
Controls the size of the padding applied to the image. The image is padded to
TYPE:
|
mask_pad_size
|
Controls the size of the padding applied to the segmentation map. The image is padded to
TYPE:
|
do_convert_rgb
|
Whether to convert the image to RGB.
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\sam\image_processing_sam.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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 | |
mindnlp.transformers.models.sam.image_processing_sam.SamImageProcessor.resize(image, size, resample=PILImageResampling.BICUBIC, 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\sam\image_processing_sam.py
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 | |
mindnlp.transformers.models.sam.image_processing_sam.batched_nms(boxes, scores, idxs, iou_threshold)
¶
Performs non-maximum suppression in a batched fashion.
Each index value correspond to a category, and NMS will not be applied between elements of different categories.
| PARAMETER | DESCRIPTION |
|---|---|
boxes
|
boxes where NMS will be performed. They
are expected to be in
TYPE:
|
scores
|
scores for each one of the boxes
TYPE:
|
idxs
|
indices of the categories for each one of the boxes.
TYPE:
|
iou_threshold
|
discards all overlapping boxes with IoU > iou_threshold
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
int64 tensor with the indices of the elements that have been kept by NMS, sorted in decreasing order of scores
TYPE:
|
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
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 | |
mindnlp.transformers.models.sam.image_processing_sam.nms(boxes, scores, iou_threshold)
¶
Performs non-maximum suppression (NMS) on a set of bounding boxes.
| PARAMETER | DESCRIPTION |
|---|---|
boxes
|
A tensor of shape (N, 4) representing the coordinates of the N bounding boxes. Each bounding box is defined by four values: (x_min, y_min, x_max, y_max).
TYPE:
|
scores
|
A tensor of shape (N,) representing the scores associated with each bounding box.
TYPE:
|
iou_threshold
|
The Intersection over Union (IoU) threshold used for NMS. Bounding boxes with IoU greater than or equal to this threshold will be suppressed.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
mindspore.Tensor: A tensor containing the indices of the selected bounding boxes after NMS. The shape of the returned tensor is (M,), where M is the number of selected bounding boxes. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If any of the input arguments are not of the expected type. |
ValueError
|
If the shape of 'boxes' and 'scores' tensors are incompatible or if 'iou_threshold' is not within the valid range. |
Source code in mindnlp\transformers\models\sam\image_processing_sam.py
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 | |
mindnlp.transformers.models.sam.modeling_sam
¶
MindSpore SAM model.
mindnlp.transformers.models.sam.modeling_sam.SamAttention
¶
Bases: Module
SAM's attention layer that allows for downscaling the size of the embedding after projection to queries, keys, and values.
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamImageSegmentationOutput
dataclass
¶
Bases: ModelOutput
Base class for Segment-Anything model's output
| PARAMETER | DESCRIPTION |
|---|---|
iou_scores
|
The iou scores of the predicted masks.
TYPE:
|
pred_masks
|
The predicted low resolutions masks. Needs to be post-processed by the processor
TYPE:
|
vision_hidden_states
|
Tuple of Hidden-states of the vision model at the output of each layer plus the optional initial embedding outputs.
TYPE:
|
vision_attentions
|
Tuple of Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TYPE:
|
mask_decoder_attentions
|
Tuple of Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamLayerNorm
¶
Bases: Module
LayerNorm that supports two data formats: channels_last (default) or channels_first. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch_size, height, width, channels) while channels_first corresponds to inputs with shape (batch_size, channels, height, width).
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamMaskDecoder
¶
Bases: Module
Source code in mindnlp\transformers\models\sam\modeling_sam.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 | |
mindnlp.transformers.models.sam.modeling_sam.SamMaskDecoder.forward(image_embeddings, image_positional_embeddings, sparse_prompt_embeddings, dense_prompt_embeddings, multimask_output, output_attentions=None, attention_similarity=None, target_embedding=None)
¶
Predict masks given image and prompt embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
image_embeddings
|
the embeddings from the image encoder
TYPE:
|
image_positional_embedding
|
positional encoding with the shape of image_embeddings
TYPE:
|
sparse_prompt_embeddings
|
The embeddings of the points and boxes
TYPE:
|
dense_prompt_embeddings
|
the embeddings of the mask inputs
TYPE:
|
multimask_output
|
Whether to return multiple masks or a single mask.
TYPE:
|
output_attentions
|
Whether or not to return the attentions tensors of all attention layers.
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
mindnlp.transformers.models.sam.modeling_sam.SamModel
¶
Bases: SamPreTrainedModel
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 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 | |
mindnlp.transformers.models.sam.modeling_sam.SamModel.forward(pixel_values=None, input_points=None, input_labels=None, input_boxes=None, input_masks=None, image_embeddings=None, multimask_output=True, attention_similarity=None, target_embedding=None, output_attentions=None, output_hidden_states=None, return_dict=None, **kwargs)
¶
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoModel, AutoProcessor
>>> model = AutoModel.from_pretrained("facebook/sam-vit-base")
>>> processor = AutoProcessor.from_pretrained("facebook/sam-vit-base")
>>> img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/sam-car.png"
>>> raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
>>> input_points = [[[400, 650]]] # 2D location of a window on the car
>>> inputs = processor(images=raw_image, input_points=input_points, return_tensors="ms")
>>> # Get segmentation mask
>>> outputs = model(**inputs)
>>> # Postprocess masks
>>> masks = processor.post_process_masks(
... outputs.pred_masks, inputs["original_sizes"], inputs["reshaped_input_sizes"]
... )
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 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 | |
mindnlp.transformers.models.sam.modeling_sam.SamModel.get_image_embeddings(pixel_values, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Returns the image embeddings by passing the pixel values through the vision encoder.
| PARAMETER | DESCRIPTION |
|---|---|
pixel_values
|
Input pixel values
TYPE:
|
output_attentions
|
Whether or not to return the attentions tensors of all attention layers.
TYPE:
|
output_hidden_states
|
Whether or not to return the hidden states of all layers.
TYPE:
|
return_dict
|
Whether or not to return a [
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamModel.get_prompt_embeddings(input_points=None, input_labels=None, input_boxes=None, input_masks=None)
¶
Returns the prompt embeddings by passing the input points, labels, boxes and masks through the prompt encoder.
| PARAMETER | DESCRIPTION |
|---|---|
input_points
|
Optional input points for the prompt encoder. The padding of the point is automatically done by the
processor.
TYPE:
|
input_labels
|
Optional input labels for the prompt encoder. The padding of the labels is automatically done by the processor, or can be fed by the user.
TYPE:
|
input_boxes
|
Optional input boxes for the prompt encoder. The padding of the boxes is automatically done by the processor. users can also pass manually the input boxes.
TYPE:
|
input_masks
|
Optional input masks for the prompt encoder.
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamPatchEmbeddings
¶
Bases: Module
This class turns pixel_values of shape (batch_size, num_channels, height, width) into the initial
hidden_states (patch embeddings) of shape (batch_size, seq_length, hidden_size) to be consumed by a
Transformer.
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamPositionalEmbedding
¶
Bases: Module
Source code in mindnlp\transformers\models\sam\modeling_sam.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
mindnlp.transformers.models.sam.modeling_sam.SamPositionalEmbedding.forward(input_coords, input_shape=None)
¶
Positionally encode points that are normalized to [0,1].
Source code in mindnlp\transformers\models\sam\modeling_sam.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
mindnlp.transformers.models.sam.modeling_sam.SamPromptEncoder
¶
Bases: Module
Source code in mindnlp\transformers\models\sam\modeling_sam.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
mindnlp.transformers.models.sam.modeling_sam.SamPromptEncoder.forward(input_points, input_labels, input_boxes, input_masks)
¶
Embeds different types of prompts, returning both sparse and dense embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
points
|
point coordinates and labels to embed.
TYPE:
|
boxes
|
boxes to embed
TYPE:
|
masks
|
masks to embed
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamTwoWayAttentionBlock
¶
Bases: Module
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamTwoWayAttentionBlock.__init__(config, attention_downsample_rate=2, skip_first_layer_pe=False)
¶
A transformer block with four layers
(1) self-attention of sparse inputs (2) cross attention of sparse inputs -> dense inputs (3) mlp block on sparse inputs (4) cross attention of dense inputs -> sparse inputs
| PARAMETER | DESCRIPTION |
|---|---|
config
|
The configuration file used to instantiate the block
TYPE:
|
attention_downsample_rate
|
The downsample ratio of the block used to reduce the inner dim of the attention.
TYPE:
|
skip_first_layer_pe
|
Whether or not to skip the addition of the query_point_embedding on the first layer.
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamVisionAttention
¶
Bases: Module
Multi-head Attention block with relative position embeddings.
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamVisionAttention.add_decomposed_rel_pos(attn, query, rel_pos_h, rel_pos_w, q_size, k_size)
¶
Calculate decomposed Relative Positional Embeddings from :paper:mvitv2.
https://github.com/facebookresearch/mvit/blob/19786631e330df9f3622e5402b4a419a263a2c80/mvit/models/attention.py
| PARAMETER | DESCRIPTION |
|---|---|
attn
|
attention map.
TYPE:
|
query
|
query q in the attention layer with shape (batch_size, query_height * query_width, channel).
TYPE:
|
rel_pos_h
|
relative position embeddings (Lh, channel) for height axis.
TYPE:
|
rel_pos_w
|
relative position embeddings (Lw, channel) for width axis.
TYPE:
|
q_size
|
spatial sequence size of query q with (query_height, query_width).
TYPE:
|
k_size
|
spatial sequence size of key k with (key_height, key_width).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
attn
|
attention map with added relative positional embeddings.
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamVisionAttention.get_rel_pos(q_size, k_size, rel_pos)
¶
Get relative positional embeddings according to the relative positions of query and key sizes.
| PARAMETER | DESCRIPTION |
|---|---|
q_size
|
size of the query.
TYPE:
|
k_size
|
size of key k.
TYPE:
|
rel_pos
|
relative position embeddings (L, channel).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Extracted positional embeddings according to relative positions. |
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamVisionEncoderOutput
dataclass
¶
Bases: ModelOutput
Base class for sam vision model's outputs that also contains image embeddings obtained by applying the projection layer to the pooler_output.
| PARAMETER | DESCRIPTION |
|---|---|
image_embeds
|
The image embeddings obtained by applying the projection layer to the pooler_output.
TYPE:
|
last_hidden_state
|
Sequence of hidden-states at the output of the last layer of the model.
TYPE:
|
hidden_states
|
Tuple of Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
TYPE:
|
attentions
|
Tuple of Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamVisionLayer
¶
Bases: Module
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.modeling_sam.SamVisionLayer.window_partition(hidden_states, window_size)
¶
Partition into non-overlapping windows with padding if needed. hidden_states (tensor): input tokens with [batch_size, height, width, channel]. window_size (int): window size.
| RETURNS | DESCRIPTION |
|---|---|
windows
|
windows after partition with [batch_size * num_windows, window_size, window_size, channel].
TYPE:
|
(pad_height, pad_width)
|
padded height and width before partition |
Source code in mindnlp\transformers\models\sam\modeling_sam.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 | |
mindnlp.transformers.models.sam.modeling_sam.SamVisionLayer.window_unpartition(windows, window_size, padding_shape, original_shape)
¶
Window unpartition into original sequences and removing padding. hidden_states (tensor): input tokens with [batch_size * num_windows, window_size, window_size, channel]. window_size (int): window size. padding_shape (Tuple): padded height and width (pad_height, pad_width). original_shape (Tuple): original height and width (height, width) before padding.
| RETURNS | DESCRIPTION |
|---|---|
hidden_states
|
unpartitioned sequences with [batch_size, height, width, channel].
TYPE:
|
Source code in mindnlp\transformers\models\sam\modeling_sam.py
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 | |
mindnlp.transformers.models.sam.processing_sam
¶
Processor class for SAM.
mindnlp.transformers.models.sam.processing_sam.SamProcessor
¶
Bases: ProcessorMixin
Constructs a SAM processor which wraps a SAM image processor and an 2D points & Bounding boxes processor into a single processor.
[SamProcessor] offers all the functionalities of [SamImageProcessor]. See the docstring of
[~SamImageProcessor.__call__] for more information.
| PARAMETER | DESCRIPTION |
|---|---|
image_processor
|
An instance of [
TYPE:
|
Source code in mindnlp\transformers\models\sam\processing_sam.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 | |
mindnlp.transformers.models.sam.processing_sam.SamProcessor.model_input_names
property
¶
This method returns a list of unique model input names used in the SamProcessor class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the SamProcessor class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list
|
A list of unique model input names extracted from the image processor. |
mindnlp.transformers.models.sam.processing_sam.SamProcessor.__call__(images=None, segmentation_maps=None, input_points=None, input_labels=None, input_boxes=None, return_tensors=None, **kwargs)
¶
This method uses [SamImageProcessor.__call__] method to prepare image(s) for the model. It also prepares 2D
points and bounding boxes for the model if they are provided.
Source code in mindnlp\transformers\models\sam\processing_sam.py
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.sam.processing_sam.SamProcessor.__init__(image_processor)
¶
Initializes a new instance of the SamProcessor class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the SamProcessor class.
|
image_processor
|
An image processor object used for image processing operations.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\sam\processing_sam.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
mindnlp.transformers.models.sam.processing_sam.SamProcessor.post_process_masks(*args, **kwargs)
¶
Post-processes masks using the image processor.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the SamProcessor class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\sam\processing_sam.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |