Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

docs: state the defaults RandomSnow and the SAM two-way transformer use (#6345)

Jooyoung Lim0b2ccb3a9146
diff --git a/changelog.d/6345.fixed.md b/changelog.d/6345.fixed.md
new file mode 100644
index 000000000..16ef250a5
--- /dev/null
+++ b/changelog.d/6345.fixed.md
@@ -0,0 +1 @@
+The `RandomSnow`, `TwoWayTransformer` and `TwoWayAttentionBlock` docstrings now state the defaults the constructors use (`p=1.0` and `nn.ReLU`) and document `attention_downsample_rate` instead of a nonexistent `attention_type` argument.
diff --git a/kornia/augmentation/_2d/intensity/random_snow.py b/kornia/augmentation/_2d/intensity/random_snow.py
index ee1a4f587..0b4913138 100644
--- a/kornia/augmentation/_2d/intensity/random_snow.py
+++ b/kornia/augmentation/_2d/intensity/random_snow.py
@@ -37,7 +37,7 @@ class RandomSnow(IntensityAugmentationBase2D):
brightness: A tuple of floats (lower and upper bound) of ``1`` or greater that controls the
brightness of the snow.
same_on_batch: If True, apply the same transformation to each image in a batch. Default: False.
- p: Probability of applying the transformation. Default: 0.5.
+ p: Probability of applying the transformation. Default: 1.0.
keepdim: Keep the output torch.Tensor with the same shape as input. Default: False.
Shape:
diff --git a/kornia/models/sam/architecture/transformer.py b/kornia/models/sam/architecture/transformer.py
index eb9026a6e..44f279913 100644
--- a/kornia/models/sam/architecture/transformer.py
+++ b/kornia/models/sam/architecture/transformer.py
@@ -43,8 +43,9 @@ class TwoWayTransformer(nn.Module):
embedding_dim: The dimension of the embeddings.
num_heads: The number of heads in the multi-head attention.
mlp_dim: The dimension of the internal MLP layers.
- activation: The activation function to use. Default: :class:`nn.GELU`.
- attention_type: The type of attention mechanism to apply. Default: "unnorm".
+ activation: The activation function to use. Default: :class:`nn.ReLU`.
+ attention_downsample_rate: The factor by which the cross-attention layers reduce the embedding dimension.
+ Default: 2.
"""
def __init__(
@@ -139,8 +140,9 @@ class TwoWayAttentionBlock(nn.Module):
embedding_dim: The dimension of the embeddings.
num_heads: The number of attention heads.
mlp_dim: The dimension of the internal MLP layers. Default: 2048.
- activation: The activation function to use. Default: :class:`nn.GELU`.
- attention_type: The type of attention mechanism. Default: "unnorm".
+ activation: The activation function to use. Default: :class:`nn.ReLU`.
+ attention_downsample_rate: The factor by which the cross-attention layers reduce the embedding dimension.
+ Default: 2.
skip_first_layer_pe: Whether to skip position encoding in the first layer. Default: False.
"""
diff --git a/tests/augmentation/test_args_docstrings.py b/tests/augmentation/test_args_docstrings.py
index 045333fe5..482dc6bc5 100644
--- a/tests/augmentation/test_args_docstrings.py
+++ b/tests/augmentation/test_args_docstrings.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+import ast
import inspect
import re
@@ -39,6 +40,7 @@ def _documented_args(cls):
K.RandomBrightness,
K.RandomCutMixV2,
K.RandomGaussianBlur,
+ K.RandomSnow,
K.RandomThinPlateSpline,
],
)
@@ -50,3 +52,18 @@ def test_args_block_matches_the_constructor(cls):
signature = list(inspect.signature(cls.__init__).parameters)[1:]
assert sorted(_documented_args(cls)) == sorted(signature)
+
+
[email protected]("cls", [K.RandomSnow])
+def test_documented_defaults_match_the_constructor(cls):
+ # RandomSnow documented `p` as defaulting to 0.5 while the constructor default is 1.0.
+ doc = inspect.getdoc(cls) or ""
+ block = re.search(r"Args:\n(.*?)\n\s*(?:Shape|Returns?|Examples?|Inputs|Convention)", doc, re.S).group(1)
+ signature = inspect.signature(cls.__init__).parameters
+ documented = {
+ match.group(1): ast.literal_eval(match.group(2))
+ for match in re.finditer(r"^\s{4}(\w+):.*?Default: ([\w.]+)\.\s*$", block, re.M)
+ }
+
+ assert documented
+ assert documented == {name: signature[name].default for name in documented}
diff --git a/tests/models/test_sam.py b/tests/models/test_sam.py
index af12a6d81..3c94dbb1b 100644
--- a/tests/models/test_sam.py
+++ b/tests/models/test_sam.py
@@ -15,6 +15,9 @@
# limitations under the License.
#
+import inspect
+import re
+
import pytest
import torch
import torch.nn.functional as F
@@ -23,6 +26,7 @@ from torch import nn
from kornia.core.exceptions import BaseError
from kornia.models.sam import Sam, SamConfig
from kornia.models.sam.architecture.prompt_encoder import PositionEmbeddingRandom, PromptEncoder
+from kornia.models.sam.architecture.transformer import TwoWayAttentionBlock, TwoWayTransformer
from testing.base import BaseTester
@@ -266,3 +270,16 @@ class TestSam(BaseTester):
self.assert_close(alone, batched, rtol=1e-4, atol=1e-4)
after = [m.running_mean for m in encoder.modules() if isinstance(m, nn.BatchNorm2d)]
assert all(torch.equal(a, b) for a, b in zip(after, running_means))
+
+
[email protected]("cls", [TwoWayTransformer, TwoWayAttentionBlock])
+def test_two_way_args_block_matches_the_constructor(cls):
+ # The class docstrings documented a nonexistent `attention_type` argument and an `nn.GELU` activation,
+ # while the constructors take `attention_downsample_rate` and default to `nn.ReLU`.
+ block = (inspect.getdoc(cls) or "").split("Args:\n", 1)[1]
+ signature = inspect.signature(cls.__init__).parameters
+ documented = re.findall(r"^\s{4}(\w+):", block, re.M)
+ activation = re.search(r"^\s{4}activation:.*Default: :class:`nn\.(\w+)`", block, re.M).group(1)
+
+ assert sorted(documented) == sorted(list(signature)[1:])
+ assert activation == signature["activation"].default.__name__