π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Add MobileSAM (#2446)
Thien Tran3c39f6467171
diff --git a/kornia/contrib/models/common.py b/kornia/contrib/models/common.pyindex 40ab7e58d..22024dd0c 100644--- a/kornia/contrib/models/common.py+++ b/kornia/contrib/models/common.py@@ -1,5 +1,6 @@from __future__ import annotations+import torchimport torch.nn.functional as Ffrom torch import nn@@ -45,3 +46,41 @@ class MLP(Module):if self.sigmoid_output:x = F.sigmoid(x)return x+++# Adapted from timm+# https://github.com/huggingface/pytorch-image-models/blob/v0.9.2/timm/layers/drop.py#L137+class DropPath(Module):+ """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""++ def __init__(self, drop_prob: float = 0.0, scale_by_keep: bool = True) -> None:+ super().__init__()+ self.drop_prob = drop_prob+ self.scale_by_keep = scale_by_keep++ def forward(self, x: Tensor) -> Tensor:+ if self.drop_prob == 0.0 or not self.training:+ return x+ keep_prob = 1 - self.drop_prob+ shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets+ random_tensor = x.new_empty(shape).bernoulli_(keep_prob)+ if keep_prob > 0.0 and self.scale_by_keep:+ random_tensor.div_(keep_prob)+ return x * random_tensor+++# From https://github.com/facebookresearch/detectron2/blob/main/detectron2/layers/batch_norm.py+# Itself from https://github.com/facebookresearch/ConvNeXt/blob/d1fa8f6fef0a165b27399986cc2bdacc92777e40/models/convnext.py#L119 # noqa+class LayerNorm2d(Module):+ def __init__(self, num_channels: int, eps: float = 1e-6) -> None:+ super().__init__()+ self.weight = nn.Parameter(torch.ones(num_channels))+ self.bias = nn.Parameter(torch.zeros(num_channels))+ self.eps = eps++ def forward(self, x: Tensor) -> Tensor:+ u = x.mean(1, keepdim=True)+ s = (x - u).pow(2).mean(1, keepdim=True)+ x = (x - u) / (s + self.eps).sqrt()+ x = self.weight[:, None, None] * x + self.bias[:, None, None]+ return xdiff --git a/kornia/contrib/models/sam/architecture/common.py b/kornia/contrib/models/sam/architecture/common.pyindex 2a2039f68..1525ddf8e 100644--- a/kornia/contrib/models/sam/architecture/common.py+++ b/kornia/contrib/models/sam/architecture/common.py@@ -7,9 +7,9 @@ from __future__ import annotationsfrom typing import Any-from torch import nn, ones+from torch import nn-from kornia.core import Module, Tensor, zeros+from kornia.core import Module, Tensorclass MLPBlock(Module):@@ -23,23 +23,6 @@ class MLPBlock(Module):return self.lin2(self.act(self.lin1(x)))-# From https://github.com/facebookresearch/detectron2/blob/main/detectron2/layers/batch_norm.py-# Itself from https://github.com/facebookresearch/ConvNeXt/blob/d1fa8f6fef0a165b27399986cc2bdacc92777e40/models/convnext.py#L119 # noqa-class LayerNorm2d(Module):- def __init__(self, num_channels: int, eps: float = 1e-6) -> None:- super().__init__()- self.weight = nn.Parameter(ones(num_channels))- self.bias = nn.Parameter(zeros(num_channels))- self.eps = eps-- def forward(self, x: Tensor) -> Tensor:- u = x.mean(1, keepdim=True)- s = (x - u).pow(2).mean(1, keepdim=True)- x = (x - u) / (s + self.eps).sqrt()- x = self.weight[:, None, None] * x + self.bias[:, None, None]- return x--class LayerNorm(nn.LayerNorm):def __init__(self, *args: Any, **kwargs: Any) -> None:super().__init__(*args, **kwargs)diff --git a/kornia/contrib/models/sam/architecture/image_encoder.py b/kornia/contrib/models/sam/architecture/image_encoder.pyindex a8c8e8d77..b279259a9 100644--- a/kornia/contrib/models/sam/architecture/image_encoder.py+++ b/kornia/contrib/models/sam/architecture/image_encoder.py@@ -9,7 +9,8 @@ import torchimport torch.nn.functional as Ffrom torch import nn-from kornia.contrib.models.sam.architecture.common import LayerNorm2d, MLPBlock+from kornia.contrib.models.common import LayerNorm2d+from kornia.contrib.models.sam.architecture.common import MLPBlockfrom kornia.core import Module, Tensor, pad, zerosdiff --git a/kornia/contrib/models/sam/architecture/mask_decoder.py b/kornia/contrib/models/sam/architecture/mask_decoder.pyindex 38943f0ad..c1817dd96 100644--- a/kornia/contrib/models/sam/architecture/mask_decoder.py+++ b/kornia/contrib/models/sam/architecture/mask_decoder.py@@ -8,8 +8,7 @@ from __future__ import annotationsimport torchfrom torch import nn-from kornia.contrib.models.common import MLP-from kornia.contrib.models.sam.architecture.common import LayerNorm2d+from kornia.contrib.models.common import MLP, LayerNorm2dfrom kornia.core import Module, Tensor, concatenate, stackdiff --git a/kornia/contrib/models/sam/architecture/prompt_encoder.py b/kornia/contrib/models/sam/architecture/prompt_encoder.pyindex 1e45a5cbb..228ec2eef 100644--- a/kornia/contrib/models/sam/architecture/prompt_encoder.py+++ b/kornia/contrib/models/sam/architecture/prompt_encoder.py@@ -10,7 +10,7 @@ from math import piimport torchfrom torch import nn-from kornia.contrib.models.sam.architecture.common import LayerNorm2d+from kornia.contrib.models.common import LayerNorm2dfrom kornia.core import Device, Module, Tensor, concatenate, stack, zerosdiff --git a/kornia/contrib/models/sam/architecture/tiny_vit.py b/kornia/contrib/models/sam/architecture/tiny_vit.pynew file mode 100644index 000000000..fb11a6102--- /dev/null+++ b/kornia/contrib/models/sam/architecture/tiny_vit.py@@ -0,0 +1,478 @@+# https://github.com/microsoft/Cream/blob/8dc38822b99fff8c262c585a32a4f09ac504d693/TinyViT/models/tiny_vit.py+# https://github.com/ChaoningZhang/MobileSAM/blob/01ea8d0f5590082f0c1ceb0a3e2272593f20154b/mobile_sam/modeling/tiny_vit_sam.py+# NOTE: make this available as an image classifier?++from __future__ import annotations++import itertools+from typing import Any++import torch+from torch import nn+from torch.utils import checkpoint++from kornia.contrib.models.common import DropPath, LayerNorm2d+from kornia.contrib.models.sam.architecture.image_encoder import window_partition, window_unpartition+from kornia.core import Module, Tensor+from kornia.core.check import KORNIA_CHECK+++def _make_pair(x: int | tuple[int, int]) -> tuple[int, int]:+ return (x, x) if isinstance(x, int) else x+++class ConvBN(nn.Sequential):+ def __init__(+ self,+ in_channels: int,+ out_channels: int,+ kernel_size: int,+ stride: int = 1,+ padding: int = 0,+ groups: int = 1,+ activation: type[Module] = nn.Identity,+ ) -> None:+ super().__init__()+ self.c = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, groups=groups, bias=False)+ self.bn = nn.BatchNorm2d(out_channels)+ self.act = activation()+++class PatchEmbed(nn.Sequential):+ def __init__(self, in_channels: int, embed_dim: int, activation: type[Module] = nn.GELU) -> None:+ super().__init__()+ self.seq = nn.Sequential(+ ConvBN(in_channels, embed_dim // 2, 3, 2, 1), activation(), ConvBN(embed_dim // 2, embed_dim, 3, 2, 1)+ )+++class MBConv(Module):+ def __init__(+ self,+ in_channels: int,+ out_channels: int,+ expansion_ratio: float,+ activation: type[Module] = nn.GELU,+ drop_path: float = 0.0,+ ) -> None:+ super().__init__()+ hidden_channels = int(in_channels * expansion_ratio)+ self.conv1 = ConvBN(in_channels, hidden_channels, 1, activation=activation) # point-wise+ self.conv2 = ConvBN(+ hidden_channels, hidden_channels, 3, 1, 1, hidden_channels, activation=activation+ ) # depth-wise+ self.conv3 = ConvBN(hidden_channels, out_channels, 1)+ self.drop_path = DropPath(drop_path)+ self.act = activation()++ def forward(self, x: Tensor) -> Tensor:+ return self.act(x + self.drop_path(self.conv3(self.conv2(self.conv1(x)))))+++class PatchMerging(Module):+ def __init__(+ self,+ input_resolution: int | tuple[int, int],+ dim: int,+ out_dim: int,+ stride: int,+ activation: type[Module] = nn.GELU,+ ) -> None:+ KORNIA_CHECK(stride in (1, 2), "stride must be either 1 or 2")+ super().__init__()+ self.input_resolution = _make_pair(input_resolution)+ self.conv1 = ConvBN(dim, out_dim, 1, activation=activation)+ self.conv2 = ConvBN(out_dim, out_dim, 3, stride, 1, groups=out_dim, activation=activation)+ self.conv3 = ConvBN(out_dim, out_dim, 1)++ def forward(self, x: Tensor) -> Tensor:+ if x.ndim == 3:+ x = x.transpose(1, 2).unflatten(2, self.input_resolution) # (B, H * W, C) -> (B, C, H, W)+ x = self.conv3(self.conv2(self.conv1(x)))+ x = x.flatten(2).transpose(1, 2) # (B, C, H, W) -> (B, H * W, C)+ return x+++class ConvLayer(Module):+ def __init__(+ self,+ dim: int,+ depth: int,+ activation: type[Module] = nn.GELU,+ drop_path: float | list[float] = 0.0,+ downsample: Module | None = None,+ use_checkpoint: bool = False,+ conv_expand_ratio: float = 4.0,+ ) -> None:+ super().__init__()+ self.use_checkpoint = use_checkpoint++ # build blocks+ if not isinstance(drop_path, list):+ drop_path = [drop_path] * depth+ self.blocks = nn.ModuleList(+ [MBConv(dim, dim, conv_expand_ratio, activation, drop_path[i]) for i in range(depth)]+ )++ # patch merging layer+ self.downsample = downsample++ def forward(self, x: Tensor) -> Tensor:+ for blk in self.blocks:+ x = checkpoint.checkpoint(blk, x) if self.use_checkpoint else blk(x)+ if self.downsample is not None:+ x = self.downsample(x)+ return x+++class MLP(nn.Sequential):+ def __init__(+ self,+ in_features: int,+ hidden_features: int,+ out_features: int,+ activation: type[Module] = nn.GELU,+ drop: float = 0.0,+ ) -> None:+ super().__init__()+ self.norm = nn.LayerNorm(in_features)+ self.fc1 = nn.Linear(in_features, hidden_features)+ self.act1 = activation()+ self.drop1 = nn.Dropout(drop)+ self.fc2 = nn.Linear(hidden_features, out_features)+ self.drop2 = nn.Dropout(drop)+++# NOTE: differences from image_encoder.Attention:+# - different relative position encoding mechanism (separable/decomposed vs joint)+# - this impl supports attn_ratio (increase output size for value), though it is not used+class Attention(Module):+ def __init__(+ self,+ dim: int,+ key_dim: int,+ num_heads: int = 8,+ attn_ratio: float = 4.0,+ resolution: tuple[int, int] = (14, 14),+ ) -> None:+ super().__init__()+ self.num_heads = num_heads+ self.scale = key_dim**-0.5+ self.key_dim = key_dim+ self.nh_kd = key_dim * num_heads+ self.d = int(attn_ratio * key_dim)+ self.dh = int(attn_ratio * key_dim) * num_heads+ self.attn_ratio = attn_ratio+ h = self.dh + self.nh_kd * 2++ self.norm = nn.LayerNorm(dim)+ self.qkv = nn.Linear(dim, h)+ self.proj = nn.Linear(self.dh, dim)++ indices, attn_offset_size = self.build_attention_bias(resolution)+ self.attention_biases = nn.Parameter(torch.zeros(num_heads, attn_offset_size))+ self.register_buffer('attention_bias_idxs', indices, persistent=False)+ self.attention_bias_idxs: Tensor+ self.ab: Tensor | None = None++ @staticmethod+ def build_attention_bias(resolution: tuple[int, int]) -> tuple[Tensor, int]:+ points = list(itertools.product(range(resolution[0]), range(resolution[1])))+ attention_offsets: dict[tuple[int, int], int] = {}+ idxs: list[int] = []+ for p1 in points:+ for p2 in points:+ offset = (abs(p1[0] - p2[0]), abs(p1[1] - p2[1]))+ if offset not in attention_offsets:+ attention_offsets[offset] = len(attention_offsets)+ idxs.append(attention_offsets[offset])++ N = len(points)+ indices = torch.LongTensor(idxs).view(N, N)+ attn_offset_size = len(attention_offsets)+ return indices, attn_offset_size++ # is this really necessary?+ @torch.no_grad()+ def train(self, mode: bool = True) -> Attention:+ super().train(mode)+ self.ab = None if (mode and self.ab is not None) else self.attention_biases[:, self.attention_bias_idxs]+ return self++ def forward(self, x: Tensor) -> Tensor:+ B, N, _ = x.shape+ x = self.norm(x)+ qkv = self.qkv(x)+ qkv = qkv.view(B, N, self.num_heads, -1).permute(0, 2, 1, 3)+ q, k, v = qkv.split([self.key_dim, self.key_dim, self.d], dim=3)++ bias = self.attention_biases[:, self.attention_bias_idxs] if self.training else self.ab+ attn = (q @ k.transpose(-2, -1)) * self.scale + bias++ attn = attn.softmax(dim=-1)+ x = (attn @ v).transpose(1, 2).reshape(B, N, self.dh)+ x = self.proj(x)+ return x+++class TinyViTBlock(Module):+ def __init__(+ self,+ dim: int,+ input_resolution: int | tuple[int, int],+ num_heads: int,+ window_size: int = 7,+ mlp_ratio: float = 4.0,+ drop: float = 0.0,+ drop_path: float = 0.0,+ local_conv_size: int = 3,+ activation: type[Module] = nn.GELU,+ ) -> None:+ """Create TinyViT Block.++ Args:+ dim (int): Number of input channels.+ input_resolution (tuple[int, int]): Input resolution.+ num_heads (int): Number of attention heads.+ window_size (int): Window size.+ mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.+ drop (float, optional): Dropout rate. Default: 0.0+ drop_path (float, optional): Stochastic depth rate. Default: 0.0+ local_conv_size (int): the kernel size of the convolution between+ Attention and MLP. Default: 3+ activation: the activation function. Default: nn.GELU+ """+ KORNIA_CHECK(dim % num_heads == 0, "dim must be divislbe by num_heads")+ super().__init__()+ self.input_resolution = _make_pair(input_resolution)+ self.window_size = window_size+ head_dim = dim // num_heads++ self.attn = Attention(dim, head_dim, num_heads, 1.0, (window_size, window_size))+ self.drop_path1 = DropPath(drop_path)+ self.local_conv = ConvBN(dim, dim, local_conv_size, 1, local_conv_size // 2, dim)+ self.mlp = MLP(dim, int(dim * mlp_ratio), dim, activation, drop)+ self.drop_path2 = DropPath(drop_path)++ def forward(self, x: Tensor) -> Tensor:+ H, W = self.input_resolution+ B, L, C = x.shape+ res_x = x++ x = x.view(B, H, W, C)+ x, pad_hw = window_partition(x, self.window_size) # (B * num_windows, window_size, window_size, C)+ x = self.attn(x.flatten(1, 2))+ x = window_unpartition(x, self.window_size, pad_hw, (H, W))+ x = x.view(B, L, C)++ x = res_x + self.drop_path1(x)++ x = x.transpose(1, 2).reshape(B, C, H, W)+ x = self.local_conv(x)+ x = x.view(B, C, L).transpose(1, 2)++ x = x + self.drop_path2(self.mlp(x))+ return x+++class BasicLayer(Module):+ def __init__(+ self,+ dim: int,+ input_resolution: int | tuple[int, int],+ depth: int,+ num_heads: int,+ window_size: int,+ mlp_ratio: float = 4.0,+ drop: float = 0.0,+ drop_path: float | list[float] = 0.0,+ downsample: Module | None = None,+ use_checkpoint: bool = False,+ local_conv_size: int = 3,+ activation: type[Module] = nn.GELU,+ ) -> None:+ """A basic TinyViT layer for one stage.++ Args:+ dim (int): Number of input channels.+ input_resolution (tuple[int]): Input resolution.+ depth (int): Number of blocks.+ num_heads (int): Number of attention heads.+ window_size (int): Local window size.+ mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.+ drop (float, optional): Dropout rate. Default: 0.0+ drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0+ downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None+ use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.+ local_conv_size: the kernel size of the depthwise convolution between attention and MLP. Default: 3+ activation: the activation function. Default: nn.GELU+ """+ super().__init__()+ self.use_checkpoint = use_checkpoint++ self.blocks = nn.ModuleList(+ [+ TinyViTBlock(+ dim,+ input_resolution,+ num_heads,+ window_size,+ mlp_ratio,+ drop,+ drop_path[i] if isinstance(drop_path, list) else drop_path,+ local_conv_size,+ activation,+ )+ for i in range(depth)+ ]+ )++ # patch merging layer+ self.downsample = downsample++ def forward(self, x: Tensor) -> Tensor:+ for blk in self.blocks:+ x = checkpoint.checkpoint(blk, x) if self.use_checkpoint else blk(x)+ if self.downsample is not None:+ x = self.downsample(x)+ return x+++class TinyViT(Module):+ def __init__(+ self,+ img_size: int = 224,+ in_chans: int = 3,+ num_classes: int = 1000,+ embed_dims: list[int] = [96, 192, 384, 768],+ depths: list[int] = [2, 2, 6, 2],+ num_heads: list[int] = [3, 6, 12, 24],+ window_sizes: list[int] = [7, 7, 14, 7],+ mlp_ratio: float = 4.0,+ drop_rate: float = 0.0,+ drop_path_rate: float = 0.0,+ use_checkpoint: bool = False,+ mbconv_expand_ratio: float = 4.0,+ local_conv_size: int = 3,+ # layer_lr_decay: float = 1.0,+ activation: type[Module] = nn.GELU,+ mobile_sam: bool = True,+ ) -> None:+ super().__init__()+ self.img_size = img_size+ self.mobile_sam = mobile_sam+ self.neck: Module | None+ if mobile_sam:+ # MobileSAM adjusts the stride to match the total stride of other ViT backbones+ # used in original SAM (stride 16)+ strides = [2, 2, 1, 1]+ self.neck = nn.Sequential(+ nn.Conv2d(embed_dims[-1], 256, 1, bias=False),+ LayerNorm2d(256),+ nn.Conv2d(256, 256, 3, 1, 1, bias=False),+ LayerNorm2d(256),+ )+ else:+ strides = [2, 2, 2, 1]+ self.neck = None++ self.patch_embed = PatchEmbed(in_chans, embed_dims[0], activation)+ input_resolution = img_size // 4++ # NOTE: if we don't support training, this might be unimportant+ # stochastic depth decay rule+ dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))]++ # build layers+ n_layers = len(depths)+ layers = []+ for i_layer, (embed_dim, depth, num_heads_i, window_size, stride) in enumerate(+ zip(embed_dims, depths, num_heads, window_sizes, strides)+ ):+ out_dim = embed_dims[min(i_layer + 1, len(embed_dims) - 1)]+ downsample = (+ PatchMerging(input_resolution, embed_dim, out_dim, stride, activation)+ if (i_layer < n_layers - 1)+ else None+ )+ kwargs: dict[str, Any] = {+ "dim": embed_dim,+ "depth": depth,+ "drop_path": dpr[sum(depths[:i_layer]) : sum(depths[: i_layer + 1])],+ "downsample": downsample,+ "use_checkpoint": use_checkpoint,+ "activation": activation,+ }+ layer: ConvLayer | BasicLayer+ if i_layer == 0:+ layer = ConvLayer(conv_expand_ratio=mbconv_expand_ratio, **kwargs)+ else:+ layer = BasicLayer(+ input_resolution=input_resolution,+ num_heads=num_heads_i,+ window_size=window_size,+ mlp_ratio=mlp_ratio,+ drop=drop_rate,+ local_conv_size=local_conv_size,+ **kwargs,+ )+ layers.append(layer)+ input_resolution //= stride+ self.layers = nn.Sequential(*layers)+ self.feat_size = input_resolution # final feature map size++ # Classifier head+ # NOTE: needs this to load pre-trained weights with strict=True+ # TODO: enable strict=False, or host our own weights+ self.norm_head = nn.LayerNorm(embed_dims[-1])+ self.head = nn.Linear(embed_dims[-1], num_classes)++ def forward(self, x: Tensor) -> Tensor:+ x = self.patch_embed(x)+ x = self.layers(x)++ if self.mobile_sam:+ # MobileSAM+ x = x.unflatten(1, (self.feat_size, self.feat_size)).permute(0, 3, 1, 2)+ x = self.neck(x) # type: ignore+ else:+ # classification+ x = x.mean(1)+ x = self.head(self.norm_head(x))+ return x+++def tiny_vit_5m(img_size: int, **kwargs: Any) -> TinyViT:+ return TinyViT(+ img_size=img_size,+ embed_dims=[64, 128, 160, 320],+ depths=[2, 2, 6, 2],+ num_heads=[2, 4, 5, 10],+ window_sizes=[7, 7, 14, 7],+ drop_path_rate=0.0,+ **kwargs,+ )+++def tiny_vit_11m(img_size: int, **kwargs: Any) -> TinyViT:+ return TinyViT(+ img_size=img_size,+ embed_dims=[64, 128, 256, 448],+ depths=[2, 2, 6, 2],+ num_heads=[2, 4, 8, 14],+ window_sizes=[7, 7, 14, 7],+ drop_path_rate=0.1,+ **kwargs,+ )+++def tiny_vit_21m(img_size: int, **kwargs: Any) -> TinyViT:+ return TinyViT(+ img_size=img_size,+ embed_dims=[96, 192, 384, 576],+ depths=[2, 2, 6, 2],+ num_heads=[3, 6, 12, 18],+ window_sizes=[7, 7, 14, 7],+ drop_path_rate=0.2,+ **kwargs,+ )diff --git a/kornia/contrib/models/sam/model.py b/kornia/contrib/models/sam/model.pyindex 7c5cde292..a9ce78e1b 100644--- a/kornia/contrib/models/sam/model.py+++ b/kornia/contrib/models/sam/model.py@@ -20,6 +20,7 @@ from kornia.contrib.models.sam.architecture.common import LayerNormfrom kornia.contrib.models.sam.architecture.image_encoder import ImageEncoderViTfrom kornia.contrib.models.sam.architecture.mask_decoder import MaskDecoderfrom kornia.contrib.models.sam.architecture.prompt_encoder import PromptEncoder+from kornia.contrib.models.sam.architecture.tiny_vit import TinyViT, tiny_vit_5mfrom kornia.contrib.models.sam.architecture.transformer import TwoWayTransformerfrom kornia.core import Tensorfrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE@@ -31,6 +32,7 @@ class SamModelType(Enum):vit_h = 0vit_l = 1vit_b = 2+ mobile_sam = 3@dataclass@@ -64,7 +66,7 @@ class Sam(ModelBase[SamConfig]):mask_threshold: float = 0.0def __init__(- self, image_encoder: ImageEncoderViT, prompt_encoder: PromptEncoder, mask_decoder: MaskDecoder+ self, image_encoder: ImageEncoderViT | TinyViT, prompt_encoder: PromptEncoder, mask_decoder: MaskDecoder) -> None:"""SAM predicts object masks from an image and input prompts.@@ -99,7 +101,12 @@ class Sam(ModelBase[SamConfig]):if isinstance(model_type, int):model_type = SamModelType(model_type)elif isinstance(model_type, str):- _map_sam_type = {'vit_h': SamModelType.vit_h, 'vit_l': SamModelType.vit_l, 'vit_b': SamModelType.vit_b}+ _map_sam_type = {+ 'vit_h': SamModelType.vit_h,+ 'vit_l': SamModelType.vit_l,+ 'vit_b': SamModelType.vit_b,+ 'mobile_sam': SamModelType.mobile_sam,+ }model_type = _map_sam_type[model_type]if model_type == SamModelType.vit_b:@@ -122,6 +129,33 @@ class Sam(ModelBase[SamConfig]):encoder_num_heads=16,encoder_global_attn_indexes=(7, 15, 23, 31),)++ elif model_type == SamModelType.mobile_sam:+ # TODO: merge this with _build_sam()+ prompt_embed_dim = 256+ image_size = 1024+ vit_patch_size = 16+ image_embedding_size = image_size // vit_patch_size++ model = Sam(+ image_encoder=tiny_vit_5m(image_size, mobile_sam=True),+ prompt_encoder=PromptEncoder(+ embed_dim=prompt_embed_dim,+ image_embedding_size=(image_embedding_size, image_embedding_size),+ input_image_size=(image_size, image_size),+ mask_in_chans=16,+ ),+ mask_decoder=MaskDecoder(+ num_multimask_outputs=3,+ transformer=TwoWayTransformer(depth=2, embedding_dim=prompt_embed_dim, mlp_dim=2048, num_heads=8),+ transformer_dim=prompt_embed_dim,+ iou_head_depth=3,+ iou_head_hidden_dim=256,+ ),+ # pixel_mean=[123.675, 116.28, 103.53],+ # pixel_std=[58.395, 57.12, 57.375],+ )+elif (isinstance(config.encoder_embed_dim, int)and isinstance(config.encoder_depth, int)@@ -134,6 +168,7 @@ class Sam(ModelBase[SamConfig]):encoder_num_heads=config.num_heads,encoder_global_attn_indexes=config.encoder_global_attn_indexes,)+else:raise NotImplementedError('Unexpected config. The model_type should be provide or the encoder configs.')diff --git a/test/contrib/models/test_sam.py b/test/contrib/models/test_sam.pyindex 2359999ba..738f92322 100644--- a/test/contrib/models/test_sam.py+++ b/test/contrib/models/test_sam.py@@ -14,8 +14,17 @@ def _pad_rb(x, size):class TestSam(BaseTester):- def test_smoke(self):- ...+ @pytest.mark.parametrize('model_type', ['vit_b', 'mobile_sam'])+ def test_smoke(self, device, model_type):+ model = Sam.from_config(SamConfig(model_type)).to(device)+ assert isinstance(model, Sam)++ img_size = model.image_encoder.img_size+ inpt = torch.randn(1, 3, img_size, img_size, device=device)+ keypoints = torch.randint(0, img_size, (1, 2, 2), device=device, dtype=torch.float)+ labels = torch.randint(0, 1, (1, 2), device=device, dtype=torch.float)++ model(inpt, [{'points': (keypoints, labels)}], False)@pytest.mark.parametrize('batch_size', [1, 3])@pytest.mark.parametrize('N', [2, 5])@@ -49,7 +58,7 @@ class TestSam(BaseTester):model(inpt, [{}], False)assert 'The number of images (`B`) should match with the length of prompts!' in str(errinfo)- @pytest.mark.parametrize('model_type', ['vit_b', 'vit_l', 'vit_h'])+ @pytest.mark.parametrize('model_type', ['vit_b', 'vit_l', 'vit_h', 'mobile_sam'])def test_config(self, device, model_type):model = Sam.from_config(SamConfig(model_type))model = model.to(device=device)