U œPÓhæã@sJddlmZddlZddlmZddlmZddiZGdd„dejƒZdS) é)Ú annotationsN)Únn)Ú model_zooÚ clip_encoding_universal_model_32zphttps://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/clip_encoding_universal_model.pthc s<eZdZdZddddddd d d œ‡fd d „ Zdd„Z‡ZS)Ú TextEncoderaV Text to vision encoding by Contrastive Language-Image Pre-training (CLIP) or random embedding. The text to vision encoder loads the pre-trained or random initialized weights with connection to 2D/3D vision models. Contrastive Language-Image Pre-training (CLIP), based on: "Radford et al., Learning Transferable Visual Models From Natural Language Supervision " Connecting text and medical 3D image, based on: "Liu et al., CLIP-Driven Universal Model for Organ Segmentation and Tumor Detection " ééérTÚintÚstrÚboolÚNone)Ú out_channelsÚ spatial_dimsÚtext_dimÚ hidden_sizeÚencodingÚ pretrainedÚreturnc s tƒ ¡||_||_|dkr&tdƒ‚|jdkr@t ||¡|_n\| dt   ||¡¡|r~t |j}t j |dd}| ¡|j_nt|j›dƒt ||¡|_dS) a# Args: out_channels: number of output channels, to control text-based embedding for classes. spatial_dims: number of spatial dims. text_dim: dimension of text embeddings. hidden_size: dimension of hidden features, compatible to different vision feature dimensions. encoding: the text embedding type, default to use clip text pretrained weights. pretrained: whether to load pretrained weights from e.g., (CLIP) to initialize text embeddings, default to False. )érz#spatial dimension should be 2 or 3.Úrand_embeddingÚtext_embeddingÚcpu)Ú map_locationzD is not implemented, and can not be downloaded, please load your ownN)ÚsuperÚ__init__rrÚ ValueErrorrÚ EmbeddingrÚregister_bufferÚtorchÚrandnÚurl_maprÚload_urlÚfloatÚdataÚprintÚLinearÚtext_to_vision) ÚselfrrrrrrÚ model_urlÚpretrain_state_dict©Ú __class__©úY/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/blocks/text_embedding.pyr&s   zTextEncoder.__init__cCsr|jdkr|jj}nt|jƒtj | |j¡¡}|jdkrT|  d¡  d¡  d¡}n|jdkrn|  d¡  d¡}|S)Nrrr) rrÚweightr%rÚ functionalÚrelur'rÚ unsqueeze)r(rr-r-r.ÚforwardMs     zTextEncoder.forward)rrr rT)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr3Ú __classcell__r-r-r+r.rsù 'r) Ú __future__rrrZ torch.utilsrr!ÚModulerr-r-r-r.Ú s   þ