o , i*1@stddlmZddlmZddlmZddlZddlmZddl m Z m Z ddl m Z mZdgZGdddejZdS) ) annotations)Sequence)AnyN) Convolution ResidualUnit)ActNorm AutoEncoderc szeZdZdZddddddejejdddf d8fd"d# Zd9d%d&Z d:d(d)Z d9d*d+Z d;d.d/Z dr a Simple definition of an autoencoder and base class for the architecture implementing :py:class:`monai.networks.nets.VarAutoEncoder`. The network is composed of an encode sequence of blocks, followed by an intermediary sequence of blocks, and finally a decode sequence of blocks. The encode and decode blocks are default :py:class:`monai.networks.blocks.Convolution` instances with the encode blocks having the given stride and the decode blocks having transpose convolutions with the same stride. If `num_res_units` is given residual blocks are used instead. By default the intermediary sequence is empty but if `inter_channels` is given to specify the output channels of blocks then this will be become a sequence of Convolution blocks or of residual blocks if `num_inter_units` is given. The optional parameter `inter_dilations` can be used to specify the dilation values of the convolutions in these blocks, this allows a network to use dilated kernels in this middle section. Since the intermediary section isn't meant to change the size of the output the strides for all these kernels is 1. Args: spatial_dims: number of spatial dimensions. in_channels: number of input channels. out_channels: number of output channels. channels: sequence of channels. Top block first. The length of `channels` should be no less than 2. strides: sequence of convolution strides. The length of `stride` should equal to `len(channels) - 1`. kernel_size: convolution kernel size, the value(s) should be odd. If sequence, its length should equal to dimensions. Defaults to 3. up_kernel_size: upsampling convolution kernel size, the value(s) should be odd. If sequence, its length should equal to dimensions. Defaults to 3. num_res_units: number of residual units. Defaults to 0. inter_channels: sequence of channels defining the blocks in the intermediate layer between encode and decode. inter_dilations: defines the dilation value for each block of the intermediate layer. Defaults to 1. num_inter_units: number of residual units for each block of the intermediate layer. Defaults to 0. 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. bias: whether to have a bias term in convolution blocks. Defaults to True. According to `Performance Tuning Guide `_, if a conv layer is directly followed by a batch norm layer, bias should be False. padding: controls the amount of implicit zero-paddings on both sides for padding number of points for each dimension in convolution blocks. Defaults to None. Examples:: from monai.networks.nets import AutoEncoder # 3 layers each down/up sampling their inputs by a factor 2 with no intermediate layer net = AutoEncoder( spatial_dims=2, in_channels=1, out_channels=1, channels=(2, 4, 8), strides=(2, 2, 2) ) # 1 layer downsampling by 2, followed by a sequence of residual units with 2 convolutions defined by # progressively increasing dilations, then final upsample layer net = AutoEncoder( spatial_dims=2, in_channels=1, out_channels=1, channels=(4,), strides=(2,), inter_channels=(8, 8, 8), inter_dilations=(1, 2, 4), num_inter_units=2 ) rNT spatial_dimsint in_channels out_channelschannels Sequence[int]strides kernel_sizeSequence[int] | intup_kernel_size num_res_unitsinter_channels list | Noneinter_dilationsnum_inter_unitsacttuple | str | Nonenorm tuple | strdropouttuple | str | float | NonebiasboolpaddingSequence[int] | int | NonereturnNonecs t||_||_||_t||_t||_||_||_ ||_ | |_ | |_ ||_ ||_||_| |_| dur9| ng|_t| pFdgt|j|_t|t|krUtd||_t|ddd|g}||j||\|_|_||j| \|_|_||j||dddpdg\|_}dS)Nz;Autoencoder expects matching number of channels and strides)super__init__ dimensionsrrlistrrrrrrrrr!r#rrlenr ValueErrorZencoded_channels_get_encode_moduleencode_get_intermediate_module intermediate_get_decode_moduledecode)selfr rrrrrrrrrrrrrr!r#Zdecode_channel_list_ __class__a/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/autoencoder.pyr+\s0   *zAutoEncoder.__init__tuple[nn.Sequential, int]c CsTt}|}tt||D]\}\}}||||d} |d|| |}q ||fS)z} Returns the encode part of the network by building up a sequence of layers returned by `_get_encode_layer`. Fz encode_%i)nn Sequential enumeratezip_get_encode_layer add_module) r6rrrr1layer_channelsicslayerr:r:r;r0szAutoEncoder._get_encode_moduletuple[nn.Module, int]c Cst}|}|jr[t}tt|j|jD]D\}\}}|jdkr:t|j ||d|j |j|j |j |j ||j|jd }nt|j ||d|j |j |j |j ||j|jd }|d|||}q||fS)z Returns the intermediate block of the network which accepts input from the encoder and whose output goes to the decoder. rr') r rrrrsubunitsrrrdilationr!r#) r rrrrrrrrJr!r#zinter_%i)r=Identityrr>r?r@rrrr,rrrrr!r#rrB) r6rrr3rCrDdcdiunitr:r:r;r2sH  z$AutoEncoder._get_intermediate_modulec Cs`t}|}tt||D]\}\}}|||||t|dk} |d|| |}q ||fS)z} Returns the decode part of the network by building up a sequence of layers returned by `_get_decode_layer`. r'z decode_%i)r=r>r?r@_get_decode_layerr.rB) r6rrrr5rCrDrErFrGr:r:r;r4szAutoEncoder._get_decode_moduleis_last nn.ModulecCsn|jdkrt|j||||j|j|j|j|j|j|j|d }|St |j||||j|j|j|j|j|j|d }|S)zL Returns a single layer of the encoder part of the network. r r rrrrrIrrrr!r#last_conv_only) r rrrrrrrr!r# conv_only) rrr,rrrrr!r#r)r6rrrrPmodr:r:r;rAs<  zAutoEncoder._get_encode_layer nn.SequentialcCst}t|j||||j|j|j|j|j|j |o|j dkdd }| d||j dkrHt |j||d|j d|j|j|j|j|j |d }| d||S)zL Returns a single layer of the decoder part of the network. rT) r rrrrrrrr!r#rT is_transposedconvr'rRZresunit)r=r>rr,rrrrr!r#rrBrr)r6rrrrPr5rXrur:r:r;rOsB    zAutoEncoder._get_decode_layerx torch.TensorrcCs"||}||}||}|S)N)r1r3r5)r6rZr:r:r;forward.s   zAutoEncoder.forward)"r r rr rr rrrrrrrrrr rrrrrr rrrrrr r!r"r#r$r%r&)rr rrrrr%r<)rr rr r%rH) rr rr rr rPr"r%rQ) rr rr rr rPr"r%rV)rZr[r%r)__name__ __module__ __qualname____doc__rPRELUrINSTANCEr+r0r2r4rArOr\ __classcell__r:r:r8r;r s(H 0  3  $+) __future__rcollections.abcrtypingrtorchtorch.nnr=Zmonai.networks.blocksrrmonai.networks.layers.factoriesrr__all__Moduler r:r:r:r;s