From f1eb3e3084fe13d854fb188fd9e0d2a784197c12 Mon Sep 17 00:00:00 2001 From: Parth Chadha Date: Wed, 27 Nov 2024 08:49:26 -0800 Subject: [PATCH] Use ascii char for tolerance; remove redundant forward calls in memory_encoder; fix test_with_tolerance function --- .../segment-anything-model-v2/README.md | 6 +++--- .../sam2/modeling/memory_encoder.py | 18 ------------------ .../sam2/modeling/sam2_utils.py | 4 ++-- tripy/tests/test_examples.py | 12 +++--------- 4 files changed, 8 insertions(+), 32 deletions(-) diff --git a/tripy/examples/segment-anything-model-v2/README.md b/tripy/examples/segment-anything-model-v2/README.md index b33cee6bd..2116b29c2 100644 --- a/tripy/examples/segment-anything-model-v2/README.md +++ b/tripy/examples/segment-anything-model-v2/README.md @@ -25,9 +25,9 @@ This is an implementation of SAM2 model ([original repository](https://github.co diff --git a/tripy/examples/segment-anything-model-v2/sam2/modeling/memory_encoder.py b/tripy/examples/segment-anything-model-v2/sam2/modeling/memory_encoder.py index f1233df2d..aa317ebca 100644 --- a/tripy/examples/segment-anything-model-v2/sam2/modeling/memory_encoder.py +++ b/tripy/examples/segment-anything-model-v2/sam2/modeling/memory_encoder.py @@ -74,12 +74,8 @@ def __init__( self.encoder.append(tp.Conv(mask_out_chans, embed_dim, kernel_dims=(1, 1), dtype=self.dtype)) def __call__(self, x): - return self.forward(x) - - def forward(self, x): for l in self.encoder: x = l(x) - return x @@ -117,9 +113,6 @@ def __init__( self.drop_path = Dummy() def __call__(self, x): - return self.forward(x) - - def forward(self, x): input = x x = self.dwconv(x) x = self.norm(x) @@ -146,9 +139,6 @@ def __init__(self, layer, num_layers, dim=None, input_projection=False, dtype="f self.proj = tp.Conv(dim, dim, kernel_dims=(1, 1), dtype=self.dtype) def __call__(self, x): - return self.forward(x) - - def forward(self, x): x = self.proj(x) for layer in self.layers: x = layer(x) @@ -175,14 +165,6 @@ def __call__( pix_feat: tp.Tensor, masks: tp.Tensor, skip_mask_sigmoid: bool = False, - ): - return self.forward(pix_feat, masks, skip_mask_sigmoid) - - def forward( - self, - pix_feat: tp.Tensor, - masks: tp.Tensor, - skip_mask_sigmoid: bool = False, ) -> Tuple[tp.Tensor, tp.Tensor]: if not skip_mask_sigmoid: masks = tp.sigmoid(masks) diff --git a/tripy/examples/segment-anything-model-v2/sam2/modeling/sam2_utils.py b/tripy/examples/segment-anything-model-v2/sam2/modeling/sam2_utils.py index 2b9604a6b..0a64bd69a 100755 --- a/tripy/examples/segment-anything-model-v2/sam2/modeling/sam2_utils.py +++ b/tripy/examples/segment-anything-model-v2/sam2/modeling/sam2_utils.py @@ -14,7 +14,7 @@ # limitations under the License. import copy -from typing import Optional +from typing import Optional, Callable import torch import torch.nn as nn @@ -150,7 +150,7 @@ def __init__( hidden_dim: int, output_dim: int, num_layers: int, - activation: tp.Module = tp.relu, + activation: Callable[[tp.Tensor], tp.Tensor] = tp.relu, sigmoid_output: bool = False, dtype=tp.float32, ) -> None: diff --git a/tripy/tests/test_examples.py b/tripy/tests/test_examples.py index ef42dcb62..4165ecfc2 100644 --- a/tripy/tests/test_examples.py +++ b/tripy/tests/test_examples.py @@ -87,18 +87,12 @@ def __str__(self): @pytest.mark.parametrize("example", EXAMPLES, ids=lambda case: str(case)) def test_examples(example, sandboxed_install_run): - def test_with_tolerance(number, value, tolerance): - tolerance = float(tolerance) / 100 - lower = float(number) * (1 - tolerance) - upper = float(number) * (1 + tolerance) - try: - return lower <= float(value) <= upper - except ValueError: - return False + def test_with_tolerance(expected, actual, tolerance): + return (abs(float(actual) - float(expected)) / float(expected)) * 100 <= tolerance def process_tolerances(expected_output): specs = [] - placeholder_regex = r"{(\d+\.?\d*)±(\d+)%}" + placeholder_regex = r"{(\d+\.?\d*)~(\d+)%}" pattern = expected_output # Replace tolerance patterns with more flexible capture group