title
stringclasses
1 value
text
stringlengths
30
426k
id
stringlengths
27
30
ultralytics/models/sam/modules/memory_attention.py/MemoryAttentionLayer/__init__ class MemoryAttentionLayer: def __init__( self, d_model: int = 256, dim_feedforward: int = 2048, dropout: float = 0.1, pos_enc_at_attn: bool = False, pos_enc_at_cross_attn_keys: bool = True, pos_enc_at_cross_attn_queries: bool = False, ): """Initializes a memory attention layer with self-attention, cross-attention, and feedforward components.""" super().__init__() self.d_model = d_model self.dim_feedforward = dim_feedforward self.dropout_value = dropout self.self_attn = RoPEAttention(embedding_dim=256, num_heads=1, downsample_rate=1) self.cross_attn_image = RoPEAttention( rope_k_repeat=True, embedding_dim=256, num_heads=1, downsample_rate=1, kv_in_dim=64, ) # Implementation of Feedforward model self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.norm1 = nn.LayerNorm(d_model) self.norm2 = nn.LayerNorm(d_model) self.norm3 = nn.LayerNorm(d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.dropout3 = nn.Dropout(dropout) self.activation = nn.ReLU() # Where to add pos enc self.pos_enc_at_attn = pos_enc_at_attn self.pos_enc_at_cross_attn_queries = pos_enc_at_cross_attn_queries self.pos_enc_at_cross_attn_keys = pos_enc_at_cross_attn_keys
negative_train_query659_01366
ultralytics/models/sam/modules/memory_attention.py/MemoryAttentionLayer/_forward_sa class MemoryAttentionLayer: def _forward_sa(self, tgt, query_pos): """Performs self-attention on input tensor using positional encoding and RoPE attention mechanism.""" tgt2 = self.norm1(tgt) q = k = tgt2 + query_pos if self.pos_enc_at_attn else tgt2 tgt2 = self.self_attn(q, k, v=tgt2) tgt = tgt + self.dropout1(tgt2) return tgt
negative_train_query659_01367
ultralytics/models/sam/modules/memory_attention.py/MemoryAttentionLayer/_forward_ca class MemoryAttentionLayer: def _forward_ca(self, tgt, memory, query_pos, pos, num_k_exclude_rope=0): """Performs cross-attention between target and memory tensors using RoPEAttention mechanism.""" kwds = {} if num_k_exclude_rope > 0: assert isinstance(self.cross_attn_image, RoPEAttention) kwds = {"num_k_exclude_rope": num_k_exclude_rope} # Cross-Attention tgt2 = self.norm2(tgt) tgt2 = self.cross_attn_image( q=tgt2 + query_pos if self.pos_enc_at_cross_attn_queries else tgt2, k=memory + pos if self.pos_enc_at_cross_attn_keys else memory, v=memory, **kwds, ) tgt = tgt + self.dropout2(tgt2) return tgt
negative_train_query659_01368
ultralytics/models/sam/modules/memory_attention.py/MemoryAttentionLayer/forward class MemoryAttentionLayer: def forward( self, tgt, memory, pos: Optional[Tensor] = None, query_pos: Optional[Tensor] = None, num_k_exclude_rope: int = 0, ) -> torch.Tensor: """Processes input tensors using self-attention, cross-attention, and MLP for memory-based attention.""" tgt = self._forward_sa(tgt, query_pos) tgt = self._forward_ca(tgt, memory, query_pos, pos, num_k_exclude_rope) # MLP tgt2 = self.norm3(tgt) tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2)))) tgt = tgt + self.dropout3(tgt2) return tgt
negative_train_query659_01369
ultralytics/models/sam/modules/memory_attention.py/MemoryAttention/__init__ class MemoryAttention: def __init__( self, d_model: int, pos_enc_at_input: bool, layer: nn.Module, num_layers: int, batch_first: bool = True, # Do layers expect batch first input? ): """Initializes MemoryAttention module with layers and normalization for attention processing.""" super().__init__() self.d_model = d_model self.layers = nn.ModuleList([copy.deepcopy(layer) for _ in range(num_layers)]) self.num_layers = num_layers self.norm = nn.LayerNorm(d_model) self.pos_enc_at_input = pos_enc_at_input self.batch_first = batch_first
negative_train_query659_01370
ultralytics/models/sam/modules/memory_attention.py/MemoryAttention/forward class MemoryAttention: def forward( self, curr: torch.Tensor, # self-attention inputs memory: torch.Tensor, # cross-attention inputs curr_pos: Optional[Tensor] = None, # pos_enc for self-attention inputs memory_pos: Optional[Tensor] = None, # pos_enc for cross-attention inputs num_obj_ptr_tokens: int = 0, # number of object pointer *tokens* ): """Processes input tensors through multiple attention layers, applying self and cross-attention mechanisms.""" if isinstance(curr, list): assert isinstance(curr_pos, list) assert len(curr) == len(curr_pos) == 1 curr, curr_pos = ( curr[0], curr_pos[0], ) assert curr.shape[1] == memory.shape[1], "Batch size must be the same for curr and memory" output = curr if self.pos_enc_at_input and curr_pos is not None: output = output + 0.1 * curr_pos if self.batch_first: # Convert to batch first output = output.transpose(0, 1) curr_pos = curr_pos.transpose(0, 1) memory = memory.transpose(0, 1) memory_pos = memory_pos.transpose(0, 1) for layer in self.layers: kwds = {} if isinstance(layer.cross_attn_image, RoPEAttention): kwds = {"num_k_exclude_rope": num_obj_ptr_tokens} output = layer( tgt=output, memory=memory, pos=memory_pos, query_pos=curr_pos, **kwds, ) normed_output = self.norm(output) if self.batch_first: # Convert back to seq first normed_output = normed_output.transpose(0, 1) curr_pos = curr_pos.transpose(0, 1) return normed_output
negative_train_query659_01371
ultralytics/models/sam/modules/tiny_encoder.py/Conv2d_BN/__init__ class Conv2d_BN: def __init__(self, a, b, ks=1, stride=1, pad=0, dilation=1, groups=1, bn_weight_init=1): """Initializes a sequential container with 2D convolution followed by batch normalization.""" super().__init__() self.add_module("c", torch.nn.Conv2d(a, b, ks, stride, pad, dilation, groups, bias=False)) bn = torch.nn.BatchNorm2d(b) torch.nn.init.constant_(bn.weight, bn_weight_init) torch.nn.init.constant_(bn.bias, 0) self.add_module("bn", bn)
negative_train_query659_01372
ultralytics/models/sam/modules/tiny_encoder.py/PatchEmbed/__init__ class PatchEmbed: def __init__(self, in_chans, embed_dim, resolution, activation): """Initializes patch embedding with convolutional layers for image-to-patch conversion and projection.""" super().__init__() img_size: Tuple[int, int] = to_2tuple(resolution) self.patches_resolution = (img_size[0] // 4, img_size[1] // 4) self.num_patches = self.patches_resolution[0] * self.patches_resolution[1] self.in_chans = in_chans self.embed_dim = embed_dim n = embed_dim self.seq = nn.Sequential( Conv2d_BN(in_chans, n // 2, 3, 2, 1), activation(), Conv2d_BN(n // 2, n, 3, 2, 1), )
negative_train_query659_01373
ultralytics/models/sam/modules/tiny_encoder.py/PatchEmbed/forward class PatchEmbed: def forward(self, x): """Processes input tensor through patch embedding sequence, converting images to patch embeddings.""" return self.seq(x)
negative_train_query659_01374
ultralytics/models/sam/modules/tiny_encoder.py/MBConv/__init__ class MBConv: def __init__(self, in_chans, out_chans, expand_ratio, activation, drop_path): """Initializes the MBConv layer with specified input/output channels, expansion ratio, and activation.""" super().__init__() self.in_chans = in_chans self.hidden_chans = int(in_chans * expand_ratio) self.out_chans = out_chans self.conv1 = Conv2d_BN(in_chans, self.hidden_chans, ks=1) self.act1 = activation() self.conv2 = Conv2d_BN(self.hidden_chans, self.hidden_chans, ks=3, stride=1, pad=1, groups=self.hidden_chans) self.act2 = activation() self.conv3 = Conv2d_BN(self.hidden_chans, out_chans, ks=1, bn_weight_init=0.0) self.act3 = activation() # NOTE: `DropPath` is needed only for training. # self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.drop_path = nn.Identity()
negative_train_query659_01375
ultralytics/models/sam/modules/tiny_encoder.py/MBConv/forward class MBConv: def forward(self, x): """Implements the forward pass of MBConv, applying convolutions and skip connection.""" shortcut = x x = self.conv1(x) x = self.act1(x) x = self.conv2(x) x = self.act2(x) x = self.conv3(x) x = self.drop_path(x) x += shortcut return self.act3(x)
negative_train_query659_01376
ultralytics/models/sam/modules/tiny_encoder.py/PatchMerging/__init__ class PatchMerging: def __init__(self, input_resolution, dim, out_dim, activation): """Initializes the PatchMerging module for merging and projecting neighboring patches in feature maps.""" super().__init__() self.input_resolution = input_resolution self.dim = dim self.out_dim = out_dim self.act = activation() self.conv1 = Conv2d_BN(dim, out_dim, 1, 1, 0) stride_c = 1 if out_dim in {320, 448, 576} else 2 self.conv2 = Conv2d_BN(out_dim, out_dim, 3, stride_c, 1, groups=out_dim) self.conv3 = Conv2d_BN(out_dim, out_dim, 1, 1, 0)
negative_train_query659_01377
ultralytics/models/sam/modules/tiny_encoder.py/PatchMerging/forward class PatchMerging: def forward(self, x): """Applies patch merging and dimension projection to the input feature map.""" if x.ndim == 3: H, W = self.input_resolution B = len(x) # (B, C, H, W) x = x.view(B, H, W, -1).permute(0, 3, 1, 2) x = self.conv1(x) x = self.act(x) x = self.conv2(x) x = self.act(x) x = self.conv3(x) return x.flatten(2).transpose(1, 2)
negative_train_query659_01378
ultralytics/models/sam/modules/tiny_encoder.py/ConvLayer/__init__ class ConvLayer: def __init__( self, dim, input_resolution, depth, activation, drop_path=0.0, downsample=None, use_checkpoint=False, out_dim=None, conv_expand_ratio=4.0, ): """ Initializes the ConvLayer with the given dimensions and settings. This layer consists of multiple MobileNetV3-style inverted bottleneck convolutions (MBConv) and optionally applies downsampling to the output. Args: dim (int): The dimensionality of the input and output. input_resolution (Tuple[int, int]): The resolution of the input image. depth (int): The number of MBConv layers in the block. activation (Callable): Activation function applied after each convolution. drop_path (float | List[float]): Drop path rate. Single float or a list of floats for each MBConv. downsample (Optional[Callable]): Function for downsampling the output. None to skip downsampling. use_checkpoint (bool): Whether to use gradient checkpointing to save memory. out_dim (Optional[int]): The dimensionality of the output. None means it will be the same as `dim`. conv_expand_ratio (float): Expansion ratio for the MBConv layers. Examples: >>> input_tensor = torch.randn(1, 64, 56, 56) >>> conv_layer = ConvLayer(64, (56, 56), depth=3, activation=nn.ReLU) >>> output = conv_layer(input_tensor) >>> print(output.shape) """ super().__init__() self.dim = dim self.input_resolution = input_resolution self.depth = depth self.use_checkpoint = use_checkpoint # Build blocks self.blocks = nn.ModuleList( [ MBConv( dim, dim, conv_expand_ratio, activation, drop_path[i] if isinstance(drop_path, list) else drop_path, ) for i in range(depth) ] ) # Patch merging layer self.downsample = ( None if downsample is None else downsample(input_resolution, dim=dim, out_dim=out_dim, activation=activation) )
negative_train_query659_01379
ultralytics/models/sam/modules/tiny_encoder.py/ConvLayer/forward class ConvLayer: def forward(self, x): """Processes input through convolutional layers, applying MBConv blocks and optional downsampling.""" for blk in self.blocks: x = checkpoint.checkpoint(blk, x) if self.use_checkpoint else blk(x) return x if self.downsample is None else self.downsample(x)
negative_train_query659_01380
ultralytics/models/sam/modules/tiny_encoder.py/Mlp/__init__ class Mlp: def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0): """Initializes a multi-layer perceptron with configurable input, hidden, and output dimensions.""" super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.norm = nn.LayerNorm(in_features) self.fc1 = nn.Linear(in_features, hidden_features) self.fc2 = nn.Linear(hidden_features, out_features) self.act = act_layer() self.drop = nn.Dropout(drop)
negative_train_query659_01381
ultralytics/models/sam/modules/tiny_encoder.py/Mlp/forward class Mlp: def forward(self, x): """Applies MLP operations: layer norm, FC layers, activation, and dropout to the input tensor.""" x = self.norm(x) x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) return self.drop(x)
negative_train_query659_01382
ultralytics/models/sam/modules/tiny_encoder.py/Attention/__init__ class Attention: def __init__( self, dim, key_dim, num_heads=8, attn_ratio=4, resolution=(14, 14), ): """ Initializes the Attention module for multi-head attention with spatial awareness. This module implements a multi-head attention mechanism with support for spatial awareness, applying attention biases based on spatial resolution. It includes trainable attention biases for each unique offset between spatial positions in the resolution grid. Args: dim (int): The dimensionality of the input and output. key_dim (int): The dimensionality of the keys and queries. num_heads (int): Number of attention heads. Default is 8. attn_ratio (float): Attention ratio, affecting the dimensions of the value vectors. Default is 4. resolution (Tuple[int, int]): Spatial resolution of the input feature map. Default is (14, 14). Raises: AssertionError: If 'resolution' is not a tuple of length 2. Examples: >>> attn = Attention(dim=256, key_dim=64, num_heads=8, resolution=(14, 14)) >>> x = torch.randn(1, 196, 256) >>> output = attn(x) >>> print(output.shape) torch.Size([1, 196, 256]) """ super().__init__() assert isinstance(resolution, tuple) and len(resolution) == 2, "'resolution' argument not tuple of length 2" self.num_heads = num_heads self.scale = key_dim**-0.5 self.key_dim = key_dim self.nh_kd = 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 + nh_kd * 2 self.norm = nn.LayerNorm(dim) self.qkv = nn.Linear(dim, h) self.proj = nn.Linear(self.dh, dim) points = list(itertools.product(range(resolution[0]), range(resolution[1]))) N = len(points) attention_offsets = {} idxs = [] 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]) self.attention_biases = torch.nn.Parameter(torch.zeros(num_heads, len(attention_offsets))) self.register_buffer("attention_bias_idxs", torch.LongTensor(idxs).view(N, N), persistent=False)
negative_train_query659_01383
ultralytics/models/sam/modules/tiny_encoder.py/Attention/train class Attention: def train(self, mode=True): """Performs multi-head attention with spatial awareness and trainable attention biases.""" super().train(mode) if mode and hasattr(self, "ab"): del self.ab else: self.ab = self.attention_biases[:, self.attention_bias_idxs]
negative_train_query659_01384
ultralytics/models/sam/modules/tiny_encoder.py/Attention/forward class Attention: def forward(self, x): # x """Applies multi-head attention with spatial awareness and trainable attention biases.""" B, N, _ = x.shape # B, N, C # Normalization x = self.norm(x) qkv = self.qkv(x) # (B, N, num_heads, d) q, k, v = qkv.view(B, N, self.num_heads, -1).split([self.key_dim, self.key_dim, self.d], dim=3) # (B, num_heads, N, d) q = q.permute(0, 2, 1, 3) k = k.permute(0, 2, 1, 3) v = v.permute(0, 2, 1, 3) self.ab = self.ab.to(self.attention_biases.device) attn = (q @ k.transpose(-2, -1)) * self.scale + ( self.attention_biases[:, self.attention_bias_idxs] if self.training else self.ab ) attn = attn.softmax(dim=-1) x = (attn @ v).transpose(1, 2).reshape(B, N, self.dh) return self.proj(x)
negative_train_query659_01385
ultralytics/models/sam/modules/tiny_encoder.py/TinyViTBlock/__init__ class TinyViTBlock: def __init__( self, dim, input_resolution, num_heads, window_size=7, mlp_ratio=4.0, drop=0.0, drop_path=0.0, local_conv_size=3, activation=nn.GELU, ): """ Initializes a TinyViT block with self-attention and local convolution. This block is a key component of the TinyViT architecture, combining self-attention mechanisms with local convolutions to process input features efficiently. Args: dim (int): Dimensionality of the input and output features. input_resolution (Tuple[int, int]): Spatial resolution of the input feature map (height, width). num_heads (int): Number of attention heads. window_size (int): Size of the attention window. Must be greater than 0. mlp_ratio (float): Ratio of MLP hidden dimension to embedding dimension. drop (float): Dropout rate. drop_path (float): Stochastic depth rate. local_conv_size (int): Kernel size of the local convolution. activation (torch.nn.Module): Activation function for MLP. Raises: AssertionError: If window_size is not greater than 0. AssertionError: If dim is not divisible by num_heads. Examples: >>> block = TinyViTBlock(dim=192, input_resolution=(14, 14), num_heads=3) >>> input_tensor = torch.randn(1, 196, 192) >>> output = block(input_tensor) >>> print(output.shape) torch.Size([1, 196, 192]) """ super().__init__() self.dim = dim self.input_resolution = input_resolution self.num_heads = num_heads assert window_size > 0, "window_size must be greater than 0" self.window_size = window_size self.mlp_ratio = mlp_ratio # NOTE: `DropPath` is needed only for training. # self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.drop_path = nn.Identity() assert dim % num_heads == 0, "dim must be divisible by num_heads" head_dim = dim // num_heads window_resolution = (window_size, window_size) self.attn = Attention(dim, head_dim, num_heads, attn_ratio=1, resolution=window_resolution) mlp_hidden_dim = int(dim * mlp_ratio) mlp_activation = activation self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=mlp_activation, drop=drop) pad = local_conv_size // 2 self.local_conv = Conv2d_BN(dim, dim, ks=local_conv_size, stride=1, pad=pad, groups=dim)
negative_train_query659_01386
ultralytics/models/sam/modules/tiny_encoder.py/TinyViTBlock/forward class TinyViTBlock: def forward(self, x): """Applies self-attention, local convolution, and MLP operations to the input tensor.""" h, w = self.input_resolution b, hw, c = x.shape # batch, height*width, channels assert hw == h * w, "input feature has wrong size" res_x = x if h == self.window_size and w == self.window_size: x = self.attn(x) else: x = x.view(b, h, w, c) pad_b = (self.window_size - h % self.window_size) % self.window_size pad_r = (self.window_size - w % self.window_size) % self.window_size padding = pad_b > 0 or pad_r > 0 if padding: x = F.pad(x, (0, 0, 0, pad_r, 0, pad_b)) pH, pW = h + pad_b, w + pad_r nH = pH // self.window_size nW = pW // self.window_size # Window partition x = ( x.view(b, nH, self.window_size, nW, self.window_size, c) .transpose(2, 3) .reshape(b * nH * nW, self.window_size * self.window_size, c) ) x = self.attn(x) # Window reverse x = x.view(b, nH, nW, self.window_size, self.window_size, c).transpose(2, 3).reshape(b, pH, pW, c) if padding: x = x[:, :h, :w].contiguous() x = x.view(b, hw, c) x = res_x + self.drop_path(x) x = x.transpose(1, 2).reshape(b, c, h, w) x = self.local_conv(x) x = x.view(b, c, hw).transpose(1, 2) return x + self.drop_path(self.mlp(x))
negative_train_query659_01387
ultralytics/models/sam/modules/tiny_encoder.py/TinyViTBlock/extra_repr class TinyViTBlock: def extra_repr(self) -> str: """ Returns a string representation of the TinyViTBlock's parameters. This method provides a formatted string containing key information about the TinyViTBlock, including its dimension, input resolution, number of attention heads, window size, and MLP ratio. Returns: (str): A formatted string containing the block's parameters. Examples: >>> block = TinyViTBlock(dim=192, input_resolution=(14, 14), num_heads=3, window_size=7, mlp_ratio=4.0) >>> print(block.extra_repr()) dim=192, input_resolution=(14, 14), num_heads=3, window_size=7, mlp_ratio=4.0 """ return ( f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " f"window_size={self.window_size}, mlp_ratio={self.mlp_ratio}" )
negative_train_query659_01388
ultralytics/models/sam/modules/tiny_encoder.py/BasicLayer/__init__ class BasicLayer: def __init__( self, dim, input_resolution, depth, num_heads, window_size, mlp_ratio=4.0, drop=0.0, drop_path=0.0, downsample=None, use_checkpoint=False, local_conv_size=3, activation=nn.GELU, out_dim=None, ): """ Initializes a BasicLayer in the TinyViT architecture. This layer consists of multiple TinyViT blocks and an optional downsampling operation. It is designed to process feature maps at a specific resolution and dimensionality within the TinyViT model. Args: dim (int): Dimensionality of the input and output features. input_resolution (Tuple[int, int]): Spatial resolution of the input feature map (height, width). depth (int): Number of TinyViT blocks in this layer. num_heads (int): Number of attention heads in each TinyViT block. window_size (int): Size of the local window for attention computation. mlp_ratio (float): Ratio of MLP hidden dimension to embedding dimension. drop (float): Dropout rate. drop_path (float | List[float]): Stochastic depth rate. Can be a float or a list of floats for each block. downsample (nn.Module | None): Downsampling layer at the end of the layer. None to skip downsampling. use_checkpoint (bool): Whether to use gradient checkpointing to save memory. local_conv_size (int): Kernel size for the local convolution in each TinyViT block. activation (nn.Module): Activation function used in the MLP. out_dim (int | None): Output dimension after downsampling. None means it will be the same as `dim`. Raises: ValueError: If `drop_path` is a list and its length doesn't match `depth`. Examples: >>> layer = BasicLayer(dim=96, input_resolution=(56, 56), depth=2, num_heads=3, window_size=7) >>> x = torch.randn(1, 56 * 56, 96) >>> output = layer(x) >>> print(output.shape) """ super().__init__() self.dim = dim self.input_resolution = input_resolution self.depth = depth self.use_checkpoint = use_checkpoint # Build blocks self.blocks = nn.ModuleList( [ TinyViTBlock( dim=dim, input_resolution=input_resolution, num_heads=num_heads, window_size=window_size, mlp_ratio=mlp_ratio, drop=drop, drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path, local_conv_size=local_conv_size, activation=activation, ) for i in range(depth) ] ) # Patch merging layer self.downsample = ( None if downsample is None else downsample(input_resolution, dim=dim, out_dim=out_dim, activation=activation) )
negative_train_query659_01389
ultralytics/models/sam/modules/tiny_encoder.py/BasicLayer/forward class BasicLayer: def forward(self, x): """Processes input through TinyViT blocks and optional downsampling.""" for blk in self.blocks: x = checkpoint.checkpoint(blk, x) if self.use_checkpoint else blk(x) return x if self.downsample is None else self.downsample(x)
negative_train_query659_01390
ultralytics/models/sam/modules/tiny_encoder.py/BasicLayer/extra_repr class BasicLayer: def extra_repr(self) -> str: """Returns a string with the layer's parameters for printing.""" return f"dim={self.dim}, input_resolution={self.input_resolution}, depth={self.depth}"
negative_train_query659_01391
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/__init__ class TinyViT: def __init__( self, img_size=224, in_chans=3, num_classes=1000, embed_dims=(96, 192, 384, 768), depths=(2, 2, 6, 2), num_heads=(3, 6, 12, 24), window_sizes=(7, 7, 14, 7), mlp_ratio=4.0, drop_rate=0.0, drop_path_rate=0.1, use_checkpoint=False, mbconv_expand_ratio=4.0, local_conv_size=3, layer_lr_decay=1.0, ): """ Initializes the TinyViT model. This constructor sets up the TinyViT architecture, including patch embedding, multiple layers of attention and convolution blocks, and a classification head. Args: img_size (int): Size of the input image. Default is 224. in_chans (int): Number of input channels. Default is 3. num_classes (int): Number of classes for classification. Default is 1000. embed_dims (Tuple[int, int, int, int]): Embedding dimensions for each stage. Default is (96, 192, 384, 768). depths (Tuple[int, int, int, int]): Number of blocks in each stage. Default is (2, 2, 6, 2). num_heads (Tuple[int, int, int, int]): Number of attention heads in each stage. Default is (3, 6, 12, 24). window_sizes (Tuple[int, int, int, int]): Window sizes for each stage. Default is (7, 7, 14, 7). mlp_ratio (float): Ratio of MLP hidden dim to embedding dim. Default is 4.0. drop_rate (float): Dropout rate. Default is 0.0. drop_path_rate (float): Stochastic depth rate. Default is 0.1. use_checkpoint (bool): Whether to use checkpointing to save memory. Default is False. mbconv_expand_ratio (float): Expansion ratio for MBConv layer. Default is 4.0. local_conv_size (int): Kernel size for local convolutions. Default is 3. layer_lr_decay (float): Layer-wise learning rate decay factor. Default is 1.0. Examples: >>> model = TinyViT(img_size=224, num_classes=1000) >>> x = torch.randn(1, 3, 224, 224) >>> output = model(x) >>> print(output.shape) torch.Size([1, 1000]) """ super().__init__() self.img_size = img_size self.num_classes = num_classes self.depths = depths self.num_layers = len(depths) self.mlp_ratio = mlp_ratio activation = nn.GELU self.patch_embed = PatchEmbed( in_chans=in_chans, embed_dim=embed_dims[0], resolution=img_size, activation=activation ) patches_resolution = self.patch_embed.patches_resolution self.patches_resolution = patches_resolution # Stochastic depth dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # stochastic depth decay rule # Build layers self.layers = nn.ModuleList() for i_layer in range(self.num_layers): kwargs = dict( dim=embed_dims[i_layer], input_resolution=( patches_resolution[0] // (2 ** (i_layer - 1 if i_layer == 3 else i_layer)), patches_resolution[1] // (2 ** (i_layer - 1 if i_layer == 3 else i_layer)), ), # input_resolution=(patches_resolution[0] // (2 ** i_layer), # patches_resolution[1] // (2 ** i_layer)), depth=depths[i_layer], drop_path=dpr[sum(depths[:i_layer]) : sum(depths[: i_layer + 1])], downsample=PatchMerging if (i_layer < self.num_layers - 1) else None, use_checkpoint=use_checkpoint, out_dim=embed_dims[min(i_layer + 1, len(embed_dims) - 1)], activation=activation, ) if i_layer == 0: layer = ConvLayer(conv_expand_ratio=mbconv_expand_ratio, **kwargs) else: layer = BasicLayer( num_heads=num_heads[i_layer], window_size=window_sizes[i_layer], mlp_ratio=self.mlp_ratio, drop=drop_rate, local_conv_size=local_conv_size, **kwargs, ) self.layers.append(layer) # Classifier head self.norm_head = nn.LayerNorm(embed_dims[-1]) self.head = nn.Linear(embed_dims[-1], num_classes) if num_classes > 0 else torch.nn.Identity() # Init weights self.apply(self._init_weights) self.set_layer_lr_decay(layer_lr_decay) self.neck = nn.Sequential( nn.Conv2d( embed_dims[-1], 256, kernel_size=1, bias=False, ), LayerNorm2d(256), nn.Conv2d( 256, 256, kernel_size=3, padding=1, bias=False, ), LayerNorm2d(256), )
negative_train_query659_01392
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/set_layer_lr_decay class TinyViT: def set_layer_lr_decay(self, layer_lr_decay): """Sets layer-wise learning rate decay for the TinyViT model based on depth.""" decay_rate = layer_lr_decay # Layers -> blocks (depth) depth = sum(self.depths) lr_scales = [decay_rate ** (depth - i - 1) for i in range(depth)] def _set_lr_scale(m, scale): """Sets the learning rate scale for each layer in the model based on the layer's depth.""" for p in m.parameters(): p.lr_scale = scale self.patch_embed.apply(lambda x: _set_lr_scale(x, lr_scales[0])) i = 0 for layer in self.layers: for block in layer.blocks: block.apply(lambda x: _set_lr_scale(x, lr_scales[i])) i += 1 if layer.downsample is not None: layer.downsample.apply(lambda x: _set_lr_scale(x, lr_scales[i - 1])) assert i == depth for m in [self.norm_head, self.head]: m.apply(lambda x: _set_lr_scale(x, lr_scales[-1])) for k, p in self.named_parameters(): p.param_name = k def _check_lr_scale(m): """Checks if the learning rate scale attribute is present in module's parameters.""" for p in m.parameters(): assert hasattr(p, "lr_scale"), p.param_name self.apply(_check_lr_scale)
negative_train_query659_01393
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/set_layer_lr_decay/_set_lr_scale class TinyViT: def _set_lr_scale(m, scale): """Sets the learning rate scale for each layer in the model based on the layer's depth.""" for p in m.parameters(): p.lr_scale = scale
negative_train_query659_01394
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/set_layer_lr_decay/_check_lr_scale class TinyViT: def _check_lr_scale(m): """Checks if the learning rate scale attribute is present in module's parameters.""" for p in m.parameters(): assert hasattr(p, "lr_scale"), p.param_name
negative_train_query659_01395
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/_init_weights class TinyViT: def _init_weights(self, m): """Initializes weights for linear and normalization layers in the TinyViT model.""" if isinstance(m, nn.Linear): # NOTE: This initialization is needed only for training. # trunc_normal_(m.weight, std=.02) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.LayerNorm): nn.init.constant_(m.bias, 0) nn.init.constant_(m.weight, 1.0)
negative_train_query659_01396
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/no_weight_decay_keywords class TinyViT: def no_weight_decay_keywords(self): """Returns a set of keywords for parameters that should not use weight decay.""" return {"attention_biases"}
negative_train_query659_01397
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/forward_features class TinyViT: def forward_features(self, x): """Processes input through feature extraction layers, returning spatial features.""" x = self.patch_embed(x) # x input is (N, C, H, W) x = self.layers[0](x) start_i = 1 for i in range(start_i, len(self.layers)): layer = self.layers[i] x = layer(x) batch, _, channel = x.shape x = x.view(batch, self.patches_resolution[0] // 4, self.patches_resolution[1] // 4, channel) x = x.permute(0, 3, 1, 2) return self.neck(x)
negative_train_query659_01398
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/forward class TinyViT: def forward(self, x): """Performs the forward pass through the TinyViT model, extracting features from the input image.""" return self.forward_features(x)
negative_train_query659_01399
ultralytics/models/sam/modules/tiny_encoder.py/TinyViT/set_imgsz class TinyViT: def set_imgsz(self, imgsz=[1024, 1024]): """ Set image size to make model compatible with different image sizes. Args: imgsz (Tuple[int, int]): The size of the input image. """ imgsz = [s // 4 for s in imgsz] self.patches_resolution = imgsz for i, layer in enumerate(self.layers): input_resolution = ( imgsz[0] // (2 ** (i - 1 if i == 3 else i)), imgsz[1] // (2 ** (i - 1 if i == 3 else i)), ) layer.input_resolution = input_resolution if layer.downsample is not None: layer.downsample.input_resolution = input_resolution if isinstance(layer, BasicLayer): for b in layer.blocks: b.input_resolution = input_resolution
negative_train_query659_01400
ultralytics/models/sam/modules/utils.py/select_closest_cond_frames def select_closest_cond_frames(frame_idx, cond_frame_outputs, max_cond_frame_num): """ Selects the closest conditioning frames to a given frame index. Args: frame_idx (int): Current frame index. cond_frame_outputs (Dict[int, Any]): Dictionary of conditioning frame outputs keyed by frame indices. max_cond_frame_num (int): Maximum number of conditioning frames to select. Returns: (Tuple[Dict[int, Any], Dict[int, Any]]): A tuple containing two dictionaries: - selected_outputs: Selected items from cond_frame_outputs. - unselected_outputs: Items not selected from cond_frame_outputs. Examples: >>> frame_idx = 5 >>> cond_frame_outputs = {1: "a", 3: "b", 7: "c", 9: "d"} >>> max_cond_frame_num = 2 >>> selected, unselected = select_closest_cond_frames(frame_idx, cond_frame_outputs, max_cond_frame_num) >>> print(selected) {3: 'b', 7: 'c'} >>> print(unselected) {1: 'a', 9: 'd'} """ if max_cond_frame_num == -1 or len(cond_frame_outputs) <= max_cond_frame_num: selected_outputs = cond_frame_outputs unselected_outputs = {} else: assert max_cond_frame_num >= 2, "we should allow using 2+ conditioning frames" selected_outputs = {} # the closest conditioning frame before `frame_idx` (if any) idx_before = max((t for t in cond_frame_outputs if t < frame_idx), default=None) if idx_before is not None: selected_outputs[idx_before] = cond_frame_outputs[idx_before] # the closest conditioning frame after `frame_idx` (if any) idx_after = min((t for t in cond_frame_outputs if t >= frame_idx), default=None) if idx_after is not None: selected_outputs[idx_after] = cond_frame_outputs[idx_after] # add other temporally closest conditioning frames until reaching a total # of `max_cond_frame_num` conditioning frames. num_remain = max_cond_frame_num - len(selected_outputs) inds_remain = sorted( (t for t in cond_frame_outputs if t not in selected_outputs), key=lambda x: abs(x - frame_idx), )[:num_remain] selected_outputs.update((t, cond_frame_outputs[t]) for t in inds_remain) unselected_outputs = {t: v for t, v in cond_frame_outputs.items() if t not in selected_outputs} return selected_outputs, unselected_outputs
negative_train_query659_01401
ultralytics/models/sam/modules/utils.py/get_1d_sine_pe def get_1d_sine_pe(pos_inds, dim, temperature=10000): """Generates 1D sinusoidal positional embeddings for given positions and dimensions.""" pe_dim = dim // 2 dim_t = torch.arange(pe_dim, dtype=torch.float32, device=pos_inds.device) dim_t = temperature ** (2 * (dim_t // 2) / pe_dim) pos_embed = pos_inds.unsqueeze(-1) / dim_t pos_embed = torch.cat([pos_embed.sin(), pos_embed.cos()], dim=-1) return pos_embed
negative_train_query659_01402
ultralytics/models/sam/modules/utils.py/init_t_xy def init_t_xy(end_x: int, end_y: int): """Initializes 1D and 2D coordinate tensors for a grid of specified dimensions.""" t = torch.arange(end_x * end_y, dtype=torch.float32) t_x = (t % end_x).float() t_y = torch.div(t, end_x, rounding_mode="floor").float() return t_x, t_y
negative_train_query659_01403
ultralytics/models/sam/modules/utils.py/compute_axial_cis def compute_axial_cis(dim: int, end_x: int, end_y: int, theta: float = 10000.0): """Computes axial complex exponential positional encodings for 2D spatial positions in a grid.""" freqs_x = 1.0 / (theta ** (torch.arange(0, dim, 4)[: (dim // 4)].float() / dim)) freqs_y = 1.0 / (theta ** (torch.arange(0, dim, 4)[: (dim // 4)].float() / dim)) t_x, t_y = init_t_xy(end_x, end_y) freqs_x = torch.outer(t_x, freqs_x) freqs_y = torch.outer(t_y, freqs_y) freqs_cis_x = torch.polar(torch.ones_like(freqs_x), freqs_x) freqs_cis_y = torch.polar(torch.ones_like(freqs_y), freqs_y) return torch.cat([freqs_cis_x, freqs_cis_y], dim=-1)
negative_train_query659_01404
ultralytics/models/sam/modules/utils.py/reshape_for_broadcast def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor): """Reshapes frequency tensor for broadcasting with input tensor, ensuring dimensional compatibility.""" ndim = x.ndim assert 0 <= 1 < ndim assert freqs_cis.shape == (x.shape[-2], x.shape[-1]) shape = [d if i >= ndim - 2 else 1 for i, d in enumerate(x.shape)] return freqs_cis.view(*shape)
negative_train_query659_01405
ultralytics/models/sam/modules/utils.py/apply_rotary_enc def apply_rotary_enc( xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor, repeat_freqs_k: bool = False, ): """Applies rotary positional encoding to query and key tensors using complex-valued frequency components.""" xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2)) xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2)) if xk.shape[-2] != 0 else None freqs_cis = reshape_for_broadcast(freqs_cis, xq_) xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(3) if xk_ is None: # no keys to rotate, due to dropout return xq_out.type_as(xq).to(xq.device), xk # repeat freqs along seq_len dim to match k seq_len if repeat_freqs_k: r = xk_.shape[-2] // xq_.shape[-2] freqs_cis = freqs_cis.repeat(*([1] * (freqs_cis.ndim - 2)), r, 1) xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(3) return xq_out.type_as(xq).to(xq.device), xk_out.type_as(xk).to(xk.device)
negative_train_query659_01406
ultralytics/models/sam/modules/utils.py/window_partition def window_partition(x, window_size): """ Partitions input tensor into non-overlapping windows with padding if needed. Args: x (torch.Tensor): Input tensor with shape (B, H, W, C). window_size (int): Size of each window. Returns: (Tuple[torch.Tensor, Tuple[int, int]]): A tuple containing: - windows (torch.Tensor): Partitioned windows with shape (B * num_windows, window_size, window_size, C). - (Hp, Wp) (Tuple[int, int]): Padded height and width before partition. Examples: >>> x = torch.randn(1, 16, 16, 3) >>> windows, (Hp, Wp) = window_partition(x, window_size=4) >>> print(windows.shape, Hp, Wp) torch.Size([16, 4, 4, 3]) 16 16 """ B, H, W, C = x.shape pad_h = (window_size - H % window_size) % window_size pad_w = (window_size - W % window_size) % window_size if pad_h > 0 or pad_w > 0: x = F.pad(x, (0, 0, 0, pad_w, 0, pad_h)) Hp, Wp = H + pad_h, W + pad_w x = x.view(B, Hp // window_size, window_size, Wp // window_size, window_size, C) windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C) return windows, (Hp, Wp)
negative_train_query659_01407
ultralytics/models/sam/modules/utils.py/window_unpartition def window_unpartition(windows, window_size, pad_hw, hw): """ Unpartitions windowed sequences into original sequences and removes padding. This function reverses the windowing process, reconstructing the original input from windowed segments and removing any padding that was added during the windowing process. Args: windows (torch.Tensor): Input tensor of windowed sequences with shape (B * num_windows, window_size, window_size, C), where B is the batch size, num_windows is the number of windows, window_size is the size of each window, and C is the number of channels. window_size (int): Size of each window. pad_hw (Tuple[int, int]): Padded height and width (Hp, Wp) of the input before windowing. hw (Tuple[int, int]): Original height and width (H, W) of the input before padding and windowing. Returns: (torch.Tensor): Unpartitioned sequences with shape (B, H, W, C), where B is the batch size, H and W are the original height and width, and C is the number of channels. Examples: >>> windows = torch.rand(32, 8, 8, 64) # 32 windows of size 8x8 with 64 channels >>> pad_hw = (16, 16) # Padded height and width >>> hw = (15, 14) # Original height and width >>> x = window_unpartition(windows, window_size=8, pad_hw=pad_hw, hw=hw) >>> print(x.shape) torch.Size([1, 15, 14, 64]) """ Hp, Wp = pad_hw H, W = hw B = windows.shape[0] // (Hp * Wp // window_size // window_size) x = windows.view(B, Hp // window_size, Wp // window_size, window_size, window_size, -1) x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, Hp, Wp, -1) if Hp > H or Wp > W: x = x[:, :H, :W, :].contiguous() return x
negative_train_query659_01408
ultralytics/models/sam/modules/utils.py/get_rel_pos def get_rel_pos(q_size: int, k_size: int, rel_pos: torch.Tensor) -> torch.Tensor: """ Extracts relative positional embeddings based on query and key sizes. Args: q_size (int): Size of the query. k_size (int): Size of the key. rel_pos (torch.Tensor): Relative position embeddings with shape (L, C), where L is the maximum relative distance and C is the embedding dimension. Returns: (torch.Tensor): Extracted positional embeddings according to relative positions, with shape (q_size, k_size, C). Examples: >>> q_size, k_size = 8, 16 >>> rel_pos = torch.randn(31, 64) # 31 = 2 * max(8, 16) - 1 >>> extracted_pos = get_rel_pos(q_size, k_size, rel_pos) >>> print(extracted_pos.shape) torch.Size([8, 16, 64]) """ max_rel_dist = int(2 * max(q_size, k_size) - 1) # Interpolate rel pos if needed. if rel_pos.shape[0] != max_rel_dist: # Interpolate rel pos. rel_pos_resized = F.interpolate( rel_pos.reshape(1, rel_pos.shape[0], -1).permute(0, 2, 1), size=max_rel_dist, mode="linear", ) rel_pos_resized = rel_pos_resized.reshape(-1, max_rel_dist).permute(1, 0) else: rel_pos_resized = rel_pos # Scale the coords with short length if shapes for q and k are different. q_coords = torch.arange(q_size)[:, None] * max(k_size / q_size, 1.0) k_coords = torch.arange(k_size)[None, :] * max(q_size / k_size, 1.0) relative_coords = (q_coords - k_coords) + (k_size - 1) * max(q_size / k_size, 1.0) return rel_pos_resized[relative_coords.long()]
negative_train_query659_01409
ultralytics/models/sam/modules/utils.py/add_decomposed_rel_pos def add_decomposed_rel_pos( attn: torch.Tensor, q: torch.Tensor, rel_pos_h: torch.Tensor, rel_pos_w: torch.Tensor, q_size: Tuple[int, int], k_size: Tuple[int, int], ) -> torch.Tensor: """ Adds decomposed Relative Positional Embeddings to the attention map. This function calculates and applies decomposed Relative Positional Embeddings as described in the MVITv2 paper. It enhances the attention mechanism by incorporating spatial relationships between query and key positions. Args: attn (torch.Tensor): Attention map with shape (B, q_h * q_w, k_h * k_w). q (torch.Tensor): Query tensor in the attention layer with shape (B, q_h * q_w, C). rel_pos_h (torch.Tensor): Relative position embeddings for height axis with shape (Lh, C). rel_pos_w (torch.Tensor): Relative position embeddings for width axis with shape (Lw, C). q_size (Tuple[int, int]): Spatial sequence size of query q as (q_h, q_w). k_size (Tuple[int, int]): Spatial sequence size of key k as (k_h, k_w). Returns: (torch.Tensor): Updated attention map with added relative positional embeddings, shape (B, q_h * q_w, k_h * k_w). Examples: >>> B, C, q_h, q_w, k_h, k_w = 1, 64, 8, 8, 8, 8 >>> attn = torch.rand(B, q_h * q_w, k_h * k_w) >>> q = torch.rand(B, q_h * q_w, C) >>> rel_pos_h = torch.rand(2 * max(q_h, k_h) - 1, C) >>> rel_pos_w = torch.rand(2 * max(q_w, k_w) - 1, C) >>> q_size, k_size = (q_h, q_w), (k_h, k_w) >>> updated_attn = add_decomposed_rel_pos(attn, q, rel_pos_h, rel_pos_w, q_size, k_size) >>> print(updated_attn.shape) torch.Size([1, 64, 64]) References: https://github.com/facebookresearch/mvit/blob/main/mvit/models/attention.py """ q_h, q_w = q_size k_h, k_w = k_size Rh = get_rel_pos(q_h, k_h, rel_pos_h) Rw = get_rel_pos(q_w, k_w, rel_pos_w) B, _, dim = q.shape r_q = q.reshape(B, q_h, q_w, dim) rel_h = torch.einsum("bhwc,hkc->bhwk", r_q, Rh) rel_w = torch.einsum("bhwc,wkc->bhwk", r_q, Rw) attn = (attn.view(B, q_h, q_w, k_h, k_w) + rel_h[:, :, :, :, None] + rel_w[:, :, :, None, :]).view( B, q_h * q_w, k_h * k_w ) return attn
negative_train_query659_01410
ultralytics/models/sam/modules/encoders.py/ImageEncoderViT/__init__ class ImageEncoderViT: def __init__( self, img_size: int = 1024, patch_size: int = 16, in_chans: int = 3, embed_dim: int = 768, depth: int = 12, num_heads: int = 12, mlp_ratio: float = 4.0, out_chans: int = 256, qkv_bias: bool = True, norm_layer: Type[nn.Module] = nn.LayerNorm, act_layer: Type[nn.Module] = nn.GELU, use_abs_pos: bool = True, use_rel_pos: bool = False, rel_pos_zero_init: bool = True, window_size: int = 0, global_attn_indexes: Tuple[int, ...] = (), ) -> None: """ Initializes an ImageEncoderViT instance for encoding images using Vision Transformer architecture. Args: img_size (int): Input image size, assumed to be square. patch_size (int): Size of image patches. in_chans (int): Number of input image channels. embed_dim (int): Dimension of patch embeddings. depth (int): Number of transformer blocks. num_heads (int): Number of attention heads in each block. mlp_ratio (float): Ratio of MLP hidden dimension to embedding dimension. out_chans (int): Number of output channels from the neck module. qkv_bias (bool): If True, adds learnable bias to query, key, value projections. norm_layer (Type[nn.Module]): Type of normalization layer to use. act_layer (Type[nn.Module]): Type of activation layer to use. use_abs_pos (bool): If True, uses absolute positional embeddings. use_rel_pos (bool): If True, adds relative positional embeddings to attention maps. rel_pos_zero_init (bool): If True, initializes relative positional parameters to zero. window_size (int): Size of attention window for windowed attention blocks. global_attn_indexes (Tuple[int, ...]): Indices of blocks that use global attention. Attributes: img_size (int): Dimension of input images. patch_embed (PatchEmbed): Module for patch embedding. pos_embed (nn.Parameter | None): Absolute positional embedding for patches. blocks (nn.ModuleList): List of transformer blocks. neck (nn.Sequential): Neck module for final processing. Examples: >>> encoder = ImageEncoderViT(img_size=224, patch_size=16, embed_dim=768, depth=12, num_heads=12) >>> input_image = torch.randn(1, 3, 224, 224) >>> output = encoder(input_image) >>> print(output.shape) """ super().__init__() self.img_size = img_size self.patch_embed = PatchEmbed( kernel_size=(patch_size, patch_size), stride=(patch_size, patch_size), in_chans=in_chans, embed_dim=embed_dim, ) self.pos_embed: Optional[nn.Parameter] = None if use_abs_pos: # Initialize absolute positional embedding with pretrain image size. self.pos_embed = nn.Parameter(torch.zeros(1, img_size // patch_size, img_size // patch_size, embed_dim)) self.blocks = nn.ModuleList() for i in range(depth): block = Block( dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, norm_layer=norm_layer, act_layer=act_layer, use_rel_pos=use_rel_pos, rel_pos_zero_init=rel_pos_zero_init, window_size=window_size if i not in global_attn_indexes else 0, input_size=(img_size // patch_size, img_size // patch_size), ) self.blocks.append(block) self.neck = nn.Sequential( nn.Conv2d( embed_dim, out_chans, kernel_size=1, bias=False, ), LayerNorm2d(out_chans), nn.Conv2d( out_chans, out_chans, kernel_size=3, padding=1, bias=False, ), LayerNorm2d(out_chans), )
negative_train_query659_01411
ultralytics/models/sam/modules/encoders.py/ImageEncoderViT/forward class ImageEncoderViT: def forward(self, x: torch.Tensor) -> torch.Tensor: """Processes input through patch embedding, positional embedding, transformer blocks, and neck module.""" x = self.patch_embed(x) if self.pos_embed is not None: pos_embed = ( F.interpolate(self.pos_embed.permute(0, 3, 1, 2), scale_factor=self.img_size / 1024).permute(0, 2, 3, 1) if self.img_size != 1024 else self.pos_embed ) x = x + pos_embed for blk in self.blocks: x = blk(x) return self.neck(x.permute(0, 3, 1, 2))
negative_train_query659_01412
ultralytics/models/sam/modules/encoders.py/PromptEncoder/__init__ class PromptEncoder: def __init__( self, embed_dim: int, image_embedding_size: Tuple[int, int], input_image_size: Tuple[int, int], mask_in_chans: int, activation: Type[nn.Module] = nn.GELU, ) -> None: """ Initializes the PromptEncoder module for encoding various types of prompts. This module encodes different types of prompts (points, boxes, masks) for input to SAM's mask decoder, producing both sparse and dense embeddings. Args: embed_dim (int): The dimension of the embeddings. image_embedding_size (Tuple[int, int]): The spatial size of the image embedding as (H, W). input_image_size (Tuple[int, int]): The padded size of the input image as (H, W). mask_in_chans (int): The number of hidden channels used for encoding input masks. activation (Type[nn.Module]): The activation function to use when encoding input masks. Attributes: embed_dim (int): Dimension of the embeddings. input_image_size (Tuple[int, int]): Size of the input image as (H, W). image_embedding_size (Tuple[int, int]): Spatial size of the image embedding as (H, W). pe_layer (PositionEmbeddingRandom): Module for random position embedding. num_point_embeddings (int): Number of point embeddings for different types of points. point_embeddings (nn.ModuleList): List of point embeddings. not_a_point_embed (nn.Embedding): Embedding for points that are not part of any label. mask_input_size (Tuple[int, int]): Size of the input mask. mask_downscaling (nn.Sequential): Neural network for downscaling the mask. Examples: >>> prompt_encoder = PromptEncoder(256, (64, 64), (1024, 1024), 16) >>> points = (torch.rand(1, 5, 2), torch.randint(0, 4, (1, 5))) >>> boxes = torch.rand(1, 2, 2) >>> masks = torch.rand(1, 1, 256, 256) >>> sparse_embeddings, dense_embeddings = prompt_encoder(points, boxes, masks) >>> print(sparse_embeddings.shape, dense_embeddings.shape) torch.Size([1, 7, 256]) torch.Size([1, 256, 64, 64]) """ super().__init__() self.embed_dim = embed_dim self.input_image_size = input_image_size self.image_embedding_size = image_embedding_size self.pe_layer = PositionEmbeddingRandom(embed_dim // 2) self.num_point_embeddings: int = 4 # pos/neg point + 2 box corners point_embeddings = [nn.Embedding(1, embed_dim) for _ in range(self.num_point_embeddings)] self.point_embeddings = nn.ModuleList(point_embeddings) self.not_a_point_embed = nn.Embedding(1, embed_dim) self.mask_input_size = (4 * image_embedding_size[0], 4 * image_embedding_size[1]) self.mask_downscaling = nn.Sequential( nn.Conv2d(1, mask_in_chans // 4, kernel_size=2, stride=2), LayerNorm2d(mask_in_chans // 4), activation(), nn.Conv2d(mask_in_chans // 4, mask_in_chans, kernel_size=2, stride=2), LayerNorm2d(mask_in_chans), activation(), nn.Conv2d(mask_in_chans, embed_dim, kernel_size=1), ) self.no_mask_embed = nn.Embedding(1, embed_dim)
negative_train_query659_01413
ultralytics/models/sam/modules/encoders.py/PromptEncoder/get_dense_pe class PromptEncoder: def get_dense_pe(self) -> torch.Tensor: """ Returns the dense positional encoding used for encoding point prompts. This method generates a positional encoding for a dense set of points matching the shape of the image encoding. The encoding is used to provide spatial information to the model when processing point prompts. Returns: (torch.Tensor): Positional encoding tensor with shape (1, embed_dim, H, W), where H and W are the height and width of the image embedding size, respectively. Examples: >>> prompt_encoder = PromptEncoder(256, (64, 64), (1024, 1024), 16) >>> dense_pe = prompt_encoder.get_dense_pe() >>> print(dense_pe.shape) torch.Size([1, 256, 64, 64]) """ return self.pe_layer(self.image_embedding_size).unsqueeze(0)
negative_train_query659_01414
ultralytics/models/sam/modules/encoders.py/PromptEncoder/_embed_points class PromptEncoder: def _embed_points(self, points: torch.Tensor, labels: torch.Tensor, pad: bool) -> torch.Tensor: """Embeds point prompts by applying positional encoding and label-specific embeddings.""" points = points + 0.5 # Shift to center of pixel if pad: padding_point = torch.zeros((points.shape[0], 1, 2), device=points.device) padding_label = -torch.ones((labels.shape[0], 1), device=labels.device) points = torch.cat([points, padding_point], dim=1) labels = torch.cat([labels, padding_label], dim=1) point_embedding = self.pe_layer.forward_with_coords(points, self.input_image_size) point_embedding[labels == -1] = 0.0 point_embedding[labels == -1] += self.not_a_point_embed.weight point_embedding[labels == 0] += self.point_embeddings[0].weight point_embedding[labels == 1] += self.point_embeddings[1].weight point_embedding[labels == 2] += self.point_embeddings[2].weight point_embedding[labels == 3] += self.point_embeddings[3].weight return point_embedding
negative_train_query659_01415
ultralytics/models/sam/modules/encoders.py/PromptEncoder/_embed_boxes class PromptEncoder: def _embed_boxes(self, boxes: torch.Tensor) -> torch.Tensor: """Embeds box prompts by applying positional encoding and adding corner embeddings.""" boxes = boxes + 0.5 # Shift to center of pixel coords = boxes.reshape(-1, 2, 2) corner_embedding = self.pe_layer.forward_with_coords(coords, self.input_image_size) corner_embedding[:, 0, :] += self.point_embeddings[2].weight corner_embedding[:, 1, :] += self.point_embeddings[3].weight return corner_embedding
negative_train_query659_01416
ultralytics/models/sam/modules/encoders.py/PromptEncoder/_embed_masks class PromptEncoder: def _embed_masks(self, masks: torch.Tensor) -> torch.Tensor: """Embeds mask inputs by downscaling and processing through convolutional layers.""" return self.mask_downscaling(masks)
negative_train_query659_01417
ultralytics/models/sam/modules/encoders.py/PromptEncoder/_get_batch_size class PromptEncoder: def _get_batch_size( points: Optional[Tuple[torch.Tensor, torch.Tensor]], boxes: Optional[torch.Tensor], masks: Optional[torch.Tensor], ) -> int: """Gets the batch size of the output given the batch size of the input prompts.""" if points is not None: return points[0].shape[0] elif boxes is not None: return boxes.shape[0] elif masks is not None: return masks.shape[0] else: return 1
negative_train_query659_01418
ultralytics/models/sam/modules/encoders.py/PromptEncoder/_get_device class PromptEncoder: def _get_device(self) -> torch.device: """Returns the device of the first point embedding's weight tensor.""" return self.point_embeddings[0].weight.device
negative_train_query659_01419
ultralytics/models/sam/modules/encoders.py/PromptEncoder/forward class PromptEncoder: def forward( self, points: Optional[Tuple[torch.Tensor, torch.Tensor]], boxes: Optional[torch.Tensor], masks: Optional[torch.Tensor], ) -> Tuple[torch.Tensor, torch.Tensor]: """ Embeds different types of prompts, returning both sparse and dense embeddings. Args: points (Tuple[torch.Tensor, torch.Tensor] | None): Point coordinates and labels to embed. The first tensor contains coordinates with shape (B, N, 2), and the second tensor contains labels with shape (B, N). boxes (torch.Tensor | None): Boxes to embed with shape (B, M, 2, 2), where M is the number of boxes. masks (torch.Tensor | None): Masks to embed with shape (B, 1, H, W). Returns: (Tuple[torch.Tensor, torch.Tensor]): A tuple containing: - sparse_embeddings (torch.Tensor): Sparse embeddings for points and boxes with shape (B, N, embed_dim). - dense_embeddings (torch.Tensor): Dense embeddings for masks of shape (B, embed_dim, embed_H, embed_W). Examples: >>> encoder = PromptEncoder(256, (64, 64), (1024, 1024), 16) >>> points = (torch.rand(1, 5, 2), torch.randint(0, 4, (1, 5))) >>> boxes = torch.rand(1, 2, 2, 2) >>> masks = torch.rand(1, 1, 256, 256) >>> sparse_emb, dense_emb = encoder(points, boxes, masks) >>> print(sparse_emb.shape, dense_emb.shape) torch.Size([1, 7, 256]) torch.Size([1, 256, 64, 64]) """ bs = self._get_batch_size(points, boxes, masks) sparse_embeddings = torch.empty((bs, 0, self.embed_dim), device=self._get_device()) if points is not None: coords, labels = points point_embeddings = self._embed_points(coords, labels, pad=(boxes is None)) sparse_embeddings = torch.cat([sparse_embeddings, point_embeddings], dim=1) if boxes is not None: box_embeddings = self._embed_boxes(boxes) sparse_embeddings = torch.cat([sparse_embeddings, box_embeddings], dim=1) if masks is not None: dense_embeddings = self._embed_masks(masks) else: dense_embeddings = self.no_mask_embed.weight.reshape(1, -1, 1, 1).expand( bs, -1, self.image_embedding_size[0], self.image_embedding_size[1] ) return sparse_embeddings, dense_embeddings
negative_train_query659_01420
ultralytics/models/sam/modules/encoders.py/MemoryEncoder/__init__ class MemoryEncoder: def __init__( self, out_dim, in_dim=256, # in_dim of pix_feats ): """Initializes the MemoryEncoder for encoding pixel features and masks into memory representations.""" super().__init__() self.mask_downsampler = MaskDownSampler(kernel_size=3, stride=2, padding=1) self.pix_feat_proj = nn.Conv2d(in_dim, in_dim, kernel_size=1) self.fuser = Fuser(CXBlock(dim=256), num_layers=2) self.position_encoding = PositionEmbeddingSine(num_pos_feats=64) self.out_proj = nn.Identity() if out_dim != in_dim: self.out_proj = nn.Conv2d(in_dim, out_dim, kernel_size=1)
negative_train_query659_01421
ultralytics/models/sam/modules/encoders.py/MemoryEncoder/forward class MemoryEncoder: def forward( self, pix_feat: torch.Tensor, masks: torch.Tensor, skip_mask_sigmoid: bool = False, ) -> Tuple[torch.Tensor, torch.Tensor]: """Processes pixel features and masks to generate encoded memory representations for segmentation.""" if not skip_mask_sigmoid: masks = F.sigmoid(masks) masks = self.mask_downsampler(masks) # Fuse pix_feats and downsampled masks, in case the visual features are on CPU, cast them to CUDA pix_feat = pix_feat.to(masks.device) x = self.pix_feat_proj(pix_feat) x = x + masks x = self.fuser(x) x = self.out_proj(x) pos = self.position_encoding(x).to(x.dtype) return {"vision_features": x, "vision_pos_enc": [pos]}
negative_train_query659_01422
ultralytics/models/sam/modules/encoders.py/ImageEncoder/__init__ class ImageEncoder: def __init__( self, trunk: nn.Module, neck: nn.Module, scalp: int = 0, ): """Initializes the ImageEncoder with trunk and neck networks for feature extraction and refinement.""" super().__init__() self.trunk = trunk self.neck = neck self.scalp = scalp assert ( self.trunk.channel_list == self.neck.backbone_channel_list ), f"Channel dims of trunk {self.trunk.channel_list} and neck {self.neck.backbone_channel_list} do not match."
negative_train_query659_01423
ultralytics/models/sam/modules/encoders.py/ImageEncoder/forward class ImageEncoder: def forward(self, sample: torch.Tensor): """Encodes input through patch embedding, positional embedding, transformer blocks, and neck module.""" features, pos = self.neck(self.trunk(sample)) if self.scalp > 0: # Discard the lowest resolution features features, pos = features[: -self.scalp], pos[: -self.scalp] src = features[-1] return { "vision_features": src, "vision_pos_enc": pos, "backbone_fpn": features, }
negative_train_query659_01424
ultralytics/models/sam/modules/encoders.py/FpnNeck/__init__ class FpnNeck: def __init__( self, d_model: int, backbone_channel_list: List[int], kernel_size: int = 1, stride: int = 1, padding: int = 0, fpn_interp_model: str = "bilinear", fuse_type: str = "sum", fpn_top_down_levels: Optional[List[int]] = None, ): """ Initializes a modified Feature Pyramid Network (FPN) neck. This FPN variant removes the output convolution and uses bicubic interpolation for feature resizing, similar to ViT positional embedding interpolation. Args: d_model (int): Dimension of the model. backbone_channel_list (List[int]): List of channel dimensions from the backbone. kernel_size (int): Kernel size for the convolutional layers. stride (int): Stride for the convolutional layers. padding (int): Padding for the convolutional layers. fpn_interp_model (str): Interpolation mode for FPN feature resizing. fuse_type (str): Type of feature fusion, either 'sum' or 'avg'. fpn_top_down_levels (Optional[List[int]]): Levels to have top-down features in outputs. Examples: >>> backbone_channels = [64, 128, 256, 512] >>> fpn_neck = FpnNeck(256, backbone_channels) >>> print(fpn_neck) """ super().__init__() self.position_encoding = PositionEmbeddingSine(num_pos_feats=256) self.convs = nn.ModuleList() self.backbone_channel_list = backbone_channel_list for dim in backbone_channel_list: current = nn.Sequential() current.add_module( "conv", nn.Conv2d( in_channels=dim, out_channels=d_model, kernel_size=kernel_size, stride=stride, padding=padding, ), ) self.convs.append(current) self.fpn_interp_model = fpn_interp_model assert fuse_type in {"sum", "avg"} self.fuse_type = fuse_type # levels to have top-down features in its outputs # e.g. if fpn_top_down_levels is [2, 3], then only outputs of level 2 and 3 # have top-down propagation, while outputs of level 0 and level 1 have only # lateral features from the same backbone level. if fpn_top_down_levels is None: # default is to have top-down features on all levels fpn_top_down_levels = range(len(self.convs)) self.fpn_top_down_levels = list(fpn_top_down_levels)
negative_train_query659_01425
ultralytics/models/sam/modules/encoders.py/FpnNeck/forward class FpnNeck: def forward(self, xs: List[torch.Tensor]): """ Performs forward pass through the Feature Pyramid Network (FPN) neck. This method processes a list of input tensors from the backbone through the FPN, applying lateral connections and top-down feature fusion. It generates output feature maps and corresponding positional encodings. Args: xs (List[torch.Tensor]): List of input tensors from the backbone, each with shape (B, C, H, W). Returns: (Tuple[List[torch.Tensor], List[torch.Tensor]]): A tuple containing: - out (List[torch.Tensor]): List of output feature maps after FPN processing, each with shape (B, d_model, H, W). - pos (List[torch.Tensor]): List of positional encodings corresponding to each output feature map. Examples: >>> fpn_neck = FpnNeck(d_model=256, backbone_channel_list=[64, 128, 256, 512]) >>> inputs = [torch.rand(1, c, 32, 32) for c in [64, 128, 256, 512]] >>> outputs, positions = fpn_neck(inputs) >>> print(len(outputs), len(positions)) 4 4 """ out = [None] * len(self.convs) pos = [None] * len(self.convs) assert len(xs) == len(self.convs) # fpn forward pass # see https://github.com/facebookresearch/detectron2/blob/main/detectron2/modeling/backbone/fpn.py prev_features = None # forward in top-down order (from low to high resolution) n = len(self.convs) - 1 for i in range(n, -1, -1): x = xs[i] lateral_features = self.convs[n - i](x) if i in self.fpn_top_down_levels and prev_features is not None: top_down_features = F.interpolate( prev_features.to(dtype=torch.float32), scale_factor=2.0, mode=self.fpn_interp_model, align_corners=(None if self.fpn_interp_model == "nearest" else False), antialias=False, ) prev_features = lateral_features + top_down_features if self.fuse_type == "avg": prev_features /= 2 else: prev_features = lateral_features x_out = prev_features out[i] = x_out pos[i] = self.position_encoding(x_out).to(x_out.dtype) return out, pos
negative_train_query659_01426
ultralytics/models/sam/modules/encoders.py/Hiera/__init__ class Hiera: def __init__( self, embed_dim: int = 96, # initial embed dim num_heads: int = 1, # initial number of heads drop_path_rate: float = 0.0, # stochastic depth q_pool: int = 3, # number of q_pool stages q_stride: Tuple[int, int] = (2, 2), # downsample stride bet. stages stages: Tuple[int, ...] = (2, 3, 16, 3), # blocks per stage dim_mul: float = 2.0, # dim_mul factor at stage shift head_mul: float = 2.0, # head_mul factor at stage shift window_pos_embed_bkg_spatial_size: Tuple[int, int] = (14, 14), # window size per stage, when not using global att. window_spec: Tuple[int, ...] = ( 8, 4, 14, 7, ), # global attn in these blocks global_att_blocks: Tuple[int, ...] = ( 12, 16, 20, ), return_interm_layers=True, # return feats from every stage ): """Initializes the Hiera model, configuring its hierarchical vision transformer architecture.""" super().__init__() assert len(stages) == len(window_spec) self.window_spec = window_spec depth = sum(stages) self.q_stride = q_stride self.stage_ends = [sum(stages[:i]) - 1 for i in range(1, len(stages) + 1)] assert 0 <= q_pool <= len(self.stage_ends[:-1]) self.q_pool_blocks = [x + 1 for x in self.stage_ends[:-1]][:q_pool] self.return_interm_layers = return_interm_layers self.patch_embed = PatchEmbed( embed_dim=embed_dim, kernel_size=(7, 7), stride=(4, 4), padding=(3, 3), ) # Which blocks have global att? self.global_att_blocks = global_att_blocks # Windowed positional embedding (https://arxiv.org/abs/2311.05613) self.window_pos_embed_bkg_spatial_size = window_pos_embed_bkg_spatial_size self.pos_embed = nn.Parameter(torch.zeros(1, embed_dim, *self.window_pos_embed_bkg_spatial_size)) self.pos_embed_window = nn.Parameter(torch.zeros(1, embed_dim, self.window_spec[0], self.window_spec[0])) dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule cur_stage = 1 self.blocks = nn.ModuleList() for i in range(depth): dim_out = embed_dim # lags by a block, so first block of # next stage uses an initial window size # of previous stage and final window size of current stage window_size = self.window_spec[cur_stage - 1] if self.global_att_blocks is not None: window_size = 0 if i in self.global_att_blocks else window_size if i - 1 in self.stage_ends: dim_out = int(embed_dim * dim_mul) num_heads = int(num_heads * head_mul) cur_stage += 1 block = MultiScaleBlock( dim=embed_dim, dim_out=dim_out, num_heads=num_heads, drop_path=dpr[i], q_stride=self.q_stride if i in self.q_pool_blocks else None, window_size=window_size, ) embed_dim = dim_out self.blocks.append(block) self.channel_list = ( [self.blocks[i].dim_out for i in self.stage_ends[::-1]] if return_interm_layers else [self.blocks[-1].dim_out] )
negative_train_query659_01427
ultralytics/models/sam/modules/encoders.py/Hiera/_get_pos_embed class Hiera: def _get_pos_embed(self, hw: Tuple[int, int]) -> torch.Tensor: """Generates positional embeddings by interpolating and combining window and background embeddings.""" h, w = hw window_embed = self.pos_embed_window pos_embed = F.interpolate(self.pos_embed, size=(h, w), mode="bicubic") pos_embed = pos_embed + window_embed.tile([x // y for x, y in zip(pos_embed.shape, window_embed.shape)]) pos_embed = pos_embed.permute(0, 2, 3, 1) return pos_embed
negative_train_query659_01428
ultralytics/models/sam/modules/encoders.py/Hiera/forward class Hiera: def forward(self, x: torch.Tensor) -> List[torch.Tensor]: """Performs forward pass through Hiera model, extracting multiscale features from input images.""" x = self.patch_embed(x) # x: (B, H, W, C) # Add pos embed x = x + self._get_pos_embed(x.shape[1:3]) outputs = [] for i, blk in enumerate(self.blocks): x = blk(x) if (i == self.stage_ends[-1]) or (i in self.stage_ends and self.return_interm_layers): feats = x.permute(0, 3, 1, 2) outputs.append(feats) return outputs
negative_train_query659_01429
ultralytics/models/sam/modules/sam.py/SAMModel/__init__ class SAMModel: def __init__( self, image_encoder: ImageEncoderViT, prompt_encoder: PromptEncoder, mask_decoder: MaskDecoder, pixel_mean: List[float] = (123.675, 116.28, 103.53), pixel_std: List[float] = (58.395, 57.12, 57.375), ) -> None: """ Initialize the SAMModel class to predict object masks from an image and input prompts. Args: image_encoder (ImageEncoderViT): The backbone used to encode the image into image embeddings. prompt_encoder (PromptEncoder): Encodes various types of input prompts. mask_decoder (MaskDecoder): Predicts masks from the image embeddings and encoded prompts. pixel_mean (List[float]): Mean values for normalizing pixels in the input image. pixel_std (List[float]): Std values for normalizing pixels in the input image. Examples: >>> image_encoder = ImageEncoderViT(...) >>> prompt_encoder = PromptEncoder(...) >>> mask_decoder = MaskDecoder(...) >>> sam_model = SAMModel(image_encoder, prompt_encoder, mask_decoder) >>> # Further usage depends on SAMPredictor class Notes: All forward() operations moved to SAMPredictor. """ super().__init__() self.image_encoder = image_encoder self.prompt_encoder = prompt_encoder self.mask_decoder = mask_decoder self.register_buffer("pixel_mean", torch.Tensor(pixel_mean).view(-1, 1, 1), False) self.register_buffer("pixel_std", torch.Tensor(pixel_std).view(-1, 1, 1), False)
negative_train_query659_01430
ultralytics/models/sam/modules/sam.py/SAMModel/set_imgsz class SAMModel: def set_imgsz(self, imgsz): """ Set image size to make model compatible with different image sizes. Args: imgsz (Tuple[int, int]): The size of the input image. """ if hasattr(self.image_encoder, "set_imgsz"): self.image_encoder.set_imgsz(imgsz) self.prompt_encoder.input_image_size = imgsz self.prompt_encoder.image_embedding_size = [x // 16 for x in imgsz] # 16 is fixed as patch size of ViT model self.image_encoder.img_size = imgsz[0]
negative_train_query659_01431
ultralytics/models/sam/modules/sam.py/SAM2Model/__init__ class SAM2Model: def __init__( self, image_encoder, memory_attention, memory_encoder, num_maskmem=7, image_size=512, backbone_stride=16, sigmoid_scale_for_mem_enc=1.0, sigmoid_bias_for_mem_enc=0.0, binarize_mask_from_pts_for_mem_enc=False, use_mask_input_as_output_without_sam=False, max_cond_frames_in_attn=-1, directly_add_no_mem_embed=False, use_high_res_features_in_sam=False, multimask_output_in_sam=False, multimask_min_pt_num=1, multimask_max_pt_num=1, multimask_output_for_tracking=False, use_multimask_token_for_obj_ptr: bool = False, iou_prediction_use_sigmoid=False, memory_temporal_stride_for_eval=1, non_overlap_masks_for_mem_enc=False, use_obj_ptrs_in_encoder=False, max_obj_ptrs_in_encoder=16, add_tpos_enc_to_obj_ptrs=True, proj_tpos_enc_in_obj_ptrs=False, use_signed_tpos_enc_to_obj_ptrs=False, only_obj_ptrs_in_the_past_for_eval=False, pred_obj_scores: bool = False, pred_obj_scores_mlp: bool = False, fixed_no_obj_ptr: bool = False, soft_no_obj_ptr: bool = False, use_mlp_for_obj_ptr_proj: bool = False, no_obj_embed_spatial: bool = False, sam_mask_decoder_extra_args=None, compile_image_encoder: bool = False, ): """ Initializes the SAM2Model for video object segmentation with memory-based tracking. Args: image_encoder (nn.Module): Visual encoder for extracting image features. memory_attention (nn.Module): Module for attending to memory features. memory_encoder (nn.Module): Encoder for generating memory representations. num_maskmem (int): Number of accessible memory frames. Default is 7 (1 input frame + 6 previous frames). image_size (int): Size of input images. backbone_stride (int): Stride of the image backbone output. sigmoid_scale_for_mem_enc (float): Scale factor for mask sigmoid probability. sigmoid_bias_for_mem_enc (float): Bias factor for mask sigmoid probability. binarize_mask_from_pts_for_mem_enc (bool): Whether to binarize sigmoid mask logits on interacted frames with clicks during evaluation. use_mask_input_as_output_without_sam (bool): Whether to directly output the input mask without using SAM prompt encoder and mask decoder on frames with mask input. max_cond_frames_in_attn (int): Maximum number of conditioning frames to participate in memory attention. -1 means no limit. directly_add_no_mem_embed (bool): Whether to directly add no-memory embedding to image feature on the first frame. use_high_res_features_in_sam (bool): Whether to use high-resolution feature maps in the SAM mask decoder. multimask_output_in_sam (bool): Whether to output multiple (3) masks for the first click on initial conditioning frames. multimask_min_pt_num (int): Minimum number of clicks to use multimask output in SAM. multimask_max_pt_num (int): Maximum number of clicks to use multimask output in SAM. multimask_output_for_tracking (bool): Whether to use multimask output for tracking. use_multimask_token_for_obj_ptr (bool): Whether to use multimask tokens for object pointers. iou_prediction_use_sigmoid (bool): Whether to use sigmoid to restrict IoU prediction to [0-1]. memory_temporal_stride_for_eval (int): Memory bank's temporal stride during evaluation. non_overlap_masks_for_mem_enc (bool): Whether to apply non-overlapping constraints on object masks in memory encoder during evaluation. use_obj_ptrs_in_encoder (bool): Whether to cross-attend to object pointers from other frames in the encoder. max_obj_ptrs_in_encoder (int): Maximum number of object pointers from other frames in encoder cross-attention. add_tpos_enc_to_obj_ptrs (bool): Whether to add temporal positional encoding to object pointers in the encoder. proj_tpos_enc_in_obj_ptrs (bool): Whether to add an extra linear projection layer for temporal positional encoding in object pointers. use_signed_tpos_enc_to_obj_ptrs (bool): whether to use signed distance (instead of unsigned absolute distance) in the temporal positional encoding in the object pointers, only relevant when both `use_obj_ptrs_in_encoder=True` and `add_tpos_enc_to_obj_ptrs=True`. only_obj_ptrs_in_the_past_for_eval (bool): Whether to only attend to object pointers in the past during evaluation. pred_obj_scores (bool): Whether to predict if there is an object in the frame. pred_obj_scores_mlp (bool): Whether to use an MLP to predict object scores. fixed_no_obj_ptr (bool): Whether to have a fixed no-object pointer when there is no object present. soft_no_obj_ptr (bool): Whether to mix in no-object pointer softly for easier recovery and error mitigation. use_mlp_for_obj_ptr_proj (bool): Whether to use MLP for object pointer projection. no_obj_embed_spatial (bool): Whether add no obj embedding to spatial frames. sam_mask_decoder_extra_args (Dict | None): Extra arguments for constructing the SAM mask decoder. compile_image_encoder (bool): Whether to compile the image encoder for faster inference. Examples: >>> image_encoder = ImageEncoderViT(...) >>> memory_attention = SAM2TwoWayTransformer(...) >>> memory_encoder = nn.Sequential(...) >>> model = SAM2Model(image_encoder, memory_attention, memory_encoder) >>> image_batch = torch.rand(1, 3, 512, 512) >>> features = model.forward_image(image_batch) >>> track_results = model.track_step(0, True, features, None, None, None, {}) """ super().__init__() # Part 1: the image backbone self.image_encoder = image_encoder # Use level 0, 1, 2 for high-res setting, or just level 2 for the default setting self.use_high_res_features_in_sam = use_high_res_features_in_sam self.num_feature_levels = 3 if use_high_res_features_in_sam else 1 self.use_obj_ptrs_in_encoder = use_obj_ptrs_in_encoder self.max_obj_ptrs_in_encoder = max_obj_ptrs_in_encoder if use_obj_ptrs_in_encoder: # A conv layer to downsample the mask prompt to stride 4 (the same stride as # low-res SAM mask logits) and to change its scales from 0~1 to SAM logit scale, # so that it can be fed into the SAM mask decoder to generate a pointer. self.mask_downsample = torch.nn.Conv2d(1, 1, kernel_size=4, stride=4) self.add_tpos_enc_to_obj_ptrs = add_tpos_enc_to_obj_ptrs if proj_tpos_enc_in_obj_ptrs: assert add_tpos_enc_to_obj_ptrs # these options need to be used together self.proj_tpos_enc_in_obj_ptrs = proj_tpos_enc_in_obj_ptrs self.use_signed_tpos_enc_to_obj_ptrs = use_signed_tpos_enc_to_obj_ptrs self.only_obj_ptrs_in_the_past_for_eval = only_obj_ptrs_in_the_past_for_eval # Part 2: memory attention to condition current frame's visual features # with memories (and obj ptrs) from past frames self.memory_attention = memory_attention self.hidden_dim = memory_attention.d_model # Part 3: memory encoder for the previous frame's outputs self.memory_encoder = memory_encoder self.mem_dim = self.hidden_dim if hasattr(self.memory_encoder, "out_proj") and hasattr(self.memory_encoder.out_proj, "weight"): # if there is compression of memories along channel dim self.mem_dim = self.memory_encoder.out_proj.weight.shape[0] self.num_maskmem = num_maskmem # Number of memories accessible # Temporal encoding of the memories self.maskmem_tpos_enc = torch.nn.Parameter(torch.zeros(num_maskmem, 1, 1, self.mem_dim)) trunc_normal_(self.maskmem_tpos_enc, std=0.02) # a single token to indicate no memory embedding from previous frames self.no_mem_embed = torch.nn.Parameter(torch.zeros(1, 1, self.hidden_dim)) self.no_mem_pos_enc = torch.nn.Parameter(torch.zeros(1, 1, self.hidden_dim)) trunc_normal_(self.no_mem_embed, std=0.02) trunc_normal_(self.no_mem_pos_enc, std=0.02) self.directly_add_no_mem_embed = directly_add_no_mem_embed # Apply sigmoid to the output raw mask logits (to turn them from # range (-inf, +inf) to range (0, 1)) before feeding them into the memory encoder self.sigmoid_scale_for_mem_enc = sigmoid_scale_for_mem_enc self.sigmoid_bias_for_mem_enc = sigmoid_bias_for_mem_enc self.binarize_mask_from_pts_for_mem_enc = binarize_mask_from_pts_for_mem_enc self.non_overlap_masks_for_mem_enc = non_overlap_masks_for_mem_enc self.memory_temporal_stride_for_eval = memory_temporal_stride_for_eval # On frames with mask input, whether to directly output the input mask without # using a SAM prompt encoder + mask decoder self.use_mask_input_as_output_without_sam = use_mask_input_as_output_without_sam self.multimask_output_in_sam = multimask_output_in_sam self.multimask_min_pt_num = multimask_min_pt_num self.multimask_max_pt_num = multimask_max_pt_num self.multimask_output_for_tracking = multimask_output_for_tracking self.use_multimask_token_for_obj_ptr = use_multimask_token_for_obj_ptr self.iou_prediction_use_sigmoid = iou_prediction_use_sigmoid # Part 4: SAM-style prompt encoder (for both mask and point inputs) # and SAM-style mask decoder for the final mask output self.image_size = image_size self.backbone_stride = backbone_stride self.sam_mask_decoder_extra_args = sam_mask_decoder_extra_args self.pred_obj_scores = pred_obj_scores self.pred_obj_scores_mlp = pred_obj_scores_mlp self.fixed_no_obj_ptr = fixed_no_obj_ptr self.soft_no_obj_ptr = soft_no_obj_ptr if self.fixed_no_obj_ptr: assert self.pred_obj_scores assert self.use_obj_ptrs_in_encoder if self.pred_obj_scores and self.use_obj_ptrs_in_encoder: self.no_obj_ptr = torch.nn.Parameter(torch.zeros(1, self.hidden_dim)) trunc_normal_(self.no_obj_ptr, std=0.02) self.use_mlp_for_obj_ptr_proj = use_mlp_for_obj_ptr_proj self.no_obj_embed_spatial = None if no_obj_embed_spatial: self.no_obj_embed_spatial = torch.nn.Parameter(torch.zeros(1, self.mem_dim)) trunc_normal_(self.no_obj_embed_spatial, std=0.02) self._build_sam_heads() self.max_cond_frames_in_attn = max_cond_frames_in_attn # Model compilation if compile_image_encoder: # Compile the forward function (not the full module) to allow loading checkpoints. print("Image encoder compilation is enabled. First forward pass will be slow.") self.image_encoder.forward = torch.compile( self.image_encoder.forward, mode="max-autotune", fullgraph=True, dynamic=False, )
negative_train_query659_01432
ultralytics/models/sam/modules/sam.py/SAM2Model/device class SAM2Model: def device(self): """Returns the device on which the model's parameters are stored.""" return next(self.parameters()).device
negative_train_query659_01433
ultralytics/models/sam/modules/sam.py/SAM2Model/forward class SAM2Model: def forward(self, *args, **kwargs): """Processes image and prompt inputs to generate object masks and scores in video sequences.""" raise NotImplementedError( "Please use the corresponding methods in SAM2VideoPredictor for inference." "See notebooks/video_predictor_example.ipynb for an example." )
negative_train_query659_01434
ultralytics/models/sam/modules/sam.py/SAM2Model/_build_sam_heads class SAM2Model: def _build_sam_heads(self): """Builds SAM-style prompt encoder and mask decoder for image segmentation tasks.""" self.sam_prompt_embed_dim = self.hidden_dim self.sam_image_embedding_size = self.image_size // self.backbone_stride # build PromptEncoder and MaskDecoder from SAM # (their hyperparameters like `mask_in_chans=16` are from SAM code) self.sam_prompt_encoder = PromptEncoder( embed_dim=self.sam_prompt_embed_dim, image_embedding_size=( self.sam_image_embedding_size, self.sam_image_embedding_size, ), input_image_size=(self.image_size, self.image_size), mask_in_chans=16, ) self.sam_mask_decoder = SAM2MaskDecoder( num_multimask_outputs=3, transformer=SAM2TwoWayTransformer( depth=2, embedding_dim=self.sam_prompt_embed_dim, mlp_dim=2048, num_heads=8, ), transformer_dim=self.sam_prompt_embed_dim, iou_head_depth=3, iou_head_hidden_dim=256, use_high_res_features=self.use_high_res_features_in_sam, iou_prediction_use_sigmoid=self.iou_prediction_use_sigmoid, pred_obj_scores=self.pred_obj_scores, pred_obj_scores_mlp=self.pred_obj_scores_mlp, use_multimask_token_for_obj_ptr=self.use_multimask_token_for_obj_ptr, **(self.sam_mask_decoder_extra_args or {}), ) if self.use_obj_ptrs_in_encoder: # a linear projection on SAM output tokens to turn them into object pointers self.obj_ptr_proj = torch.nn.Linear(self.hidden_dim, self.hidden_dim) if self.use_mlp_for_obj_ptr_proj: self.obj_ptr_proj = MLP(self.hidden_dim, self.hidden_dim, self.hidden_dim, 3) else: self.obj_ptr_proj = torch.nn.Identity() if self.proj_tpos_enc_in_obj_ptrs: # a linear projection on temporal positional encoding in object pointers to # avoid potential interference with spatial positional encoding self.obj_ptr_tpos_proj = torch.nn.Linear(self.hidden_dim, self.mem_dim) else: self.obj_ptr_tpos_proj = torch.nn.Identity()
negative_train_query659_01435
ultralytics/models/sam/modules/sam.py/SAM2Model/_forward_sam_heads class SAM2Model: def _forward_sam_heads( self, backbone_features, point_inputs=None, mask_inputs=None, high_res_features=None, multimask_output=False, ): """ Forward pass through SAM prompt encoders and mask heads. This method processes image features and optional point/mask inputs to generate object masks and scores. Args: backbone_features (torch.Tensor): Image features with shape (B, C, H, W). point_inputs (Dict[str, torch.Tensor] | None): Dictionary containing point prompts. 'point_coords': Tensor of shape (B, P, 2) with float32 dtype, containing absolute pixel-unit coordinates in (x, y) format for P input points. 'point_labels': Tensor of shape (B, P) with int32 dtype, where 1 means positive clicks, 0 means negative clicks, and -1 means padding. mask_inputs (torch.Tensor | None): Mask of shape (B, 1, H*16, W*16), float or bool, with the same spatial size as the image. high_res_features (List[torch.Tensor] | None): List of two feature maps with shapes (B, C, 4*H, 4*W) and (B, C, 2*H, 2*W) respectively, used as high-resolution feature maps for SAM decoder. multimask_output (bool): If True, output 3 candidate masks and their IoU estimates; if False, output only 1 mask and its IoU estimate. Returns: (Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]): low_res_multimasks: Tensor of shape (B, M, H*4, W*4) with SAM output mask logits. high_res_multimasks: Tensor of shape (B, M, H*16, W*16) with upsampled mask logits. ious: Tensor of shape (B, M) with estimated IoU for each output mask. low_res_masks: Tensor of shape (B, 1, H*4, W*4) with best low-resolution mask. high_res_masks: Tensor of shape (B, 1, H*16, W*16) with best high-resolution mask. obj_ptr: Tensor of shape (B, C) with object pointer vector for the output mask. object_score_logits: Tensor of shape (B,) with object score logits. Where M is 3 if multimask_output=True, and 1 if multimask_output=False. Examples: >>> backbone_features = torch.rand(1, 256, 32, 32) >>> point_inputs = {"point_coords": torch.rand(1, 2, 2), "point_labels": torch.tensor([[1, 0]])} >>> mask_inputs = torch.rand(1, 1, 512, 512) >>> results = model._forward_sam_heads(backbone_features, point_inputs, mask_inputs) >>> ( ... low_res_multimasks, ... high_res_multimasks, ... ious, ... low_res_masks, ... high_res_masks, ... obj_ptr, ... object_score_logits, ... ) = results """ B = backbone_features.size(0) device = backbone_features.device assert backbone_features.size(1) == self.sam_prompt_embed_dim assert backbone_features.size(2) == self.sam_image_embedding_size assert backbone_features.size(3) == self.sam_image_embedding_size # a) Handle point prompts if point_inputs is not None: sam_point_coords = point_inputs["point_coords"] sam_point_labels = point_inputs["point_labels"] assert sam_point_coords.size(0) == B and sam_point_labels.size(0) == B else: # If no points are provide, pad with an empty point (with label -1) sam_point_coords = torch.zeros(B, 1, 2, device=device) sam_point_labels = -torch.ones(B, 1, dtype=torch.int32, device=device) # b) Handle mask prompts if mask_inputs is not None: # If mask_inputs is provided, downsize it into low-res mask input if needed # and feed it as a dense mask prompt into the SAM mask encoder assert len(mask_inputs.shape) == 4 and mask_inputs.shape[:2] == (B, 1) if mask_inputs.shape[-2:] != self.sam_prompt_encoder.mask_input_size: sam_mask_prompt = F.interpolate( mask_inputs.float(), size=self.sam_prompt_encoder.mask_input_size, align_corners=False, mode="bilinear", antialias=True, # use antialias for downsampling ) else: sam_mask_prompt = mask_inputs else: # Otherwise, simply feed None (and SAM's prompt encoder will add # a learned `no_mask_embed` to indicate no mask input in this case). sam_mask_prompt = None sparse_embeddings, dense_embeddings = self.sam_prompt_encoder( points=(sam_point_coords, sam_point_labels), boxes=None, masks=sam_mask_prompt, ) ( low_res_multimasks, ious, sam_output_tokens, object_score_logits, ) = self.sam_mask_decoder( image_embeddings=backbone_features, image_pe=self.sam_prompt_encoder.get_dense_pe(), sparse_prompt_embeddings=sparse_embeddings, dense_prompt_embeddings=dense_embeddings, multimask_output=multimask_output, repeat_image=False, # the image is already batched high_res_features=high_res_features, ) if self.pred_obj_scores: is_obj_appearing = object_score_logits > 0 # Mask used for spatial memories is always a *hard* choice between obj and no obj, # consistent with the actual mask prediction low_res_multimasks = torch.where( is_obj_appearing[:, None, None], low_res_multimasks, NO_OBJ_SCORE, ) # convert masks from possibly bfloat16 (or float16) to float32 # (older PyTorch versions before 2.1 don't support `interpolate` on bf16) low_res_multimasks = low_res_multimasks.float() high_res_multimasks = F.interpolate( low_res_multimasks, size=(self.image_size, self.image_size), mode="bilinear", align_corners=False, ) sam_output_token = sam_output_tokens[:, 0] if multimask_output: # take the best mask prediction (with the highest IoU estimation) best_iou_inds = torch.argmax(ious, dim=-1) batch_inds = torch.arange(B, device=device) low_res_masks = low_res_multimasks[batch_inds, best_iou_inds].unsqueeze(1) high_res_masks = high_res_multimasks[batch_inds, best_iou_inds].unsqueeze(1) if sam_output_tokens.size(1) > 1: sam_output_token = sam_output_tokens[batch_inds, best_iou_inds] else: low_res_masks, high_res_masks = low_res_multimasks, high_res_multimasks # Extract object pointer from the SAM output token (with occlusion handling) obj_ptr = self.obj_ptr_proj(sam_output_token) if self.pred_obj_scores: # Allow *soft* no obj ptr, unlike for masks if self.soft_no_obj_ptr: lambda_is_obj_appearing = object_score_logits.sigmoid() else: lambda_is_obj_appearing = is_obj_appearing.float() if self.fixed_no_obj_ptr: obj_ptr = lambda_is_obj_appearing * obj_ptr obj_ptr = obj_ptr + (1 - lambda_is_obj_appearing) * self.no_obj_ptr return ( low_res_multimasks, high_res_multimasks, ious, low_res_masks, high_res_masks, obj_ptr, object_score_logits, )
negative_train_query659_01436
ultralytics/models/sam/modules/sam.py/SAM2Model/_use_mask_as_output class SAM2Model: def _use_mask_as_output(self, backbone_features, high_res_features, mask_inputs): """Processes mask inputs directly as output, bypassing SAM encoder/decoder.""" # Use -10/+10 as logits for neg/pos pixels (very close to 0/1 in prob after sigmoid). out_scale, out_bias = 20.0, -10.0 # sigmoid(-10.0)=4.5398e-05 mask_inputs_float = mask_inputs.float() high_res_masks = mask_inputs_float * out_scale + out_bias low_res_masks = F.interpolate( high_res_masks, size=(high_res_masks.size(-2) // 4, high_res_masks.size(-1) // 4), align_corners=False, mode="bilinear", antialias=True, # use antialias for downsampling ) # a dummy IoU prediction of all 1's under mask input ious = mask_inputs.new_ones(mask_inputs.size(0), 1).float() if not self.use_obj_ptrs_in_encoder: # all zeros as a dummy object pointer (of shape [B, C]) obj_ptr = torch.zeros(mask_inputs.size(0), self.hidden_dim, device=mask_inputs.device) else: # produce an object pointer using the SAM decoder from the mask input _, _, _, _, _, obj_ptr, _ = self._forward_sam_heads( backbone_features=backbone_features, mask_inputs=self.mask_downsample(mask_inputs_float), high_res_features=high_res_features, ) # In this method, we are treating mask_input as output, e.g. using it directly to create spatial mem; # Below, we follow the same design axiom to use mask_input to decide if obj appears or not instead of relying # on the object_scores from the SAM decoder. is_obj_appearing = torch.any(mask_inputs.flatten(1).float() > 0.0, dim=1) is_obj_appearing = is_obj_appearing[..., None] lambda_is_obj_appearing = is_obj_appearing.float() object_score_logits = out_scale * lambda_is_obj_appearing + out_bias if self.pred_obj_scores: if self.fixed_no_obj_ptr: obj_ptr = lambda_is_obj_appearing * obj_ptr obj_ptr = obj_ptr + (1 - lambda_is_obj_appearing) * self.no_obj_ptr return ( low_res_masks, high_res_masks, ious, low_res_masks, high_res_masks, obj_ptr, object_score_logits, )
negative_train_query659_01437
ultralytics/models/sam/modules/sam.py/SAM2Model/forward_image class SAM2Model: def forward_image(self, img_batch: torch.Tensor): """Processes image batch through encoder to extract multi-level features for SAM model.""" backbone_out = self.image_encoder(img_batch) if self.use_high_res_features_in_sam: # precompute projected level 0 and level 1 features in SAM decoder # to avoid running it again on every SAM click backbone_out["backbone_fpn"][0] = self.sam_mask_decoder.conv_s0(backbone_out["backbone_fpn"][0]) backbone_out["backbone_fpn"][1] = self.sam_mask_decoder.conv_s1(backbone_out["backbone_fpn"][1]) return backbone_out
negative_train_query659_01438
ultralytics/models/sam/modules/sam.py/SAM2Model/_prepare_backbone_features class SAM2Model: def _prepare_backbone_features(self, backbone_out): """Prepares and flattens visual features from the image backbone output for further processing.""" backbone_out = backbone_out.copy() assert len(backbone_out["backbone_fpn"]) == len(backbone_out["vision_pos_enc"]) assert len(backbone_out["backbone_fpn"]) >= self.num_feature_levels feature_maps = backbone_out["backbone_fpn"][-self.num_feature_levels :] vision_pos_embeds = backbone_out["vision_pos_enc"][-self.num_feature_levels :] feat_sizes = [(x.shape[-2], x.shape[-1]) for x in vision_pos_embeds] # flatten NxCxHxW to HWxNxC vision_feats = [x.flatten(2).permute(2, 0, 1) for x in feature_maps] vision_pos_embeds = [x.flatten(2).permute(2, 0, 1) for x in vision_pos_embeds] return backbone_out, vision_feats, vision_pos_embeds, feat_sizes
negative_train_query659_01439
ultralytics/models/sam/modules/sam.py/SAM2Model/_prepare_memory_conditioned_features class SAM2Model: def _prepare_memory_conditioned_features( self, frame_idx, is_init_cond_frame, current_vision_feats, current_vision_pos_embeds, feat_sizes, output_dict, num_frames, track_in_reverse=False, # tracking in reverse time order (for demo usage) ): """Prepares memory-conditioned features by fusing current frame's visual features with previous memories.""" B = current_vision_feats[-1].size(1) # batch size on this frame C = self.hidden_dim H, W = feat_sizes[-1] # top-level (lowest-resolution) feature size device = current_vision_feats[-1].device # The case of `self.num_maskmem == 0` below is primarily used for reproducing SAM on images. # In this case, we skip the fusion with any memory. if self.num_maskmem == 0: # Disable memory and skip fusion return current_vision_feats[-1].permute(1, 2, 0).view(B, C, H, W) num_obj_ptr_tokens = 0 tpos_sign_mul = -1 if track_in_reverse else 1 # Step 1: condition the visual features of the current frame on previous memories if not is_init_cond_frame: # Retrieve the memories encoded with the maskmem backbone to_cat_memory, to_cat_memory_pos_embed = [], [] # Add conditioning frames's output first (all cond frames have t_pos=0 for # when getting temporal positional embedding below) assert len(output_dict["cond_frame_outputs"]) > 0 # Select a maximum number of temporally closest cond frames for cross attention cond_outputs = output_dict["cond_frame_outputs"] selected_cond_outputs, unselected_cond_outputs = select_closest_cond_frames( frame_idx, cond_outputs, self.max_cond_frames_in_attn ) t_pos_and_prevs = [(0, out) for out in selected_cond_outputs.values()] # Add last (self.num_maskmem - 1) frames before current frame for non-conditioning memory # the earliest one has t_pos=1 and the latest one has t_pos=self.num_maskmem-1 # We also allow taking the memory frame non-consecutively (with r>1), in which case # we take (self.num_maskmem - 2) frames among every r-th frames plus the last frame. r = 1 if self.training else self.memory_temporal_stride_for_eval for t_pos in range(1, self.num_maskmem): t_rel = self.num_maskmem - t_pos # how many frames before current frame if t_rel == 1: # for t_rel == 1, we take the last frame (regardless of r) prev_frame_idx = frame_idx + t_rel if track_in_reverse else frame_idx - t_rel elif not track_in_reverse: # first find the nearest frame among every r-th frames before this frame # for r=1, this would be (frame_idx - 2) prev_frame_idx = ((frame_idx - 2) // r) * r # then seek further among every r-th frames prev_frame_idx = prev_frame_idx - (t_rel - 2) * r else: # first find the nearest frame among every r-th frames after this frame # for r=1, this would be (frame_idx + 2) prev_frame_idx = -(-(frame_idx + 2) // r) * r # then seek further among every r-th frames prev_frame_idx = prev_frame_idx + (t_rel - 2) * r out = output_dict["non_cond_frame_outputs"].get(prev_frame_idx, None) if out is None: # If an unselected conditioning frame is among the last (self.num_maskmem - 1) # frames, we still attend to it as if it's a non-conditioning frame. out = unselected_cond_outputs.get(prev_frame_idx, None) t_pos_and_prevs.append((t_pos, out)) for t_pos, prev in t_pos_and_prevs: if prev is None: continue # skip padding frames # "maskmem_features" might have been offloaded to CPU in demo use cases, # so we load it back to GPU (it's a no-op if it's already on GPU). feats = prev["maskmem_features"].cuda(non_blocking=True) to_cat_memory.append(feats.flatten(2).permute(2, 0, 1)) # Spatial positional encoding (it might have been offloaded to CPU in eval) maskmem_enc = prev["maskmem_pos_enc"][-1].cuda() maskmem_enc = maskmem_enc.flatten(2).permute(2, 0, 1) # Temporal positional encoding maskmem_enc = maskmem_enc + self.maskmem_tpos_enc[self.num_maskmem - t_pos - 1] to_cat_memory_pos_embed.append(maskmem_enc) # Construct the list of past object pointers if self.use_obj_ptrs_in_encoder: max_obj_ptrs_in_encoder = min(num_frames, self.max_obj_ptrs_in_encoder) # First add those object pointers from selected conditioning frames # (optionally, only include object pointers in the past during evaluation) if not self.training and self.only_obj_ptrs_in_the_past_for_eval: ptr_cond_outputs = { t: out for t, out in selected_cond_outputs.items() if (t >= frame_idx if track_in_reverse else t <= frame_idx) } else: ptr_cond_outputs = selected_cond_outputs pos_and_ptrs = [ # Temporal pos encoding contains how far away each pointer is from current frame ( ( (frame_idx - t) * tpos_sign_mul if self.use_signed_tpos_enc_to_obj_ptrs else abs(frame_idx - t) ), out["obj_ptr"], ) for t, out in ptr_cond_outputs.items() ] # Add up to (max_obj_ptrs_in_encoder - 1) non-conditioning frames before current frame for t_diff in range(1, max_obj_ptrs_in_encoder): t = frame_idx + t_diff if track_in_reverse else frame_idx - t_diff if t < 0 or (num_frames is not None and t >= num_frames): break out = output_dict["non_cond_frame_outputs"].get(t, unselected_cond_outputs.get(t, None)) if out is not None: pos_and_ptrs.append((t_diff, out["obj_ptr"])) # If we have at least one object pointer, add them to the across attention if pos_and_ptrs: pos_list, ptrs_list = zip(*pos_and_ptrs) # stack object pointers along dim=0 into [ptr_seq_len, B, C] shape obj_ptrs = torch.stack(ptrs_list, dim=0) # a temporal positional embedding based on how far each object pointer is from # the current frame (sine embedding normalized by the max pointer num). if self.add_tpos_enc_to_obj_ptrs: t_diff_max = max_obj_ptrs_in_encoder - 1 tpos_dim = C if self.proj_tpos_enc_in_obj_ptrs else self.mem_dim obj_pos = torch.tensor(pos_list, device=device) obj_pos = get_1d_sine_pe(obj_pos / t_diff_max, dim=tpos_dim) obj_pos = self.obj_ptr_tpos_proj(obj_pos) obj_pos = obj_pos.unsqueeze(1).expand(-1, B, self.mem_dim) else: obj_pos = obj_ptrs.new_zeros(len(pos_list), B, self.mem_dim) if self.mem_dim < C: # split a pointer into (C // self.mem_dim) tokens for self.mem_dim < C obj_ptrs = obj_ptrs.reshape(-1, B, C // self.mem_dim, self.mem_dim) obj_ptrs = obj_ptrs.permute(0, 2, 1, 3).flatten(0, 1) obj_pos = obj_pos.repeat_interleave(C // self.mem_dim, dim=0) to_cat_memory.append(obj_ptrs) to_cat_memory_pos_embed.append(obj_pos) num_obj_ptr_tokens = obj_ptrs.shape[0] else: num_obj_ptr_tokens = 0 else: # for initial conditioning frames, encode them without using any previous memory if self.directly_add_no_mem_embed: # directly add no-mem embedding (instead of using the transformer encoder) pix_feat_with_mem = current_vision_feats[-1] + self.no_mem_embed pix_feat_with_mem = pix_feat_with_mem.permute(1, 2, 0).view(B, C, H, W) return pix_feat_with_mem # Use a dummy token on the first frame (to avoid empty memory input to transformer encoder) to_cat_memory = [self.no_mem_embed.expand(1, B, self.mem_dim)] to_cat_memory_pos_embed = [self.no_mem_pos_enc.expand(1, B, self.mem_dim)] # Step 2: Concatenate the memories and forward through the transformer encoder memory = torch.cat(to_cat_memory, dim=0) memory_pos_embed = torch.cat(to_cat_memory_pos_embed, dim=0) pix_feat_with_mem = self.memory_attention( curr=current_vision_feats, curr_pos=current_vision_pos_embeds, memory=memory, memory_pos=memory_pos_embed, num_obj_ptr_tokens=num_obj_ptr_tokens, ) # reshape the output (HW)BC => BCHW pix_feat_with_mem = pix_feat_with_mem.permute(1, 2, 0).view(B, C, H, W) return pix_feat_with_mem
negative_train_query659_01440
ultralytics/models/sam/modules/sam.py/SAM2Model/_encode_new_memory class SAM2Model: def _encode_new_memory( self, current_vision_feats, feat_sizes, pred_masks_high_res, object_score_logits, is_mask_from_pts, ): """Encodes frame features and masks into a new memory representation for video segmentation.""" B = current_vision_feats[-1].size(1) # batch size on this frame C = self.hidden_dim H, W = feat_sizes[-1] # top-level (lowest-resolution) feature size # top-level feature, (HW)BC => BCHW pix_feat = current_vision_feats[-1].permute(1, 2, 0).view(B, C, H, W) if self.non_overlap_masks_for_mem_enc and not self.training: # optionally, apply non-overlapping constraints to the masks (it's applied # in the batch dimension and should only be used during eval, where all # the objects come from the same video under batch size 1). pred_masks_high_res = self._apply_non_overlapping_constraints(pred_masks_high_res) # scale the raw mask logits with a temperature before applying sigmoid binarize = self.binarize_mask_from_pts_for_mem_enc and is_mask_from_pts if binarize and not self.training: mask_for_mem = (pred_masks_high_res > 0).float() else: # apply sigmoid on the raw mask logits to turn them into range (0, 1) mask_for_mem = torch.sigmoid(pred_masks_high_res) # apply scale and bias terms to the sigmoid probabilities if self.sigmoid_scale_for_mem_enc != 1.0: mask_for_mem = mask_for_mem * self.sigmoid_scale_for_mem_enc if self.sigmoid_bias_for_mem_enc != 0.0: mask_for_mem = mask_for_mem + self.sigmoid_bias_for_mem_enc maskmem_out = self.memory_encoder( pix_feat, mask_for_mem, skip_mask_sigmoid=True, # sigmoid already applied ) maskmem_features = maskmem_out["vision_features"] maskmem_pos_enc = maskmem_out["vision_pos_enc"] # add a no-object embedding to the spatial memory to indicate that the frame # is predicted to be occluded (i.e. no object is appearing in the frame) if self.no_obj_embed_spatial is not None: is_obj_appearing = (object_score_logits > 0).float() maskmem_features += (1 - is_obj_appearing[..., None, None]) * self.no_obj_embed_spatial[ ..., None, None ].expand(*maskmem_features.shape) return maskmem_features, maskmem_pos_enc
negative_train_query659_01441
ultralytics/models/sam/modules/sam.py/SAM2Model/_track_step class SAM2Model: def _track_step( self, frame_idx, is_init_cond_frame, current_vision_feats, current_vision_pos_embeds, feat_sizes, point_inputs, mask_inputs, output_dict, num_frames, track_in_reverse, prev_sam_mask_logits, ): """Performs a single tracking step, updating object masks and memory features based on current frame inputs.""" current_out = {"point_inputs": point_inputs, "mask_inputs": mask_inputs} # High-resolution feature maps for the SAM head, reshape (HW)BC => BCHW if len(current_vision_feats) > 1: high_res_features = [ x.permute(1, 2, 0).view(x.size(1), x.size(2), *s) for x, s in zip(current_vision_feats[:-1], feat_sizes[:-1]) ] else: high_res_features = None if mask_inputs is not None and self.use_mask_input_as_output_without_sam: # When use_mask_input_as_output_without_sam=True, we directly output the mask input # (see it as a GT mask) without using a SAM prompt encoder + mask decoder. pix_feat = current_vision_feats[-1].permute(1, 2, 0) pix_feat = pix_feat.view(-1, self.hidden_dim, *feat_sizes[-1]) sam_outputs = self._use_mask_as_output(pix_feat, high_res_features, mask_inputs) else: # fused the visual feature with previous memory features in the memory bank pix_feat = self._prepare_memory_conditioned_features( frame_idx=frame_idx, is_init_cond_frame=is_init_cond_frame, current_vision_feats=current_vision_feats[-1:], current_vision_pos_embeds=current_vision_pos_embeds[-1:], feat_sizes=feat_sizes[-1:], output_dict=output_dict, num_frames=num_frames, track_in_reverse=track_in_reverse, ) # apply SAM-style segmentation head # here we might feed previously predicted low-res SAM mask logits into the SAM mask decoder, # e.g. in demo where such logits come from earlier interaction instead of correction sampling # (in this case, any `mask_inputs` shouldn't reach here as they are sent to _use_mask_as_output instead) if prev_sam_mask_logits is not None: assert point_inputs is not None and mask_inputs is None mask_inputs = prev_sam_mask_logits multimask_output = self._use_multimask(is_init_cond_frame, point_inputs) sam_outputs = self._forward_sam_heads( backbone_features=pix_feat, point_inputs=point_inputs, mask_inputs=mask_inputs, high_res_features=high_res_features, multimask_output=multimask_output, ) return current_out, sam_outputs, high_res_features, pix_feat
negative_train_query659_01442
ultralytics/models/sam/modules/sam.py/SAM2Model/_encode_memory_in_output class SAM2Model: def _encode_memory_in_output( self, current_vision_feats, feat_sizes, point_inputs, run_mem_encoder, high_res_masks, object_score_logits, current_out, ): """Finally run the memory encoder on the predicted mask to encode, it into a new memory feature (that can be used in future frames). """ if run_mem_encoder and self.num_maskmem > 0: high_res_masks_for_mem_enc = high_res_masks maskmem_features, maskmem_pos_enc = self._encode_new_memory( current_vision_feats=current_vision_feats, feat_sizes=feat_sizes, pred_masks_high_res=high_res_masks_for_mem_enc, object_score_logits=object_score_logits, is_mask_from_pts=(point_inputs is not None), ) current_out["maskmem_features"] = maskmem_features current_out["maskmem_pos_enc"] = maskmem_pos_enc else: current_out["maskmem_features"] = None current_out["maskmem_pos_enc"] = None
negative_train_query659_01443
ultralytics/models/sam/modules/sam.py/SAM2Model/track_step class SAM2Model: def track_step( self, frame_idx, is_init_cond_frame, current_vision_feats, current_vision_pos_embeds, feat_sizes, point_inputs, mask_inputs, output_dict, num_frames, track_in_reverse=False, # tracking in reverse time order (for demo usage) # Whether to run the memory encoder on the predicted masks. Sometimes we might want # to skip the memory encoder with `run_mem_encoder=False`. For example, # in demo we might call `track_step` multiple times for each user click, # and only encode the memory when the user finalizes their clicks. And in ablation # settings like SAM training on static images, we don't need the memory encoder. run_mem_encoder=True, # The previously predicted SAM mask logits (which can be fed together with new clicks in demo). prev_sam_mask_logits=None, ): """Performs a single tracking step, updating object masks and memory features based on current frame inputs.""" current_out, sam_outputs, _, _ = self._track_step( frame_idx, is_init_cond_frame, current_vision_feats, current_vision_pos_embeds, feat_sizes, point_inputs, mask_inputs, output_dict, num_frames, track_in_reverse, prev_sam_mask_logits, ) ( _, _, _, low_res_masks, high_res_masks, obj_ptr, object_score_logits, ) = sam_outputs current_out["pred_masks"] = low_res_masks current_out["pred_masks_high_res"] = high_res_masks current_out["obj_ptr"] = obj_ptr if not self.training: # Only add this in inference (to avoid unused param in activation checkpointing; # it's mainly used in the demo to encode spatial memories w/ consolidated masks) current_out["object_score_logits"] = object_score_logits # Finally run the memory encoder on the predicted mask to encode # it into a new memory feature (that can be used in future frames) self._encode_memory_in_output( current_vision_feats, feat_sizes, point_inputs, run_mem_encoder, high_res_masks, object_score_logits, current_out, ) return current_out
negative_train_query659_01444
ultralytics/models/sam/modules/sam.py/SAM2Model/_use_multimask class SAM2Model: def _use_multimask(self, is_init_cond_frame, point_inputs): """Determines whether to use multiple mask outputs in the SAM head based on configuration and inputs.""" num_pts = 0 if point_inputs is None else point_inputs["point_labels"].size(1) return ( self.multimask_output_in_sam and (is_init_cond_frame or self.multimask_output_for_tracking) and (self.multimask_min_pt_num <= num_pts <= self.multimask_max_pt_num) )
negative_train_query659_01445
ultralytics/models/sam/modules/sam.py/SAM2Model/_apply_non_overlapping_constraints class SAM2Model: def _apply_non_overlapping_constraints(self, pred_masks): """Applies non-overlapping constraints to masks, keeping highest scoring object per location.""" batch_size = pred_masks.size(0) if batch_size == 1: return pred_masks device = pred_masks.device # "max_obj_inds": object index of the object with the highest score at each location max_obj_inds = torch.argmax(pred_masks, dim=0, keepdim=True) # "batch_obj_inds": object index of each object slice (along dim 0) in `pred_masks` batch_obj_inds = torch.arange(batch_size, device=device)[:, None, None, None] keep = max_obj_inds == batch_obj_inds # suppress overlapping regions' scores below -10.0 so that the foreground regions # don't overlap (here sigmoid(-10.0)=4.5398e-05) pred_masks = torch.where(keep, pred_masks, torch.clamp(pred_masks, max=-10.0)) return pred_masks
negative_train_query659_01446
ultralytics/models/sam/modules/sam.py/SAM2Model/set_imgsz class SAM2Model: def set_imgsz(self, imgsz): """ Set image size to make model compatible with different image sizes. Args: imgsz (Tuple[int, int]): The size of the input image. """ self.image_size = imgsz[0] self.sam_prompt_encoder.input_image_size = imgsz self.sam_prompt_encoder.image_embedding_size = [x // 16 for x in imgsz]
negative_train_query659_01447
ultralytics/models/utils/ops.py/HungarianMatcher/__init__ class HungarianMatcher: def __init__(self, cost_gain=None, use_fl=True, with_mask=False, num_sample_points=12544, alpha=0.25, gamma=2.0): """Initializes a HungarianMatcher module for optimal assignment of predicted and ground truth bounding boxes.""" super().__init__() if cost_gain is None: cost_gain = {"class": 1, "bbox": 5, "giou": 2, "mask": 1, "dice": 1} self.cost_gain = cost_gain self.use_fl = use_fl self.with_mask = with_mask self.num_sample_points = num_sample_points self.alpha = alpha self.gamma = gamma
negative_train_query659_01448
ultralytics/models/utils/ops.py/HungarianMatcher/forward class HungarianMatcher: def forward(self, pred_bboxes, pred_scores, gt_bboxes, gt_cls, gt_groups, masks=None, gt_mask=None): """ Forward pass for HungarianMatcher. This function computes costs based on prediction and ground truth (classification cost, L1 cost between boxes and GIoU cost between boxes) and finds the optimal matching between predictions and ground truth based on these costs. Args: pred_bboxes (Tensor): Predicted bounding boxes with shape [batch_size, num_queries, 4]. pred_scores (Tensor): Predicted scores with shape [batch_size, num_queries, num_classes]. gt_cls (torch.Tensor): Ground truth classes with shape [num_gts, ]. gt_bboxes (torch.Tensor): Ground truth bounding boxes with shape [num_gts, 4]. gt_groups (List[int]): List of length equal to batch size, containing the number of ground truths for each image. masks (Tensor, optional): Predicted masks with shape [batch_size, num_queries, height, width]. Defaults to None. gt_mask (List[Tensor], optional): List of ground truth masks, each with shape [num_masks, Height, Width]. Defaults to None. Returns: (List[Tuple[Tensor, Tensor]]): A list of size batch_size, each element is a tuple (index_i, index_j), where: - index_i is the tensor of indices of the selected predictions (in order) - index_j is the tensor of indices of the corresponding selected ground truth targets (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes) """ bs, nq, nc = pred_scores.shape if sum(gt_groups) == 0: return [(torch.tensor([], dtype=torch.long), torch.tensor([], dtype=torch.long)) for _ in range(bs)] # We flatten to compute the cost matrices in a batch # [batch_size * num_queries, num_classes] pred_scores = pred_scores.detach().view(-1, nc) pred_scores = F.sigmoid(pred_scores) if self.use_fl else F.softmax(pred_scores, dim=-1) # [batch_size * num_queries, 4] pred_bboxes = pred_bboxes.detach().view(-1, 4) # Compute the classification cost pred_scores = pred_scores[:, gt_cls] if self.use_fl: neg_cost_class = (1 - self.alpha) * (pred_scores**self.gamma) * (-(1 - pred_scores + 1e-8).log()) pos_cost_class = self.alpha * ((1 - pred_scores) ** self.gamma) * (-(pred_scores + 1e-8).log()) cost_class = pos_cost_class - neg_cost_class else: cost_class = -pred_scores # Compute the L1 cost between boxes cost_bbox = (pred_bboxes.unsqueeze(1) - gt_bboxes.unsqueeze(0)).abs().sum(-1) # (bs*num_queries, num_gt) # Compute the GIoU cost between boxes, (bs*num_queries, num_gt) cost_giou = 1.0 - bbox_iou(pred_bboxes.unsqueeze(1), gt_bboxes.unsqueeze(0), xywh=True, GIoU=True).squeeze(-1) # Final cost matrix C = ( self.cost_gain["class"] * cost_class + self.cost_gain["bbox"] * cost_bbox + self.cost_gain["giou"] * cost_giou ) # Compute the mask cost and dice cost if self.with_mask: C += self._cost_mask(bs, gt_groups, masks, gt_mask) # Set invalid values (NaNs and infinities) to 0 (fixes ValueError: matrix contains invalid numeric entries) C[C.isnan() | C.isinf()] = 0.0 C = C.view(bs, nq, -1).cpu() indices = [linear_sum_assignment(c[i]) for i, c in enumerate(C.split(gt_groups, -1))] gt_groups = torch.as_tensor([0, *gt_groups[:-1]]).cumsum_(0) # (idx for queries, idx for gt) return [ (torch.tensor(i, dtype=torch.long), torch.tensor(j, dtype=torch.long) + gt_groups[k]) for k, (i, j) in enumerate(indices) ]
negative_train_query659_01449
ultralytics/models/utils/ops.py/get_cdn_group def get_cdn_group( batch, num_classes, num_queries, class_embed, num_dn=100, cls_noise_ratio=0.5, box_noise_scale=1.0, training=False ): """ Get contrastive denoising training group. This function creates a contrastive denoising training group with positive and negative samples from the ground truths (gt). It applies noise to the class labels and bounding box coordinates, and returns the modified labels, bounding boxes, attention mask and meta information. Args: batch (dict): A dict that includes 'gt_cls' (torch.Tensor with shape [num_gts, ]), 'gt_bboxes' (torch.Tensor with shape [num_gts, 4]), 'gt_groups' (List(int)) which is a list of batch size length indicating the number of gts of each image. num_classes (int): Number of classes. num_queries (int): Number of queries. class_embed (torch.Tensor): Embedding weights to map class labels to embedding space. num_dn (int, optional): Number of denoising. Defaults to 100. cls_noise_ratio (float, optional): Noise ratio for class labels. Defaults to 0.5. box_noise_scale (float, optional): Noise scale for bounding box coordinates. Defaults to 1.0. training (bool, optional): If it's in training mode. Defaults to False. Returns: (Tuple[Optional[Tensor], Optional[Tensor], Optional[Tensor], Optional[Dict]]): The modified class embeddings, bounding boxes, attention mask and meta information for denoising. If not in training mode or 'num_dn' is less than or equal to 0, the function returns None for all elements in the tuple. """ if (not training) or num_dn <= 0: return None, None, None, None gt_groups = batch["gt_groups"] total_num = sum(gt_groups) max_nums = max(gt_groups) if max_nums == 0: return None, None, None, None num_group = num_dn // max_nums num_group = 1 if num_group == 0 else num_group # Pad gt to max_num of a batch bs = len(gt_groups) gt_cls = batch["cls"] # (bs*num, ) gt_bbox = batch["bboxes"] # bs*num, 4 b_idx = batch["batch_idx"] # Each group has positive and negative queries. dn_cls = gt_cls.repeat(2 * num_group) # (2*num_group*bs*num, ) dn_bbox = gt_bbox.repeat(2 * num_group, 1) # 2*num_group*bs*num, 4 dn_b_idx = b_idx.repeat(2 * num_group).view(-1) # (2*num_group*bs*num, ) # Positive and negative mask # (bs*num*num_group, ), the second total_num*num_group part as negative samples neg_idx = torch.arange(total_num * num_group, dtype=torch.long, device=gt_bbox.device) + num_group * total_num if cls_noise_ratio > 0: # Half of bbox prob mask = torch.rand(dn_cls.shape) < (cls_noise_ratio * 0.5) idx = torch.nonzero(mask).squeeze(-1) # Randomly put a new one here new_label = torch.randint_like(idx, 0, num_classes, dtype=dn_cls.dtype, device=dn_cls.device) dn_cls[idx] = new_label if box_noise_scale > 0: known_bbox = xywh2xyxy(dn_bbox) diff = (dn_bbox[..., 2:] * 0.5).repeat(1, 2) * box_noise_scale # 2*num_group*bs*num, 4 rand_sign = torch.randint_like(dn_bbox, 0, 2) * 2.0 - 1.0 rand_part = torch.rand_like(dn_bbox) rand_part[neg_idx] += 1.0 rand_part *= rand_sign known_bbox += rand_part * diff known_bbox.clip_(min=0.0, max=1.0) dn_bbox = xyxy2xywh(known_bbox) dn_bbox = torch.logit(dn_bbox, eps=1e-6) # inverse sigmoid num_dn = int(max_nums * 2 * num_group) # total denoising queries # class_embed = torch.cat([class_embed, torch.zeros([1, class_embed.shape[-1]], device=class_embed.device)]) dn_cls_embed = class_embed[dn_cls] # bs*num * 2 * num_group, 256 padding_cls = torch.zeros(bs, num_dn, dn_cls_embed.shape[-1], device=gt_cls.device) padding_bbox = torch.zeros(bs, num_dn, 4, device=gt_bbox.device) map_indices = torch.cat([torch.tensor(range(num), dtype=torch.long) for num in gt_groups]) pos_idx = torch.stack([map_indices + max_nums * i for i in range(num_group)], dim=0) map_indices = torch.cat([map_indices + max_nums * i for i in range(2 * num_group)]) padding_cls[(dn_b_idx, map_indices)] = dn_cls_embed padding_bbox[(dn_b_idx, map_indices)] = dn_bbox tgt_size = num_dn + num_queries attn_mask = torch.zeros([tgt_size, tgt_size], dtype=torch.bool) # Match query cannot see the reconstruct attn_mask[num_dn:, :num_dn] = True # Reconstruct cannot see each other for i in range(num_group): if i == 0: attn_mask[max_nums * 2 * i : max_nums * 2 * (i + 1), max_nums * 2 * (i + 1) : num_dn] = True if i == num_group - 1: attn_mask[max_nums * 2 * i : max_nums * 2 * (i + 1), : max_nums * i * 2] = True else: attn_mask[max_nums * 2 * i : max_nums * 2 * (i + 1), max_nums * 2 * (i + 1) : num_dn] = True attn_mask[max_nums * 2 * i : max_nums * 2 * (i + 1), : max_nums * 2 * i] = True dn_meta = { "dn_pos_idx": [p.reshape(-1) for p in pos_idx.cpu().split(list(gt_groups), dim=1)], "dn_num_group": num_group, "dn_num_split": [num_dn, num_queries], } return ( padding_cls.to(class_embed.device), padding_bbox.to(class_embed.device), attn_mask.to(class_embed.device), dn_meta, )
negative_train_query659_01450
ultralytics/models/utils/loss.py/DETRLoss/__init__ class DETRLoss: def __init__( self, nc=80, loss_gain=None, aux_loss=True, use_fl=True, use_vfl=False, use_uni_match=False, uni_match_ind=0 ): """ Initialize DETR loss function with customizable components and gains. Uses default loss_gain if not provided. Initializes HungarianMatcher with preset cost gains. Supports auxiliary losses and various loss types. Args: nc (int): Number of classes. loss_gain (dict): Coefficients for different loss components. aux_loss (bool): Use auxiliary losses from each decoder layer. use_fl (bool): Use FocalLoss. use_vfl (bool): Use VarifocalLoss. use_uni_match (bool): Use fixed layer for auxiliary branch label assignment. uni_match_ind (int): Index of fixed layer for uni_match. """ super().__init__() if loss_gain is None: loss_gain = {"class": 1, "bbox": 5, "giou": 2, "no_object": 0.1, "mask": 1, "dice": 1} self.nc = nc self.matcher = HungarianMatcher(cost_gain={"class": 2, "bbox": 5, "giou": 2}) self.loss_gain = loss_gain self.aux_loss = aux_loss self.fl = FocalLoss() if use_fl else None self.vfl = VarifocalLoss() if use_vfl else None self.use_uni_match = use_uni_match self.uni_match_ind = uni_match_ind self.device = None
negative_train_query659_01451
ultralytics/models/utils/loss.py/DETRLoss/_get_loss_class class DETRLoss: def _get_loss_class(self, pred_scores, targets, gt_scores, num_gts, postfix=""): """Computes the classification loss based on predictions, target values, and ground truth scores.""" # Logits: [b, query, num_classes], gt_class: list[[n, 1]] name_class = f"loss_class{postfix}" bs, nq = pred_scores.shape[:2] # one_hot = F.one_hot(targets, self.nc + 1)[..., :-1] # (bs, num_queries, num_classes) one_hot = torch.zeros((bs, nq, self.nc + 1), dtype=torch.int64, device=targets.device) one_hot.scatter_(2, targets.unsqueeze(-1), 1) one_hot = one_hot[..., :-1] gt_scores = gt_scores.view(bs, nq, 1) * one_hot if self.fl: if num_gts and self.vfl: loss_cls = self.vfl(pred_scores, gt_scores, one_hot) else: loss_cls = self.fl(pred_scores, one_hot.float()) loss_cls /= max(num_gts, 1) / nq else: loss_cls = nn.BCEWithLogitsLoss(reduction="none")(pred_scores, gt_scores).mean(1).sum() # YOLO CLS loss return {name_class: loss_cls.squeeze() * self.loss_gain["class"]}
negative_train_query659_01452
ultralytics/models/utils/loss.py/DETRLoss/_get_loss_bbox class DETRLoss: def _get_loss_bbox(self, pred_bboxes, gt_bboxes, postfix=""): """Computes bounding box and GIoU losses for predicted and ground truth bounding boxes.""" # Boxes: [b, query, 4], gt_bbox: list[[n, 4]] name_bbox = f"loss_bbox{postfix}" name_giou = f"loss_giou{postfix}" loss = {} if len(gt_bboxes) == 0: loss[name_bbox] = torch.tensor(0.0, device=self.device) loss[name_giou] = torch.tensor(0.0, device=self.device) return loss loss[name_bbox] = self.loss_gain["bbox"] * F.l1_loss(pred_bboxes, gt_bboxes, reduction="sum") / len(gt_bboxes) loss[name_giou] = 1.0 - bbox_iou(pred_bboxes, gt_bboxes, xywh=True, GIoU=True) loss[name_giou] = loss[name_giou].sum() / len(gt_bboxes) loss[name_giou] = self.loss_gain["giou"] * loss[name_giou] return {k: v.squeeze() for k, v in loss.items()}
negative_train_query659_01453
ultralytics/models/utils/loss.py/DETRLoss/_get_loss_aux class DETRLoss: def _get_loss_aux( self, pred_bboxes, pred_scores, gt_bboxes, gt_cls, gt_groups, match_indices=None, postfix="", masks=None, gt_mask=None, ): """Get auxiliary losses.""" # NOTE: loss class, bbox, giou, mask, dice loss = torch.zeros(5 if masks is not None else 3, device=pred_bboxes.device) if match_indices is None and self.use_uni_match: match_indices = self.matcher( pred_bboxes[self.uni_match_ind], pred_scores[self.uni_match_ind], gt_bboxes, gt_cls, gt_groups, masks=masks[self.uni_match_ind] if masks is not None else None, gt_mask=gt_mask, ) for i, (aux_bboxes, aux_scores) in enumerate(zip(pred_bboxes, pred_scores)): aux_masks = masks[i] if masks is not None else None loss_ = self._get_loss( aux_bboxes, aux_scores, gt_bboxes, gt_cls, gt_groups, masks=aux_masks, gt_mask=gt_mask, postfix=postfix, match_indices=match_indices, ) loss[0] += loss_[f"loss_class{postfix}"] loss[1] += loss_[f"loss_bbox{postfix}"] loss[2] += loss_[f"loss_giou{postfix}"] # if masks is not None and gt_mask is not None: # loss_ = self._get_loss_mask(aux_masks, gt_mask, match_indices, postfix) # loss[3] += loss_[f'loss_mask{postfix}'] # loss[4] += loss_[f'loss_dice{postfix}'] loss = { f"loss_class_aux{postfix}": loss[0], f"loss_bbox_aux{postfix}": loss[1], f"loss_giou_aux{postfix}": loss[2], } # if masks is not None and gt_mask is not None: # loss[f'loss_mask_aux{postfix}'] = loss[3] # loss[f'loss_dice_aux{postfix}'] = loss[4] return loss
negative_train_query659_01454
ultralytics/models/utils/loss.py/DETRLoss/_get_index class DETRLoss: def _get_index(match_indices): """Returns batch indices, source indices, and destination indices from provided match indices.""" batch_idx = torch.cat([torch.full_like(src, i) for i, (src, _) in enumerate(match_indices)]) src_idx = torch.cat([src for (src, _) in match_indices]) dst_idx = torch.cat([dst for (_, dst) in match_indices]) return (batch_idx, src_idx), dst_idx
negative_train_query659_01455
ultralytics/models/utils/loss.py/DETRLoss/_get_assigned_bboxes class DETRLoss: def _get_assigned_bboxes(self, pred_bboxes, gt_bboxes, match_indices): """Assigns predicted bounding boxes to ground truth bounding boxes based on the match indices.""" pred_assigned = torch.cat( [ t[i] if len(i) > 0 else torch.zeros(0, t.shape[-1], device=self.device) for t, (i, _) in zip(pred_bboxes, match_indices) ] ) gt_assigned = torch.cat( [ t[j] if len(j) > 0 else torch.zeros(0, t.shape[-1], device=self.device) for t, (_, j) in zip(gt_bboxes, match_indices) ] ) return pred_assigned, gt_assigned
negative_train_query659_01456
ultralytics/models/utils/loss.py/DETRLoss/_get_loss class DETRLoss: def _get_loss( self, pred_bboxes, pred_scores, gt_bboxes, gt_cls, gt_groups, masks=None, gt_mask=None, postfix="", match_indices=None, ): """Get losses.""" if match_indices is None: match_indices = self.matcher( pred_bboxes, pred_scores, gt_bboxes, gt_cls, gt_groups, masks=masks, gt_mask=gt_mask ) idx, gt_idx = self._get_index(match_indices) pred_bboxes, gt_bboxes = pred_bboxes[idx], gt_bboxes[gt_idx] bs, nq = pred_scores.shape[:2] targets = torch.full((bs, nq), self.nc, device=pred_scores.device, dtype=gt_cls.dtype) targets[idx] = gt_cls[gt_idx] gt_scores = torch.zeros([bs, nq], device=pred_scores.device) if len(gt_bboxes): gt_scores[idx] = bbox_iou(pred_bboxes.detach(), gt_bboxes, xywh=True).squeeze(-1) loss = {} loss.update(self._get_loss_class(pred_scores, targets, gt_scores, len(gt_bboxes), postfix)) loss.update(self._get_loss_bbox(pred_bboxes, gt_bboxes, postfix)) # if masks is not None and gt_mask is not None: # loss.update(self._get_loss_mask(masks, gt_mask, match_indices, postfix)) return loss
negative_train_query659_01457
ultralytics/models/utils/loss.py/DETRLoss/forward class DETRLoss: def forward(self, pred_bboxes, pred_scores, batch, postfix="", **kwargs): """ Calculate loss for predicted bounding boxes and scores. Args: pred_bboxes (torch.Tensor): Predicted bounding boxes, shape [l, b, query, 4]. pred_scores (torch.Tensor): Predicted class scores, shape [l, b, query, num_classes]. batch (dict): Batch information containing: cls (torch.Tensor): Ground truth classes, shape [num_gts]. bboxes (torch.Tensor): Ground truth bounding boxes, shape [num_gts, 4]. gt_groups (List[int]): Number of ground truths for each image in the batch. postfix (str): Postfix for loss names. **kwargs (Any): Additional arguments, may include 'match_indices'. Returns: (dict): Computed losses, including main and auxiliary (if enabled). Note: Uses last elements of pred_bboxes and pred_scores for main loss, and the rest for auxiliary losses if self.aux_loss is True. """ self.device = pred_bboxes.device match_indices = kwargs.get("match_indices", None) gt_cls, gt_bboxes, gt_groups = batch["cls"], batch["bboxes"], batch["gt_groups"] total_loss = self._get_loss( pred_bboxes[-1], pred_scores[-1], gt_bboxes, gt_cls, gt_groups, postfix=postfix, match_indices=match_indices ) if self.aux_loss: total_loss.update( self._get_loss_aux( pred_bboxes[:-1], pred_scores[:-1], gt_bboxes, gt_cls, gt_groups, match_indices, postfix ) ) return total_loss
negative_train_query659_01458
ultralytics/models/utils/loss.py/RTDETRDetectionLoss/forward class RTDETRDetectionLoss: def forward(self, preds, batch, dn_bboxes=None, dn_scores=None, dn_meta=None): """ Forward pass to compute the detection loss. Args: preds (tuple): Predicted bounding boxes and scores. batch (dict): Batch data containing ground truth information. dn_bboxes (torch.Tensor, optional): Denoising bounding boxes. Default is None. dn_scores (torch.Tensor, optional): Denoising scores. Default is None. dn_meta (dict, optional): Metadata for denoising. Default is None. Returns: (dict): Dictionary containing the total loss and, if applicable, the denoising loss. """ pred_bboxes, pred_scores = preds total_loss = super().forward(pred_bboxes, pred_scores, batch) # Check for denoising metadata to compute denoising training loss if dn_meta is not None: dn_pos_idx, dn_num_group = dn_meta["dn_pos_idx"], dn_meta["dn_num_group"] assert len(batch["gt_groups"]) == len(dn_pos_idx) # Get the match indices for denoising match_indices = self.get_dn_match_indices(dn_pos_idx, dn_num_group, batch["gt_groups"]) # Compute the denoising training loss dn_loss = super().forward(dn_bboxes, dn_scores, batch, postfix="_dn", match_indices=match_indices) total_loss.update(dn_loss) else: # If no denoising metadata is provided, set denoising loss to zero total_loss.update({f"{k}_dn": torch.tensor(0.0, device=self.device) for k in total_loss.keys()}) return total_loss
negative_train_query659_01459
ultralytics/models/utils/loss.py/RTDETRDetectionLoss/get_dn_match_indices class RTDETRDetectionLoss: def get_dn_match_indices(dn_pos_idx, dn_num_group, gt_groups): """ Get the match indices for denoising. Args: dn_pos_idx (List[torch.Tensor]): List of tensors containing positive indices for denoising. dn_num_group (int): Number of denoising groups. gt_groups (List[int]): List of integers representing the number of ground truths for each image. Returns: (List[tuple]): List of tuples containing matched indices for denoising. """ dn_match_indices = [] idx_groups = torch.as_tensor([0, *gt_groups[:-1]]).cumsum_(0) for i, num_gt in enumerate(gt_groups): if num_gt > 0: gt_idx = torch.arange(end=num_gt, dtype=torch.long) + idx_groups[i] gt_idx = gt_idx.repeat(dn_num_group) assert len(dn_pos_idx[i]) == len(gt_idx), "Expected the same length, " f"but got {len(dn_pos_idx[i])} and {len(gt_idx)} respectively." dn_match_indices.append((dn_pos_idx[i], gt_idx)) else: dn_match_indices.append((torch.zeros([0], dtype=torch.long), torch.zeros([0], dtype=torch.long))) return dn_match_indices
negative_train_query659_01460
ultralytics/models/rtdetr/val.py/RTDETRDataset/__init__ class RTDETRDataset: def __init__(self, *args, data=None, **kwargs): """Initialize the RTDETRDataset class by inheriting from the YOLODataset class.""" super().__init__(*args, data=data, **kwargs)
negative_train_query659_01461
ultralytics/models/rtdetr/val.py/RTDETRDataset/load_image class RTDETRDataset: def load_image(self, i, rect_mode=False): """Loads 1 image from dataset index 'i', returns (im, resized hw).""" return super().load_image(i=i, rect_mode=rect_mode)
negative_train_query659_01462
ultralytics/models/rtdetr/val.py/RTDETRDataset/build_transforms class RTDETRDataset: def build_transforms(self, hyp=None): """Temporary, only for evaluation.""" if self.augment: hyp.mosaic = hyp.mosaic if self.augment and not self.rect else 0.0 hyp.mixup = hyp.mixup if self.augment and not self.rect else 0.0 transforms = v8_transforms(self, self.imgsz, hyp, stretch=True) else: # transforms = Compose([LetterBox(new_shape=(self.imgsz, self.imgsz), auto=False, scaleFill=True)]) transforms = Compose([]) transforms.append( Format( bbox_format="xywh", normalize=True, return_mask=self.use_segments, return_keypoint=self.use_keypoints, batch_idx=True, mask_ratio=hyp.mask_ratio, mask_overlap=hyp.overlap_mask, ) ) return transforms
negative_train_query659_01463
ultralytics/models/rtdetr/val.py/RTDETRValidator/build_dataset class RTDETRValidator: def build_dataset(self, img_path, mode="val", batch=None): """ Build an RTDETR Dataset. Args: img_path (str): Path to the folder containing images. mode (str): `train` mode or `val` mode, users are able to customize different augmentations for each mode. batch (int, optional): Size of batches, this is for `rect`. Defaults to None. """ return RTDETRDataset( img_path=img_path, imgsz=self.args.imgsz, batch_size=batch, augment=False, # no augmentation hyp=self.args, rect=False, # no rect cache=self.args.cache or None, prefix=colorstr(f"{mode}: "), data=self.data, )
negative_train_query659_01464
ultralytics/models/rtdetr/val.py/RTDETRValidator/postprocess class RTDETRValidator: def postprocess(self, preds): """Apply Non-maximum suppression to prediction outputs.""" if not isinstance(preds, (list, tuple)): # list for PyTorch inference but list[0] Tensor for export inference preds = [preds, None] bs, _, nd = preds[0].shape bboxes, scores = preds[0].split((4, nd - 4), dim=-1) bboxes *= self.args.imgsz outputs = [torch.zeros((0, 6), device=bboxes.device)] * bs for i, bbox in enumerate(bboxes): # (300, 4) bbox = ops.xywh2xyxy(bbox) score, cls = scores[i].max(-1) # (300, ) # Do not need threshold for evaluation as only got 300 boxes here # idx = score > self.args.conf pred = torch.cat([bbox, score[..., None], cls[..., None]], dim=-1) # filter # Sort by confidence to correctly get internal metrics pred = pred[score.argsort(descending=True)] outputs[i] = pred # [idx] return outputs
negative_train_query659_01465