lazyslide.tl.image_generation

lazyslide.tl.image_generation#

image_generation(wsi=None, model='cytosyn', prompt_tiles=None, tile_key='tiles', device=None, amp=None, autocast_dtype=None, num_images_per_tiles=2, seed=0, *, compile=None, compile_kws=None, **kwargs)#

Generation of tile images unconditionally or conditionally.

Conditional generation (prompt_tiles) needs a model with generate_conditionally, like CytoSyn, and H0-mini features of the tiles.

Parameters:
wsiWSIData

The WSIData object to work on.

modelstr or ImageGenerationModelProtocol, default: “cytosyn”

The image generation model: a model registry key (see Models) or a model instance.

prompt_tilesslice, default: None

The tiles to generate images for, please use index to select tiles. If None, unconditional generation is performed.

tile_keystr, default: “tiles”

Which tile table to use.

devicestr, optional

The device to use for inference. If not provided, the device will be automatically selected.

ampbool, optional

Whether to use automatic mixed precision.

autocast_dtypetorch.dtype, optional

The dtype for automatic mixed precision.

num_images_per_tilesint, default: 2

The number of images to generate for each tile if conditional generation is used. Otherwise, it’s the total number of images to generate if unconditional generation is used.

seedint, default: 0

The random seed to ensure reproducible image generation (May not work for all models).

**kwargsdict

Additional keyword arguments passed to the model’s generation function. Please refer to the documentation of the specific model for details.

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:
list of PIL.Image.Image

A list of generated images in PIL format.

Examples

>>> import lazyslide as zs
>>> # Unconditional generation
>>> imgs = zs.tl.image_generation()
>>> # Conditional generation
>>> wsi = zs.datasets.sample()
>>> zs.tl.feature_extraction(wsi, "h0-mini")
>>> imgs = zs.tl.image_generation(wsi, prompt_tiles=slice(0, 2)) # Generate images for the first two tiles