lazyslide.tl.text_embedding

Contents

lazyslide.tl.text_embedding#

text_embedding(texts, model='plip', amp=None, autocast_dtype=None, device='cpu', *, compile=None, compile_kws=None)#

Embed the text into a vector in the text-vision co-embedding of a multimodal model, for example PLIP (the default), CONCH or OmiCLIP.

Parameters:
textsList[str]

The list of texts.

modelstr or ImageTextModelProtocol, default: “plip”

The text embedding multimodal model: a model registry key (see Models) or a model instance.

ampbool, optional

Whether to use automatic mixed precision (AMP) for inference.

autocast_dtypetorch.dtype, optional

The dtype for automatic mixed precision.

devicestr, default: “cpu”

The device to use for computation (e.g., ‘cpu’, ‘cuda’, ‘mps’). Defaults to CPU on purpose: embedding a handful of short strings is a tiny amount of compute, and moving the text encoder to a GPU/MPS device costs more than it saves. This one does not follow settings.device — pass the device explicitly to override.

compilebool, optional

Whether to compile the model with torch.compile(). Compilation is best-effort and is silently skipped for models that do not support it.

compile_kwsdict, optional

Keyword arguments passed to torch.compile().

Returns:
DataFrame

The embeddings of the texts, with texts as index.

Examples

>>> import lazyslide as zs
>>> wsi = zs.datasets.sample()
>>> zs.pp.find_tissues(wsi)
>>> zs.pp.tile_tissues(wsi, 256, mpp=0.5, key_added="text_tiles")
>>> zs.tl.feature_extraction(wsi, "plip", tile_key="text_tiles")
>>> terms = ["mucosa", "submucosa", "musclaris", "lymphocyte"]
>>> zs.tl.text_embedding(terms, model="plip")