o - in @sHddlmZddlZddlZddlmZddlmZddlm Z ddl m Z ddl Z ddl mZddlmZddlmZmZdd lmZmZmZdd lmZdd lmZmZed d d\ZZeddd\ZZdZ dZ!gdZ"dgddddfdgddddfdgddddfdgddddfdgddddfdgddddfdgddddfd Z#e$e%Z&d!d"Z'd#d$Z(Gd%d&d&ej)Z*Gd'd(d(ej)Z+Gd)d*d*ej)Z,Gd+d,d,e,Z-Gd-d.d.e-eZ.d\d=d>Z/d]d^d?d@Z0d]d^dAdBZ1d]d^dCdDZ2d]d^dEdFZ3d]d^dGdHZ4d]d^dIdJZ5d]d^dKdLZ6d_d`dRdSZ7dadTdUZ8dbdcdZd[Z9dS)d) annotationsN)Callable)partial)Path)Any) BaseEncoder)ConvPool) get_act_layerget_norm_layerget_pool_layer)ensure_tuple_rep)look_up_optionoptional_importZhuggingface_hubhf_hub_downloadnamezhuggingface_hub.utils._errorsEntryNotFoundError#TencentMedicalNet/MedicalNet-Resnetresnet_) ResNet ResNetBlockResNetBottleneckresnet10resnet18resnet34resnet50 resnet101 resnet152 resnet200basicr"r"r"BFTr%r%r%Ar( bottleneckr(r)r(r($r(r(r0r(rrrrrrrcCgdS)N)@r9r9r9\/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/resnet.py get_inplanes?r;cCr4)N)rr")r"r")r"r"r"r9r9r9r9r: get_avgpoolCr<r=cs@eZdZdZddddddifdfdfdd ZdddZZS)rr"r(NreluinplaceTbatch in_planesintplanes spatial_dimsstride downsamplenn.Module | partial | Noneact str | tuplenormreturnNonec s~tttj|f}|||dd|dd|_t|||d|_t|d|_|||dddd|_ t|||d|_ ||_ ||_ dS) a Args: in_planes: number of input channels. planes: number of output channels. spatial_dims: number of spatial dimensions of the input image. stride: stride to use for first conv layer. downsample: which downsample layer to use. act: activation type and arguments. Defaults to relu. norm: feature normalization type and arguments. Defaults to batch norm. r(r"F) kernel_sizepaddingrEbiasrrDchannelsr)rMrNrON) super__init__rCONVconv1r bn1r rHconv2bn2rFrE) selfrArCrDrErFrHrJ conv_type __class__r9r:rSJs   zResNetBlock.__init__x torch.TensorcCs`|}||}||}||}||}||}|jdur%||}||7}||}|SN)rUrVrHrWrXrFrYr]residualoutr9r9r:forwardjs        zResNetBlock.forwardrArBrCrBrDrBrErBrFrGrHrIrJrIrKrLr]r^rKr^__name__ __module__ __qualname__ expansionrSrc __classcell__r9r9r[r:rGs  rcs@eZdZdZddddddifdfdfdd ZdddZZS)rr)r(r"Nr>r?Tr@rArBrCrDrErFrGrHrIrJrKrLc stttj|f}tt||d} |||ddd|_| |d|_|||d|ddd|_| |d|_ ||||j ddd|_ | ||j d|_ t |d|_||_||_d S) a Args: in_planes: number of input channels. planes: number of output channels (taking expansion into account). spatial_dims: number of spatial dimensions of the input image. stride: stride to use for second conv layer. downsample: which downsample layer to use. act: activation type and arguments. Defaults to relu. norm: feature normalization type and arguments. Defaults to batch norm. )rrDr"F)rMrO)rQr(rMrErNrOrN)rRrSrrTrr rUrVrWrXrjconv3bn3r rHrFrE) rYrArCrDrErFrHrJrZ norm_layerr[r9r:rSs     zResNetBottleneck.__init__r]r^cCs~|}||}||}||}||}||}||}||}||}|jdur4||}||7}||}|Sr_)rUrVrHrWrXrmrnrFr`r9r9r:rcs           zResNetBottleneck.forwardrdrerfr9r9r[r:r}s $rc sjeZdZdZddddddddd d d d d ifd f d5fd&d' Zd6d7d,d-Z  d8d9d1d2Zd:d3d4ZZS);ra ResNet based on: `Deep Residual Learning for Image Recognition `_ and `Can Spatiotemporal 3D CNNs Retrace the History of 2D CNNs and ImageNet? `_. Adapted from ``_. Args: block: which ResNet block to use, either Basic or Bottleneck. ResNet block class or str. for Basic: ResNetBlock or 'basic' for Bottleneck: ResNetBottleneck or 'bottleneck' layers: how many layers to use. block_inplanes: determine the size of planes at each step. Also tunable with widen_factor. spatial_dims: number of spatial dimensions of the input image. n_input_channels: number of input channels for first convolutional layer. conv1_t_size: size of first convolution layer, determines kernel and padding. conv1_t_stride: stride of first convolution layer. no_max_pool: bool argument to determine if to use maxpool layer. shortcut_type: which downsample block to use. Options are 'A', 'B', default to 'B'. - 'A': using `self._downsample_basic_block`. - 'B': kernel_size 1 conv + norm. widen_factor: widen output for each layer. num_classes: number of output (classifications). feed_forward: whether to add the FC layer for the output, default to `True`. bias_downsample: whether to use bias term in the downsampling block when `shortcut_type` is 'B', default to `True`. act: activation type and arguments. Defaults to relu. norm: feature normalization type and arguments. Defaults to batch norm. r(r"Fr#g?iTr>r?r@block*type[ResNetBlock | ResNetBottleneck] | strlayers list[int]block_inplanesrDrBn_input_channels conv1_t_sizetuple[int] | intconv1_t_stride no_max_poolbool shortcut_typestr widen_factorfloat num_classes feed_forwardbias_downsamplerHrIrJrKrLcsDtt|tr|dkrt}n |dkrt}ntd|ttj|f}t t j |f}t t j |f}t }fdd|D}|d|_ ||_| |_t||}t||}|||j ||tdd|Dd d |_t|||j d }||_t|d |_|d ddd|_|||d|d|| |_|j||d|d|| dd|_|j||d|d|| dd|_|j||d |d || dd|_||||_| rt|d |j | nd|_!|"D]F}t||rtj#j$t%&|j'dddqt|t(|r tj#)t%&|j'dtj#)t%&|j*dqt|tjrtj#)t%&|j*dqdS)Nr r+z+Unknown block '%s', use basic or bottleneckcsg|]}t|qSr9)rB).0r]r~r9r: sz#ResNet.__init__..rcss|]}|dVqdS)r%Nr9)rkr9r9r: sz"ResNet.__init__..FrlrPrr(r%r")rMrErN)rEfan_outr>)mode nonlinearity)+rRrS isinstancer}rr ValueErrorrrTr MAXZ ADAPTIVEAVGr=rArzrr tuplerUr rVr rHmaxpool _make_layerlayer1layer2layer3layer4avgpoolnnLinearrjfcmodulesinitkaiming_normal_torch as_tensorweighttype constant_rO)rYrqrsrurDrvrwryrzr|r~rrrrHrJrZ pool_typeZ avgp_typeZ block_avgpoolZconv1_kernel_sizeZ conv1_strideromr[rr:rSs^              zResNet.__init__r]r^rCrEcCsltdd|df|d|}tj|d||dg|jddR|j|jd}tj|j|gdd}|S) Navgr")rMrE)rDrr%)dtypedevice)dim) r rzerossizeshaperrcatdata)rYr]rCrErDrbZ zero_padsr9r9r:_downsample_basic_block"s:zResNet._downsample_basic_block$type[ResNetBlock | ResNetBottleneck]blocks nn.Sequentialc Csttj|f}d} |dks|j||jkrEt|ddhdkr+t|j||j||d} nt||j||jd||j dt ||||jd} ||j|||| |dg} ||j|_t d|D]} | ||j|||dq\tj| S) Nr"r&r#)rCrErD)rMrErOrP)rArCrDrErFrJ)rDrJ) rrTrArjrrrr Sequentialrr rangeappend) rYrqrCrrDr|rErJrZrFrs_ir9r9r:r(sB    zResNet._make_layercCs||}||}||}|js||}||}||}||}||}| |}| | dd}|j durC| |}|S)Nr) rUrVrHrzrrrrrrviewrr)rYr]r9r9r:rcZs           zResNet.forward) rqrrrsrtrurtrDrBrvrBrwrxryrxrzr{r|r}r~rrrBrr{rr{rHrIrJrIrKrL)r() r]r^rCrBrErBrDrBrKr^)r"r@)rqrrCrBrrBrDrBr|r}rErBrJrIrKrre) rgrhri__doc__rSrrrcrkr9r9r[r:rs("  I  2rcs*eZdZddfd d ZdddZZS)ResNetFeaturesTr(r" model_namer} pretrainedr{rDrB in_channelsrKrLc s|tvrdt}td|d|dt|\}}}} } tj||t||d|d| d |rF|dkrB|d krBt||| d d Std d S) aInitialize resnet18 to resnet200 models as a backbone, the backbone can be used as an encoder for segmentation and objection models. Compared with the class `ResNet`, the only different place is the forward function. Args: model_name: name of model to initialize, can be from [resnet10, ..., resnet200]. pretrained: whether to initialize pretrained MedicalNet weights, only available for spatial_dims=3 and in_channels=1. spatial_dims: number of spatial dimensions of the input image. in_channels: number of input channels for first convolutional layer. z, zinvalid model_name z found, must be one of  r%F) rqrsrurDrvryr|rrr(r") datasets23zQPretrained resnet models are only available for in_channels=1 and spatial_dims=3.N) resnet_paramsjoinkeysrrRrSr;_load_state_dict) rYrrrDrZmodel_name_stringrqrsr|rrr[r9r:rSqs(  zResNetFeatures.__init__inputsr^cCs||}||}||}g}|||js||}||}||||}||||}||| |}|||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. ) rUrVrHrrzrrrrr)rYrr]featuresr9r9r:rcs            zResNetFeatures.forward)Tr(r") rr}rr{rDrBrrBrKrL)rr^)rgrhrirSrcrkr9r9r[r:ros$rc@sPeZdZdZgdZedddZeddd Zedd d ZedddZ dS) ResNetEncoderz9Wrap the original resnet to an encoder for flexible-unet.r3rK list[dict]cCs(g}|jD] }||ddddq|S)z6Get the initialization parameter for resnet backbones.Tr(r")rrrDr)backbone_namesr)clsparameter_listZ backbone_namer9r9r:get_encoder_parameterss   z$ResNetEncoder.get_encoder_parameterslist[tuple[int, ...]]cCr4)z:Get number of resnet backbone output feature maps channel.)r5r5r6r7r8rrr5r7r8iirrrr9rr9r9r:num_channels_per_outputsz%ResNetEncoder.num_channels_per_outputrtcCs dgdS)zGet number of resnet backbone output feature maps. Since every backbone contains the same 5 output feature maps, the number list should be `[5] * 7`. rpr9rr9r9r: num_outputss zResNetEncoder.num_outputs list[str]cCs|jS)zGet names of resnet backbones.)rrr9r9r:get_encoder_namesszResNetEncoder.get_encoder_namesN)rKr)rKr)rKrt)rKr) rgrhrirr classmethodrrrrr9r9r9r:rs   rarchr}rqrrsrtrur bool | strprogressr{kwargsrrKcKsNt|||fi|}|rtjrdnd}t|tr6t|r2t d|dtj ||dd} nct d| dd d kr| d d d kr| d dd urt d|} | r^t| d } ntdt| \} } | | ddkr| | ddkrt| |dd} ntd| d| d| tdtddd| D} |j| dd|S)NcudacpuzLoading weights from z...T map_location weights_onlyz+The pretrained checkpoint file is not foundrDr(rvr"rF resnet(\d+)z1arch argument should be as 'resnet_{resnet_depth}r|r#rrrzPlease set shortcut_type to z and bias_downsample to z( when using pretrained MedicalNet resnetzgPlease set n_input_channels to 1and feed_forward to False in order to use MedicalNet pretrained weightsz>MedicalNet pretrained weights are only avalaible for 3D modelscSi|] \}}|dd|qSzmodule.replacerkeyvaluer9r9r: z_resnet..)strict)rrr is_availablerr}rexistsloggerinfoloadFileNotFoundErrorgetresearchrBgroupr%get_medicalnet_pretrained_resnet_args get_pretrained_resnet_medicalnetNotImplementedErroritemsload_state_dict)rrqrsrurrrmodelrmodel_state_dict search_res resnet_depthrr|r9r9r:_resnets@      rcK tdtgdt||fi|S)awResNet-10 with optional pretrained support when `spatial_dims` is 3. Pretraining from `Med3D: Transfer Learning for 3D Medical Image Analysis `_. Args: pretrained (bool): If True, returns a model pre-trained on 23 medical datasets progress (bool): If True, displays a progress bar of the download to stderr rr!rrr;rrrr9r9r:r rcKr)awResNet-18 with optional pretrained support when `spatial_dims` is 3. Pretraining from `Med3D: Transfer Learning for 3D Medical Image Analysis `_. Args: pretrained (bool): If True, returns a model pre-trained on 23 medical datasets progress (bool): If True, displays a progress bar of the download to stderr rr$rrr9r9r:r$rrcKr)awResNet-34 with optional pretrained support when `spatial_dims` is 3. Pretraining from `Med3D: Transfer Learning for 3D Medical Image Analysis `_. Args: pretrained (bool): If True, returns a model pre-trained on 23 medical datasets progress (bool): If True, displays a progress bar of the download to stderr rr'rrr9r9r:r0rrcKr)awResNet-50 with optional pretrained support when `spatial_dims` is 3. Pretraining from `Med3D: Transfer Learning for 3D Medical Image Analysis `_. Args: pretrained (bool): If True, returns a model pre-trained on 23 medical datasets progress (bool): If True, displays a progress bar of the download to stderr rr'rrr;rr9r9r:r<rrcKr)awResNet-101 with optional pretrained support when `spatial_dims` is 3. Pretraining from `Med3D: Transfer Learning for 3D Medical Image Analysis `_. Args: pretrained (bool): If True, returns a model pre-trained on 8 medical datasets progress (bool): If True, displays a progress bar of the download to stderr rr,rrr9r9r:rHrrcKr)awResNet-152 with optional pretrained support when `spatial_dims` is 3. Pretraining from `Med3D: Transfer Learning for 3D Medical Image Analysis `_. Args: pretrained (bool): If True, returns a model pre-trained on 8 medical datasets progress (bool): If True, displays a progress bar of the download to stderr rr.rrr9r9r:rTrrcKr)awResNet-200 with optional pretrained support when `spatial_dims` is 3. Pretraining from `Med3D: Transfer Learning for 3D Medical Image Analysis `_. Args: pretrained (bool): If True, returns a model pre-trained on 8 medical datasets progress (bool): If True, displays a progress bar of the download to stderr rr1rrr9r9r:r`rrrrrBrrc Csd}d}gd}td||||vrz|s||dn||d}z t|||d}Wn;tym|r_t|d|||d}td |t|||d}n t|d ||d Ynwtj|t|d d }ntdt|d| dS)a Download resnet pretrained weights from https://huggingface.co/TencentMedicalNet Args: resnet_depth: depth of the pretrained model. Supported values are 10, 18, 34, 50, 101, 152 and 200 device: device on which the returned state dict will be loaded. "cpu" or "cuda" for example. datasets23: if True, get the weights trained on more datasets (23). Not all depths are available. If not, standard weights are returned. Returns: Pretrained state dict Raises: huggingface_hub.utils._errors.EntryNotFoundError: if pretrained weights are not found on huggingface hub NotImplementedError: if `resnet_depth` is not supported rr) "2ez@Loading MedicalNet pretrained model from https://huggingface.co/z.pthz_23dataset.pth)Zrepo_idfilenamez not available for resnetz Trying with z not found on NTrz;Supported resnet_depth are: [10, 18, 34, 50, 101, 152, 200]z downloaded state_dict) rrr Exceptionrrrrrr) rrrZ$medicalnet_huggingface_repo_basenameZ%medicalnet_huggingface_files_basenameZsupported_depthr Zpretrained_path checkpointr9r9r:rlsD       rcCs |dv}|dvr dnd}||fS)z{ Return correct shortcut_type and bias_downsample for pretrained MedicalNet weights according to resnet depth. )rrr&r#r9)rrr|r9r9r:rsrr nn.ModulerrLcCs`td|}|rt|d}|d}ntdt|d|d}dd|D}||dS) Nrr"Z _23datasetszZmodel_name argument should contain resnet depth. Example: resnet18 or resnet18_23datasets.rrcSrrrrr9r9r:rrz$_load_state_dict..) rrrBrendswithrrrr)rrrrrrr9r9r:rs  r)rr}rqrrsrtrurtrrrr{rrrKr)FT)rr{rr{rrrKr)rT)rrBrr}rr{)rrB)T)rr rr}rr{rKrL): __future__rloggingrcollections.abcr functoolsrpathlibrtypingrrtorch.nnrZmonai.networks.blocks.encoderrmonai.networks.layers.factoriesrr Zmonai.networks.layers.utilsr r r monai.utilsr monai.utils.modulerrr_rZ$MEDICALNET_HUGGINGFACE_REPO_BASENAMEZ%MEDICALNET_HUGGINGFACE_FILES_BASENAME__all__r getLoggerrgrr;r=Modulerrrrrrrrrrrrrrrrr9r9r9r:s^         6>5H * 7 7