U Ph@sddlmZddlmZddlZddlZddlmZddlm Z ddl m Z m Z ddlmZmZddlmZdgZGd ddeZdS) ) annotations)SequenceN) functional)calculate_out_shape same_padding)ActNorm) AutoEncoderVarAutoEncodercseZdZdZddddddejejdddf ddddddd d dd d dd d d ddddfdd ZdddddZ d!ddddddZ ddddddZ ddddd Z Z S)"r a Variational Autoencoder based on the paper - https://arxiv.org/abs/1312.6114 Args: spatial_dims: number of spatial dimensions. in_shape: shape of input data starting with channel dimension. out_channels: number of output channels. latent_size: size of the latent variable. 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. use_sigmoid: whether to use the sigmoid function on final output. Defaults to True. Examples:: from monai.networks.nets import VarAutoEncoder # 3 layer network accepting images with dimensions (1, 32, 32) and using a latent vector with 2 values model = VarAutoEncoder( spatial_dims=2, in_shape=(32, 32), # image spatial shape out_channels=1, latent_size=2, channels=(16, 32, 64), strides=(1, 2, 2), ) see also: - Variational autoencoder network with MedNIST Dataset https://github.com/Project-MONAI/tutorials/blob/master/modules/varautoencoder_mednist.ipynb rNTintz Sequence[int]zSequence[int] | intz list | Noneztuple | str | Nonez tuple | strztuple | str | float | NoneboolNone) spatial_dimsin_shape out_channels latent_sizechannelsstrides kernel_sizeup_kernel_size num_res_unitsinter_channelsinter_dilationsnum_inter_unitsactnormdropoutbias use_sigmoidreturncs|^|_|_||_||_tj|jtd|_t ||j|||||| | | | | |||t |j }|D]}t |j|j |||_qbtt |j|j}t||j|_t||j|_t|j||_dS)N)dtype) in_channelsrr rnpasarrayr final_sizesuper__init__rrrprodencoded_channelsnnLinearmulogvardecodeL)selfrrrrrrrrrrrrrrrrr paddingsZ linear_size __class__W/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/varautoencoder.pyr(Js8  zVarAutoEncoder.__init__z torch.Tensorz!tuple[torch.Tensor, torch.Tensor])xr!cCsB||}||}||jdd}||}||}||fSNr)encode intermediateviewshaper-r.)r0r7r-r.r5r5r6encode_forwards     zVarAutoEncoder.encode_forward)zr r!cCsLt||}|j|jd|jdf|j}||}|rHt |}|Sr8) Frelur/r<r=rr&decodetorchsigmoid)r0r?r r7r5r5r6decode_forwards    zVarAutoEncoder.decode_forward)r-r.r!cCs.td|}|jr$t||}||S)Ng?)rCexptraining randn_likemuladd_)r0r-r.stdr5r5r6reparameterizeszVarAutoEncoder.reparameterizez=tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]cCs0||\}}|||}|||j|||fS)N)r>rLrEr )r0r7r-r.r?r5r5r6forwards zVarAutoEncoder.forward)T)__name__ __module__ __qualname____doc__rPRELUrINSTANCEr(r>rErLrM __classcell__r5r5r3r6r s"566) __future__rcollections.abcrnumpyr$rCtorch.nnr+rr@monai.networks.layers.convutilsrrmonai.networks.layers.factoriesrrmonai.networks.netsr __all__r r5r5r5r6 s