brails.processors.vlm_segmenter.segment_anything.modeling.transformer module

class brails.processors.vlm_segmenter.segment_anything.modeling.transformer.Attention(embedding_dim: int, num_heads: int, downsample_rate: int = 1)

Bases: Module

An attention layer that allows for downscaling the size of the embedding after projection to queries, keys, and values.

forward(q: Tensor, k: Tensor, v: Tensor) Tensor

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class brails.processors.vlm_segmenter.segment_anything.modeling.transformer.TwoWayAttentionBlock(embedding_dim: int, num_heads: int, mlp_dim: int = 2048, activation: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.activation.ReLU'>, attention_downsample_rate: int = 2, skip_first_layer_pe: bool = False)

Bases: Module

forward(queries: Tensor, keys: Tensor, query_pe: Tensor, key_pe: Tensor) Tuple[Tensor, Tensor]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class brails.processors.vlm_segmenter.segment_anything.modeling.transformer.TwoWayTransformer(depth: int, embedding_dim: int, num_heads: int, mlp_dim: int, activation: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.activation.ReLU'>, attention_downsample_rate: int = 2)

Bases: Module

forward(image_embedding: Tensor, image_pe: Tensor, point_embedding: Tensor) Tuple[Tensor, Tensor]
Args:
image_embedding (torch.Tensor): image to attend to. Should be shape

B x embedding_dim x h x w for any h and w.

image_pe (torch.Tensor): the positional encoding to add to the image. Must

have the same shape as image_embedding.

point_embedding (torch.Tensor): the embedding to add to the query points.

Must have shape B x N_points x embedding_dim for any N_points.

Returns:

torch.Tensor: the processed point_embedding torch.Tensor: the processed image_embedding