U PhD@sddlmZddlmZddlmZddlZddlmZddl mm Z ddl m Z ddlmZmZddlmZmZddlmZd d d d gZGd ddejZGdddejZGdddejZGdddejZGdddejZeZZ Z!dS)) annotations)Sequence)OptionalN)pad) ConvolutionUpSample)ConvPool)ensure_tuple_repBasicUnetPlusPlusBasicunetPlusPlusbasicunetplusplusBasicUNetPlusPlusKernelModifiedc@seZdZdZddZdS) MCDropout3du>MC Dropout:无论 model.train()/eval() 都启用随机失活cCstj||jd|jdS)NT)ptraininginplace)F dropout3drr)selfxrb/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/basic_unetplusplus_origin.pyforward&szMCDropout3d.forwardN)__name__ __module__ __qualname____doc__rrrrrr#src s:eZdZdZdddddddd d d d d fd d ZZS)TwoConvztwo convolutions.r!r!r#r!r#r#r#int str | tuplebool float | tupleOptional[tuple]) spatial_dimsin_chnsout_chnsactnormbiasdropout kernel_sizepaddingstridec sZtt||||||||| d } t||||||||| d } |d| |d| dS) Args: spatial_dims: number of spatial dimensions. in_chns: number of input channels. out_chns: number of output channels. act: activation type and arguments. norm: feature normalization type and arguments. bias: whether to have a bias term in convolution blocks. dropout: dropout ratio. Defaults to no dropout. )r-r.r0r/r1r2conv_0conv_1N)super__init__r add_module) rr*r+r,r-r.r/r0r1r2r3r5r6 __class__rrr8-s2   zTwoConv.__init__)rr r"r$rrrrr8 __classcell__rrr:rr*s  rc s8eZdZdZd dddddddd d d fd d ZZS)Downz-maxpooling downsampling and two convolutions.rr r"r%r&r'r(r)) r*r+r,r-r.r/r0r1r2c sRttd|fdd} t||||||||| d } |d| |d| dS)r4MAXrAr#r1r1r2 max_poolingconvsN)r7r8r rr9) rr*r+r,r-r.r/r0r1r2rDrEr:rrr8ds  z Down.__init__)rr r"r<rrr:rr>as  r>csVeZdZdZdd d d d d d d d d dd dd d dddfdd ZdddddZZS)UpCatzHupsampling, concatenation with the encoder feature map, two convolutionsrdeconvdefaultlinearTr r"r%r&r'r(strznn.Module | str | Nonez bool | Noner))r*r+cat_chnsr,r-r.r/r0upsamplepre_conv interp_mode align_cornershalvesis_padr1r2c svt| dkr | dkr |}n| r,|dn|}t|||d| | | | |d |_t||||||||||d |_||_dS)a6 Args: spatial_dims: number of spatial dimensions. in_chns: number of input channels to be upsampled. cat_chns: number of channels from the encoder. out_chns: number of output channels. act: activation type and arguments. norm: feature normalization type and arguments. bias: whether to have a bias term in convolution blocks. dropout: dropout ratio. Defaults to no dropout. upsample: upsampling mode, available options are ``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``. pre_conv: a conv block applied before upsampling. Only used in the "nontrainable" or "pixelshuffle" mode. interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``} Only used in the "nontrainable" mode. align_corners: set the align_corners parameter for upsample. Defaults to True. Only used in the "nontrainable" mode. halves: whether to halve the number of channels during upsampling. This parameter does not work on ``nontrainable`` mode if ``pre_conv`` is `None`. is_pad: whether to pad upsampling features to fit features from encoder. Defaults to True. nontrainableNrAr@)moderMrNrOr1rC)r7r8rrLrrErQ)rr*r+rKr,r-r.r/r0rLrMrNrOrPrQr1r2up_chnsr:rrr8s6*   zUpCat.__init__ torch.TensorzOptional[torch.Tensor])rx_ecCs||}|dk rtj|tjr|jrt|jd}dg|d}t|D]4}|j| d|j| dkrLd||dd<qLtj j ||d}| tj ||gdd}n | |}|S)z Args: x: features to be upsampled. x_e: optional features from the encoder, if None, this branch is not in use. NrArr# replicatedim)rLtorchjit isinstanceTensorrQlenshaperangenn functionalrrEcat)rrrVx_0 dimensionsspirrrrs    z UpCat.forward) rrGrHrITTTr r")rrrrr8rr=rrr:rrFs 2GrFc sneZdZddddddddd fd d difdd d d f dddddddddddd fdd ZddddZZS)BasicUNetPlusPlusr!r#rA) ri@riF LeakyReLUg?T)negative_sloperinstanceaffinerrGr%z Sequence[int]r'r&r(rJfloat) r* in_channels out_channelsfeaturesdeep_supervisionr-r.r/r0rL dropout_pc sBt||_| |_t|d} td| dt||| d|||| dd|_t|| d| d|||| dd|_ t|| d| d|||| dd|_ t|| d| d |||| dd|_ t|| d | d |||| dd|_ t || d| d| d|||| | d dd |_t || d| d| d|||| | d dd |_t || d | d| d|||| | d dd |_t || d | d | d |||| | d dd |_t || d| dd| d|||| | d dd |_t || d| dd| d|||| | d dd |_t || d | dd| d|||| | d dd |_t || d| dd | d|||| | d dd |_t || d| dd | d|||| | d dd |_t || d| dd | d |||| | d dd |_|jr|jdkrt|jnt|_td|f| d|dd|_td|f| d|dd|_td|f| d|dd|_td|f| d |dd|_ dS)a A UNet++ implementation with 1D/2D/3D supports. Based on: Zhou et al. "UNet++: A Nested U-Net Architecture for Medical Image Segmentation". 4th Deep Learning in Medical Image Analysis (DLMIA) Workshop, DOI: https://doi.org/10.48550/arXiv.1807.10165 Args: spatial_dims: number of spatial dimensions. Defaults to 3 for spatial 3D inputs. in_channels: number of input channels. Defaults to 1. out_channels: number of output channels. Defaults to 2. features: six integers as numbers of features. Defaults to ``(32, 32, 64, 128, 256, 32)``, - the first five values correspond to the five-level encoder feature sizes. - the last value corresponds to the feature size after the last upsampling. deep_supervision: whether to prune the network at inference time. Defaults to False. If true, returns a list, whose elements correspond to outputs at different nodes. act: activation type and arguments. Defaults to LeakyReLU. norm: feature normalization type and arguments. Defaults to instance norm. 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. dropout: dropout ratio. Defaults to no dropout. upsample: upsampling mode, available options are ``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``. Examples:: # for spatial 2D >>> net = BasicUNetPlusPlus(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128)) # for spatial 2D, with deep supervision enabled >>> net = BasicUNetPlusPlus(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128), deep_supervision=True) # for spatial 2D, with group norm >>> net = BasicUNetPlusPlus(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128), norm=("group", {"num_groups": 4})) # for spatial 3D >>> net = BasicUNetPlusPlus(spatial_dims=3, features=(32, 32, 64, 128, 256, 32)) See Also - :py:class:`monai.networks.nets.BasicUNet` - :py:class:`monai.networks.nets.DynUNet` - :py:class:`monai.networks.nets.UNet` zBasicUNetPlusPlus features: .r)r!r!rBr#rAr!F)rPr1convN)!r7r8rurvr printrconv_0_0r>conv_1_0conv_2_0conv_3_0conv_4_0rF upcat_0_1 upcat_1_1 upcat_2_1 upcat_3_1 upcat_0_2 upcat_1_2 upcat_2_2 upcat_0_3 upcat_1_3 upcat_0_4rraIdentitymc_dropout_outrfinal_conv_0_1final_conv_0_2final_conv_0_3final_conv_0_4) rr*rrrsrtrur-r.r/r0rLrvfear:rrr8sA                       zBasicUNetPlusPlus.__init__rU)rcCs$||}||}|||}||}|||}||tj||gdd}||}| ||} | | tj||gdd} | | tj|||gdd} | |} | | |} || tj|| gdd}||tj||| gdd}||tj|||| gdd}||}||}|g}|S)a Args: x: input should have spatially N dimensions ``(Batch, in_channels, dim_0[, dim_1, ..., dim_N-1])``, N is defined by `dimensions`. It is recommended to have ``dim_n % 16 == 0`` to ensure all maxpooling inputs have even edge lengths. Returns: A torch Tensor of "raw" predictions in shape ``(Batch, out_channels, dim_0[, dim_1, ..., dim_N-1])``. r#rX)r~rrrrrrZrcrrrrrrrrrrr)rrx_0_0x_1_0x_0_1x_2_0x_1_1x_0_2x_3_0x_2_1x_1_2x_0_3x_4_0x_3_1x_2_2x_1_3x_0_4 output_0_4outputrrrrs&           zBasicUNetPlusPlus.forward)rrrr8rr=rrr:rrhs  (rh)" __future__rcollections.abcrtypingrrZtorch.nnratorch.nn.functionalrbrnumpyrmonai.networks.blocksrrmonai.networks.layers.factoriesrr monai.utilsr __all__ Dropout3dr Sequentialrr>ModulerFrhr r r rrrr s,      7,eB