Update model to latest code (#6)
Browse files- Update return types (101e5336b8ef0f7d11a9377bde632bc50c9a8b91)
- Remove unused transform (aa4749c4e268dd893a359c201ea8b7a401a3eebd)
- Sync model code with repo code (0dc766bbae9eb38e1bdaf2b97a1615b54386abf1)
- Remove unused code (f3261e5b29172da8b5c983f869b3c894dd65d516)
- Update README (e8c0181afd4a9db194a21a9bbd3b49e0197b58f3)
- README.md +11 -12
- config.json +1 -0
- configuration_cased.py +8 -3
- modeling_cased.py +99 -199
- retrieval_cased.py +278 -0
- transforms_cased.py +22 -82
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
pipeline_tag: image-classification
|
3 |
tags:
|
4 |
-
- vision
|
5 |
inference: false
|
6 |
widget:
|
7 |
-
- src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/cat-dog-music.png
|
8 |
-
|
9 |
---
|
|
|
10 |
# Category Search from External Databases (CaSED)
|
11 |
|
12 |
Disclaimer: The model card is taken and modified from the official repository, which can be found [here](https://github.com/altndrr/vic). The paper can be found [here](https://arxiv.org/abs/2306.00917).
|
@@ -34,11 +35,11 @@ processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
|
34 |
|
35 |
# get the model outputs
|
36 |
images = processor(images=[image], return_tensors="pt", padding=True)
|
37 |
-
outputs = model(images, alpha=0.
|
38 |
labels, scores = outputs["vocabularies"][0], outputs["scores"][0]
|
39 |
|
40 |
# print the top 5 most likely labels for the image
|
41 |
-
values, indices = scores.
|
42 |
print("\nTop predictions:\n")
|
43 |
for value, index in zip(values, indices):
|
44 |
print(f"{labels[index]:>16s}: {100 * value.item():.2f}%")
|
@@ -47,18 +48,16 @@ for value, index in zip(values, indices):
|
|
47 |
The model depends on some libraries you have to install manually before execution:
|
48 |
|
49 |
```bash
|
50 |
-
pip install torch faiss-cpu flair inflect nltk transformers
|
51 |
```
|
52 |
|
53 |
## Citation
|
54 |
|
55 |
```latex
|
56 |
-
@
|
57 |
-
title={Vocabulary-free Image Classification},
|
58 |
author={Alessandro Conti and Enrico Fini and Massimiliano Mancini and Paolo Rota and Yiming Wang and Elisa Ricci},
|
59 |
year={2023},
|
60 |
-
|
61 |
-
archivePrefix={arXiv},
|
62 |
-
primaryClass={cs.CV}
|
63 |
}
|
64 |
-
```
|
|
|
1 |
---
|
2 |
pipeline_tag: image-classification
|
3 |
tags:
|
4 |
+
- vision
|
5 |
inference: false
|
6 |
widget:
|
7 |
+
- src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/cat-dog-music.png
|
8 |
+
example_title: Cat & Dog
|
9 |
---
|
10 |
+
|
11 |
# Category Search from External Databases (CaSED)
|
12 |
|
13 |
Disclaimer: The model card is taken and modified from the official repository, which can be found [here](https://github.com/altndrr/vic). The paper can be found [here](https://arxiv.org/abs/2306.00917).
|
|
|
35 |
|
36 |
# get the model outputs
|
37 |
images = processor(images=[image], return_tensors="pt", padding=True)
|
38 |
+
outputs = model(images, alpha=0.7)
|
39 |
labels, scores = outputs["vocabularies"][0], outputs["scores"][0]
|
40 |
|
41 |
# print the top 5 most likely labels for the image
|
42 |
+
values, indices = scores.sort(dim=-1, descending=True)
|
43 |
print("\nTop predictions:\n")
|
44 |
for value, index in zip(values, indices):
|
45 |
print(f"{labels[index]:>16s}: {100 * value.item():.2f}%")
|
|
|
48 |
The model depends on some libraries you have to install manually before execution:
|
49 |
|
50 |
```bash
|
51 |
+
pip install torch faiss-cpu flair inflect nltk pyarrow transformers
|
52 |
```
|
53 |
|
54 |
## Citation
|
55 |
|
56 |
```latex
|
57 |
+
@article{conti2023vocabularyfree,
|
58 |
+
title={Vocabulary-free Image Classification},
|
59 |
author={Alessandro Conti and Enrico Fini and Massimiliano Mancini and Paolo Rota and Yiming Wang and Elisa Ricci},
|
60 |
year={2023},
|
61 |
+
journal={NeurIPS},
|
|
|
|
|
62 |
}
|
63 |
+
```
|
config.json
CHANGED
@@ -7,6 +7,7 @@
|
|
7 |
"AutoConfig": "configuration_cased.CaSEDConfig",
|
8 |
"AutoModel": "modeling_cased.CaSEDModel"
|
9 |
},
|
|
|
10 |
"index_name": "cc12m",
|
11 |
"model_type": "cased",
|
12 |
"retrieval_num_results": 10,
|
|
|
7 |
"AutoConfig": "configuration_cased.CaSEDConfig",
|
8 |
"AutoModel": "modeling_cased.CaSEDModel"
|
9 |
},
|
10 |
+
"cache_dir": "~/.cache/cased",
|
11 |
"index_name": "cc12m",
|
12 |
"model_type": "cased",
|
13 |
"retrieval_num_results": 10,
|
configuration_cased.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
from transformers.modeling_utils import PretrainedConfig
|
2 |
|
3 |
|
@@ -5,9 +7,10 @@ class CaSEDConfig(PretrainedConfig):
|
|
5 |
"""Configuration class for CaSED.
|
6 |
|
7 |
Args:
|
8 |
-
index_name (str
|
9 |
-
alpha (float
|
10 |
-
retrieval_num_results (int
|
|
|
11 |
"""
|
12 |
|
13 |
model_type = "cased"
|
@@ -18,9 +21,11 @@ class CaSEDConfig(PretrainedConfig):
|
|
18 |
index_name: str = "cc12m",
|
19 |
alpha: float = 0.5,
|
20 |
retrieval_num_results: int = 10,
|
|
|
21 |
**kwargs,
|
22 |
):
|
23 |
super().__init__(**kwargs)
|
24 |
self.index_name = index_name
|
25 |
self.alpha = alpha
|
26 |
self.retrieval_num_results = retrieval_num_results
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
from transformers.modeling_utils import PretrainedConfig
|
4 |
|
5 |
|
|
|
7 |
"""Configuration class for CaSED.
|
8 |
|
9 |
Args:
|
10 |
+
index_name (str): Name of the index. Defaults to "cc12m".
|
11 |
+
alpha (float): Weight of the vision loss. Defaults to 0.5.
|
12 |
+
retrieval_num_results (int): Number of results to return. Defaults to 10.
|
13 |
+
cache_dir (str): Path to cache directory. Defaults to "~/.cache/cased".
|
14 |
"""
|
15 |
|
16 |
model_type = "cased"
|
|
|
21 |
index_name: str = "cc12m",
|
22 |
alpha: float = 0.5,
|
23 |
retrieval_num_results: int = 10,
|
24 |
+
cache_dir: str = os.path.expanduser("~/.cache/cased"),
|
25 |
**kwargs,
|
26 |
):
|
27 |
super().__init__(**kwargs)
|
28 |
self.index_name = index_name
|
29 |
self.alpha = alpha
|
30 |
self.retrieval_num_results = retrieval_num_results
|
31 |
+
self.cache_dir = cache_dir
|
modeling_cased.py
CHANGED
@@ -1,66 +1,21 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
from pathlib import Path
|
4 |
-
from typing import Optional
|
5 |
|
6 |
-
import faiss
|
7 |
import numpy as np
|
8 |
-
import pyarrow as pa
|
9 |
-
import requests
|
10 |
import torch
|
11 |
-
from tqdm import tqdm
|
12 |
from transformers import CLIPModel, CLIPProcessor
|
13 |
from transformers.modeling_utils import PreTrainedModel
|
14 |
|
15 |
from .configuration_cased import CaSEDConfig
|
|
|
16 |
from .transforms_cased import default_vocabulary_transforms
|
17 |
|
18 |
-
DATABASES = {
|
19 |
-
"cc12m": {
|
20 |
-
"url": "https://storage-cased.alessandroconti.me/cc12m.tar.gz",
|
21 |
-
"cache_subdir": "./cc12m/vit-l-14/",
|
22 |
-
},
|
23 |
-
}
|
24 |
-
|
25 |
-
|
26 |
-
class MetadataProvider:
|
27 |
-
"""Metadata provider.
|
28 |
-
|
29 |
-
It uses arrow files to store metadata and retrieve it efficiently.
|
30 |
-
|
31 |
-
Code reference:
|
32 |
-
- https://github.dev/rom1504/clip-retrieval
|
33 |
-
"""
|
34 |
-
|
35 |
-
def __init__(self, arrow_folder: Path):
|
36 |
-
arrow_files = [str(a) for a in sorted(arrow_folder.glob("**/*")) if a.is_file()]
|
37 |
-
self.table = pa.concat_tables(
|
38 |
-
[
|
39 |
-
pa.ipc.RecordBatchFileReader(pa.memory_map(arrow_file, "r")).read_all()
|
40 |
-
for arrow_file in arrow_files
|
41 |
-
]
|
42 |
-
)
|
43 |
-
|
44 |
-
def get(self, ids: np.ndarray, cols: Optional[list] = None):
|
45 |
-
"""Get arrow metadata from ids.
|
46 |
-
|
47 |
-
Args:
|
48 |
-
ids (np.ndarray): Ids to retrieve.
|
49 |
-
cols (Optional[list], optional): Columns to retrieve. Defaults to None.
|
50 |
-
"""
|
51 |
-
if cols is None:
|
52 |
-
cols = self.table.schema.names
|
53 |
-
else:
|
54 |
-
cols = list(set(self.table.schema.names) & set(cols))
|
55 |
-
t = pa.concat_tables([self.table[i:j] for i, j in zip(ids, ids + 1)])
|
56 |
-
return t.select(cols).to_pandas().to_dict("records")
|
57 |
-
|
58 |
|
59 |
class CaSEDModel(PreTrainedModel):
|
60 |
"""Transformers module for Category Search from External Databases (CaSED).
|
61 |
|
62 |
Reference:
|
63 |
-
- Conti et al. Vocabulary-free Image Classification.
|
64 |
|
65 |
Args:
|
66 |
config (CaSEDConfig): Configuration class for CaSED.
|
@@ -80,125 +35,60 @@ class CaSEDModel(PreTrainedModel):
|
|
80 |
self.logit_scale = model.logit_scale.exp()
|
81 |
self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
82 |
|
83 |
-
# load transforms
|
84 |
-
self.vocabulary_transforms = default_vocabulary_transforms()
|
85 |
-
|
86 |
# set hparams
|
87 |
self.hparams = {}
|
88 |
self.hparams["alpha"] = config.alpha
|
89 |
self.hparams["index_name"] = config.index_name
|
90 |
self.hparams["retrieval_num_results"] = config.retrieval_num_results
|
|
|
91 |
|
92 |
-
#
|
93 |
-
self.hparams["cache_dir"] = Path(os.path.expanduser("~/.cache/cased"))
|
94 |
os.makedirs(self.hparams["cache_dir"], exist_ok=True)
|
95 |
|
96 |
-
# download
|
97 |
-
self.
|
98 |
-
|
99 |
-
#
|
100 |
-
self.
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
"""Download data if needed."""
|
120 |
-
databases_path = Path(self.hparams["cache_dir"]) / "databases"
|
121 |
-
|
122 |
-
for name, items in DATABASES.items():
|
123 |
-
url = items["url"]
|
124 |
-
database_path = Path(databases_path, name)
|
125 |
-
if database_path.exists():
|
126 |
-
continue
|
127 |
-
|
128 |
-
# download data
|
129 |
-
target_path = Path(databases_path, name + ".tar.gz")
|
130 |
-
os.makedirs(target_path.parent, exist_ok=True)
|
131 |
-
with requests.get(url, stream=True) as r:
|
132 |
-
r.raise_for_status()
|
133 |
-
total_bytes_size = int(r.headers.get('content-length', 0))
|
134 |
-
chunk_size = 8192
|
135 |
-
p_bar = tqdm(
|
136 |
-
desc="Downloading cc12m index",
|
137 |
-
total=total_bytes_size,
|
138 |
-
unit='iB',
|
139 |
-
unit_scale=True,
|
140 |
-
)
|
141 |
-
with open(target_path, 'wb') as f:
|
142 |
-
for chunk in r.iter_content(chunk_size=chunk_size):
|
143 |
-
f.write(chunk)
|
144 |
-
p_bar.update(len(chunk))
|
145 |
-
p_bar.close()
|
146 |
-
|
147 |
-
# extract data
|
148 |
-
tar = tarfile.open(target_path, "r:gz")
|
149 |
-
tar.extractall(target_path.parent)
|
150 |
-
tar.close()
|
151 |
-
target_path.unlink()
|
152 |
-
|
153 |
-
@torch.no_grad()
|
154 |
-
def query_index(self, sample_z: torch.Tensor) -> torch.Tensor:
|
155 |
-
"""Query the external database index.
|
156 |
|
157 |
Args:
|
158 |
-
|
159 |
"""
|
160 |
-
|
161 |
-
resources = self.resources[self.hparams["index_name"]]
|
162 |
-
text_index = resources["text_index"]
|
163 |
-
metadata_provider = resources["metadata_provider"]
|
164 |
-
|
165 |
-
# query the index
|
166 |
-
sample_z = sample_z.squeeze(0)
|
167 |
-
sample_z = sample_z / sample_z.norm(dim=-1, keepdim=True)
|
168 |
-
query_input = sample_z.cpu().detach().numpy().tolist()
|
169 |
-
query = np.expand_dims(np.array(query_input).astype("float32"), 0)
|
170 |
-
|
171 |
-
distances, idxs, _ = text_index.search_and_reconstruct(
|
172 |
-
query, self.hparams["retrieval_num_results"]
|
173 |
-
)
|
174 |
-
results = idxs[0]
|
175 |
-
nb_results = np.where(results == -1)[0]
|
176 |
-
nb_results = nb_results[0] if len(nb_results) > 0 else len(results)
|
177 |
-
indices = results[:nb_results]
|
178 |
-
distances = distances[0][:nb_results]
|
179 |
-
|
180 |
-
if len(distances) == 0:
|
181 |
-
return []
|
182 |
-
|
183 |
-
# get the metadata
|
184 |
-
results = []
|
185 |
-
metadata = metadata_provider.get(indices[:20], ["caption"])
|
186 |
-
for key, (d, i) in enumerate(zip(distances, indices)):
|
187 |
-
output = {}
|
188 |
-
meta = None if key + 1 > len(metadata) else metadata[key]
|
189 |
-
if meta is not None:
|
190 |
-
output.update(meta)
|
191 |
-
output["id"] = i.item()
|
192 |
-
output["similarity"] = d.item()
|
193 |
-
results.append(output)
|
194 |
-
|
195 |
-
# get the captions only
|
196 |
-
vocabularies = [result["caption"] for result in results]
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
return vocabularies
|
199 |
|
200 |
-
|
201 |
-
def forward(self, images: dict, alpha: Optional[float] = None) -> torch.Tensor():
|
202 |
"""Forward pass.
|
203 |
|
204 |
Args:
|
@@ -206,52 +96,62 @@ class CaSEDModel(PreTrainedModel):
|
|
206 |
- pixel_values (torch.Tensor): Pixel values of the images.
|
207 |
alpha (Optional[float]): Alpha value for the interpolation.
|
208 |
"""
|
|
|
|
|
209 |
# forward the images
|
210 |
images["pixel_values"] = images["pixel_values"].to(self.device)
|
211 |
images_z = self.vision_proj(self.vision_encoder(**images)[1])
|
212 |
-
|
213 |
-
vocabularies
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
#
|
255 |
-
|
256 |
-
|
257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from typing import Callable, Optional
|
|
|
|
|
3 |
|
|
|
4 |
import numpy as np
|
|
|
|
|
5 |
import torch
|
|
|
6 |
from transformers import CLIPModel, CLIPProcessor
|
7 |
from transformers.modeling_utils import PreTrainedModel
|
8 |
|
9 |
from .configuration_cased import CaSEDConfig
|
10 |
+
from .retrieval_cased import RetrievalDatabase, download_retrieval_databases
|
11 |
from .transforms_cased import default_vocabulary_transforms
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
class CaSEDModel(PreTrainedModel):
|
15 |
"""Transformers module for Category Search from External Databases (CaSED).
|
16 |
|
17 |
Reference:
|
18 |
+
- Conti et al. Vocabulary-free Image Classification. NeurIPS 2023.
|
19 |
|
20 |
Args:
|
21 |
config (CaSEDConfig): Configuration class for CaSED.
|
|
|
35 |
self.logit_scale = model.logit_scale.exp()
|
36 |
self.processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
|
37 |
|
|
|
|
|
|
|
38 |
# set hparams
|
39 |
self.hparams = {}
|
40 |
self.hparams["alpha"] = config.alpha
|
41 |
self.hparams["index_name"] = config.index_name
|
42 |
self.hparams["retrieval_num_results"] = config.retrieval_num_results
|
43 |
+
self.hparams["cache_dir"] = config.cache_dir
|
44 |
|
45 |
+
# create cache dir
|
|
|
46 |
os.makedirs(self.hparams["cache_dir"], exist_ok=True)
|
47 |
|
48 |
+
# download data
|
49 |
+
download_retrieval_databases(cache_dir=self.hparams["cache_dir"])
|
50 |
+
|
51 |
+
# setup vocabulary
|
52 |
+
self.vocabulary = RetrievalDatabase("cc12m", self.hparams["cache_dir"])
|
53 |
+
self._vocab_transform = default_vocabulary_transforms()
|
54 |
+
|
55 |
+
@property
|
56 |
+
def vocab_transform(self) -> Callable:
|
57 |
+
"""Get image preprocess transform.
|
58 |
+
|
59 |
+
The getter wraps the transform in a map_reduce function and applies it to a list of images.
|
60 |
+
If interested in the transform itself, use `self._vocab_transform`.
|
61 |
+
"""
|
62 |
+
vocab_transform = self._vocab_transform
|
63 |
+
|
64 |
+
def vocabs_transforms(texts: list[str]) -> list[torch.Tensor]:
|
65 |
+
return [vocab_transform(text) for text in texts]
|
66 |
+
|
67 |
+
return vocabs_transforms
|
68 |
+
|
69 |
+
def get_vocabulary(self, images_z: Optional[torch.Tensor] = None) -> list[list[str]]:
|
70 |
+
"""Get the vocabulary for a batch of images.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
Args:
|
73 |
+
images_z (torch.Tensor): Batch of image embeddings.
|
74 |
"""
|
75 |
+
num_samples = self.hparams["retrieval_num_results"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
assert images_z is not None
|
78 |
+
|
79 |
+
images_z = images_z / images_z.norm(dim=-1, keepdim=True)
|
80 |
+
images_z = images_z.cpu().detach().numpy().tolist()
|
81 |
+
|
82 |
+
if isinstance(images_z[0], float):
|
83 |
+
images_z = [images_z]
|
84 |
+
|
85 |
+
query = np.matrix(images_z).astype("float32")
|
86 |
+
results = self.vocabulary.query(query, modality="text", num_samples=num_samples)
|
87 |
+
|
88 |
+
vocabularies = [[r["caption"] for r in result] for result in results]
|
89 |
return vocabularies
|
90 |
|
91 |
+
def forward(self, images: dict, alpha: Optional[float] = None) -> torch.Tensor:
|
|
|
92 |
"""Forward pass.
|
93 |
|
94 |
Args:
|
|
|
96 |
- pixel_values (torch.Tensor): Pixel values of the images.
|
97 |
alpha (Optional[float]): Alpha value for the interpolation.
|
98 |
"""
|
99 |
+
alpha = alpha or self.hparams["alpha"]
|
100 |
+
|
101 |
# forward the images
|
102 |
images["pixel_values"] = images["pixel_values"].to(self.device)
|
103 |
images_z = self.vision_proj(self.vision_encoder(**images)[1])
|
104 |
+
images_z = images_z / images_z.norm(dim=-1, keepdim=True)
|
105 |
+
vocabularies = self.get_vocabulary(images_z=images_z)
|
106 |
+
|
107 |
+
# encode unfiltered words
|
108 |
+
unfiltered_words = sum(vocabularies, [])
|
109 |
+
texts_z = self.processor(unfiltered_words, return_tensors="pt", padding=True)
|
110 |
+
texts_z["input_ids"] = texts_z["input_ids"][:, :77].to(self.device)
|
111 |
+
texts_z["attention_mask"] = texts_z["attention_mask"][:, :77].to(self.device)
|
112 |
+
texts_z = self.language_encoder(**texts_z)[1]
|
113 |
+
texts_z = self.language_proj(texts_z)
|
114 |
+
texts_z = texts_z / texts_z.norm(dim=-1, keepdim=True)
|
115 |
+
|
116 |
+
# generate a text embedding for each image from their unfiltered words
|
117 |
+
unfiltered_words_per_image = [len(vocab) for vocab in vocabularies]
|
118 |
+
texts_z = torch.split(texts_z, unfiltered_words_per_image)
|
119 |
+
texts_z = torch.stack([text_z.mean(dim=0) for text_z in texts_z])
|
120 |
+
texts_z = texts_z / texts_z.norm(dim=-1, keepdim=True)
|
121 |
+
|
122 |
+
# filter the words and embed them
|
123 |
+
vocabularies = self.vocab_transform(vocabularies)
|
124 |
+
vocabularies = [vocab or ["object"] for vocab in vocabularies]
|
125 |
+
words = sum(vocabularies, [])
|
126 |
+
words_z = self.processor(words, return_tensors="pt", padding=True)
|
127 |
+
words_z = {k: v.to(self.device) for k, v in words_z.items()}
|
128 |
+
words_z = self.language_encoder(**words_z)[1]
|
129 |
+
words_z = self.language_proj(words_z)
|
130 |
+
words_z = words_z / words_z.norm(dim=-1, keepdim=True)
|
131 |
+
|
132 |
+
# create a one-hot relation mask between images and words
|
133 |
+
words_per_image = [len(vocab) for vocab in vocabularies]
|
134 |
+
col_indices = torch.arange(sum(words_per_image))
|
135 |
+
row_indices = torch.arange(len(images_z)).repeat_interleave(torch.tensor(words_per_image))
|
136 |
+
mask = torch.zeros(len(images_z), sum(words_per_image), device=self.device)
|
137 |
+
mask[row_indices, col_indices] = 1
|
138 |
+
|
139 |
+
# get the image and text similarities
|
140 |
+
images_z = images_z / images_z.norm(dim=-1, keepdim=True)
|
141 |
+
texts_z = texts_z / texts_z.norm(dim=-1, keepdim=True)
|
142 |
+
words_z = words_z / words_z.norm(dim=-1, keepdim=True)
|
143 |
+
images_sim = self.logit_scale * images_z @ words_z.T
|
144 |
+
texts_sim = self.logit_scale * texts_z @ words_z.T
|
145 |
+
|
146 |
+
# mask unrelated words
|
147 |
+
images_sim = torch.masked_fill(images_sim, mask == 0, float("-inf"))
|
148 |
+
texts_sim = torch.masked_fill(texts_sim, mask == 0, float("-inf"))
|
149 |
+
|
150 |
+
# get the image and text predictions
|
151 |
+
images_p = images_sim.softmax(dim=-1)
|
152 |
+
texts_p = texts_sim.softmax(dim=-1)
|
153 |
+
|
154 |
+
# average the image and text predictions
|
155 |
+
samples_p = alpha * images_p + (1 - alpha) * texts_p
|
156 |
+
|
157 |
+
return {"scores": samples_p, "words": words, "vocabularies": vocabularies}
|
retrieval_cased.py
ADDED
@@ -0,0 +1,278 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tarfile
|
2 |
+
from collections import defaultdict
|
3 |
+
from pathlib import Path
|
4 |
+
|
5 |
+
import faiss
|
6 |
+
import numpy as np
|
7 |
+
import pyarrow as pa
|
8 |
+
import requests
|
9 |
+
from tqdm import tqdm
|
10 |
+
|
11 |
+
__all__ = ["RetrievalDatabase", "download_retrieval_databases"]
|
12 |
+
|
13 |
+
RETRIEVAL_DATABASES_URLS = {
|
14 |
+
"cc12m": {
|
15 |
+
"url": "https://storage-cased.alessandroconti.me/cc12m.tar.gz",
|
16 |
+
"cache_subdir": "./cc12m/vit-l-14/",
|
17 |
+
},
|
18 |
+
}
|
19 |
+
|
20 |
+
|
21 |
+
def download_retrieval_databases(cache_dir: str = "~/.cache/cased"):
|
22 |
+
"""Download data if needed.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
cache_dir (str): Path to cache directory. Defaults to "~/.cache/cased".
|
26 |
+
"""
|
27 |
+
databases_path = Path(cache_dir, "databases")
|
28 |
+
|
29 |
+
for name, items in RETRIEVAL_DATABASES_URLS.items():
|
30 |
+
url = items["url"]
|
31 |
+
database_path = Path(databases_path, name)
|
32 |
+
if database_path.exists():
|
33 |
+
continue
|
34 |
+
|
35 |
+
# download data
|
36 |
+
target_path = Path(databases_path, name + ".tar.gz")
|
37 |
+
target_path.parent.mkdir(parents=True, exist_ok=True)
|
38 |
+
with requests.get(url, stream=True) as r:
|
39 |
+
r.raise_for_status()
|
40 |
+
total_bytes_size = int(r.headers.get("content-length", 0))
|
41 |
+
chunk_size = 8192
|
42 |
+
p_bar = tqdm(
|
43 |
+
desc="Downloading cc12m index",
|
44 |
+
total=total_bytes_size,
|
45 |
+
unit="iB",
|
46 |
+
unit_scale=True,
|
47 |
+
)
|
48 |
+
with open(target_path, "wb") as f:
|
49 |
+
for chunk in r.iter_content(chunk_size=chunk_size):
|
50 |
+
f.write(chunk)
|
51 |
+
p_bar.update(len(chunk))
|
52 |
+
p_bar.close()
|
53 |
+
|
54 |
+
# extract data
|
55 |
+
tar = tarfile.open(target_path, "r:gz")
|
56 |
+
tar.extractall(target_path.parent)
|
57 |
+
tar.close()
|
58 |
+
target_path.unlink()
|
59 |
+
|
60 |
+
|
61 |
+
class RetrievalDatabaseMetadataProvider:
|
62 |
+
"""Metadata provider for the retrieval database.
|
63 |
+
|
64 |
+
Args:
|
65 |
+
metadata_dir (str): Path to the metadata directory.
|
66 |
+
"""
|
67 |
+
|
68 |
+
def __init__(self, metadata_dir: str):
|
69 |
+
metadatas = [str(a) for a in sorted(Path(metadata_dir).glob("**/*")) if a.is_file()]
|
70 |
+
self.table = pa.concat_tables(
|
71 |
+
[
|
72 |
+
pa.ipc.RecordBatchFileReader(pa.memory_map(metadata, "r")).read_all()
|
73 |
+
for metadata in metadatas
|
74 |
+
]
|
75 |
+
)
|
76 |
+
|
77 |
+
def get(self, ids):
|
78 |
+
"""Get the metadata for the given ids.
|
79 |
+
|
80 |
+
Args:
|
81 |
+
ids (list): List of ids.
|
82 |
+
"""
|
83 |
+
columns = self.table.schema.names
|
84 |
+
end_ids = [i + 1 for i in ids]
|
85 |
+
t = pa.concat_tables([self.table[start:end] for start, end in zip(ids, end_ids)])
|
86 |
+
return t.select(columns).to_pandas().to_dict("records")
|
87 |
+
|
88 |
+
|
89 |
+
class RetrievalDatabase:
|
90 |
+
"""Retrieval database.
|
91 |
+
|
92 |
+
Args:
|
93 |
+
database_name (str): Name of the database.
|
94 |
+
cache_dir (str): Path to cache directory. Defaults to "~/.cache/cased".
|
95 |
+
"""
|
96 |
+
|
97 |
+
def __init__(self, database_name: str, cache_dir: str = "~/.cache/cased"):
|
98 |
+
assert database_name in RETRIEVAL_DATABASES_URLS.keys(), (
|
99 |
+
f"Database name should be one of "
|
100 |
+
f"{list(RETRIEVAL_DATABASES_URLS.keys())}, got {database_name}."
|
101 |
+
)
|
102 |
+
|
103 |
+
database_dir = Path(cache_dir) / "databases"
|
104 |
+
database_dir = database_dir / RETRIEVAL_DATABASES_URLS[database_name]["cache_subdir"]
|
105 |
+
self._database_dir = database_dir
|
106 |
+
|
107 |
+
image_index_fp = Path(database_dir) / "image.index"
|
108 |
+
text_index_fp = Path(database_dir) / "text.index"
|
109 |
+
|
110 |
+
image_index = (
|
111 |
+
faiss.read_index(str(image_index_fp), faiss.IO_FLAG_MMAP | faiss.IO_FLAG_READ_ONLY)
|
112 |
+
if image_index_fp.exists()
|
113 |
+
else None
|
114 |
+
)
|
115 |
+
text_index = (
|
116 |
+
faiss.read_index(str(text_index_fp), faiss.IO_FLAG_MMAP | faiss.IO_FLAG_READ_ONLY)
|
117 |
+
if text_index_fp.exists()
|
118 |
+
else None
|
119 |
+
)
|
120 |
+
|
121 |
+
metadata_dir = str(Path(database_dir) / "metadata")
|
122 |
+
metadata_provider = RetrievalDatabaseMetadataProvider(metadata_dir)
|
123 |
+
|
124 |
+
self._image_index = image_index
|
125 |
+
self._text_index = text_index
|
126 |
+
self._metadata_provider = metadata_provider
|
127 |
+
|
128 |
+
def _map_to_metadata(self, indices: list, distances: list, embs: list, num_images: int):
|
129 |
+
"""Map the indices to metadata.
|
130 |
+
|
131 |
+
Args:
|
132 |
+
indices (list): List of indices.
|
133 |
+
distances (list): List of distances.
|
134 |
+
embs (list): List of results embeddings.
|
135 |
+
num_images (int): Number of images.
|
136 |
+
"""
|
137 |
+
results = []
|
138 |
+
metas = self._metadata_provider.get(indices[:num_images])
|
139 |
+
for key, (d, i, emb) in enumerate(zip(distances, indices, embs)):
|
140 |
+
output = {}
|
141 |
+
meta = None if key + 1 > len(metas) else metas[key]
|
142 |
+
if meta is not None:
|
143 |
+
output.update(self._meta_to_dict(meta))
|
144 |
+
output["id"] = i.item()
|
145 |
+
output["similarity"] = d.item()
|
146 |
+
output["sample_z"] = emb.tolist()
|
147 |
+
results.append(output)
|
148 |
+
|
149 |
+
return results
|
150 |
+
|
151 |
+
def _meta_to_dict(self, metadata):
|
152 |
+
"""Convert metadata to dict.
|
153 |
+
|
154 |
+
Args:
|
155 |
+
metadata (dict): Metadata.
|
156 |
+
"""
|
157 |
+
output = {}
|
158 |
+
for k, v in metadata.items():
|
159 |
+
if isinstance(v, bytes):
|
160 |
+
v = v.decode()
|
161 |
+
elif type(v).__module__ == np.__name__:
|
162 |
+
v = v.item()
|
163 |
+
output[k] = v
|
164 |
+
return output
|
165 |
+
|
166 |
+
def _get_connected_components(self, neighbors):
|
167 |
+
"""Find connected components in a graph.
|
168 |
+
|
169 |
+
Args:
|
170 |
+
neighbors (dict): Dictionary of neighbors.
|
171 |
+
"""
|
172 |
+
seen = set()
|
173 |
+
|
174 |
+
def component(node):
|
175 |
+
r = []
|
176 |
+
nodes = {node}
|
177 |
+
while nodes:
|
178 |
+
node = nodes.pop()
|
179 |
+
seen.add(node)
|
180 |
+
nodes |= set(neighbors[node]) - seen
|
181 |
+
r.append(node)
|
182 |
+
return r
|
183 |
+
|
184 |
+
u = []
|
185 |
+
for node in neighbors:
|
186 |
+
if node not in seen:
|
187 |
+
u.append(component(node))
|
188 |
+
return u
|
189 |
+
|
190 |
+
def _deduplicate_embeddings(self, embeddings, threshold=0.94):
|
191 |
+
"""Deduplicate embeddings.
|
192 |
+
|
193 |
+
Args:
|
194 |
+
embeddings (np.matrix): Embeddings to deduplicate.
|
195 |
+
threshold (float): Threshold to use for deduplication. Default is 0.94.
|
196 |
+
"""
|
197 |
+
index = faiss.IndexFlatIP(embeddings.shape[1])
|
198 |
+
index.add(embeddings)
|
199 |
+
l, _, indices = index.range_search(embeddings, threshold)
|
200 |
+
|
201 |
+
same_mapping = defaultdict(list)
|
202 |
+
|
203 |
+
for i in range(embeddings.shape[0]):
|
204 |
+
start = l[i]
|
205 |
+
end = l[i + 1]
|
206 |
+
for j in indices[start:end]:
|
207 |
+
same_mapping[int(i)].append(int(j))
|
208 |
+
|
209 |
+
groups = self._get_connected_components(same_mapping)
|
210 |
+
non_uniques = set()
|
211 |
+
for g in groups:
|
212 |
+
for e in g[1:]:
|
213 |
+
non_uniques.add(e)
|
214 |
+
|
215 |
+
return set(list(non_uniques))
|
216 |
+
|
217 |
+
def query(
|
218 |
+
self, query: np.matrix, modality: str = "text", num_samples: int = 10
|
219 |
+
) -> list[list[dict]]:
|
220 |
+
"""Query the database.
|
221 |
+
|
222 |
+
Args:
|
223 |
+
query (np.matrix): Query to search.
|
224 |
+
modality (str): Modality to search. One of `image` or `text`. Default to `text`.
|
225 |
+
num_samples (int): Number of samples to return. Default is 40.
|
226 |
+
"""
|
227 |
+
index = self._image_index if modality == "image" else self._text_index
|
228 |
+
|
229 |
+
distances, indices, embeddings = index.search_and_reconstruct(query, num_samples)
|
230 |
+
results = [indices[i] for i in range(len(indices))]
|
231 |
+
|
232 |
+
nb_results = [np.where(r == -1)[0] for r in results]
|
233 |
+
total_distances = []
|
234 |
+
total_indices = []
|
235 |
+
total_embeddings = []
|
236 |
+
for i in range(len(results)):
|
237 |
+
num_res = nb_results[i][0] if len(nb_results[i]) > 0 else len(results[i])
|
238 |
+
|
239 |
+
result_indices = results[i][:num_res]
|
240 |
+
result_distances = distances[i][:num_res]
|
241 |
+
result_embeddings = embeddings[i][:num_res]
|
242 |
+
|
243 |
+
# normalise embeddings
|
244 |
+
l2 = np.atleast_1d(np.linalg.norm(result_embeddings, 2, -1))
|
245 |
+
l2[l2 == 0] = 1
|
246 |
+
result_embeddings = result_embeddings / np.expand_dims(l2, -1)
|
247 |
+
|
248 |
+
# deduplicate embeddings
|
249 |
+
local_indices_to_remove = self._deduplicate_embeddings(result_embeddings)
|
250 |
+
indices_to_remove = set()
|
251 |
+
for local_index in local_indices_to_remove:
|
252 |
+
indices_to_remove.add(result_indices[local_index])
|
253 |
+
|
254 |
+
curr_indices = []
|
255 |
+
curr_distances = []
|
256 |
+
curr_embeddings = []
|
257 |
+
for ind, dis, emb in zip(result_indices, result_distances, result_embeddings):
|
258 |
+
if ind not in indices_to_remove:
|
259 |
+
indices_to_remove.add(ind)
|
260 |
+
curr_indices.append(ind)
|
261 |
+
curr_distances.append(dis)
|
262 |
+
curr_embeddings.append(emb)
|
263 |
+
|
264 |
+
total_indices.append(curr_indices)
|
265 |
+
total_distances.append(curr_distances)
|
266 |
+
total_embeddings.append(curr_embeddings)
|
267 |
+
|
268 |
+
if len(total_distances) == 0:
|
269 |
+
return []
|
270 |
+
|
271 |
+
total_results = []
|
272 |
+
for i in range(len(total_distances)):
|
273 |
+
results = self._map_to_metadata(
|
274 |
+
total_indices[i], total_distances[i], total_embeddings[i], num_samples
|
275 |
+
)
|
276 |
+
total_results.append(results)
|
277 |
+
|
278 |
+
return total_results
|
transforms_cased.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import re
|
2 |
from abc import ABC, abstractmethod
|
3 |
-
from typing import
|
4 |
|
5 |
import inflect
|
6 |
import nltk
|
@@ -17,7 +17,6 @@ __all__ = [
|
|
17 |
"DropWords",
|
18 |
"FilterPOS",
|
19 |
"FrequencyMinWordCount",
|
20 |
-
"FrequencyTopK",
|
21 |
"ReplaceSeparators",
|
22 |
"ToLowercase",
|
23 |
"ToSingular",
|
@@ -28,7 +27,7 @@ class BaseTextTransform(ABC):
|
|
28 |
"""Base class for string transforms."""
|
29 |
|
30 |
@abstractmethod
|
31 |
-
def __call__(self, text: str):
|
32 |
raise NotImplementedError
|
33 |
|
34 |
def __repr__(self) -> str:
|
@@ -38,7 +37,7 @@ class BaseTextTransform(ABC):
|
|
38 |
class DropFileExtensions(BaseTextTransform):
|
39 |
"""Remove file extensions from the input text."""
|
40 |
|
41 |
-
def __call__(self, text: str):
|
42 |
"""
|
43 |
Args:
|
44 |
text (str): Text to remove file extensions from.
|
@@ -51,7 +50,7 @@ class DropFileExtensions(BaseTextTransform):
|
|
51 |
class DropNonAlpha(BaseTextTransform):
|
52 |
"""Remove non-alpha words from the input text."""
|
53 |
|
54 |
-
def __call__(self, text: str):
|
55 |
"""
|
56 |
Args:
|
57 |
text (str): Text to remove non-alpha words from.
|
@@ -72,7 +71,7 @@ class DropShortWords(BaseTextTransform):
|
|
72 |
super().__init__()
|
73 |
self.min_length = min_length
|
74 |
|
75 |
-
def __call__(self, text: str):
|
76 |
"""
|
77 |
Args:
|
78 |
text (str): Text to remove short words from.
|
@@ -92,7 +91,7 @@ class DropSpecialCharacters(BaseTextTransform):
|
|
92 |
hyphen, period, apostrophe, or ampersand.
|
93 |
"""
|
94 |
|
95 |
-
def __call__(self, text: str):
|
96 |
"""
|
97 |
Args:
|
98 |
text (str): Text to remove special characters from.
|
@@ -108,7 +107,7 @@ class DropTokens(BaseTextTransform):
|
|
108 |
Tokens are defined as strings enclosed in angle brackets, e.g. <token>.
|
109 |
"""
|
110 |
|
111 |
-
def __call__(self, text: str):
|
112 |
"""
|
113 |
Args:
|
114 |
text (str): Text to remove tokens from.
|
@@ -121,7 +120,7 @@ class DropTokens(BaseTextTransform):
|
|
121 |
class DropURLs(BaseTextTransform):
|
122 |
"""Remove URLs from the input text."""
|
123 |
|
124 |
-
def __call__(self, text: str):
|
125 |
"""
|
126 |
Args:
|
127 |
text (str): Text to remove URLs from.
|
@@ -142,7 +141,7 @@ class DropWords(BaseTextTransform):
|
|
142 |
self.words = words
|
143 |
self.pattern = r"\b(?:{})\b".format("|".join(words))
|
144 |
|
145 |
-
def __call__(self, text: str):
|
146 |
"""
|
147 |
Args:
|
148 |
text (str): Text to remove words from.
|
@@ -161,14 +160,12 @@ class FilterPOS(BaseTextTransform):
|
|
161 |
Args:
|
162 |
tags (list): List of POS tags to remove.
|
163 |
engine (str): POS tagger to use. Must be one of "nltk" or "flair". Defaults to "nltk".
|
164 |
-
keep_compound_nouns (bool): Whether to keep composed words. Defaults to True.
|
165 |
"""
|
166 |
|
167 |
-
def __init__(self, tags: list, engine: str = "nltk"
|
168 |
super().__init__()
|
169 |
self.tags = tags
|
170 |
self.engine = engine
|
171 |
-
self.keep_compound_nouns = keep_compound_nouns
|
172 |
|
173 |
if engine == "nltk":
|
174 |
nltk.download("averaged_perceptron_tagger", quiet=True)
|
@@ -177,7 +174,7 @@ class FilterPOS(BaseTextTransform):
|
|
177 |
elif engine == "flair":
|
178 |
self.tagger = SequenceTagger.load("flair/pos-english-fast").predict
|
179 |
|
180 |
-
def __call__(self, text: str):
|
181 |
"""
|
182 |
Args:
|
183 |
text (str): Text to remove words with specific POS tags from.
|
@@ -190,30 +187,6 @@ class FilterPOS(BaseTextTransform):
|
|
190 |
self.tagger(sentence)
|
191 |
text = " ".join([token.text for token in sentence.tokens if token.tag in self.tags])
|
192 |
|
193 |
-
if self.keep_compound_nouns:
|
194 |
-
compound_nouns = []
|
195 |
-
|
196 |
-
if self.engine == "nltk":
|
197 |
-
for i in range(len(word_tags) - 1):
|
198 |
-
if word_tags[i][1] == "NN" and word_tags[i + 1][1] == "NN":
|
199 |
-
# if they are the same word, skip
|
200 |
-
if word_tags[i][0] == word_tags[i + 1][0]:
|
201 |
-
continue
|
202 |
-
|
203 |
-
compound_noun = word_tags[i][0] + "_" + word_tags[i + 1][0]
|
204 |
-
compound_nouns.append(compound_noun)
|
205 |
-
elif self.engine == "flair":
|
206 |
-
for i in range(len(sentence.tokens) - 1):
|
207 |
-
if sentence.tokens[i].tag == "NN" and sentence.tokens[i + 1].tag == "NN":
|
208 |
-
# if they are the same word, skip
|
209 |
-
if sentence.tokens[i].text == sentence.tokens[i + 1].text:
|
210 |
-
continue
|
211 |
-
|
212 |
-
compound_noun = sentence.tokens[i].text + "_" + sentence.tokens[i + 1].text
|
213 |
-
compound_nouns.append(compound_noun)
|
214 |
-
|
215 |
-
text = " ".join([text, " ".join(compound_nouns)])
|
216 |
-
|
217 |
return text
|
218 |
|
219 |
def __repr__(self) -> str:
|
@@ -234,7 +207,7 @@ class FrequencyMinWordCount(BaseTextTransform):
|
|
234 |
super().__init__()
|
235 |
self.min_count = min_count
|
236 |
|
237 |
-
def __call__(self, text: str):
|
238 |
"""
|
239 |
Args:
|
240 |
text (str): Text to remove infrequent words from.
|
@@ -257,47 +230,10 @@ class FrequencyMinWordCount(BaseTextTransform):
|
|
257 |
return f"{self.__class__.__name__}(min_count={self.min_count})"
|
258 |
|
259 |
|
260 |
-
class FrequencyTopK(BaseTextTransform):
|
261 |
-
"""Keep only the top k most frequent words in the input text.
|
262 |
-
|
263 |
-
In case of a tie, all words with the same count as the last word are kept.
|
264 |
-
|
265 |
-
Args:
|
266 |
-
top_k (int): Number of top words to keep.
|
267 |
-
"""
|
268 |
-
|
269 |
-
def __init__(self, top_k: int) -> None:
|
270 |
-
super().__init__()
|
271 |
-
self.top_k = top_k
|
272 |
-
|
273 |
-
def __call__(self, text: str):
|
274 |
-
"""
|
275 |
-
Args:
|
276 |
-
text (str): Text to remove infrequent words from.
|
277 |
-
"""
|
278 |
-
if self.top_k < 1:
|
279 |
-
return text
|
280 |
-
|
281 |
-
words = text.split()
|
282 |
-
word_counts = {word: words.count(word) for word in words}
|
283 |
-
top_words = sorted(word_counts, key=word_counts.get, reverse=True)
|
284 |
-
|
285 |
-
# in case of a tie, keep all words with the same count
|
286 |
-
top_words = top_words[: self.top_k]
|
287 |
-
top_words = [word for word in top_words if word_counts[word] == word_counts[top_words[-1]]]
|
288 |
-
|
289 |
-
text = " ".join([word for word in words if word in top_words])
|
290 |
-
|
291 |
-
return text
|
292 |
-
|
293 |
-
def __repr__(self) -> str:
|
294 |
-
return f"{self.__class__.__name__}(top_k={self.top_k})"
|
295 |
-
|
296 |
-
|
297 |
class ReplaceSeparators(BaseTextTransform):
|
298 |
"""Replace underscores and dashes with spaces."""
|
299 |
|
300 |
-
def __call__(self, text: str):
|
301 |
"""
|
302 |
Args:
|
303 |
text (str): Text to replace separators in.
|
@@ -313,7 +249,7 @@ class ReplaceSeparators(BaseTextTransform):
|
|
313 |
class RemoveDuplicates(BaseTextTransform):
|
314 |
"""Remove duplicate words from the input text."""
|
315 |
|
316 |
-
def __call__(self, text: str):
|
317 |
"""
|
318 |
Args:
|
319 |
text (str): Text to remove duplicate words from.
|
@@ -337,7 +273,11 @@ class TextCompose:
|
|
337 |
def __init__(self, transforms: list[BaseTextTransform]) -> None:
|
338 |
self.transforms = transforms
|
339 |
|
340 |
-
def __call__(self, text: Union[str, list[str]]) ->
|
|
|
|
|
|
|
|
|
341 |
if isinstance(text, list):
|
342 |
text = " ".join(text)
|
343 |
|
@@ -357,7 +297,7 @@ class TextCompose:
|
|
357 |
class ToLowercase(BaseTextTransform):
|
358 |
"""Convert text to lowercase."""
|
359 |
|
360 |
-
def __call__(self, text: str):
|
361 |
"""
|
362 |
Args:
|
363 |
text (str): Text to convert to lowercase.
|
@@ -374,7 +314,7 @@ class ToSingular(BaseTextTransform):
|
|
374 |
super().__init__()
|
375 |
self.transform = inflect.engine().singular_noun
|
376 |
|
377 |
-
def __call__(self, text: str):
|
378 |
"""
|
379 |
Args:
|
380 |
text (str): Text to convert to singular form.
|
@@ -430,7 +370,7 @@ def default_vocabulary_transforms() -> TextCompose:
|
|
430 |
transforms.append(ToSingular())
|
431 |
transforms.append(DropWords(words=words_to_drop))
|
432 |
transforms.append(FrequencyMinWordCount(min_count=2))
|
433 |
-
transforms.append(FilterPOS(tags=pos_tags, engine="flair"
|
434 |
transforms.append(RemoveDuplicates())
|
435 |
|
436 |
transforms = TextCompose(transforms)
|
|
|
1 |
import re
|
2 |
from abc import ABC, abstractmethod
|
3 |
+
from typing import Union
|
4 |
|
5 |
import inflect
|
6 |
import nltk
|
|
|
17 |
"DropWords",
|
18 |
"FilterPOS",
|
19 |
"FrequencyMinWordCount",
|
|
|
20 |
"ReplaceSeparators",
|
21 |
"ToLowercase",
|
22 |
"ToSingular",
|
|
|
27 |
"""Base class for string transforms."""
|
28 |
|
29 |
@abstractmethod
|
30 |
+
def __call__(self, text: str) -> str:
|
31 |
raise NotImplementedError
|
32 |
|
33 |
def __repr__(self) -> str:
|
|
|
37 |
class DropFileExtensions(BaseTextTransform):
|
38 |
"""Remove file extensions from the input text."""
|
39 |
|
40 |
+
def __call__(self, text: str) -> str:
|
41 |
"""
|
42 |
Args:
|
43 |
text (str): Text to remove file extensions from.
|
|
|
50 |
class DropNonAlpha(BaseTextTransform):
|
51 |
"""Remove non-alpha words from the input text."""
|
52 |
|
53 |
+
def __call__(self, text: str) -> str:
|
54 |
"""
|
55 |
Args:
|
56 |
text (str): Text to remove non-alpha words from.
|
|
|
71 |
super().__init__()
|
72 |
self.min_length = min_length
|
73 |
|
74 |
+
def __call__(self, text: str) -> str:
|
75 |
"""
|
76 |
Args:
|
77 |
text (str): Text to remove short words from.
|
|
|
91 |
hyphen, period, apostrophe, or ampersand.
|
92 |
"""
|
93 |
|
94 |
+
def __call__(self, text: str) -> str:
|
95 |
"""
|
96 |
Args:
|
97 |
text (str): Text to remove special characters from.
|
|
|
107 |
Tokens are defined as strings enclosed in angle brackets, e.g. <token>.
|
108 |
"""
|
109 |
|
110 |
+
def __call__(self, text: str) -> str:
|
111 |
"""
|
112 |
Args:
|
113 |
text (str): Text to remove tokens from.
|
|
|
120 |
class DropURLs(BaseTextTransform):
|
121 |
"""Remove URLs from the input text."""
|
122 |
|
123 |
+
def __call__(self, text: str) -> str:
|
124 |
"""
|
125 |
Args:
|
126 |
text (str): Text to remove URLs from.
|
|
|
141 |
self.words = words
|
142 |
self.pattern = r"\b(?:{})\b".format("|".join(words))
|
143 |
|
144 |
+
def __call__(self, text: str) -> str:
|
145 |
"""
|
146 |
Args:
|
147 |
text (str): Text to remove words from.
|
|
|
160 |
Args:
|
161 |
tags (list): List of POS tags to remove.
|
162 |
engine (str): POS tagger to use. Must be one of "nltk" or "flair". Defaults to "nltk".
|
|
|
163 |
"""
|
164 |
|
165 |
+
def __init__(self, tags: list, engine: str = "nltk") -> None:
|
166 |
super().__init__()
|
167 |
self.tags = tags
|
168 |
self.engine = engine
|
|
|
169 |
|
170 |
if engine == "nltk":
|
171 |
nltk.download("averaged_perceptron_tagger", quiet=True)
|
|
|
174 |
elif engine == "flair":
|
175 |
self.tagger = SequenceTagger.load("flair/pos-english-fast").predict
|
176 |
|
177 |
+
def __call__(self, text: str) -> str:
|
178 |
"""
|
179 |
Args:
|
180 |
text (str): Text to remove words with specific POS tags from.
|
|
|
187 |
self.tagger(sentence)
|
188 |
text = " ".join([token.text for token in sentence.tokens if token.tag in self.tags])
|
189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
return text
|
191 |
|
192 |
def __repr__(self) -> str:
|
|
|
207 |
super().__init__()
|
208 |
self.min_count = min_count
|
209 |
|
210 |
+
def __call__(self, text: str) -> str:
|
211 |
"""
|
212 |
Args:
|
213 |
text (str): Text to remove infrequent words from.
|
|
|
230 |
return f"{self.__class__.__name__}(min_count={self.min_count})"
|
231 |
|
232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
class ReplaceSeparators(BaseTextTransform):
|
234 |
"""Replace underscores and dashes with spaces."""
|
235 |
|
236 |
+
def __call__(self, text: str) -> str:
|
237 |
"""
|
238 |
Args:
|
239 |
text (str): Text to replace separators in.
|
|
|
249 |
class RemoveDuplicates(BaseTextTransform):
|
250 |
"""Remove duplicate words from the input text."""
|
251 |
|
252 |
+
def __call__(self, text: str) -> str:
|
253 |
"""
|
254 |
Args:
|
255 |
text (str): Text to remove duplicate words from.
|
|
|
273 |
def __init__(self, transforms: list[BaseTextTransform]) -> None:
|
274 |
self.transforms = transforms
|
275 |
|
276 |
+
def __call__(self, text: Union[str, list[str]]) -> list[str]:
|
277 |
+
"""
|
278 |
+
Args:
|
279 |
+
text (Union[str, list[str]]): Text to transform.
|
280 |
+
"""
|
281 |
if isinstance(text, list):
|
282 |
text = " ".join(text)
|
283 |
|
|
|
297 |
class ToLowercase(BaseTextTransform):
|
298 |
"""Convert text to lowercase."""
|
299 |
|
300 |
+
def __call__(self, text: str) -> str:
|
301 |
"""
|
302 |
Args:
|
303 |
text (str): Text to convert to lowercase.
|
|
|
314 |
super().__init__()
|
315 |
self.transform = inflect.engine().singular_noun
|
316 |
|
317 |
+
def __call__(self, text: str) -> str:
|
318 |
"""
|
319 |
Args:
|
320 |
text (str): Text to convert to singular form.
|
|
|
370 |
transforms.append(ToSingular())
|
371 |
transforms.append(DropWords(words=words_to_drop))
|
372 |
transforms.append(FrequencyMinWordCount(min_count=2))
|
373 |
+
transforms.append(FilterPOS(tags=pos_tags, engine="flair"))
|
374 |
transforms.append(RemoveDuplicates())
|
375 |
|
376 |
transforms = TextCompose(transforms)
|