U Ph;3 @sXdZddlmZddlZddlZddlZddlmZddlm Z ddl m Z ddl Z ddl mmZddlmZmZddlmZdd lmZdd lmZd d lmZd d lmZdddgZdddddZd4ddZ d5dddddddZ!d6ddddddZ"d7d"d#d$d$d%d&d'd(dZ#d8d+d#d$d%d,d$d$dd$d#d-d. d/dZ$d9d0dd,d,d1d2d3Z%dS):zr Utilities for accessing Nvidia MMARs See Also: - https://docs.nvidia.com/clara/clara-train-sdk/pt/mmar.html ) annotationsN)Mapping)Path)Any)download_and_extractlogger)PathLike)copy_model_state)optional_import) MODEL_DESC)RemoteMMARKeysget_model_spec download_mmarload_from_mmarz int | strz dict | Any)idxreturncCsZt|trt|St|trV|}tD](}t|tj|kr,|Sq,|S)zlget model specification by `idx`. `idx` could be index of the constant tuple of dict or the actual model ID.) isinstanceintr strstriplowergetKeysID)rkeycandrK/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/mmars/mmars.pyr)s    2c Cs d}ddddgdddd d gd d d d ddddddg dd}td d|ddg}||d<||d<||d<t|}|d|}tddd\}} | r||} | ntdt| j} i} | dD]T} | dD]F}|d }d |d i| |<|d D] }|dd kr|d!| |d"<qqq| S)#Nz>> from monai.apps import download_mmar >>> download_mmar("clara_pt_prostate_mri_segmentation_1", mmar_dir=".") >>> download_mmar("prostate_mri_segmentation", mmar_dir=".", api=True) Returns: The local directory of the downloaded model. If api is True, a list of local directories of downloaded models. z torch.hubget_dirr0mmarszTmmar_dir=None, but no suitable default directory computed. Upgrade Pytorch to 1.6+ ?rz&api query returns no item for pattern z. Please change or shorten it.rOr3r'_z.zipNmd5zipF)r=filepath output_dirhash_val hash_type file_typehas_baserSz'api query download no item for pattern r z nvidia/med/)rKrL.)r rr8rIrrrrNAMElenitemsrrMrappendrVERSIONURLZ FILE_TYPEZHASH_VALZ HASH_TYPE)rQrRrSrTrKrUZhas_homeZ _mmar_dirrEZmodel_dir_listkvver download_url model_dirZmodel_fullnameZ model_urlrrrrjs`$      " FrGzMapping | str | intz Any | Noner) rQrRrSrK map_location pretrained weights_only model_keyrT model_filerc  Cs|r(tjt|tr t|tjn|i}t|ts:t|}t|||||d} | dkrbtj dd} | | tj | } t d| tjd| d| jdr|std |rtd tjj| |d Stj| |d } |r| || Stt|  d i|id } | rt| tsf| | tjtj dd}t|}t|}W5QRXt|}t||id } | s~tt| |id } | rt| tstd| tjd| tj d| dr| d}tj|}nj| dr0| dddd\}}t||d\}}|s@td| ddd| dntd| dt d|| dd}|r~|f|}t d|n|}|rt|| || dd\}}}|r|rt d t!|d!t!|dt d"| tj"pt#|tjd#d$}t d%|d&|S)'ak Download and extract Medical Model Archive (MMAR) model weights from Nvidia Clara Train. Args: item: the corresponding model item from `MODEL_DESC`. mmar_dir: : target directory to store the MMAR, default is mmars subfolder under `torch.hub get_dir()`. progress: whether to display a progress bar when downloading the content. version: version number of the MMAR. Set it to `-1` to use `item[Keys.VERSION]`. map_location: pytorch API parameter for `torch.load` or `torch.jit.load`. pretrained: whether to load the pretrained weights after initializing a network module. weights_only: whether to load only the weights instead of initializing the network module and assign weights. model_key: a key to search in the model file or config file for the model dictionary. Currently this function assumes that the model dictionary has `{"[name|path]": "test.module", "args": {'kw': 'test'}}`. api: whether to query NGC API to get model infomation. model_file: the relative path to the model file within an MMAR. Examples:: >>> from monai.apps import load_from_mmar >>> unet_model = load_from_mmar("clara_pt_prostate_mri_segmentation_1", mmar_dir=".", map_location="cpu") >>> print(unet_model) See Also: https://docs.nvidia.com/clara/ )rQrRrSrKrTNmodelszmodel.ptz *** "z" available at r`z.tsz4Loading a ScriptModule, 'pretrained' option ignored.z6Loading a ScriptModule, 'weights_only' option ignored.)rlZ train_confrdefaultconfigzconfig_train.jsonz4Could not load model config dictionary from config: z, or from model file: r'pathr )moduler'z!Could not load MMAR model config r z*, Please make sure MMAR's sub-folders in 'z[' is on the PYTHONPATH.See also: https://docs.nvidia.com/clara/clara-train-sdk/pt/byom.htmlzCould not load model config z *** Model: argsz*** Model params: T)inplacez&*** Loading model state -- unchanged: z , changed: z ---z nvidia:med:)rLz#For more information, please visit  )$rrarrrrrosrujoinrZ MODEL_FILErinfor'endswithwarningswarntorchjitload_get_valr4Z CONFIG_FILEopenr5r8 monai_nets__dict__rsplitr r warningrbZDOCrN)rQrRrSrKrlrmrnrorTrprkZ _model_filerEZ model_configZ json_pathf conf_dictrJZ model_clsZ model_moduleZhas_clsZ model_kwargsZ model_instrWchanged unchangedZdoc_urlrrrrsp%$                 r) input_dictrrsrcCsN||kr||S|D]4}||}t|trt||dd}|dk r|Sq|S)z Search for the item with `key` in `config_dict`. Returns: the first occurrence of `key` in a breadth first search. Nrr)rrr)rrrsZsub_dictvalZ found_valrrrr-s  r)rr)r )r )NTTrO) NTrONTFrGTN)rGN)&__doc__ __future__rr5rzr~collections.abcrpathlibrtypingrrmonai.networks.netsnetworksnetsrmonai.apps.utilsrrmonai.config.type_definitionsrmonai.networks.utilsr monai.utils.moduler model_descr r r__all__rrIrMrNrrrrrrr sH           -T$q