U Phm@sddlmZddlmZddlmZddlZddlmZddl mm Z ddl m Z ddlmZddlmZmZddlmZd d d d gZGd ddejZGdddejZGdddejZGdddejZGdd d ejZeZZZ dS)) annotations)Sequence)OptionalN) Convolution)UpSample)ConvPool)ensure_tuple_repBasicUnetPlusPlusBasicunetPlusPlusbasicunetplusplusBasicUNetPlusPlusc@seZdZdZddZdS) MCDropout3du>MC Dropout:无论 model.train()/eval() 都启用随机失活cCstj||jd|jdS)NT)ptraininginplace)F dropout3drr)selfinputr[/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/basic_unetplusplus.pyforward%szMCDropout3d.forwardN)__name__ __module__ __qualname____doc__rrrrrr"src sJeZdZdZddddddddd d d d fd d Zd d dddZZS)TwoConvzResidual two-conv block (ResUNet++ style). If input/output channels differ, applies a 1x1 projection on the skip path. r r r"r"int str | tuplebool float | tupleSequence[int] | int) spatial_dimsin_chnsout_chnsactnormbiasdropout kernel_sizepaddingstridec sltt||||||||| d |_t||||||||| d |_d|_||krhtd|f||dd|_dS)N)r+r,r.r-r/r0convr"r/)super__init__rconv_0conv_1projr) rr(r)r*r+r,r-r.r/r0r1 __class__rrr5/s4   zTwoConv.__init__ torch.Tensor)xreturncCs8|}||}||}|jdk r,||}||}|S)N)r6r7r8)rr<identityoutrrrrYs    zTwoConv.forward)rrr!r!rrrrr5r __classcell__rrr9rr)s &*rc s8eZdZdZd dddddddd d d fd d ZZS)Downz-maxpooling downsampling and two convolutions.rrr!r#r$r%r&r') r(r)r*r+r,r-r.r/r0c sRttd|fdd} t||||||||| d } |d| |d| dS)a 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. MAXrErEr3r/r0 max_poolingconvsN)r4r5rr add_module) rr(r)r*r+r,r-r.r/r0rGrHr9rrr5fs  z Down.__init__)rrr!)rrrrr5rArrr9rrBcs  rBcsVeZdZdZdd d d d d d d d d dd dd d dddfdd ZdddddZZS)UpCatzHupsampling, concatenation with the encoder feature map, two convolutionsrdeconvdefaultlinearTrr!r#r$r%r&strznn.Module | str | Nonez bool | Noner')r(r)cat_chnsr*r+r,r-r.upsamplepre_conv interp_mode align_cornershalvesis_padr/r0c 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. nontrainableNrErD)moderQrRrSr/rF)r4r5rrPrrHrU)rr(r)rOr*r+r,r-r.rPrQrRrSrTrUr/r0up_chnsr9rrr5s6*   zUpCat.__init__r;zOptional[torch.Tensor])r<x_ecCs||}|dk rxtj|tjrxt|jd}dg|d}t|D]`}|j|d }|j|d }||krJ||} | d} | | } | ||d<| ||dd<qJt|rtj j j ||dd}t dg| } t|D]n}|j|d }|j|d }||kr||} | d}| |}|}||}| |d}t ||| |<q|t| }|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. NrErr" replicate)rWdim)rPtorchjit isinstanceTensorlenshaperangeanynn functionalpadslicer\tuplerHcat)rr<rYx_0 dimensionsZpad_listiZsize_x0Zsize_xediffpad_left pad_rightslicescrop crop_left crop_rightstartendZ dim_indexrrrrs>      z UpCat.forward) rrKrLrMTTTrr!r@rrr9rrJs 2GrJc sneZdZddddddddd fd d difdd d d f dddddddddddd fdd ZddddZZS)r r r"rE)0@`rwF LeakyReLUg?T)negative_sloperinstanceaffinerrKr#z Sequence[int]r%r$r&rNfloat) r( in_channels out_channelsfeaturesdeep_supervisionr+r,r-r.rP dropout_pc st||_| |_t|d} td| dtt|| d| d|||| ddd t|| d| d|||| ddd d t|| d| d|||| ddd d |_ t ||| d|||| ddd |_ t || d| d |||| d dd |_ t || d | d |||| ddd |_t || d | d|||| ddd |_t || d| d|||| ddd |_t|| d | d| d|||| dddd dd |_t|| d | d | d |||| dddd dd |_t|| d| d | d |||| dddd dd |_t|| d| d| d|||| dddd dd |_t|| d | dd | d|||| dddd dd |_t|| d | d d | d |||| dddd dd |_t|| d| d d | d |||| dddd dd |_t|| d | dd| d|||| dddd dd |_t|| d | d d| d |||| dddd dd |_t|| d | dd| d|||| dddd dd |_|jrd|jdkrdt|jnt|_td|f| d|d d|_ td|f| d|d d|_!td|f| d|d d|_"td|f| d|d d|_#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)r+r,r-r.r/r0rr")r+r,r-r.r/r0dilationrErF)r r )r"r"rE)r r )r"r"r )r r )r"r"rrVrLT)rPrQrTr/r0rr2r3N)$r4r5rrr printre Sequentialraspprconv_0_0rBconv_1_0conv_2_0conv_3_0conv_4_0rJ 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_4rIdentitymc_dropout_outrfinal_conv_0_1final_conv_0_2final_conv_0_3final_conv_0_4) rr(rrrrr+r,r-r.rPrfear9rrr5 s"A    &               zBasicUNetPlusPlus.__init__r;)r<cCsl||}||}|||}||}|||}||tj||gdd}||}| |}| ||} | | tj||gdd} | | tj|||gdd} t | j||} || |} || tj|| gdd}||tj||| gdd}||tj|||| gdd}||}||}|| }||}||}|jrb||||g}n|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"r[)rrrrrrr]rjrrrrrrrbrrrrrrrrrrr)rr<Zx_0_0Zx_1_0Zx_0_1Zx_2_0Zx_1_1Zx_0_2Zx_3_0Zx_2_1Zx_1_2Zx_0_3Zx_4_0Zx_3_1Zx_2_2Zx_1_3Zx_0_4Z output_0_1Z output_0_2Z output_0_3Z output_0_4outputrrrr[s4                zBasicUNetPlusPlus.forward)rrrr5rrArrr9rr s  (T)! __future__rcollections.abcrtypingrr]torch.nnretorch.nn.functionalrfr"monai.networks.blocks.convolutionsrmonai.networks.blocks.upsamplermonai.networks.layers.factoriesrrmonai.utils.miscr __all__ Dropout3drModulerrrBrJr r r r rrrr s.       :,y