o , i@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_extractorcsFeZdZdZddddddddfd d d fdfdd 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_sizestrideTfcmodeltorch.nn.Module num_classesintdim in_channels int | Noneuse_convboolpool!tuple[str, dict[str, Any]] | Nonebiasfc_namestr node_namec sTtt|} t||} | dur| d} |dur)t| ds%td| j} n|} |durV| dkr7tdt||rGt||t j |_ n t j j | dd|_ d|_n,| rptrpt| t||jrddnd} t|| g|_ n t j j | dd|_ t||d |_||rttj|f| |d|d |_n t 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%) selfrrrrrr r"r#r%layersZorig_fcZ in_channels_ __class__`/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/netadapter.pyr-4s:     zNetAdapter.__init__cCs||}t|tr|d}ntj|tttjfr ||j}|j dur*| |}|j s4t |d}nt |j |jdkrL|d}t |j |jdks>||}|S)Nrrr).N)r5 isinstancetupler2jitrr$Tensorr%r rflattenlenshaperr)r;xr?r?r@forwardls       zNetAdapter.forward)rrrrrrrrrrr r!r"rr#r$r%r$)__name__ __module__ __qualname____doc__r-rI __classcell__r?r?r=r@rs 8r) __future__rtypingrrr2monai.networks.layersrrmonai.networks.utilsrr monai.utilsr r r r7r _r3Modulerr?r?r?r@s