U
    PhR                     @  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
mZ d dlmZmZ d dlmZmZ d dlmZ d dlmZmZ d	gZG d
d	 d	ejZdS )    )annotations)SequenceN)ConvolutionResidualUnit)calculate_out_shapesame_padding)ActNorm)Reshape)ensure_tupleensure_tuple_rep	Regressorc                      s|   e Zd ZdZddejejddfddddddd	d
dd	 fddZdddd
ddddZ	ddddZ
dddddZ  ZS )r   a  
    This defines a network for relating large-sized input tensors to small output tensors, ie. regressing large
    values to a prediction. An output of a single dimension can be used as value regression or multi-label
    classification prediction, an output of a single value can be used as a discriminator or critic prediction.

    The network is constructed as a sequence of layers, either :py:class:`monai.networks.blocks.Convolution` or
    :py:class:`monai.networks.blocks.ResidualUnit`, with a final fully-connected layer resizing the output from the
    blocks to the final size. Each block is defined with a stride value typically used to downsample the input using
    strided convolutions. In this way each block progressively condenses information from the input into a deep
    representation the final fully-connected layer relates to a final result.

    Args:
        in_shape: tuple of integers stating the dimension of the input tensor (minus batch dimension)
        out_shape: tuple of integers stating the dimension of the final output tensor (minus batch dimension)
        channels: tuple of integers stating the output channels of each convolutional layer
        strides: tuple of integers stating the stride (downscale factor) of each convolutional layer
        kernel_size: integer or tuple of integers stating size of convolutional kernels
        num_res_units: integer stating number of convolutions in residual units, 0 means no residual units
        act: name or type defining activation layers
        norm: name or type defining normalization layers
        dropout: optional float value in range [0, 1] stating dropout probability for layers, None for no dropout
        bias: boolean stating if convolution layers should have a bias component

    Examples::

        # infers a 2-value result (eg. a 2D cartesian coordinate) from a 64x64 image
        net = Regressor((1, 64, 64), (2,), (2, 4, 8), (2, 2, 2))

          NTzSequence[int]zSequence[int] | intintzfloat | NoneboolNone)	in_shape	out_shapechannelsstrideskernel_sizenum_res_unitsdropoutbiasreturnc              	     s"  t    t|^| _| _t| j| _t|| _t|| _t|| _	t
|| j| _|| _|| _|| _|	| _|
| _t | _| j}t|}tj| jtd| _t| j	 | _tt| j| jD ]P\}\}}| ||||t|d k}|}| jd| | t| j|||| _q|  |f| j | _!d S )N)dtype   zlayer_%i)"super__init__r   in_channelsr   len
dimensionsr   r   r   r   r   r   actnormr   r   nn
Sequentialnetr   npasarrayr   
final_sizer
   reshape	enumeratezip
_get_layer
add_moduler   _get_final_layerfinal)selfr   r   r   r   r   r   r#   r$   r   r   echannelpaddingicslayer	__class__ R/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/regressor.pyr   <   s.    




zRegressor.__init__zResidualUnit | Convolution)r    out_channelsr   is_lastr   c                 C  sd   | j dkr8t| j || j|||| j| j| j| j| jd}n(t|| j|||| j| j| j| j| jd
}|S )a\  
        Returns a layer accepting inputs with `in_channels` number of channels and producing outputs of `out_channels`
        number of channels. The `strides` indicates downsampling factor, ie. convolutional stride. If `is_last`
        is True this is the final layer and is not expected to include activation and normalization layers.
        r   )subunitslast_conv_onlyspatial_dimsr    r=   r   r   r#   r$   r   r   )
	conv_onlyrA   r    r=   r   r   r#   r$   r   r   )	r   r   r"   r   r#   r$   r   r   r   )r2   r    r=   r   r>   r8   r;   r;   r<   r.   h   s6    
zRegressor._get_layer)r   c                 C  s2   t tt|tt| j}t t  |S N)r%   Linearr   r(   prodr   r&   Flatten)r2   r   linearr;   r;   r<   r0      s    "zRegressor._get_final_layerztorch.Tensor)xr   c                 C  s"   |  |}| |}| |}|S rC   )r'   r1   r+   )r2   rH   r;   r;   r<   forward   s    


zRegressor.forward)__name__
__module____qualname____doc__r   PRELUr	   INSTANCEr   r.   r0   rI   __classcell__r;   r;   r9   r<   r      s   $$,))
__future__r   collections.abcr   numpyr(   torchtorch.nnr%   monai.networks.blocksr   r   monai.networks.layers.convutilsr   r   monai.networks.layers.factoriesr   r	   "monai.networks.layers.simplelayersr
   monai.utilsr   r   __all__Moduler   r;   r;   r;   r<   <module>   s   