Skip to content

Embeddings

polars_ts.imaging.embeddings

Vision model embedding extraction for time series images.

Extracts feature embeddings from pretrained vision models (ResNet, ViT, CLIP) applied to time series image representations (recurrence plots, GAF, MTF, etc.).

Requires torch and torchvision (for ResNet/ViT) or transformers (for CLIP).

_images_to_tensor(images, target_size=(224, 224))

Convert dict of 2D arrays to a batch tensor suitable for vision models.

Grayscale images are duplicated to 3 channels. All images are resized to target_size and normalised with ImageNet statistics.

_get_torchvision_model(model_name)

Load a pretrained torchvision model and return (model, layer_name).

_extract_resnet(model, batch)

Extract avgpool features from a ResNet model.

_extract_vit(model, batch)

Extract CLS token features from a ViT model.

Replaces model.heads with an identity to get pre-head features, then restores it. This avoids relying on private ViT internals.

_extract_clip(images, model_name, batch_size=32)

Extract CLIP vision embeddings.

extract_vision_embeddings(images, model='resnet18', batch_size=32)

Extract feature embeddings from a pretrained vision model.

Takes time series images (from to_recurrence_plot, to_gasf, to_gadf, or to_mtf) and passes them through a pretrained vision model to extract feature vectors.

Parameters

images Mapping from series ID to a 2D numpy array (image). Typically the output of to_gasf, to_recurrence_plot, etc. model Model name. Supported: "resnet18", "resnet50", "vit_b_16", "clip" (uses openai/clip-vit-base-patch32). batch_size Number of images to process at once (for ResNet/ViT).

Returns

pl.DataFrame DataFrame with columns ["unique_id", "emb_0", "emb_1", ..., "emb_d"].