o
    )i-                     @  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                      s@   e Zd ZdZ																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spatial_dimsintin_channelsout_channelsstridesSequence[int] | intkernel_sizeadn_orderingstracttuple | str | Nonenormdropouttuple | str | float | Nonedropout_dim
int | Nonedilationgroupsbiasbool	conv_onlyis_transposedpaddingSequence[int] | int | Noneoutput_paddingreturnNonec                   s   t    || _|| _|| _|| _|d u rt||}t|r tjntj	| jf }|r@|d u r2t
d|}||||||||||d	}n|||||||||d}| d| |rVd S |d u rd|d u rd|	d u rdd 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__ d/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/blocks/convolutions.pyr.   b   sb   


zConvolution.__init__)r	   r
   r   r   r   Nr	   r	   r	   TFFNN)$r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r!   r#   r!   r$   r%   r&   r%   r'   r(   )__name__
__module____qualname____doc__r.   __classcell__r6   r6   r4   r7   r      s"    Mr   c                      sF   e Zd ZdZ												
	d*d+ fd$d%Z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   subunitsr   r   r   r   r   r   r   r   r   r   r    r!   last_conv_onlyr$   r%   r'   r(   c                   s  t    || _|| _|| _t | _t | _	|st
||}|}|}td|}t|D ],}|o5||d k}t| j|||||||	|
|||||d}| jd|d| |}d}q,t|dksd||kr|}|}t|dkrsd}d}ttj| jf }|||||||d| _	d S 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sstridesZsur"   rA   Zrkernel_sizeZrpaddingr3   r4   r6   r7   r.      sR   




zResidualUnit.__init__xtorch.Tensorc                 C  s   |  |}| |}|| S )N)rF   r*   )r2   rK   rescxr6   r6   r7   forward;  s   

zResidualUnit.forward)r	   r
   r>   r   r   r   Nr	   r	   TFN) r   r   r   r   r   r   r   r   r   r   r?   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r@   r!   r$   r%   r'   r(   )rK   rL   r'   rL   )r8   r9   r:   r;   r.   rO   r<   r6   r6   r4   r7   r=      s     NCr=   )
__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    