MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / MLP

Class MLP

sam2_train/modeling/sam2_utils.py:108–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106# Lightly adapted from
107# https://github.com/facebookresearch/MaskFormer/blob/main/mask_former/modeling/transformer/transformer_predictor.py # noqa
108class MLP(nn.Module):
109 def __init__(
110 self,
111 input_dim: int,
112 hidden_dim: int,
113 output_dim: int,
114 num_layers: int,
115 activation: nn.Module = nn.ReLU,
116 sigmoid_output: bool = False,
117 ) -> None:
118 super().__init__()
119 self.num_layers = num_layers
120 h = [hidden_dim] * (num_layers - 1)
121 self.layers = nn.ModuleList(
122 nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim])
123 )
124 self.sigmoid_output = sigmoid_output
125 self.act = activation()
126
127 def forward(self, x):
128 for i, layer in enumerate(self.layers):
129 x = self.act(layer(x)) if i < self.num_layers - 1 else layer(x)
130 if self.sigmoid_output:
131 x = F.sigmoid(x)
132 return x
133
134
135# From https://github.com/facebookresearch/detectron2/blob/main/detectron2/layers/batch_norm.py # noqa

Callers 4

_build_sam_headsMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected