U Ph@sddlmZddlmZmZddlZddlmZmZddl m Z m Z ddl m Z mZeddd \ZZedd d \ZZGd d d ejjZdS) ) annotations)AnyDictN)Convget_pool_layer)look_up_named_moduleset_named_module)look_up_optionoptional_importz%torchvision.models.feature_extractionget_graph_node_names)namecreate_feature_extractorc sZeZdZdZddddddddfd d d fd d d ddddddd fdd ZddZZS) NetAdapteraa Wrapper to replace the last layer of model by convolutional layer or FC layer. See also: :py:class:`monai.networks.nets.TorchVisionFCModel` Args: model: a PyTorch model, which can be both 2D and 3D models. typically, it can be a pretrained model in Torchvision, like: ``resnet18``, ``resnet34``, ``resnet50``, ``resnet101``, ``resnet152``, etc. more details: https://pytorch.org/vision/stable/models.html. num_classes: number of classes for the last classification layer. Default to 1. dim: number of supported spatial dimensions in the specified model, depends on the model implementation. default to 2 as most Torchvision models are for 2D image processing. in_channels: number of the input channels of last layer. if None, get it from `in_features` of last layer. use_conv: whether to use convolutional layer to replace the last layer, default to False. pool: parameters for the pooling layer, it should be a tuple, the first item is name of the pooling layer, the second item is dictionary of the initialization args. if None, will not replace the `layers[-2]`. default to `("avg", {"kernel_size": 7, "stride": 1})`. bias: the bias value when replacing the last layer. if False, the layer will not learn an additive bias, default to True. fc_name: the corresponding layer attribute of the last fully connected layer. Defaults to ``"fc"``. node_name: the corresponding feature extractor node name of `model`. Defaults to "", the extractor is not in use. NFavg) kernel_sizestrideTfcztorch.nn.Moduleintz int | Noneboolz!tuple[str, dict[str, Any]] | Nonestr) model num_classesdim in_channelsuse_convpoolbiasfc_name node_namec sVtt|} t||} | dkr0| d} |dkrRt| dsJtd| j} n|} |dkr| dkrntdt||rt||t j |_ nt j j | dd|_ d|_nX| rtrt| t||jrdnd} t|| g|_ nt j j | dd|_ t||d |_||r,ttj|f| |d|d |_nt j j| ||d |_||_||_| |_dS) N in_featureszSplease specify input channels of the last fully connected layer with `in_channels`.rzE`node_name` is not compatible with `pool=None`, please set `pool=''`.rr)r spatial_dims)r out_channelsrr )r$ out_featuresr )super__init__listchildrenrhasattr ValueErrorr$rtorchnnIdentityfeatures Sequentialr _has_utilsr r trainingr rrCONVrLinearrrr") selfrrrrrrr r!r"layersZorig_fcZ in_channels_ __class__S/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/nets/netadapter.pyr*4s:     zNetAdapter.__init__cCs||}t|tr|d}n"tj|tttjfr@||j}|j dk rT| |}|j sht |d}nt |j |jdkr|d}qh||}|S)Nrrr).N)r2 isinstancetupler/jitrrTensorr"rrflattenlenshaperr)r8xr<r<r=forwardls        zNetAdapter.forward)__name__ __module__ __qualname____doc__r*rF __classcell__r<r<r:r=rs $8r) __future__rtypingrrr/monai.networks.layersrrmonai.networks.utilsrr monai.utilsr r r r4r _r0Modulerr<r<r<r= s