o & i7+@sddlmZddlmZddlZddlZddlmZddl m Z ddl m Z m Z ddlmZmZGdddejZGd d d ejZGd d d ejZGd ddejZdde je jddddfd,d#d$Zd-d'd(Zd.d*d+ZdS)/) annotations)SequenceN) Convolution)ActNorm) get_act_layerget_norm_layerc:eZdZdZddddfdfdfdd ZddZZS) UnetResBlocka A skip-connection based module that can be used for DynUNet, based on: `Automated Design of Deep Learning Methods for Biomedical Image Segmentation `_. `nnU-Net: Self-adapting Framework for U-Net-Based Medical Image Segmentation `_. Args: spatial_dims: number of spatial dimensions. in_channels: number of input channels. out_channels: number of output channels. kernel_size: convolution kernel size. stride: convolution stride. norm_name: feature normalization type and arguments. act_name: activation layer type and arguments. dropout: dropout probability. leakyreluT{Gz?inplacenegative_slopeN spatial_dimsint in_channels out_channels kernel_sizeSequence[int] | intstride norm_name tuple | stract_namedropouttuple | str | float | Nonec stt||||||dddd |_t||||d|dddd |_t|d|_t|||d|_t|||d|_ ||k|_ t |} t | dksKd|_ |j rft|||d||dddd |_t|||d|_dSdS)NFrrractnorm conv_onlynamer"rchannelsT)super__init__get_conv_layerconv1conv2rlrelurnorm1norm2 downsamplenp atleast_1dallconv3norm3) selfrrrrrrrr stride_np __class__e/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/blocks/dynunet_block.pyr&+sX      zUnetResBlock.__init__cCst|}||}||}||}||}||}t|dr%||}t|dr/||}||7}||}|S)Nr1r2)r(r+r*r)r,hasattrr1r2)r3inpresidualoutr7r7r8forwardbs          zUnetResBlock.forwardrrrrrrrrrrrrrrrr__name__ __module__ __qualname____doc__r&r= __classcell__r7r7r5r8r s  7r cr )UnetBasicBlocka A CNN module that can be used for DynUNet, based on: `Automated Design of Deep Learning Methods for Biomedical Image Segmentation `_. `nnU-Net: Self-adapting Framework for U-Net-Based Medical Image Segmentation `_. Args: spatial_dims: number of spatial dimensions. in_channels: number of input channels. out_channels: number of output channels. kernel_size: convolution kernel size. stride: convolution stride. norm_name: feature normalization type and arguments. act_name: activation layer type and arguments. dropout: dropout probability. r Tr r Nrrrrrrrrrrrrc srtt||||||dddd |_t||||d|dddd |_t|d|_t|||d|_t|||d|_ dS)NFrr r!r#) r%r&r'r(r)rr*rr+r,) r3rrrrrrrrr5r7r8r&s4   zUnetBasicBlock.__init__cCs@||}||}||}||}||}||}|SN)r(r+r*r)r,)r3r:r<r7r7r8r=s      zUnetBasicBlock.forwardr>r?r7r7r5r8rErs  &rEcs<eZdZdZddddfddfdfdd ZddZZS) UnetUpBlockaJ An upsampling module that can be used for DynUNet, based on: `Automated Design of Deep Learning Methods for Biomedical Image Segmentation `_. `nnU-Net: Self-adapting Framework for U-Net-Based Medical Image Segmentation `_. Args: spatial_dims: number of spatial dimensions. in_channels: number of input channels. out_channels: number of output channels. kernel_size: convolution kernel size. stride: convolution stride. upsample_kernel_size: convolution kernel size for transposed convolution layers. norm_name: feature normalization type and arguments. act_name: activation layer type and arguments. dropout: dropout probability. trans_bias: transposed convolution bias. r Tr r NFrrrrrrrupsample_kernel_sizerrrrr trans_biasboolc sPt|} t||||| | | ddddd |_t|||||d| ||d|_dS)NFT)rrrbiasrrr is_transposedr )rrrrr)r%r&r' transp_convrE conv_block) r3rrrrrrHrrrrIZupsample_strider5r7r8r&s2   zUnetUpBlock.__init__cCs*||}tj||fdd}||}|S)Nr )dim)rMtorchcatrN)r3r:skipr<r7r7r8r=s  zUnetUpBlock.forward)rrrrrrrrrrrHrrrrrrrrIrJr?r7r7r5r8rGs 'rGcs*eZdZ d d fdd Zd d ZZS) UnetOutBlockNrrrrrrc s,tt|||dd|ddddd |_dS)Nr TF)rrrrKrrr)r%r&r'conv)r3rrrrr5r7r8r&s  zUnetOutBlock.__init__cCs ||SrF)rT)r3r:r7r7r8r= s zUnetOutBlock.forwardrF)rrrrrrrr)r@rArBr&r=rDr7r7r5r8rSsrSr FTrrrrrrrrtuple | str | NonerrrrKrJrrLc Cs@t||} d} | rt||| } t|||||||||| | | | d S)N) stridesrrrrrKrrLpaddingoutput_padding) get_paddingget_output_paddingr) rrrrrrrrrKrrLrXrYr7r7r8r's&  r'returntuple[int, ...] | intcCsdt|}t|}||dd}t|dkrtdtdd|D}t|dkr.|S|dS)Nr rzRpadding value should not be negative, please change the kernel size and/or stride.cs|]}t|VqdSrFr.0pr7r7r8 6zget_padding..r.r/minAssertionErrortuplelen)rrkernel_size_npr4 padding_nprXr7r7r8rZ0s  rZrXcCsnt|}t|}t|}d|||}t|dkr"tdtdd|D}t|dkr3|S|dS)Nr^rzVout_padding value should not be negative, please change the kernel size and/or stride.csr_rFr`rar7r7r8rdErez%get_output_padding..r rf)rrrXrkr4rlout_padding_np out_paddingr7r7r8r[;s   r[)rrrrrrrrrrrrVrrVrrrKrJrrJrLrJ)rrrrr\r])rrrrrXrr\r]) __future__rcollections.abcrnumpyr.rPtorch.nnnn"monai.networks.blocks.convolutionsrmonai.networks.layers.factoriesrrmonai.networks.layers.utilsrrModuler rErGrSPRELUINSTANCEr'rZr[r7r7r7r8s.   YBC "