Spaces:
Build error
Build error
Process images as numpy arrays
Browse files- README.md +12 -14
- vendiscore.py +7 -16
README.md
CHANGED
@@ -92,16 +92,17 @@ Given n samples, the value of the Vendi Score ranges between 1 and n, with highe
|
|
92 |
>>> samples = [0, 0, 10, 10, 20, 20]
|
93 |
>>> k = lambda a, b: np.exp(-np.abs(a - b))
|
94 |
>>> vendiscore.compute(samples=samples, k=k)
|
95 |
-
{
|
96 |
```
|
97 |
|
98 |
If you already have precomputed a similarity matrix:
|
99 |
```
|
|
|
100 |
>>> K = np.array([[1.0, 0.9, 0.0],
|
101 |
[0.9, 1.0, 0.0],
|
102 |
[0.0, 0.0, 1.0]])
|
103 |
>>> vendiscore.compute(samples=K, score_K=True)
|
104 |
-
2.1573
|
105 |
```
|
106 |
|
107 |
If your similarity function is a dot product between `n` normalized
|
@@ -109,9 +110,10 @@ If your similarity function is a dot product between `n` normalized
|
|
109 |
to compute the Vendi Score using the covariance matrix, `X @ X.T`.
|
110 |
(If the rows of `X` are not normalized, set `normalize = True`.)
|
111 |
```
|
112 |
-
>>>
|
|
|
113 |
>>> vendiscore.compute(samples=X, score_dual=True, normalize=True)
|
114 |
-
1.
|
115 |
```
|
116 |
|
117 |
Image similarity can be calculated using inner products between pixel vectors or between embeddings from a neural network.
|
@@ -137,16 +139,12 @@ The default embeddings are from the pool-2048 layer of the torchvision version o
|
|
137 |
|
138 |
Text similarity can be calculated using n-gram overlap or using inner products between embeddings from a neural network.
|
139 |
```
|
140 |
-
>>>
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
>>> bert_vs = vendiscore.compute(samples=sents, k="text_embeddings", model_path="bert-base-uncased")
|
147 |
-
>>> simcse_vs = vendiscore.compute(samples=sents, k="text_embeddings", model_path="princeton-nlp/unsup-simcse-bert-base-uncased")
|
148 |
-
>>> print(f"N-grams: {ngram_vs:.02f}, BERT: {bert_vs:.02f}, SimCSE: {simcse_vs:.02f})
|
149 |
-
N-grams: 3.91, BERT: 1.21, SimCSE: 2.81
|
150 |
```
|
151 |
|
152 |
## Limitations and Bias
|
|
|
92 |
>>> samples = [0, 0, 10, 10, 20, 20]
|
93 |
>>> k = lambda a, b: np.exp(-np.abs(a - b))
|
94 |
>>> vendiscore.compute(samples=samples, k=k)
|
95 |
+
{'VS': 2.9999...}
|
96 |
```
|
97 |
|
98 |
If you already have precomputed a similarity matrix:
|
99 |
```
|
100 |
+
>>> vendiscore = evaluate.load("danf0/vendiscore", "K")
|
101 |
>>> K = np.array([[1.0, 0.9, 0.0],
|
102 |
[0.9, 1.0, 0.0],
|
103 |
[0.0, 0.0, 1.0]])
|
104 |
>>> vendiscore.compute(samples=K, score_K=True)
|
105 |
+
{'VS': 2.1573...}
|
106 |
```
|
107 |
|
108 |
If your similarity function is a dot product between `n` normalized
|
|
|
110 |
to compute the Vendi Score using the covariance matrix, `X @ X.T`.
|
111 |
(If the rows of `X` are not normalized, set `normalize = True`.)
|
112 |
```
|
113 |
+
>>> vendiscore = evaluate.load("danf0/vendiscore", "X")
|
114 |
+
>>> X = np.array([[100, 0], [99, 1], [1, 99], [0, 100]])
|
115 |
>>> vendiscore.compute(samples=X, score_dual=True, normalize=True)
|
116 |
+
{'VS': 1.99989...}
|
117 |
```
|
118 |
|
119 |
Image similarity can be calculated using inner products between pixel vectors or between embeddings from a neural network.
|
|
|
139 |
|
140 |
Text similarity can be calculated using n-gram overlap or using inner products between embeddings from a neural network.
|
141 |
```
|
142 |
+
>>> vendiscore = evaluate.load("danf0/vendiscore", "text")
|
143 |
+
>>> sents = ["Look, Jane.", "See Spot.", "See Spot run.", "Run, Spot, run.", "Jane sees Spot run."]
|
144 |
+
>>> ngram_vs = vendiscore.compute(samples=sents, k="ngram_overlap", ns=[1, 2])["VS"]
|
145 |
+
>>> bert_vs = vendiscore.compute(samples=sents, k="text_embeddings", model_path="bert-base-uncased")["VS"]
|
146 |
+
>>> print(f"N-grams: {ngram_vs:.02f}, BERT: {bert_vs:.02f}")
|
147 |
+
N-grams: 3.91, BERT: 1.21
|
|
|
|
|
|
|
|
|
148 |
```
|
149 |
|
150 |
## Limitations and Bias
|
vendiscore.py
CHANGED
@@ -14,6 +14,8 @@
|
|
14 |
import evaluate
|
15 |
import datasets
|
16 |
import numpy as np
|
|
|
|
|
17 |
|
18 |
from vendi_score import vendi, image_utils, text_utils
|
19 |
|
@@ -69,24 +71,11 @@ Examples:
|
|
69 |
"""
|
70 |
|
71 |
|
72 |
-
def get_dtype(config_name):
|
73 |
-
if config_name == "text":
|
74 |
-
return datasets.Features({"samples": datasets.Value("string")})
|
75 |
-
if config_name == "image":
|
76 |
-
return datasets.Features({"samples": datasets.Image})
|
77 |
-
elif config_name in ("X", "K"):
|
78 |
-
return datasets.Array2D
|
79 |
-
elif config_name == "default":
|
80 |
-
return datasets.Value("string")
|
81 |
-
else:
|
82 |
-
return datasets.Value(config_name)
|
83 |
-
|
84 |
-
|
85 |
def get_features(config_name):
|
86 |
if config_name in ("text", "default"):
|
87 |
return datasets.Features({"samples": datasets.Value("string")})
|
88 |
if config_name == "image":
|
89 |
-
return datasets.Features({"samples": datasets.
|
90 |
if config_name in ("K", "X"):
|
91 |
return [
|
92 |
datasets.Features(
|
@@ -164,10 +153,12 @@ class VendiScore(evaluate.Metric):
|
|
164 |
model_path=model_path,
|
165 |
)
|
166 |
elif type(k) == str and k == "pixels":
|
167 |
-
vs = image_utils.pixel_vendi_score(
|
|
|
|
|
168 |
elif type(k) == str and k == "image_embeddings":
|
169 |
vs = image_utils.embedding_vendi_score(
|
170 |
-
samples,
|
171 |
batch_size=batch_size,
|
172 |
device=device,
|
173 |
model=model,
|
|
|
14 |
import evaluate
|
15 |
import datasets
|
16 |
import numpy as np
|
17 |
+
import PIL
|
18 |
+
from PIL import Image
|
19 |
|
20 |
from vendi_score import vendi, image_utils, text_utils
|
21 |
|
|
|
71 |
"""
|
72 |
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
def get_features(config_name):
|
75 |
if config_name in ("text", "default"):
|
76 |
return datasets.Features({"samples": datasets.Value("string")})
|
77 |
if config_name == "image":
|
78 |
+
return datasets.Features({"samples": datasets.Array3D})
|
79 |
if config_name in ("K", "X"):
|
80 |
return [
|
81 |
datasets.Features(
|
|
|
153 |
model_path=model_path,
|
154 |
)
|
155 |
elif type(k) == str and k == "pixels":
|
156 |
+
vs = image_utils.pixel_vendi_score(
|
157 |
+
[Image.fromarray(x) for x in samples]
|
158 |
+
)
|
159 |
elif type(k) == str and k == "image_embeddings":
|
160 |
vs = image_utils.embedding_vendi_score(
|
161 |
+
[Image.fromarray(x) for x in samples],
|
162 |
batch_size=batch_size,
|
163 |
device=device,
|
164 |
model=model,
|