์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ ultralytics/models/sam/modules/decoders.py

์ฐธ๊ณ 

์ด ํŒŒ์ผ์€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/models/ sam/modules/decoders .py์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ (๐Ÿ› ๏ธ) ๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋„๋ก ๋„์™€์ฃผ์„ธ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™!



ultralytics.models.sam.modules.decoders.MaskDecoder

๋ฒ ์ด์Šค: Module

๋งˆ์Šคํฌ ๋ฐ ๊ด€๋ จ ํ’ˆ์งˆ ์ ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋””์ฝ”๋” ๋ชจ๋“ˆ๋กœ, ํŠธ๋žœ์Šคํฌ๋จธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ์™€ ํ”„๋กฌํ”„ํŠธ ์ž„๋ฒ ๋”ฉ์„ ์˜ˆ์ธกํ•˜๋Š” ๋””์ฝ”๋” ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค.

์†์„ฑ:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช…
transformer_dim int

๋ณ€์••๊ธฐ ๋ชจ๋“ˆ์˜ ์ฑ„๋„ ์น˜์ˆ˜์ž…๋‹ˆ๋‹ค.

transformer Module

๋งˆ์Šคํฌ ์˜ˆ์ธก์— ์‚ฌ์šฉ๋˜๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค.

num_multimask_outputs int

๋งˆ์Šคํฌ ๊ตฌ๋ถ„์„ ์œ„ํ•ด ์˜ˆ์ธกํ•  ๋งˆ์Šคํฌ ์ˆ˜์ž…๋‹ˆ๋‹ค.

iou_token Embedding

IoU ํ† ํฐ์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ.

num_mask_tokens int

๋งˆ์Šคํฌ ํ† ํฐ ์ˆ˜์ž…๋‹ˆ๋‹ค.

mask_tokens Embedding

๋งˆ์Šคํฌ ํ† ํฐ์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ.

output_upscaling Sequential

์ถœ๋ ฅ ์—…์Šค์ผ€์ผ๋ง์„ ์œ„ํ•œ ์‹ ๊ฒฝ๋ง ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค.

output_hypernetworks_mlps ModuleList

๋งˆ์Šคํฌ ์ƒ์„ฑ์„ ์œ„ํ•œ ํ•˜์ดํผ๋„คํŠธ์›Œํฌ MLP.

iou_prediction_head Module

๋งˆ์Šคํฌ ํ’ˆ์งˆ ์˜ˆ์ธก์„ ์œ„ํ•œ MLP.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/sam/modules/decoders.py
class MaskDecoder(nn.Module):
    """
    Decoder module for generating masks and their associated quality scores, using a transformer architecture to predict
    masks given image and prompt embeddings.

    Attributes:
        transformer_dim (int): Channel dimension for the transformer module.
        transformer (nn.Module): The transformer module used for mask prediction.
        num_multimask_outputs (int): Number of masks to predict for disambiguating masks.
        iou_token (nn.Embedding): Embedding for the IoU token.
        num_mask_tokens (int): Number of mask tokens.
        mask_tokens (nn.Embedding): Embedding for the mask tokens.
        output_upscaling (nn.Sequential): Neural network sequence for upscaling the output.
        output_hypernetworks_mlps (nn.ModuleList): Hypernetwork MLPs for generating masks.
        iou_prediction_head (nn.Module): MLP for predicting mask quality.
    """

    def __init__(
        self,
        *,
        transformer_dim: int,
        transformer: nn.Module,
        num_multimask_outputs: int = 3,
        activation: Type[nn.Module] = nn.GELU,
        iou_head_depth: int = 3,
        iou_head_hidden_dim: int = 256,
    ) -> None:
        """
        Predicts masks given an image and prompt embeddings, using a transformer architecture.

        Args:
            transformer_dim (int): the channel dimension of the transformer module
            transformer (nn.Module): the transformer used to predict masks
            num_multimask_outputs (int): the number of masks to predict when disambiguating masks
            activation (nn.Module): the type of activation to use when upscaling masks
            iou_head_depth (int): the depth of the MLP used to predict mask quality
            iou_head_hidden_dim (int): the hidden dimension of the MLP used to predict mask quality
        """
        super().__init__()
        self.transformer_dim = transformer_dim
        self.transformer = transformer

        self.num_multimask_outputs = num_multimask_outputs

        self.iou_token = nn.Embedding(1, transformer_dim)
        self.num_mask_tokens = num_multimask_outputs + 1
        self.mask_tokens = nn.Embedding(self.num_mask_tokens, transformer_dim)

        self.output_upscaling = nn.Sequential(
            nn.ConvTranspose2d(transformer_dim, transformer_dim // 4, kernel_size=2, stride=2),
            LayerNorm2d(transformer_dim // 4),
            activation(),
            nn.ConvTranspose2d(transformer_dim // 4, transformer_dim // 8, kernel_size=2, stride=2),
            activation(),
        )
        self.output_hypernetworks_mlps = nn.ModuleList(
            [MLP(transformer_dim, transformer_dim, transformer_dim // 8, 3) for _ in range(self.num_mask_tokens)]
        )

        self.iou_prediction_head = MLP(transformer_dim, iou_head_hidden_dim, self.num_mask_tokens, iou_head_depth)

    def forward(
        self,
        image_embeddings: torch.Tensor,
        image_pe: torch.Tensor,
        sparse_prompt_embeddings: torch.Tensor,
        dense_prompt_embeddings: torch.Tensor,
        multimask_output: bool,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Predict masks given image and prompt embeddings.

        Args:
            image_embeddings (torch.Tensor): the embeddings from the image encoder
            image_pe (torch.Tensor): positional encoding with the shape of image_embeddings
            sparse_prompt_embeddings (torch.Tensor): the embeddings of the points and boxes
            dense_prompt_embeddings (torch.Tensor): the embeddings of the mask inputs
            multimask_output (bool): Whether to return multiple masks or a single mask.

        Returns:
            torch.Tensor: batched predicted masks
            torch.Tensor: batched predictions of mask quality
        """
        masks, iou_pred = self.predict_masks(
            image_embeddings=image_embeddings,
            image_pe=image_pe,
            sparse_prompt_embeddings=sparse_prompt_embeddings,
            dense_prompt_embeddings=dense_prompt_embeddings,
        )

        # Select the correct mask or masks for output
        mask_slice = slice(1, None) if multimask_output else slice(0, 1)
        masks = masks[:, mask_slice, :, :]
        iou_pred = iou_pred[:, mask_slice]

        # Prepare output
        return masks, iou_pred

    def predict_masks(
        self,
        image_embeddings: torch.Tensor,
        image_pe: torch.Tensor,
        sparse_prompt_embeddings: torch.Tensor,
        dense_prompt_embeddings: torch.Tensor,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Predicts masks.

        See 'forward' for more details.
        """
        # Concatenate output tokens
        output_tokens = torch.cat([self.iou_token.weight, self.mask_tokens.weight], dim=0)
        output_tokens = output_tokens.unsqueeze(0).expand(sparse_prompt_embeddings.shape[0], -1, -1)
        tokens = torch.cat((output_tokens, sparse_prompt_embeddings), dim=1)

        # Expand per-image data in batch direction to be per-mask
        src = torch.repeat_interleave(image_embeddings, tokens.shape[0], dim=0)
        src = src + dense_prompt_embeddings
        pos_src = torch.repeat_interleave(image_pe, tokens.shape[0], dim=0)
        b, c, h, w = src.shape

        # Run the transformer
        hs, src = self.transformer(src, pos_src, tokens)
        iou_token_out = hs[:, 0, :]
        mask_tokens_out = hs[:, 1 : (1 + self.num_mask_tokens), :]

        # Upscale mask embeddings and predict masks using the mask tokens
        src = src.transpose(1, 2).view(b, c, h, w)
        upscaled_embedding = self.output_upscaling(src)
        hyper_in_list: List[torch.Tensor] = [
            self.output_hypernetworks_mlps[i](mask_tokens_out[:, i, :]) for i in range(self.num_mask_tokens)
        ]
        hyper_in = torch.stack(hyper_in_list, dim=1)
        b, c, h, w = upscaled_embedding.shape
        masks = (hyper_in @ upscaled_embedding.view(b, c, h * w)).view(b, -1, h, w)

        # Generate mask quality predictions
        iou_pred = self.iou_prediction_head(iou_token_out)

        return masks, iou_pred

__init__(*, transformer_dim, transformer, num_multimask_outputs=3, activation=nn.GELU, iou_head_depth=3, iou_head_hidden_dim=256)

ํŠธ๋žœ์Šคํฌ๋จธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€์™€ ํ”„๋กฌํ”„ํŠธ ์ž„๋ฒ ๋”ฉ์ด ์ฃผ์–ด์ง„ ๋งˆ์Šคํฌ๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
transformer_dim int

๋ณ€์••๊ธฐ ๋ชจ๋“ˆ์˜ ์ฑ„๋„ ์น˜์ˆ˜

ํ•„์ˆ˜
transformer Module

๋งˆ์Šคํฌ ์˜ˆ์ธก์— ์‚ฌ์šฉ๋˜๋Š” ํŠธ๋žœ์Šคํฌ๋จธ

ํ•„์ˆ˜
num_multimask_outputs int

๋งˆ์Šคํฌ๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ ์˜ˆ์ธกํ•  ๋งˆ์Šคํฌ ์ˆ˜

3
activation Module

๋งˆ์Šคํฌ ์—…์Šค์ผ€์ผ๋ง ์‹œ ์‚ฌ์šฉํ•  ํ™œ์„ฑํ™” ์œ ํ˜•

GELU
iou_head_depth int

๋งˆ์Šคํฌ ํ’ˆ์งˆ ์˜ˆ์ธก์— ์‚ฌ์šฉ๋˜๋Š” MLP์˜ ๊นŠ์ด

3
iou_head_hidden_dim int

๋งˆ์Šคํฌ ํ’ˆ์งˆ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” MLP์˜ ์ˆจ๊ฒจ์ง„ ์ฐจ์›์ž…๋‹ˆ๋‹ค.

256
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/sam/modules/decoders.py
def __init__(
    self,
    *,
    transformer_dim: int,
    transformer: nn.Module,
    num_multimask_outputs: int = 3,
    activation: Type[nn.Module] = nn.GELU,
    iou_head_depth: int = 3,
    iou_head_hidden_dim: int = 256,
) -> None:
    """
    Predicts masks given an image and prompt embeddings, using a transformer architecture.

    Args:
        transformer_dim (int): the channel dimension of the transformer module
        transformer (nn.Module): the transformer used to predict masks
        num_multimask_outputs (int): the number of masks to predict when disambiguating masks
        activation (nn.Module): the type of activation to use when upscaling masks
        iou_head_depth (int): the depth of the MLP used to predict mask quality
        iou_head_hidden_dim (int): the hidden dimension of the MLP used to predict mask quality
    """
    super().__init__()
    self.transformer_dim = transformer_dim
    self.transformer = transformer

    self.num_multimask_outputs = num_multimask_outputs

    self.iou_token = nn.Embedding(1, transformer_dim)
    self.num_mask_tokens = num_multimask_outputs + 1
    self.mask_tokens = nn.Embedding(self.num_mask_tokens, transformer_dim)

    self.output_upscaling = nn.Sequential(
        nn.ConvTranspose2d(transformer_dim, transformer_dim // 4, kernel_size=2, stride=2),
        LayerNorm2d(transformer_dim // 4),
        activation(),
        nn.ConvTranspose2d(transformer_dim // 4, transformer_dim // 8, kernel_size=2, stride=2),
        activation(),
    )
    self.output_hypernetworks_mlps = nn.ModuleList(
        [MLP(transformer_dim, transformer_dim, transformer_dim // 8, 3) for _ in range(self.num_mask_tokens)]
    )

    self.iou_prediction_head = MLP(transformer_dim, iou_head_hidden_dim, self.num_mask_tokens, iou_head_depth)

forward(image_embeddings, image_pe, sparse_prompt_embeddings, dense_prompt_embeddings, multimask_output)

์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์™€ ํ”„๋กฌํ”„ํŠธ ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•œ ๋งˆ์Šคํฌ๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
image_embeddings Tensor

์ด๋ฏธ์ง€ ์ธ์ฝ”๋”์—์„œ ์ž„๋ฒ ๋”ฉ๋œ ์ด๋ฏธ์ง€

ํ•„์ˆ˜
image_pe Tensor

์ด๋ฏธ์ง€_์ž„๋ฒ ๋”ฉ ๋ชจ์–‘์„ ์‚ฌ์šฉํ•œ ์œ„์น˜ ์ธ์ฝ”๋”ฉ

ํ•„์ˆ˜
sparse_prompt_embeddings Tensor

ํฌ์ธํŠธ์™€ ์ƒ์ž์˜ ์ž„๋ฒ ๋”ฉ

ํ•„์ˆ˜
dense_prompt_embeddings Tensor

๋งˆ์Šคํฌ ์ž…๋ ฅ์˜ ์ž„๋ฒ ๋”ฉ

ํ•„์ˆ˜
multimask_output bool

์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋งˆ์Šคํฌ๋ฅผ ๋ฐ˜ํ™˜ํ• ์ง€ ๋‹จ์ผ ๋งˆ์Šคํฌ๋ฅผ ๋ฐ˜ํ™˜ํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
Tensor

torch.Tensor: ์˜ˆ์ธก ๋งˆ์Šคํฌ ์ผ๊ด„ ์ฒ˜๋ฆฌ

Tensor

torch.Tensor: ๋งˆ์Šคํฌ ํ’ˆ์งˆ ์ผ๊ด„ ์˜ˆ์ธก

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/sam/modules/decoders.py
def forward(
    self,
    image_embeddings: torch.Tensor,
    image_pe: torch.Tensor,
    sparse_prompt_embeddings: torch.Tensor,
    dense_prompt_embeddings: torch.Tensor,
    multimask_output: bool,
) -> Tuple[torch.Tensor, torch.Tensor]:
    """
    Predict masks given image and prompt embeddings.

    Args:
        image_embeddings (torch.Tensor): the embeddings from the image encoder
        image_pe (torch.Tensor): positional encoding with the shape of image_embeddings
        sparse_prompt_embeddings (torch.Tensor): the embeddings of the points and boxes
        dense_prompt_embeddings (torch.Tensor): the embeddings of the mask inputs
        multimask_output (bool): Whether to return multiple masks or a single mask.

    Returns:
        torch.Tensor: batched predicted masks
        torch.Tensor: batched predictions of mask quality
    """
    masks, iou_pred = self.predict_masks(
        image_embeddings=image_embeddings,
        image_pe=image_pe,
        sparse_prompt_embeddings=sparse_prompt_embeddings,
        dense_prompt_embeddings=dense_prompt_embeddings,
    )

    # Select the correct mask or masks for output
    mask_slice = slice(1, None) if multimask_output else slice(0, 1)
    masks = masks[:, mask_slice, :, :]
    iou_pred = iou_pred[:, mask_slice]

    # Prepare output
    return masks, iou_pred

predict_masks(image_embeddings, image_pe, sparse_prompt_embeddings, dense_prompt_embeddings)

๋งˆ์Šคํฌ๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ '์•ž์œผ๋กœ'๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/sam/modules/decoders.py
def predict_masks(
    self,
    image_embeddings: torch.Tensor,
    image_pe: torch.Tensor,
    sparse_prompt_embeddings: torch.Tensor,
    dense_prompt_embeddings: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
    """
    Predicts masks.

    See 'forward' for more details.
    """
    # Concatenate output tokens
    output_tokens = torch.cat([self.iou_token.weight, self.mask_tokens.weight], dim=0)
    output_tokens = output_tokens.unsqueeze(0).expand(sparse_prompt_embeddings.shape[0], -1, -1)
    tokens = torch.cat((output_tokens, sparse_prompt_embeddings), dim=1)

    # Expand per-image data in batch direction to be per-mask
    src = torch.repeat_interleave(image_embeddings, tokens.shape[0], dim=0)
    src = src + dense_prompt_embeddings
    pos_src = torch.repeat_interleave(image_pe, tokens.shape[0], dim=0)
    b, c, h, w = src.shape

    # Run the transformer
    hs, src = self.transformer(src, pos_src, tokens)
    iou_token_out = hs[:, 0, :]
    mask_tokens_out = hs[:, 1 : (1 + self.num_mask_tokens), :]

    # Upscale mask embeddings and predict masks using the mask tokens
    src = src.transpose(1, 2).view(b, c, h, w)
    upscaled_embedding = self.output_upscaling(src)
    hyper_in_list: List[torch.Tensor] = [
        self.output_hypernetworks_mlps[i](mask_tokens_out[:, i, :]) for i in range(self.num_mask_tokens)
    ]
    hyper_in = torch.stack(hyper_in_list, dim=1)
    b, c, h, w = upscaled_embedding.shape
    masks = (hyper_in @ upscaled_embedding.view(b, c, h * w)).view(b, -1, h, w)

    # Generate mask quality predictions
    iou_pred = self.iou_prediction_head(iou_token_out)

    return masks, iou_pred



ultralytics.models.sam.modules.decoders.MLP

๋ฒ ์ด์Šค: Module

MLP(๋ฉ€ํ‹ฐ ๋ ˆ์ด์–ด ํผ์…‰ํŠธ๋ก ) ๋ชจ๋ธ์„ ์•ฝ๊ฐ„ ์ˆ˜์ •ํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. https://github.com/facebookresearch/MaskFormer/blob/main/mask_former/modeling/transformer/transformer_predictor.py

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/sam/modules/decoders.py
class MLP(nn.Module):
    """
    MLP (Multi-Layer Perceptron) model lightly adapted from
    https://github.com/facebookresearch/MaskFormer/blob/main/mask_former/modeling/transformer/transformer_predictor.py
    """

    def __init__(
        self,
        input_dim: int,
        hidden_dim: int,
        output_dim: int,
        num_layers: int,
        sigmoid_output: bool = False,
    ) -> None:
        """
        Initializes the MLP (Multi-Layer Perceptron) model.

        Args:
            input_dim (int): The dimensionality of the input features.
            hidden_dim (int): The dimensionality of the hidden layers.
            output_dim (int): The dimensionality of the output layer.
            num_layers (int): The number of hidden layers.
            sigmoid_output (bool, optional): Apply a sigmoid activation to the output layer. Defaults to False.
        """
        super().__init__()
        self.num_layers = num_layers
        h = [hidden_dim] * (num_layers - 1)
        self.layers = nn.ModuleList(nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim]))
        self.sigmoid_output = sigmoid_output

    def forward(self, x):
        """Executes feedforward within the neural network module and applies activation."""
        for i, layer in enumerate(self.layers):
            x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
        if self.sigmoid_output:
            x = torch.sigmoid(x)
        return x

__init__(input_dim, hidden_dim, output_dim, num_layers, sigmoid_output=False)

MLP(๋ฉ€ํ‹ฐ ๋ ˆ์ด์–ด ํผ์…‰ํŠธ๋ก ) ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
input_dim int

์ž…๋ ฅ ํ”ผ์ฒ˜์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
hidden_dim int

์ˆจ๊ฒจ์ง„ ๋ ˆ์ด์–ด์˜ ์น˜์ˆ˜์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
output_dim int

์ถœ๋ ฅ ๋ ˆ์ด์–ด์˜ ์น˜์ˆ˜์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
num_layers int

์ˆจ๊ฒจ์ง„ ๋ ˆ์ด์–ด์˜ ์ˆ˜์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
sigmoid_output bool

์ถœ๋ ฅ ๋ ˆ์ด์–ด์— ์‹œ๊ทธ๋ชจ์ด๋“œ ํ™œ์„ฑํ™”๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ False์ž…๋‹ˆ๋‹ค.

False
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/sam/modules/decoders.py
def __init__(
    self,
    input_dim: int,
    hidden_dim: int,
    output_dim: int,
    num_layers: int,
    sigmoid_output: bool = False,
) -> None:
    """
    Initializes the MLP (Multi-Layer Perceptron) model.

    Args:
        input_dim (int): The dimensionality of the input features.
        hidden_dim (int): The dimensionality of the hidden layers.
        output_dim (int): The dimensionality of the output layer.
        num_layers (int): The number of hidden layers.
        sigmoid_output (bool, optional): Apply a sigmoid activation to the output layer. Defaults to False.
    """
    super().__init__()
    self.num_layers = num_layers
    h = [hidden_dim] * (num_layers - 1)
    self.layers = nn.ModuleList(nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim]))
    self.sigmoid_output = sigmoid_output

forward(x)

์‹ ๊ฒฝ๋ง ๋ชจ๋“ˆ ๋‚ด์—์„œ ํ”ผ๋“œํฌ์›Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํ™œ์„ฑํ™”๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/sam/modules/decoders.py
def forward(self, x):
    """Executes feedforward within the neural network module and applies activation."""
    for i, layer in enumerate(self.layers):
        x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
    if self.sigmoid_output:
        x = torch.sigmoid(x)
    return x





Created 2023-11-12, Updated 2024-06-02
Authors: glenn-jocher (5), Burhan-Q (1), Laughing-q (1)