o - i=@sBddlmZddlZddlmZddlmZmZddlZddl m Z ddl m Z ddl mZmZmZddlmZmZddlmZgd ZGd d d e jZGd d d e jZGddde jZGddde jZd"ddZGdddeZGdddeZGdddeZ Gd d!d!eZ!eZ"eZ#Z$eZ%Z&e Z'Z(e!Z)Z*dS)#) annotationsN) OrderedDict)CallableSequence)load_state_dict_from_url)ConvDropoutPool) get_act_layerget_norm_layer)look_up_option)DenseNetDensenet DenseNet121 densenet121 Densenet121 DenseNet169 densenet169 Densenet169 DenseNet201 densenet201 Densenet201 DenseNet264 densenet264 Densenet264cs6eZdZdddifdfdfdd ZdddZZS) _DenseLayerreluinplaceTbatch spatial_dimsint in_channels growth_ratebn_size dropout_probfloatact str | tuplenormreturnNonec st||}ttj|f} ttj|f} t|_|j dt |||d|j dt |d|j d| ||ddd|j d t |||d|j d t |d|j d | ||d ddd |dkrr|j d| |dSdS)aH Args: spatial_dims: number of spatial dimensions of the input image. in_channels: number of the input channel. growth_rate: how many filters to add each layer (k in paper). bn_size: multiplicative factor for number of bottle neck layers. (i.e. bn_size * k features in the bottleneck layer) dropout_prob: dropout rate after each dense layer. act: activation type and arguments. Defaults to relu. norm: feature normalization type and arguments. Defaults to batch norm. norm1namerchannelsrelu1r-conv1F kernel_sizebiasnorm2relu2conv2)r4paddingr5rdropoutN) super__init__rCONVrDROPOUTnn Sequentiallayers add_moduler r ) selfrr!r"r#r$r&r( out_channels conv_typeZ dropout_type __class__^/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/densenet.pyr=.s  z_DenseLayer.__init__x torch.TensorcCs||}t||gdS)Nr2)rBtorchcat)rDrK new_featuresrIrIrJforwardVs z_DenseLayer.forward)rr r!r r"r r#r r$r%r&r'r(r'r)r*rKrLr)rL)__name__ __module__ __qualname__r=rP __classcell__rIrIrGrJr,s  (rcs,eZdZdddifdfdfdd ZZS) _DenseBlockrrTrrr rBr!r#r"r$r%r&r'r(r)r*c sNtt|D]} t|||||||d} ||7}|d| d| q dS)a{ Args: spatial_dims: number of spatial dimensions of the input image. layers: number of layers in the block. in_channels: number of the input channel. bn_size: multiplicative factor for number of bottle neck layers. (i.e. bn_size * k features in the bottleneck layer) growth_rate: how many filters to add each layer (k in paper). dropout_prob: dropout rate after each dense layer. act: activation type and arguments. Defaults to relu. norm: feature normalization type and arguments. Defaults to batch norm. )r&r(z denselayer%dr2N)r<r=rangerrC) rDrrBr!r#r"r$r&r(ilayerrGrIrJr=]s  z_DenseBlock.__init__)rr rBr r!r r#r r"r r$r%r&r'r(r'r)r*rRrSrTr=rUrIrIrGrJrV[s rVcs,eZdZdddifdfdfdd ZZS) _TransitionrrTrrr r!rEr&r'r(r)r*c s~tttj|f}ttj|f}|dt|||d|dt|d|d|||ddd|d |d d d d S) af Args: spatial_dims: number of spatial dimensions of the input image. in_channels: number of the input channel. out_channels: number of the output classes. act: activation type and arguments. Defaults to relu. norm: feature normalization type and arguments. Defaults to batch norm. r(r,rr0convr2Fr3pool)r4strideN) r<r=rr>r AVGrCr r )rDrr!rEr&r(rF pool_typerGrIrJr=}s z_Transition.__init__) rr r!r rEr r&r'r(r'r)r*rZrIrIrGrJr[{s r[csDeZdZdZdddddddifd d fd!fdd Zd"dd ZZS)#r a Densenet based on: `Densely Connected Convolutional Networks `_. Adapted from PyTorch Hub 2D version: https://pytorch.org/vision/stable/models.html#id16. This network is non-deterministic When `spatial_dims` is 3 and CUDA is enabled. Please check the link below for more details: https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html#torch.use_deterministic_algorithms Args: spatial_dims: number of spatial dimensions of the input image. in_channels: number of the input channel. out_channels: number of the output classes. init_features: number of filters in the first convolution layer. growth_rate: how many filters to add each layer (k in paper). block_config: how many layers in each pooling block. bn_size: multiplicative factor for number of bottle neck layers. (i.e. bn_size * k features in the bottleneck layer) act: activation type and arguments. Defaults to relu. norm: feature normalization type and arguments. Defaults to batch norm. dropout_prob: dropout rate after each dense layer. @  rrTrgrr r!rE init_featuresr" block_config Sequence[int]r#r&r'r(r$r%r)r*c  stttj|f} ttj|f} ttj|f} tt d| ||dddddfdt | ||dfd t |d fd | ddd d fg|_ |}t |D]R\}}t|||||| || d}|j d|d ||||7}|t|d kr|j dt | ||dqI|d}t||||| d}|j d|d ||}qItt dt |d fd| d fdtd fdt||fg|_|D]F}t|| rtjt|jqt|tjtjtjfrtjt|jd tjt|j dqt|tjrtjt|j dqdS)Nconv0r^r9F)r4r_r:r5norm0r,relu0r0pool0r2)r4r_r:)rrBr!r#r"r$r&r( denseblocknorm5)r!rEr&r( transitionrr]flattenoutr)!r<r=rr>r MAX ADAPTIVEAVGr@rArr r features enumeraterVrClenr[FlattenLinear class_layersmodules isinstanceinitkaiming_normal_rM as_tensorweight BatchNorm1d BatchNorm2d BatchNorm3d constant_r5)rDrr!rErjr"rkr#r&r(r$rFraZ avg_pool_typerX num_layersblockZ _out_channelstransmrGrIrJr=st         zDenseNet.__init__rKrLcCs||}||}|S)N)ryr~)rDrKrIrIrJrPs  zDenseNet.forward)rr r!r rEr rjr r"r rkrlr#r r&r'r(r'r$r%r)r*rQ)rRrSrT__doc__r=rPrUrIrIrGrJr s Or model nn.Modulearchstrprogressboolc sdddd}t||d}|durtdtd}t||dtD]#}||}|rH|d d |d |d }||<|=q%| fd d D | dS)z This function is used to load pretrained models. Adapted from PyTorch Hub 2D version: https://pytorch.org/vision/stable/models.html#id16. z!s,z$_load_state_dict..) r ValueErrorrecompilerlistkeysmatchgroupritemsupdateload_state_dict) rrr model_urls model_urlpatternkeyresnew_keyrIrrJ_load_state_dicts4   "   rc.eZdZdZ     ddfdd ZZS)rzFDenseNet121 with optional pretrained support when `spatial_dims` is 2.rbrcrdFTrr r!rErjr"rkrl pretrainedrrr)r*c Htjd||||||d| |r"|dkrtdt|d|dSdS)Nrr!rErjr"rkr^Parameter `spatial_dims` is > 2 ; currently PyTorch Hub does notprovide pretrained models for more than two spatial dimensions.rrIr<r=NotImplementedErrorr rDrr!rErjr"rkrrkwargsrGrIrJr=+"  zDenseNet121.__init__)rbrcrdFTrr r!r rEr rjr r"r rkrlrrrrr)r*rRrSrTrr=rUrIrIrGrJr(rcr)rzFDenseNet169 with optional pretrained support when `spatial_dims` is 2.rbrcrerfrcrcFTrr r!rErjr"rkrlrrrr)r*c r)Nrr^rrrIrrrGrIrJr=LrzDenseNet169.__init__)rbrcrFTrrrIrIrGrJrIrrcr)rzFDenseNet201 with optional pretrained support when `spatial_dims` is 2.rbrcrerf0rcFTrr r!rErjr"rkrlrrrr)r*c r)Nrr^rrrIrrrGrIrJr=mrzDenseNet201.__init__)rbrcrFTrrrIrIrGrJrjrrcs.eZdZdZ     ddfdd ZZS)rrbrcrerfrbrFTrr r!rErjr"rkrlrrrr)r*c s0tjd||||||d| |rtddS)NrzECurrently PyTorch Hub does not provide densenet264 pretrained models.rI)r<r=rrrGrIrJr=s  zDenseNet264.__init__)rbrcrFTrrrIrIrGrJrrr)rrrrrr)+ __future__rr collectionsrcollections.abcrrrMtorch.nnr@Z torch.hubrmonai.networks.layers.factoriesrrr monai.networks.layers.utilsr r monai.utils.moduler __all__ModulerrArVr[r rrrrrrrrrrrrrrrIrIrIrJs2     /  k%!!!