U Ph*@sddlmZddlmZddlmZddlZddlmZddl m Z m Z ddl m Z mZddlmZdd d d gZGd d d ejZGdddejZGdddejZGdd d ejZeZZZdS)) annotations)Sequence)OptionalN) ConvolutionUpSample)ConvPool)ensure_tuple_rep BasicUnet Basicunet basicunet BasicUNetc s4eZdZdZd ddddddddfdd ZZS) TwoConvztwo convolutions.int str | tuplebool float | tuple spatial_dimsin_chnsout_chnsactnormbiasdropoutc sVtt|||||||dd}t|||||||dd} |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. )rrrrpaddingconv_0conv_1N)super__init__r add_module) selfrrrrrrrrr  __class__S/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/basic_unet.pyr"s  zTwoConv.__init__)r__name__ __module__ __qualname____doc__r" __classcell__r'r'r%r(rs rc s4eZdZdZd ddddddddfdd ZZS) Downz-maxpooling downsampling and two convolutions.rrrrrrc sLttd|fdd}t|||||||} |d||d| dS)rMAX kernel_size max_poolingconvsN)r!r"rrr#) r$rrrrrrrr4r5r%r'r(r"@s   z Down.__init__)rr)r'r'r%r(r/=s r/csReZdZdZdddddddd d d d d d d d dfdd ZdddddZZS)UpCatzHupsampling, concatenation with the encoder feature map, two convolutionsrdeconvdefaultlinearTrrrrstrznn.Module | str | Nonez bool | None)rrcat_chnsrrrrrupsamplepre_conv interp_mode align_cornershalvesis_padc snt| dkr | dkr |}n| r,|dn|}t|||d| | | | d|_t|||||||||_||_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. nontrainableNr1)moder=r>r?)r!r"rr<rr5rA)r$rrr;rrrrrr<r=r>r?r@rAZup_chnsr%r'r(r"_s (  zUpCat.__init__ torch.TensorzOptional[torch.Tensor])xx_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. Nr1rr replicate)dim)r<torchjit isinstanceTensorrAlenshaperangenn functionalpadr5cat)r$rErFx_0 dimensionsspir'r'r(forwards    z UpCat.forward)rr7r8r9TTT)r*r+r,r-r"rXr.r'r'r%r(r6\s .:r6c sfeZdZddddddddfd d difdd d f d d d ddddddd fdd ZddddZZS)r rr1) rZ@rZ LeakyReLUg?T)negative_slopeinplaceinstanceaffinerr7rz Sequence[int]rrrr:) r in_channels out_channelsfeaturesrrrrr<c srtt|d} td| dt|||d|||||_t|| d| d|||||_t|| d| d|||||_t|| d| d|||||_ t|| d| d|||||_ t || d| d| d||||| |_ t || d| d| d||||| |_ t || d| d| d||||| |_t || d| d| d ||||| d d |_td |f| d |dd |_dS)u A UNet implementation with 1D/2D/3D supports. Based on: Falk et al. "U-Net – Deep Learning for Cell Counting, Detection, and Morphometry". Nature Methods 16, 67–70 (2019), DOI: http://dx.doi.org/10.1038/s41592-018-0261-2 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. 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 = BasicUNet(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128)) # for spatial 2D, with group norm >>> net = BasicUNet(spatial_dims=2, features=(64, 128, 256, 512, 1024, 128), norm=("group", {"num_groups": 4})) # for spatial 3D >>> net = BasicUNet(spatial_dims=3, features=(32, 32, 64, 128, 256, 32)) See Also - :py:class:`monai.networks.nets.DynUNet` - :py:class:`monai.networks.nets.UNet` zBasicUNet features: .rrr1rYF)r@convr2N)r!r"r printrrr/down_1down_2down_3down_4r6upcat_4upcat_3upcat_2upcat_1r final_conv) r$rrcrdrerrrrr<Zfear%r'r(r"s9  &&&*zBasicUNet.__init__rD)rEc Csp||}||}||}||}||}|||}|||}|||} || |} | | } | S)a Args: x: input should have spatially N dimensions ``(Batch, in_channels, dim_0[, dim_1, ..., dim_N-1])``, N is defined by `spatial_dims`. 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])``. ) rrlrmrnrorprqrrrsrt) r$rEx0x1x2x3x4u4u3u2u1logitsr'r'r(rXs          zBasicUNet.forward)r*r+r,r"rXr.r'r'r%r(r s  $J) __future__rcollections.abcrtypingrrItorch.nnrPZmonai.networks.blocksrrmonai.networks.layers.factoriesrr monai.utilsr __all__ Sequentialrr/Moduler6r r r r r'r'r'r( s      "Vh