def get_norm_layer(name: Union[Tuple, str], spatial_dims: Optional[int] = 1, channels: Optional[int] = 1): """ Create a normalization layer instance. For example, to create normalization layers: .. code-block:: python from monai.networks.layers import get_norm_layer g_layer = get_norm_layer(name=("group", {"num_groups": 1})) n_layer = get_norm_layer(name="instance", spatial_dims=2) Args: name: a normalization type string or a tuple of type string and parameters. spatial_dims: number of spatial dimensions of the input. channels: number of features/channels when the normalization layer requires this parameter but it is not specified in the norm parameters. """ norm_name, norm_args = split_args(name) norm_type = Norm[norm_name, spatial_dims] kw_args = dict(norm_args) if has_option(norm_type, "num_features") and "num_features" not in kw_args: kw_args["num_features"] = channels if has_option(norm_type, "num_channels") and "num_channels" not in kw_args: kw_args["num_channels"] = channels return norm_type(**kw_args)
def __init__( self, ordering: str = "NDA", in_channels: Optional[int] = None, act: Optional[Union[Tuple, str]] = "RELU", norm: Optional[Union[Tuple, str]] = None, norm_dim: Optional[int] = None, dropout: Optional[Union[Tuple, str, float]] = None, dropout_dim: Optional[int] = None, ) -> None: super().__init__() op_dict = {"A": None, "D": None, "N": None} # define the normalization type and the arguments to the constructor if norm is not None: if norm_dim is None and dropout_dim is None: raise ValueError( "norm_dim or dropout_dim needs to be specified.") norm_name, norm_args = split_args(norm) norm_type = Norm[norm_name, norm_dim or dropout_dim] kw_args = dict(norm_args) if has_option(norm_type, "num_features") and "num_features" not in kw_args: kw_args["num_features"] = in_channels if has_option(norm_type, "num_channels") and "num_channels" not in kw_args: kw_args["num_channels"] = in_channels op_dict["N"] = norm_type(**kw_args) # define the activation type and the arguments to the constructor if act is not None: act_name, act_args = split_args(act) act_type = Act[act_name] op_dict["A"] = act_type(**act_args) if dropout is not None: # if dropout was specified simply as a p value, use default name and make a keyword map with the value if isinstance(dropout, (int, float)): drop_name = Dropout.DROPOUT drop_args = {"p": float(dropout)} else: drop_name, drop_args = split_args(dropout) if norm_dim is None and dropout_dim is None: raise ValueError( "norm_dim or dropout_dim needs to be specified.") drop_type = Dropout[drop_name, dropout_dim or norm_dim] op_dict["D"] = drop_type(**drop_args) for item in ordering.upper(): if item not in op_dict: raise ValueError( f"ordering must be a string of {op_dict}, got {item} in it." ) if op_dict[item] is not None: self.add_module(item, op_dict[item]) # type: ignore