o - i@sddlmZddlmZddlZddlZddlmZddlm Z ddl m Z m Z ddlmZmZddlmZdgZGd ddeZdS) ) annotations)SequenceN) functional)calculate_out_shape same_padding)ActNorm) AutoEncoderVarAutoEncoderc sheZdZdZddddddejejdddf d3fd"d# Zd4d'd(Z d5d6d*d+Z d7d.d/Z d8d1d2Z Z S)9r 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 rNT spatial_dimsintin_shape Sequence[int] out_channels latent_sizechannelsstrides kernel_sizeSequence[int] | intup_kernel_size num_res_unitsinter_channels list | Noneinter_dilationsnum_inter_unitsacttuple | str | Nonenorm tuple | strdropouttuple | str | float | Nonebiasbool use_sigmoidreturnNonecs|^|_|_||_||_tj|jtd|_t ||j|||||| | | | | |||t |j }|D] }t |j|j |||_q1tt |j|j}t||j|_t||j|_t|j||_dS)N)dtype) in_channelsrr%rnpasarrayr final_sizesuper__init__rrrprodencoded_channelsnnLinearmulogvardecodeL)selfr rrrrrrrrrrrrrr!r#r%paddingsZ linear_size __class__d/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/varautoencoder.pyr.Js8  zVarAutoEncoder.__init__x torch.Tensor!tuple[torch.Tensor, torch.Tensor]cCsB||}||}||jdd}||}||}||fSNr)encode intermediateviewshaper3r4)r6r=r3r4r;r;r<encode_forwards    zVarAutoEncoder.encode_forwardzcCsNt||}|j|jd|jdg|jR}||}|r%t |}|Sr@) Frelur5rDrErr,decodetorchsigmoid)r6rGr%r=r;r;r<decode_forwards "  zVarAutoEncoder.decode_forwardr3r4cCs.td|}|jrt||}||S)Ng?)rKexptraining randn_likemuladd_)r6r3r4stdr;r;r<reparameterizes zVarAutoEncoder.reparameterize=tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]cCs0||\}}|||}|||j|||fS)N)rFrTrMr%)r6r=r3r4rGr;r;r<forwards zVarAutoEncoder.forward)$r rrrrrrrrrrrrrrrrrrrrrrrrrrr r!r"r#r$r%r$r&r')r=r>r&r?)T)rGr>r%r$r&r>)r3r>r4r>r&r>)r=r>r&rU)__name__ __module__ __qualname____doc__rPRELUrINSTANCEr.rFrMrTrV __classcell__r;r;r9r<r s$5 6  ) __future__rcollections.abcrnumpyr*rKtorch.nnr1rrHmonai.networks.layers.convutilsrrmonai.networks.layers.factoriesrrmonai.networks.netsr __all__r r;r;r;r<s