MCPcopy Create free account
hub / github.com/apple/ml-sharp / __init__

Method __init__

src/sharp/models/decoders/unet_decoder.py:26–94  ·  view source on GitHub ↗

Initialize UNet Decoder. Args: dim_out: The number of output channels. width: Width of last input feature map from encoder or the width list of all input feature maps from encoder. steps: The number of upsampling steps. norm_ty

(
        self,
        dim_out: int,
        width: List[int] | int,
        steps: int = 5,
        norm_type: NormLayerName = "group_norm",
        norm_num_groups=8,
        blocks_per_layer=2,
    )

Source from the content-addressed store, hash-verified

24 """Decoder of UNet model."""
25
26 def __init__(
27 self,
28 dim_out: int,
29 width: List[int] | int,
30 steps: int = 5,
31 norm_type: NormLayerName = "group_norm",
32 norm_num_groups=8,
33 blocks_per_layer=2,
34 ) -> None:
35 """Initialize UNet Decoder.
36
37 Args:
38 dim_out: The number of output channels.
39 width: Width of last input feature map from encoder
40 or the width list of all input feature maps from encoder.
41 steps: The number of upsampling steps.
42 norm_type: Which kind of normalization layer to use.
43 norm_num_groups: How many groups to use for group norm (if relevant).
44 blocks_per_layer: How many blocks per layer to use.
45 """
46 super().__init__()
47
48 if blocks_per_layer < 1:
49 raise ValueError("blocks_per_layer must be greater or equal to one.")
50
51 self.dim_out = dim_out
52
53 self.convs_up = nn.ModuleList()
54
55 self.output_dims: list[int]
56 # If only one number is specified, we assume each layer will double the channel dimension.
57 if isinstance(width, int):
58 self.input_dims = [width >> i for i in range(0, steps + 1)]
59 else:
60 self.input_dims = width[::-1][: steps + 1]
61
62 for i_step in range(steps):
63 input_width = self.input_dims[i_step]
64 current_width = self.input_dims[i_step + 1]
65 convs_up_i = nn.Sequential(
66 nn.Upsample(scale_factor=2),
67 residual_block_2d(
68 input_width * (1 if i_step == 0 else 2),
69 current_width,
70 norm_type=norm_type,
71 norm_num_groups=norm_num_groups,
72 ),
73 *[
74 residual_block_2d(
75 current_width,
76 current_width,
77 norm_type=norm_type,
78 norm_num_groups=norm_num_groups,
79 )
80 for _ in range(blocks_per_layer - 1)
81 ],
82 )
83 self.convs_up.append(convs_up_i)

Callers

nothing calls this directly

Calls 2

residual_block_2dFunction · 0.90
norm_layer_2dFunction · 0.90

Tested by

no test coverage detected