class_docstr: A flexible implementation of UNet-like encoder-decoder architecture.
class_docstr_tok: ["A","flexible","implementation","of","UNet-like","encoder-decoder","architecture","."]
docstr: A flexible implement of UNet, in which the backbone/encoder can be replaced with
  any efficient network. Currently the input must have a 2 or 3 spatial dimension
  and the spatial size of each dimension must be a multiple of 32 if is_pad parameter
  is False.
  Please notice each output of backbone must be 2x downsample in spatial dimension
  of last output. For example, if given a 512x256 2D image and a backbone with 4 outputs.
  Spatial size of each encoder output should be 256x128, 128x64, 64x32 and 32x16.
  
  Args:
      in_channels: number of input channels.
      out_channels: number of output channels.
      backbone: name of backbones to initialize, only support efficientnet right now,
          can be from [efficientnet-b0,..., efficientnet-b8, efficientnet-l2].
      pretrained: whether to initialize pretrained ImageNet weights, only available
          for spatial_dims=2 and batch norm is used, default to False.
      decoder_channels: number of output channels for all feature maps in decoder.
          `len(decoder_channels)` should equal to `len(encoder_channels) - 1`,default
          to (256, 128, 64, 32, 16).
      spatial_dims: number of spatial dimensions, default to 2.
      norm: normalization type and arguments, default to ("batch", {"eps": 1e-3,
          "momentum": 0.1}).
      act: activation type and arguments, default to ("relu", {"inplace": True}).
      dropout: dropout ratio, default to 0.0.
      decoder_bias: whether to have a bias term in decoder's convolution blocks.
      upsample: upsampling mode, available options are``"deconv"``, ``"pixelshuffle"``,
          ``"nontrainable"``.
      pre_conv:a conv block applied before upsampling. Only used in the "nontrainable" or
          "pixelshuffle" mode, default to `default`.
      interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``}
          Only used in the "nontrainable" mode.
      is_pad: whether to pad upsampling features to fit features from encoder. Default to True.
          If this parameter is set to "True", the spatial dim of network input can be arbitrary
          size, which is not supported by TensorRT. Otherwise, it must be a multiple of 32.
docstr_tok: ["A","flexible","implement","of","UNet",",","in","which","the","backbone\/encoder","can","be","replaced","with","any","efficient","network",".","Currently","the","input","must","have","a","2","or","3","spatial","dimension","and","the","spatial","size","of","each","dimension","must","be","a","multiple","of","32","if","is_pad","parameter","is","False",".","Please","notice","each","output","of","backbone","must","be","2x","downsample","in","spatial","dimension","of","last","output",".","For","example",",","if","given","a","512x256","2D","image","and","a","backbone","with","4","outputs",".","Spatial","size","of","each","encoder","output","should","be","256x128",",","128x64",",","64x32","and","32x16",".","Args",":","in_channels",":","number","of","input","channels",".","out_channels",":","number","of","output","channels",".","backbone",":","name","of","backbones","to","initialize",",","only","support","efficientnet","right","now",",","can","be","from","[","efficientnet-b0",",","...",",","efficientnet-b8",",","efficientnet-l2","]",".","pretrained",":","whether","to","initialize","pretrained","ImageNet","weights",",","only","available","for","spatial_dims=2","and","batch","norm","is","used",",","default","to","False",".","decoder_channels",":","number","of","output","channels","for","all","feature","maps","in","decoder",".","`","len","(","decoder_channels",")","`","should","equal","to","`","len","(","encoder_channels",")","-","1","`",",","default","to","(","256",",","128",",","64",",","32",",","16",")",".","spatial_dims",":","number","of","spatial","dimensions",",","default","to","2.","norm",":","normalization","type","and","arguments",",","default","to","(","``","batch","''",",","{","``","eps","''",":","1e-3",",","``","momentum","''",":","0.1","}",")",".","act",":","activation","type","and","arguments",",","default","to","(","``","relu","''",",","{","``","inplace","''",":","True","}",")",".","dropout",":","dropout","ratio",",","default","to","0.0.","decoder_bias",":","whether","to","have","a","bias","term","in","decoder","'s","convolution","blocks",".","upsample",":","upsampling","mode",",","available","options","are","``","``","deconv","''","``",",","``","``","pixelshuffle","''","``",",","``","``","nontrainable","''","``",".","pre_conv",":","a","conv","block","applied","before","upsampling",".","Only","used","in","the","``","nontrainable","''","or","``","pixelshuffle","''","mode",",","default","to","`","default","`",".","interp_mode",":","{","``","``","nearest","''","``",",","``","``","linear","''","``",",","``","``","bilinear","''","``",",","``","``","bicubic","''","``",",","``","``","trilinear","''","``","}","Only","used","in","the","``","nontrainable","''","mode",".","is_pad",":","whether","to","pad","upsampling","features","to","fit","features","from","encoder",".","Default","to","True",".","If","this","parameter","is","set","to","``","True","''",",","the","spatial","dim","of","network","input","can","be","arbitrary","size",",","which","is","not","supported","by","TensorRT",".","Otherwise",",","it","must","be","a","multiple","of","32","."]
code: def __init__(
      self,
      in_channels: int,
      out_channels: int,
      backbone: str,
      pretrained: bool = False,
      decoder_channels: tuple = (256, 128, 64, 32, 16),
      spatial_dims: int = 2,
      norm: str | tuple = ("batch", {"eps": 1e-3, "momentum": 0.1}),
      act: str | tuple = ("relu", {"inplace": True}),
      dropout: float | tuple = 0.0,
      decoder_bias: bool = False,
      upsample: str = "nontrainable",
      pre_conv: str = "default",
      interp_mode: str = "nearest",
      is_pad: bool = True,
  ) -> None:
      """
      A flexible implement of UNet, in which the backbone/encoder can be replaced with
      any efficient network. Currently the input must have a 2 or 3 spatial dimension
      and the spatial size of each dimension must be a multiple of 32 if is_pad parameter
      is False.
      Please notice each output of backbone must be 2x downsample in spatial dimension
      of last output. For example, if given a 512x256 2D image and a backbone with 4 outputs.
      Spatial size of each encoder output should be 256x128, 128x64, 64x32 and 32x16.
  
      Args:
          in_channels: number of input channels.
          out_channels: number of output channels.
          backbone: name of backbones to initialize, only support efficientnet right now,
              can be from [efficientnet-b0,..., efficientnet-b8, efficientnet-l2].
          pretrained: whether to initialize pretrained ImageNet weights, only available
              for spatial_dims=2 and batch norm is used, default to False.
          decoder_channels: number of output channels for all feature maps in decoder.
              `len(decoder_channels)` should equal to `len(encoder_channels) - 1`,default
              to (256, 128, 64, 32, 16).
          spatial_dims: number of spatial dimensions, default to 2.
          norm: normalization type and arguments, default to ("batch", {"eps": 1e-3,
              "momentum": 0.1}).
          act: activation type and arguments, default to ("relu", {"inplace": True}).
          dropout: dropout ratio, default to 0.0.
          decoder_bias: whether to have a bias term in decoder's convolution blocks.
          upsample: upsampling mode, available options are``"deconv"``, ``"pixelshuffle"``,
              ``"nontrainable"``.
          pre_conv:a conv block applied before upsampling. Only used in the "nontrainable" or
              "pixelshuffle" mode, default to `default`.
          interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``}
              Only used in the "nontrainable" mode.
          is_pad: whether to pad upsampling features to fit features from encoder. Default to True.
              If this parameter is set to "True", the spatial dim of network input can be arbitrary
              size, which is not supported by TensorRT. Otherwise, it must be a multiple of 32.
      """
      super().__init__()
  
      if backbone not in FLEXUNET_BACKBONE.register_dict:
          raise ValueError(
              f"invalid model_name {backbone} found, must be one of {FLEXUNET_BACKBONE.register_dict.keys()}."
          )
  
      if spatial_dims not in (2, 3):
          raise ValueError("spatial_dims can only be 2 or 3.")
  
      encoder = FLEXUNET_BACKBONE.register_dict[backbone]
      self.backbone = backbone
      self.spatial_dims = spatial_dims
      encoder_parameters = encoder["parameter"]
      if not (
          ("spatial_dims" in encoder_parameters)
          and ("in_channels" in encoder_parameters)
          and ("pretrained" in encoder_parameters)
      ):
          raise ValueError(
              "The backbone init method must have spatial_dims, in_channels and pretrained parameters."
          )
      encoder_feature_num = encoder["feature_number"]
      if encoder_feature_num > 5:
          raise ValueError(
              "Flexible unet can only accept no more than 5 encoder feature maps."
          )
  
      decoder_channels = decoder_channels[:encoder_feature_num]
      self.skip_connect = encoder_feature_num - 1
      encoder_parameters.update(
          {
              "spatial_dims": spatial_dims,
              "in_channels": in_channels,
              "pretrained": pretrained,
          }
      )
      encoder_channels = tuple(
          [in_channels] + list(encoder["feature_channel"])
      )
      encoder_type = encoder["type"]
      self.encoder = encoder_type(**encoder_parameters)
  
      self.decoder = UNetDecoder(
          spatial_dims=spatial_dims,
          encoder_channels=encoder_channels,
          decoder_channels=decoder_channels,
          act=act,
          norm=norm,
          dropout=dropout,
          bias=decoder_bias,
          upsample=upsample,
          interp_mode=interp_mode,
          pre_conv=pre_conv,
          align_corners=None,
          is_pad=is_pad,
      )
      self.segmentation_head = SegmentationHead(
          spatial_dims=spatial_dims,
          in_channels=decoder_channels[-1],
          out_channels=out_channels,
          kernel_size=3,
          act=None,
      )
  
imports: from __future__ import annotations
  import warnings
  from collections.abc import Sequence
  from pydoc import locate
  from typing import Any
  import torch
  from torch import nn
  from monai.networks.blocks import BaseEncoder, UpSample
  from monai.networks.layers.factories import Conv
  from monai.networks.layers.utils import get_act_layer
  from monai.networks.nets import EfficientNetEncoder
  from monai.networks.nets.basic_unet import UpCat
  from monai.utils import InterpolateMode, optional_import
label_desc: Use image node_id 1 to create a new FlexibleUNet object from inherited base classes: nn with example: obj = FlexibleUNet(in_channels, out_channels, backbone, pretrained, decoder_channels, spatial_dims, norm, act, dropout, decoder_bias, upsample, pre_conv, interp_mode, is_pad)