U
    Ph-                     @  s   d dl mZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZ d dlmZ G dd dejZG d	d
 d
ejZdS )    )annotations)SequenceN)ADN)same_paddingstride_minus_kernel_padding)Convc                      sJ   e Zd ZdZdd
d
d
ddddddddd
ddddddd fddZ  ZS )Convolutiona
  
    Constructs a convolution with normalization, optional dropout, and optional activation layers::

        -- (Conv|ConvTrans) -- (Norm -- Dropout -- Acti) --

    if ``conv_only`` set to ``True``::

        -- (Conv|ConvTrans) --

    For example:

    .. code-block:: python

        from monai.networks.blocks import Convolution

        conv = Convolution(
            spatial_dims=3,
            in_channels=1,
            out_channels=1,
            adn_ordering="ADN",
            act=("prelu", {"init": 0.2}),
            dropout=0.1,
            norm=("layer", {"normalized_shape": (10, 10, 10)}),
        )
        print(conv)

    output::

        Convolution(
          (conv): Conv3d(1, 1, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
          (adn): ADN(
            (A): PReLU(num_parameters=1)
            (D): Dropout(p=0.1, inplace=False)
            (N): LayerNorm((10, 10, 10), eps=1e-05, elementwise_affine=True)
          )
        )

    Args:
        spatial_dims: number of spatial dimensions.
        in_channels: number of input channels.
        out_channels: number of output channels.
        strides: convolution stride. Defaults to 1.
        kernel_size: convolution kernel size. Defaults to 3.
        adn_ordering: a string representing the ordering of activation, normalization, and dropout.
            Defaults to "NDA".
        act: activation type and arguments. Defaults to PReLU.
        norm: feature normalization type and arguments. Defaults to instance norm.
        dropout: dropout ratio. Defaults to no dropout.
        dropout_dim: determine the spatial dimensions of dropout. Defaults to 1.

            - When dropout_dim = 1, randomly zeroes some of the elements for each channel.
            - When dropout_dim = 2, Randomly zeroes out entire channels (a channel is a 2D feature map).
            - When dropout_dim = 3, Randomly zeroes out entire channels (a channel is a 3D feature map).

            The value of dropout_dim should be no larger than the value of `spatial_dims`.
        dilation: dilation rate. Defaults to 1.
        groups: controls the connections between inputs and outputs. Defaults to 1.
        bias: whether to have a bias term. Defaults to True.
        conv_only: whether to use the convolutional layer only. Defaults to False.
        is_transposed: if True uses ConvTrans instead of Conv. Defaults to False.
        padding: controls the amount of implicit zero-paddings on both sides for padding number of points
            for each dimension. Defaults to None.
        output_padding: controls the additional size added to one side of the output shape.
            Defaults to None.

    See also:

        :py:class:`monai.networks.layers.Conv`
        :py:class:`monai.networks.blocks.ADN`

          NDAPRELUINSTANCENTFintSequence[int] | intstrtuple | str | Nonetuple | str | float | None
int | NoneboolSequence[int] | int | NoneNone)spatial_dimsin_channelsout_channelsstrideskernel_sizeadn_orderingactnormdropoutdropout_dimdilationgroupsbias	conv_onlyis_transposedpaddingoutput_paddingreturnc                   s   t    || _|| _|| _|| _|d kr4t||}t|r@tjntj	| jf }|r|d krdt
d|}||||||||||d	}n|||||||||d}| d| |rd S |d kr|d kr|	d krd S | dt||||| j|	|
d d S )Nr	   )r   strider&   r'   r"   r#   r!   )r   r)   r&   r!   r"   r#   convZadn)orderingr   r   r   norm_dimr   r    )super__init__r   r   r   r%   r   r   	CONVTRANSCONVr   
add_moduler   )selfr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   	conv_typer*   	__class__ W/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/blocks/convolutions.pyr.   b   sb    


zConvolution.__init__)r	   r
   r   r   r   Nr	   r	   r	   TFFNN)__name__
__module____qualname____doc__r.   __classcell__r6   r6   r4   r7   r      s    M              r   c                      sV   e Zd ZdZdddddddddddddddddd fddZdddddZ  ZS )ResidualUnita
  
    Residual module with multiple convolutions and a residual connection.

    For example:

    .. code-block:: python

        from monai.networks.blocks import ResidualUnit

        convs = ResidualUnit(
            spatial_dims=3,
            in_channels=1,
            out_channels=1,
            adn_ordering="AN",
            act=("prelu", {"init": 0.2}),
            norm=("layer", {"normalized_shape": (10, 10, 10)}),
        )
        print(convs)

    output::

        ResidualUnit(
          (conv): Sequential(
            (unit0): Convolution(
              (conv): Conv3d(1, 1, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
              (adn): ADN(
                (A): PReLU(num_parameters=1)
                (N): LayerNorm((10, 10, 10), eps=1e-05, elementwise_affine=True)
              )
            )
            (unit1): Convolution(
              (conv): Conv3d(1, 1, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
              (adn): ADN(
                (A): PReLU(num_parameters=1)
                (N): LayerNorm((10, 10, 10), eps=1e-05, elementwise_affine=True)
              )
            )
          )
          (residual): Identity()
        )

    Args:
        spatial_dims: number of spatial dimensions.
        in_channels: number of input channels.
        out_channels: number of output channels.
        strides: convolution stride. Defaults to 1.
        kernel_size: convolution kernel size. Defaults to 3.
        subunits: number of convolutions. Defaults to 2.
        adn_ordering: a string representing the ordering of activation, normalization, and dropout.
            Defaults to "NDA".
        act: activation type and arguments. Defaults to PReLU.
        norm: feature normalization type and arguments. Defaults to instance norm.
        dropout: dropout ratio. Defaults to no dropout.
        dropout_dim: determine the dimensions of dropout. Defaults to 1.

            - When dropout_dim = 1, randomly zeroes some of the elements for each channel.
            - When dropout_dim = 2, Randomly zero out entire channels (a channel is a 2D feature map).
            - When dropout_dim = 3, Randomly zero out entire channels (a channel is a 3D feature map).

            The value of dropout_dim should be no larger than the value of `dimensions`.
        dilation: dilation rate. Defaults to 1.
        bias: whether to have a bias term. Defaults to True.
        last_conv_only: for the last subunit, whether to use the convolutional layer only.
            Defaults to False.
        padding: controls the amount of implicit zero-paddings on both sides for padding number of points
            for each dimension. Defaults to None.

    See also:

        :py:class:`monai.networks.blocks.Convolution`

    r	   r
      r   r   r   NTFr   r   r   r   r   r   r   r   r   )r   r   r   r   r   subunitsr   r   r   r   r    r!   r#   last_conv_onlyr&   r(   c                   s  t    || _|| _|| _t | _t | _	|s>t
||}|}|}td|}t|D ]X}|oj||d k}t| j|||||||	|
|||||d}| jd|d| |}d}qXt|dks||kr|}|}t|dkrd}d}ttj| jf }|||||||d| _	d S )Nr	   )r   r   r   r   r   r   r    r!   r#   r$   r&   unitdr   )r#   )r-   r.   r   r   r   nn
Sequentialr*   Identityresidualr   maxranger   r1   npprodr   r0   )r2   r   r   r   r   r   r?   r   r   r   r   r    r!   r#   r@   r&   Z	schannelsZsstridessur$   rA   Zrkernel_sizeZrpaddingr3   r4   r6   r7   r.      sP    




zResidualUnit.__init__ztorch.Tensor)xr(   c                 C  s   |  |}| |}|| S )N)rF   r*   )r2   rL   rescxr6   r6   r7   forward;  s    

zResidualUnit.forward)r	   r
   r>   r   r   r   Nr	   r	   TFN)r8   r9   r:   r;   r.   rO   r<   r6   r6   r4   r7   r=      s   N            2Cr=   )
__future__r   collections.abcr   numpyrI   torchtorch.nnrC   Zmonai.networks.blocksr   monai.networks.layers.convutilsr   r   monai.networks.layers.factoriesr   rD   r   Moduler=   r6   r6   r6   r7   <module>   s    