class_docstr: AHNet based on `Anisotropic Hybrid Network <https://arxiv.org/pdf/1711.08580.pdf>`_.
  Adapted from `lsqshr's official code <https://github.com/lsqshr/AH-Net/blob/master/net3d.py>`_.
  Except from the original network that supports 3D inputs, this implementation also supports 2D inputs.
  According to the `tests for deconvolutions <https://github.com/Project-MONAI/MONAI/issues/1023>`_, using
  ``"transpose"`` rather than linear interpolations is faster. Therefore, this implementation sets ``"transpose"``
  as the default upsampling method.
  
  To meet the requirements of the structure, the input size for each spatial dimension
  (except the last one) should be: divisible by 2 ** (psp_block_num + 3) and no less than 32 in ``transpose`` mode,
  and should be divisible by 32 and no less than 2 ** (psp_block_num + 3) in other upsample modes.
  In addition, the input size for the last spatial dimension should be divisible by 32, and at least one spatial size
  should be no less than 64.
  
  Args:
      layers: number of residual blocks for 4 layers of the network (layer1...layer4). Defaults to ``(3, 4, 6, 3)``.
      spatial_dims: spatial dimension of the input data. Defaults to 3.
      in_channels: number of input channels for the network. Default to 1.
      out_channels: number of output channels for the network. Defaults to 1.
      psp_block_num: the number of pyramid volumetric pooling modules used at the end of the network before the final
          output layer for extracting multiscale features. The number should be an integer that belongs to [0,4]. Defaults
          to 4.
      upsample_mode: [``"transpose"``, ``"bilinear"``, ``"trilinear"``, ``nearest``]
          The mode of upsampling manipulations.
          Using the last two modes cannot guarantee the model's reproducibility. Defaults to ``transpose``.
  
          - ``"transpose"``, uses transposed convolution layers.
          - ``"bilinear"``, uses bilinear interpolate.
          - ``"trilinear"``, uses trilinear interpolate.
          - ``"nearest"``, uses nearest interpolate.
      pretrained: whether to load pretrained weights from ResNet50 to initialize convolution layers, default to False.
      progress: If True, displays a progress bar of the download of pretrained weights to stderr.
class_docstr_tok: ["AHNet","based","on","`","Anisotropic","Hybrid","Network","<","https",":","\/\/arxiv.org\/pdf\/1711.08580.pdf",">","`","_",".","Adapted","from","`","lsqshr","'s","official","code","<","https",":","\/\/github.com\/lsqshr\/AH-Net\/blob\/master\/net3d.py",">","`","_",".","Except","from","the","original","network","that","supports","3D","inputs",",","this","implementation","also","supports","2D","inputs",".","According","to","the","`","tests","for","deconvolutions","<","https",":","\/\/github.com\/Project-MONAI\/MONAI\/issues\/1023",">","`","_",",","using","``","``","transpose","''","``","rather","than","linear","interpolations","is","faster",".","Therefore",",","this","implementation","sets","``","``","transpose","''","``","as","the","default","upsampling","method",".","To","meet","the","requirements","of","the","structure",",","the","input","size","for","each","spatial","dimension","(","except","the","last","one",")","should","be",":","divisible","by","2","*","*","(","psp_block_num","+","3",")","and","no","less","than","32","in","``","transpose","``","mode",",","and","should","be","divisible","by","32","and","no","less","than","2","*","*","(","psp_block_num","+","3",")","in","other","upsample","modes",".","In","addition",",","the","input","size","for","the","last","spatial","dimension","should","be","divisible","by","32",",","and","at","least","one","spatial","size","should","be","no","less","than","64",".","Args",":","layers",":","number","of","residual","blocks","for","4","layers","of","the","network","(","layer1","...","layer4",")",".","Defaults","to","``","(","3",",","4",",","6",",","3",")","``",".","spatial_dims",":","spatial","dimension","of","the","input","data",".","Defaults","to","3.","in_channels",":","number","of","input","channels","for","the","network",".","Default","to","1.","out_channels",":","number","of","output","channels","for","the","network",".","Defaults","to","1.","psp_block_num",":","the","number","of","pyramid","volumetric","pooling","modules","used","at","the","end","of","the","network","before","the","final","output","layer","for","extracting","multiscale","features",".","The","number","should","be","an","integer","that","belongs","to","[","0,4","]",".","Defaults","to","4.","upsample_mode",":","[","``","``","transpose","''","``",",","``","``","bilinear","''","``",",","``","``","trilinear","''","``",",","``","nearest","``","]","The","mode","of","upsampling","manipulations",".","Using","the","last","two","modes","can","not","guarantee","the","model","'s","reproducibility",".","Defaults","to","``","transpose","``",".","-","``","``","transpose","''","``",",","uses","transposed","convolution","layers",".","-","``","``","bilinear","''","``",",","uses","bilinear","interpolate",".","-","``","``","trilinear","''","``",",","uses","trilinear","interpolate",".","-","``","``","nearest","''","``",",","uses","nearest","interpolate",".","pretrained",":","whether","to","load","pretrained","weights","from","ResNet50","to","initialize","convolution","layers",",","default","to","False",".","progress",":","If","True",",","displays","a","progress","bar","of","the","download","of","pretrained","weights","to","stderr","."]
code: def __init__(
      self,
      layers: tuple = (3, 4, 6, 3),
      spatial_dims: int = 3,
      in_channels: int = 1,
      out_channels: int = 1,
      psp_block_num: int = 4,
      upsample_mode: str = "transpose",
      pretrained: bool = False,
      progress: bool = True,
  ):
      self.inplanes = 64
      super().__init__()
  
      conv_type = Conv[Conv.CONV, spatial_dims]
      conv_trans_type = Conv[Conv.CONVTRANS, spatial_dims]
      norm_type = Norm[Norm.BATCH, spatial_dims]
      pool_type: type[nn.MaxPool2d | nn.MaxPool3d] = Pool[
          Pool.MAX, spatial_dims
      ]
      relu_type: type[nn.ReLU] = Act[Act.RELU]
      conv2d_type: type[nn.Conv2d] = Conv[Conv.CONV, 2]
      norm2d_type: type[nn.BatchNorm2d] = Norm[Norm.BATCH, 2]
  
      self.conv2d_type = conv2d_type
      self.norm2d_type = norm2d_type
      self.conv_type = conv_type
      self.norm_type = norm_type
      self.relu_type = relu_type
      self.pool_type = pool_type
      self.spatial_dims = spatial_dims
      self.psp_block_num = psp_block_num
      self.psp: PSP
  
      if spatial_dims not in [2, 3]:
          raise AssertionError("spatial_dims can only be 2 or 3.")
      if psp_block_num not in [0, 1, 2, 3, 4]:
          raise AssertionError(
              "psp_block_num should be an integer that belongs to [0, 4]."
          )
  
      self.conv1 = conv_type(
          in_channels,
          64,
          kernel_size=(7, 7, 3)[-spatial_dims:],
          stride=(2, 2, 1)[-spatial_dims:],
          padding=(3, 3, 1)[-spatial_dims:],
          bias=False,
      )
      self.pool1 = pool_type(
          kernel_size=(1, 1, 2)[-spatial_dims:],
          stride=(1, 1, 2)[-spatial_dims:],
      )
      self.bn0 = norm_type(64)
      self.relu = relu_type(inplace=True)
      if upsample_mode in ["transpose", "nearest"]:
          # To maintain the determinism, the value of kernel_size and stride should be the same.
          # (you can check this link for reference: https://github.com/Project-MONAI/MONAI/pull/815 )
          self.maxpool = pool_type(
              kernel_size=(2, 2, 2)[-spatial_dims:], stride=2
          )
      else:
          self.maxpool = pool_type(
              kernel_size=(3, 3, 3)[-spatial_dims:], stride=2, padding=1
          )
  
      self.layer1 = self._make_layer(
          Bottleneck3x3x1, 64, layers[0], stride=1
      )
      self.layer2 = self._make_layer(
          Bottleneck3x3x1, 128, layers[1], stride=2
      )
      self.layer3 = self._make_layer(
          Bottleneck3x3x1, 256, layers[2], stride=2
      )
      self.layer4 = self._make_layer(
          Bottleneck3x3x1, 512, layers[3], stride=2
      )
  
      # Make the 3D dense decoder layers
      densegrowth = 20
      densebn = 4
      ndenselayer = 3
  
      num_init_features = 64
      noutres1 = 256
      noutres2 = 512
      noutres3 = 1024
      noutres4 = 2048
  
      self.up0 = UpTransition(
          spatial_dims, noutres4, noutres3, upsample_mode
      )
      self.dense0 = DenseBlock(
          spatial_dims, ndenselayer, noutres3, densebn, densegrowth, 0.0
      )
      noutdense = noutres3 + ndenselayer * densegrowth
  
      self.up1 = UpTransition(
          spatial_dims, noutdense, noutres2, upsample_mode
      )
      self.dense1 = DenseBlock(
          spatial_dims, ndenselayer, noutres2, densebn, densegrowth, 0.0
      )
      noutdense1 = noutres2 + ndenselayer * densegrowth
  
      self.up2 = UpTransition(
          spatial_dims, noutdense1, noutres1, upsample_mode
      )
      self.dense2 = DenseBlock(
          spatial_dims, ndenselayer, noutres1, densebn, densegrowth, 0.0
      )
      noutdense2 = noutres1 + ndenselayer * densegrowth
  
      self.trans1 = Projection(
          spatial_dims, noutdense2, num_init_features
      )
      self.dense3 = DenseBlock(
          spatial_dims,
          ndenselayer,
          num_init_features,
          densebn,
          densegrowth,
          0.0,
      )
      noutdense3 = num_init_features + densegrowth * ndenselayer
  
      self.up3 = UpTransition(
          spatial_dims, noutdense3, num_init_features, upsample_mode
      )
      self.dense4 = DenseBlock(
          spatial_dims,
          ndenselayer,
          num_init_features,
          densebn,
          densegrowth,
          0.0,
      )
      noutdense4 = num_init_features + densegrowth * ndenselayer
  
      self.psp = PSP(
          spatial_dims, psp_block_num, noutdense4, upsample_mode
      )
      self.final = Final(
          spatial_dims,
          psp_block_num + noutdense4,
          out_channels,
          upsample_mode,
      )
  
      # Initialise parameters
      for m in self.modules():
          if isinstance(m, (conv_type, conv_trans_type)):
              n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
              m.weight.data.normal_(0, math.sqrt(2.0 / n))
          elif isinstance(m, norm_type):
              m.weight.data.fill_(1)
              m.bias.data.zero_()
  
      if pretrained:
          net2d = FCN(pretrained=True, progress=progress)
          self.copy_from(net2d)
  
imports: from __future__ import annotations
  import math
  from collections.abc import Sequence
  from typing import Union
  import torch
  import torch.nn
  import torch.nn.functional
  from monai.networks.blocks.fcn import FCN
  from monai.networks.layers.factories import Act, Conv, Norm, Pool
label_desc: Use image node_id 1 to create a new AHNet object from inherited base classes: nn with example: obj = AHNet(layers, spatial_dims, in_channels, out_channels, psp_block_num, upsample_mode, pretrained, progress)