U Phߞ@sddlmZddlZddlZddlZddlmZddlmZddl Z ddl m Z ddl m Z ddl mZddlmZmZmZmZdd lmZdd lmZd d d ddddgZddddddddddd Zdddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.ZGd/d0d0e jZGd1d d e jZGd2d d eZGd3ddeZGd4ddeeZ d5d6d7d8d Z!d9d:d;d9d<d=dZ"d>d5d;d;d?d@dAdBZ#dCdDdDdDdCdEdFdGZ$dHdCdIdJdKZ%d6dLd:d6dMdNdOZ&d6dLd6dPdQdRZ'dCdSdTdUdVZ(GdWddeZ)dS)X) annotationsN)reduce) NamedTuple)nn) model_zoo) BaseEncoder)ActConvPadPool)get_norm_layer)look_up_option EfficientNetEfficientNetBNget_efficientnet_image_size drop_connectEfficientNetBNFeatures BlockArgsEfficientNetEncoder)?r皙?r)r皙?rr)r333333?i333333?r)rffffff?i,rr)r?i|皙?r)g?皙@irr)rg@i?r)@g@iXr r)rg @ir r)g333333@g333333@i r r) efficientnet-b0efficientnet-b1efficientnet-b2efficientnet-b3efficientnet-b4efficientnet-b5efficientnet-b6efficientnet-b7efficientnet-b8efficientnet-l2zdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b0-355c32eb.pthzdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b1-f1951068.pthzdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b2-8bb594d6.pthzdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b3-5fb5a3c3.pthzdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b4-6ed6700e.pthzdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b5-b6417697.pthzdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b6-c76e70fd.pthzdhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b7-dcc49843.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b0-b64d5a18.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b1-0f3ce85a.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b2-6e9d97e5.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b3-cdd7c0f4.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b4-44fb3a87.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b5-86493f6b.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b6-ac80338e.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b7-4652b6dd.pthzhhttps://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/adv-efficientnet-b8-22a8fe65.pth)r"r#r$r%r&r'r(r)zb0-apzb1-apzb2-apzb3-apzb4-apzb5-apzb6-apzb7-apzb8-apcsjeZdZdddddfdfdddddddd d d d d d fdd ZddddZddd dddZZS) MBConvBlockTbatchMbP?{Gz?epsmomentumrint list[int] float | Nonez bool | None str | tupleNone) spatial_dims in_channels out_channels kernel_sizestride image_size expand_ratiose_ratioid_skipnormdrop_connect_ratereturnc sttd|f} td|f} ||_||_| |_||_||_| |_ |dk rtd|krbdkrtnnd|_ ||_ nd|_ |}||}|jdkr| ||ddd |_ t |j ||_t| ||d |_nt|_ t|_t|_| |||||jdd |_t |j||_t| ||d |_t||j}|j r| d|_tdt||j }| ||dd |_t |jdg||_| ||dd |_t |jdg||_|}| ||ddd |_t |j||_t| ||d |_ t!d dd|_"dS)a Mobile Inverted Residual Bottleneck Block. Args: spatial_dims: number of spatial dimensions. in_channels: number of input channels. out_channels: number of output channels. kernel_size: size of the kernel for conv ops. stride: stride to use for conv ops. image_size: input image resolution. expand_ratio: expansion ratio for inverted bottleneck. se_ratio: squeeze-excitation ratio for se layers. id_skip: whether to use skip connection. norm: feature normalization type and arguments. Defaults to batch norm. drop_connect_rate: dropconnect rate for drop connection (individual weights) layers. References: [1] https://arxiv.org/abs/1704.04861 (MobileNet v1) [2] https://arxiv.org/abs/1801.04381 (MobileNet v2) [3] https://arxiv.org/abs/1905.02244 (MobileNet v3) conv adaptiveavgNrTF)r9r:r;biasnamer8channels)r9r:groupsr;r<rH)r9r:r;memswishinplace)#super__init__r r r9r:r@r<r>rBhas_ser? _expand_conv_make_same_padder_expand_conv_paddingr _bn0rIdentity_depthwise_conv_depthwise_conv_padding_bn1_calculate_output_image_size _se_adaptpoolmaxr3 _se_reduce_se_reduce_padding _se_expand_se_expand_padding _project_conv_project_conv_padding_bn2r_swish)selfr8r9r:r;r<r=r>r?r@rArB conv_typeadaptivepool_typeinpoupZnum_squeezed_channelsZ final_oup __class__U/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/efficientnet.pyrQMsZ#          zMBConvBlock.__init__ torch.TensorinputscCs|}|jdkr2|||}||}||}|||}||}||}|jr| |}| | |}||}| | |}t||}|||}||}|jr|jdkr|j|jkr|jrt||j|jd}||}|S)zMBConvBlock"s forward function. Args: inputs: Input tensor. Returns: Output of this block after processing. rG)ptraining)r>rSrUrVrerXrYrZrRr\r^r_r`ratorchsigmoidrbrcrdr@r<r9r:rBrrs)rfrqxZ x_squeezedrmrmrnforwards*         zMBConvBlock.forwardboolmemory_efficientrCcCs&|rtdddn tddd|_dS)zSets swish function as memory efficient (for training) or standard (for export). Args: memory_efficient (bool): Whether to use memory-efficient version of swish. rMTrNswishralphaN)rre)rfrzrmrmrn set_swishszMBConvBlock.set_swish)T)__name__ __module__ __qualname__rQrwr~ __classcell__rmrmrkrnr,Ks   *h(r,cseZdZdddddddddd d fdd f d d d d dddd ddd dd fdd Zd dddddZddddZddddZZS)!rrrrr-r.r/r0 list[str]r3floatr6r7) blocks_args_strr8r9 num_classeswidth_coefficientdepth_coefficient dropout_rater=rArB depth_divisorrCc s t|dkrtdtd|f} td|f} dd|D}t|tsRtd|gkrbtd||_||_||_ | |_ |g|}d }t d || }| |j |d |d d |_ t |j ||_t| ||d|_t||}d}t|_g|_t|jD]^\}}|jt |j|| t |j|| t|j|d}||j|<||j7}|jdkr|j|q|jt|jd}t|jD]\}}|j }|r|t||9}t}| t!|t"||j|j|j#|j||j$|j%|j&| |d |d7}t||j}|jdkr|j|jdd}t'|jdD]b}|j }|r4|t||9}| t!|t"||j|j|j#|j||j$|j%|j&| |d |d7}q|j t!||qp||krtd|j}t d|| }| ||dd d|_(t |j(||_)t| ||d|_*| d|_+t,||_-t.||j|_/t0d|_1|2dS)a EfficientNet based on `Rethinking Model Scaling for Convolutional Neural Networks `_. Adapted from `EfficientNet-PyTorch `_. Args: blocks_args_str: block definitions. spatial_dims: number of spatial dimensions. in_channels: number of input channels. num_classes: number of output classes. width_coefficient: width multiplier coefficient (w in paper). depth_coefficient: depth multiplier coefficient (d in paper). dropout_rate: dropout rate for dropout layers. image_size: input image resolution. norm: feature normalization type and arguments. drop_connect_rate: dropconnect rate for drop connection (individual weights) layers. depth_divisor: depth divisor for channel rounding. )rGrrz#spatial_dims can only be 1, 2 or 3.rDrEcSsg|]}t|qSrm)r from_string.0srmrmrn sz)EfficientNet.__init__..zblocks_args must be a listzblock_args must be non-emptyr rF)r;r<rHrIr) input_filtersoutput_filters num_repeatrG) r8r9r:r;r<r=r>r?r@rArB)rr<z,total number of blocks created != num_blocksi)r;rHrMN)3rPrQ ValueErrorr r isinstancelistZ _blocks_argsrr9rB_round_filters _conv_stemrT_conv_stem_paddingr rVr[r Sequential_blocksextract_stacks enumerate_replacerr_round_repeatsrr<appendlenr add_modulestrr,r;r>r?r@range _conv_head_conv_head_paddingrZ _avg_poolingDropout_dropoutLinear_fcrre_initialize_weights)rfrr8r9rrrrr=rArBrrgrhZ blocks_argsZcurrent_image_sizer<r: num_blocksidxZ block_argsZ stack_idxZblk_drop_connect_rate sub_stack_Zhead_in_channelsrkrmrnrQs                     zEfficientNet.__init__TrxrycCsB|rtdn tddd|_|jD]}|D]}||q,q$dS)z Sets swish function as memory efficient (for training) or standard (for JIT export). Args: memory_efficient: whether to use memory-efficient version of swish. rMr{rr|N)rrerr~)rfrzrblockrmrmrnr~s zEfficientNet.set_swishrorpcCsx|||}|||}||}|||}|||}||}|j dd}| |}| |}|S)a! Args: inputs: input should have spatially N dimensions ``(Batch, in_channels, dim_0[, dim_1, ..., dim_N])``, N is defined by `dimensions`. Returns: a torch Tensor of classification prediction in shape ``(Batch, num_classes)``. rG) start_dim) rrrerVrrrrZrflattenrr)rfrqrvrmrmrnrws      zEfficientNet.forwardrCcCs|D]\}}t|tjtjtjfrnttj|j d|j }|j j dtd||jdk r|jj qt|tjtjtjfr|j j d|jj qt|tjr|j d}d}dt||}|j j | ||jj qdS)a Args: None, initializes weights for conv/linear/batchnorm layers following weight init methods from `official Tensorflow EfficientNet implementation `_. Adapted from `EfficientNet-PyTorch's init method `_. rGrr!Nr) named_modulesrrConv1dConv2dConv3droperatormulr;r:weightdatanormal_mathsqrtrHzero_ BatchNorm1d BatchNorm2d BatchNorm3dfill_rsizeuniform_)rfrmfan_outfan_in init_rangermrmrnrs    z EfficientNet._initialize_weights)T)rrrrQr~rwrrrmrmrkrnrs *3 c sLeZdZdddddddddfd fd d d d d d d d dd fdd ZZS)rTrrrr-r.r/r0Frrxr3r6r7 model_name pretrainedprogressr8r9rrAadv_proprCc  sdddddddg} |tkr>dt} td |d | d t|\} } } }}tj| |||| | || ||d |r|d krt||||dS)a Generic wrapper around EfficientNet, used to initialize EfficientNet-B0 to EfficientNet-B7 models model_name is mandatory argument as there is no EfficientNetBN itself, it needs the N in [0, 1, 2, 3, 4, 5, 6, 7, 8] to be a model Args: model_name: name of model to initialize, can be from [efficientnet-b0, ..., efficientnet-b8, efficientnet-l2]. pretrained: whether to initialize pretrained ImageNet weights, only available for spatial_dims=2 and batch norm is used. progress: whether to show download progress for pretrained weights download. spatial_dims: number of spatial dimensions. in_channels: number of input channels. num_classes: number of output classes. norm: feature normalization type and arguments. adv_prop: whether to use weights trained with adversarial examples. This argument only works when `pretrained` is `True`. Examples:: # for pretrained spatial 2D ImageNet >>> image_size = get_efficientnet_image_size("efficientnet-b0") >>> inputs = torch.rand(1, 3, image_size, image_size) >>> model = EfficientNetBN("efficientnet-b0", pretrained=True) >>> model.eval() >>> outputs = model(inputs) # create spatial 2D >>> model = EfficientNetBN("efficientnet-b0", spatial_dims=2) # create spatial 3D >>> model = EfficientNetBN("efficientnet-b0", spatial_dims=3) # create EfficientNetB7 for spatial 2D >>> model = EfficientNetBN("efficientnet-b7", spatial_dims=2) r1_k3_s11_e1_i32_o16_se0.25r2_k3_s22_e6_i16_o24_se0.25r2_k5_s22_e6_i24_o40_se0.25r3_k3_s22_e6_i40_o80_se0.25r3_k5_s11_e6_i80_o112_se0.25r4_k5_s22_e6_i112_o192_se0.25r1_k3_s11_e6_i192_o320_se0.25, invalid model_name  found, must be one of  rr8r9rrrrr=rBrArNefficientnet_paramsjoinkeysrrPrQ_load_state_dictrfrrrr8r9rrArrmodel_name_stringZ weight_coeffZ depth_coeffr=rZdropconnect_raterkrmrnrQs41  zEfficientNetBN.__init__)rrrrQrrmrmrkrnrs c sZeZdZdddddddddfd fd d d d d d d d dd fdd ZddddZZS)rTrrrr-r.r/r0Frrxr3r6r7rc  sdddddddg} |tkr>dt} td |d | d t|\} } } }}tj| |||| | || ||d |r|d krt||||dS)a{ Initialize EfficientNet-B0 to EfficientNet-B7 models as a backbone, the backbone can be used as an encoder for segmentation and objection models. Compared with the class `EfficientNetBN`, the only different place is the forward function. This class refers to `PyTorch image models `_. rrrrrrrrrrrrrNrrrkrmrnrQ5s4  zEfficientNetBNFeatures.__init__rorpcCsp|||}|||}g}d|jkr8||t|jD](\}}||}|d|jkrB||qB|S)z Args: inputs: input should have spatially N dimensions ``(Batch, in_channels, dim_0[, dim_1, ..., dim_N])``, N is defined by `dimensions`. Returns: a list of torch Tensors. rrG)rrrerVrrrr)rfrqrvfeaturesirrmrmrnrwls    zEfficientNetBNFeatures.forward)rrrrQrwrrmrmrkrnr3s $7c @speZdZdZdddddddd d d g Zed d ddZedd ddZedd ddZedd ddZ dS)rzI Wrap the original efficientnet to an encoder for flexible-unet. r"r#r$r%r&r'r(r)r*r+z list[dict]rc Cs>g}|jD].}||dddddddddfd |kd q |S) zN Get the initialization parameter for efficientnet backbones. Trrrr-r.r/r0ap)rrrr8r9rrAr)backbone_namesr)clsparameter_list backbone_namermrmrnget_encoder_parameterss   z*EfficientNetEncoder.get_encoder_parameterszlist[tuple[int, ...]]c Csdddddddddd g S) zS Get number of efficientnet backbone output feature maps' channel. )(pi@)rr0xi`)rrri)rr8i)rr@i)rrHi@)rrPri)rrXi)rhrii`rmrrmrmrnnum_channels_per_outputsz+EfficientNetEncoder.num_channels_per_outputr4cCs dgdS)z Get number of efficientnet backbone output feature maps. Since every backbone contains the same 5 output feature maps, the number list should be `[5] * 10`.  rmrrmrmrn num_outputsszEfficientNetEncoder.num_outputsrcCs|jS)z2 Get names of efficient backbone. )rrrmrmrnget_encoder_namessz%EfficientNetEncoder.get_encoder_namesN) rrr__doc__r classmethodrrrrrmrmrmrnrs( rr3)rrCcCsB|tkr,dt}td|d|dt|\}}}}}|S)z Get the input image size for a given efficientnet model. Args: model_name: name of model to initialize, can be from [efficientnet-b0, ..., efficientnet-b7]. Returns: Image size for single spatial dimension as integer. rrrr)rrrr)rrrresrmrmrnrs rorrx)rqrrrsrCc Cs|dks|dkrtd||s&|S|jd}d|}t|jd}|dgdg|}tj||j|jd}||7}t|}|||} | S)ah Drop connect layer that drops individual connections. Differs from dropout as dropconnect drops connections instead of whole neurons as in dropout. Based on `Deep Networks with Stochastic Depth `_. Adapted from `Official Tensorflow EfficientNet utils `_. This function is generalized for MONAI's N-Dimensional spatial activations e.g. 1D activations [B, C, H], 2D activations [B, C, H, W] and 3D activations [B, C, H, W, D] Args: inputs: input tensor with [B, C, dim_1, dim_2, ..., dim_N] where N=spatial_dims. p: probability to use for dropping connections. training: whether in training or evaluation mode. Returns: output: output tensor after applying drop connection. rFrz$p must be in range of [0, 1], found rrGr)dtypedevice)rshaperrtrandr r floor) rqrrrs batch_size keep_probnum_dimsZrandom_tensor_shape random_tensorZ binary_tensoroutputrmrmrnrs   z nn.Moduler7)modelarchrrrCc Cs|r|ddd}t|td}|dkr.cSs g|]}|d||dfqS)rrm)r_prmrmrnrHscSsg|]}|D]}|q qSrmrm)rinnerouterrmrmrnrLs)rzipreversed)r=r;r%r<r _pad_size _paddingsZ _paddings_retrmrmrn_get_same_padding_conv_nd&s   r0z!nn.Conv1d | nn.Conv2d | nn.Conv3d)conv_opr=cCsHt||j|j|j}tdt|df}t|dkr@||ddStS)a  Helper for initializing ConstantPadNd with SAME padding similar to Tensorflow. Uses output of _get_same_padding_conv_nd() to get the padding size. This function is generalized for MONAI's N-Dimensional spatial operations (e.g. Conv1D, Conv2D, Conv3D) Args: conv_op: nn.ConvNd operation to extract parameters for op from image_size: input image/feature spatial size Returns: If padding required then nn.ConstandNd() padder initialized to paddings otherwise nn.Identity() Z constantpadrrrF)paddingr$) r0r;r%r<r rsumrrW)r1r=r2padderrmrmrnrTPs   rTr5)filtersrrrCcCsR|s|S|}|}||}t|t||d||}|d|krJ||7}t|S)aN Calculate and round number of filters based on width coefficient multiplier and depth divisor. Args: filters: number of input filters. width_coefficient: width coefficient for model. depth_divisor: depth divisor to use. Returns: new_filters: new number of filters after calculation. rg?)r]r3)r5rr multiplierdivisorZ filters_floatZ new_filtersrmrmrnrhs  r)repeatsrrCcCs|s|Stt||S)a Re-calculate module's repeat number of a block based on depth coefficient multiplier. Args: repeats: number of original repeats. depth_coefficient: depth coefficient for model. Returns: new repeat: new number of repeat after calculating. r3rr&)r8rrmrmrnrs rzint | tuple[int])input_image_sizer<csLttr:tfddD}|s2tddfdd|DS)a5 Calculates the output image size when using _make_same_padder with a stride. Required for static padding. Args: input_image_size: input image/feature spatial size. stride: Conv2d operation"s stride. Returns: output_image_size: output image/feature spatial size. c3s|]}d|kVqdS)rNrmrr<rmrn sz/_calculate_output_image_size..z&unequal strides are not possible, got rcsg|]}tt|qSrmr9)rZim_szr;rmrnrsz0_calculate_output_image_size..)rtupleallr)r:r<Zall_strides_equalrmr;rnr[s  r[c@speZdZUdZded<ded<ded<ded<ded<ded<d ed <d Zd ed <eddddZddZd S)rzq BlockArgs object to assist in decoding string notation of arguments for MBConvBlock definition. r3rr;r<r>rrrxr@Nr5r?r) block_stringc Cs:|d}i}|D]4}td|}t|dkr|dd\}}|||<qd|kr`t|ddkpt|ddkr|dd|ddkpt|ddko|dd|ddko|dd|ddk}|std tt|d t|d t|ddt|d t|d t|dd|kd|kr2t|dnddS)a> Get a BlockArgs object from a string notation of arguments. Args: block_string (str): A string notation of arguments. Examples: "r1_k3_s11_e1_i32_o16_se0.25". Returns: BlockArgs: namedtuple defined at the top of this function. rz(\d.*)rNrrGrrzinvalid stride option receivedrkeroZnoskipse)rr;r<r>rrr@r?)rrrrrr3r)r?opsoptionsopsplitsr#r$Z stride_checkrmrmrnrs0    &>     zBlockArgs.from_stringcCsTd|jd|jd|j|jd|jd|jd|jd|j}|jsP|d7}|S) z Return a block string notation for current BlockArgs object Returns: A string notation of BlockArgs object arguments. Example: "r1_k3_s11_e1_i32_o16_se0.25_noskip". r@_kr(_e_i_o_seZ_noskip)rr;r<r>rrr?r@)rfstringrmrmrn to_strings @zBlockArgs.to_string) rrrr__annotations__r? staticmethodrrOrmrmrmrnrs  ')* __future__rrrr functoolsrtypingrrtrZ torch.utilsrZmonai.networks.blocksrmonai.networks.layers.factoriesrr r r monai.networks.layers.utilsr monai.utils.moduler __all__rrModuler,rrrrrrrr0rTrrr[rrmrmrmrn s         wWPJ/*