-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathembedding_generator.py
More file actions
188 lines (141 loc) · 6.07 KB
/
Copy pathembedding_generator.py
File metadata and controls
188 lines (141 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""EmbeddingGenerator implementations."""
from __future__ import annotations
import random
from dataclasses import dataclass
from typing import Protocol, runtime_checkable
from uuid import UUID
import numpy as np
from numpy.typing import NDArray
from PIL import Image
from lightly_studio.models.embedding_model import EmbeddingModelCreate
@dataclass(frozen=True)
class ImageCrop:
"""Image crop to embed."""
filepath: str
x: int
y: int
width: int
height: int
@runtime_checkable
class EmbeddingGenerator(Protocol):
"""Protocol defining the interface for embedding models.
This protocol defines the interface that all embedding models must
implement. Concrete implementations will use different techniques
for creating embeddings.
"""
def get_embedding_model_input(self, collection_id: UUID) -> EmbeddingModelCreate:
"""Generate an EmbeddingModelCreate instance.
Args:
collection_id: The ID of the collection.
Returns:
An EmbeddingModelCreate instance with the model details.
"""
def embed_text(self, text: str) -> list[float]:
"""Generate an embedding for a text sample.
Args:
text: The text to embed.
Returns:
A list of floats representing the generated embedding.
"""
...
@runtime_checkable
class ImageEmbeddingGenerator(EmbeddingGenerator, Protocol):
"""Protocol defining the interface for image embedding models.
This protocol defines the interface that all image embedding models must
implement. Concrete implementations will use different techniques
for creating embeddings.
"""
def embed_images(self, filepaths: list[str], show_progress: bool = True) -> NDArray[np.float32]:
"""Generate embeddings for multiple image samples.
TODO(Michal, 04/2025): Use DatasetLoader as input instead.
Args:
filepaths: A list of file paths to the images to embed.
show_progress: Whether to show a progress bar during embedding.
Returns:
A numpy array representing the generated embeddings
in the same order as the input file paths.
"""
...
def embed_image_crops(
self, image_crops: list[ImageCrop], show_progress: bool = True
) -> NDArray[np.float32]:
"""Generate embeddings for image crops.
Args:
image_crops: A list of image crop definitions to embed.
show_progress: Whether to show a progress bar during embedding.
Returns:
A numpy array representing the generated embeddings in the same order
as the input crops.
"""
...
def embed_pil_images(
self, images: list[Image.Image], show_progress: bool = True
) -> NDArray[np.float32]:
"""Generate embeddings for in-memory PIL images.
Args:
images: PIL images to embed.
show_progress: Whether to show a progress bar during embedding.
Returns:
A numpy array representing the generated embeddings in the same order
as the input images.
"""
...
@runtime_checkable
class VideoEmbeddingGenerator(EmbeddingGenerator, Protocol):
"""Protocol defining the interface for video embedding models.
This protocol defines the interface that all video embedding models must
implement. Concrete implementations will use different techniques
for creating embeddings.
"""
def embed_videos(self, filepaths: list[str]) -> NDArray[np.float32]:
"""Generate embeddings for multiple video samples.
Args:
filepaths: A list of file paths to the videos to embed.
Returns:
A numpy array representing the generated embeddings
in the same order as the input file paths.
"""
...
class RandomEmbeddingGenerator(ImageEmbeddingGenerator, VideoEmbeddingGenerator):
"""Model that produces random embeddings with a fixed dimension."""
def __init__(self, dimension: int = 3):
"""Initialize the random embedding model.
Args:
dimension: The dimension of the embedding vectors to generate.
"""
self._dimension = dimension
def get_embedding_model_input(self, collection_id: UUID) -> EmbeddingModelCreate:
"""Generate an EmbeddingModelCreate instance.
Args:
collection_id: The ID of the collection.
Returns:
An EmbeddingModelCreate instance with the model details.
"""
return EmbeddingModelCreate(
name="Random",
embedding_model_hash="random_model",
embedding_dimension=self._dimension,
collection_id=collection_id,
)
def embed_text(self, _text: str) -> list[float]:
"""Generate a random embedding for a text sample."""
return [random.random() for _ in range(self._dimension)]
def embed_images(self, filepaths: list[str], show_progress: bool = True) -> NDArray[np.float32]:
"""Generate random embeddings for multiple image samples."""
_ = show_progress # Not used for random embeddings.
return np.random.rand(len(filepaths), self._dimension).astype(np.float32)
def embed_image_crops(
self, image_crops: list[ImageCrop], show_progress: bool = True
) -> NDArray[np.float32]:
"""Generate random embeddings for multiple image crops."""
_ = show_progress # Not used for random embeddings.
return np.random.rand(len(image_crops), self._dimension).astype(np.float32)
def embed_pil_images(
self, images: list[Image.Image], show_progress: bool = True
) -> NDArray[np.float32]:
"""Generate random embeddings for in-memory PIL images."""
_ = show_progress # Not used for random embeddings.
return np.random.rand(len(images), self._dimension).astype(np.float32)
def embed_videos(self, filepaths: list[str]) -> NDArray[np.float32]:
"""Generate random embeddings for multiple video samples."""
return np.random.rand(len(filepaths), self._dimension).astype(np.float32)