// https://github.com/unum-cloud/usearch/blob/main/python/README.md
$ pip install usearch
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting usearch
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ba/f4/24124f65ea3e940e54af29d55204ddfbeafa86d6b94b63c2e99baff2f7d6/usearch-2.8.14-cp38-cp38-manylinux_2_28_x86_64.whl (1.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 17.0 MB/s eta 0:00:00
Requirement already satisfied: numpy in /home/ubuntu/anaconda3/envs/vglm2/lib/python3.8/site-packages (from usearch) (1.23.1)
Requirement already satisfied: tqdm in /home/ubuntu/anaconda3/envs/vglm2/lib/python3.8/site-packages (from usearch) (4.66.1)
Installing collected packages: usearch
Successfully installed usearch-2.8.14
import numpy as np
from usearch.index import Index, MetricKind, Matches
ndim = 131072
index_path = "test.usearch"
index = Index(
ndim=ndim, # Define the number of dimensions in input vectors
metric='cos', # Choose 'l2sq', 'haversine' or other metric, default = 'ip'
dtype='f32', # Quantize to 'f16' or 'i8' if needed, default = 'f32'
connectivity=16, # How frequent should the connections in the graph be, optional
expansion_add=128, # Control the recall of indexing, optional
expansion_search=64, # Control the quality of search, optional
)# index = Index(ndim=ndim, metric=MetricKind.Cos)
for i in range(1,10):
vector = np.random.random((1000, ndim)).astype('float32')
index.add(None, vector, log=True)
index.save(index_path)
vector = np.random.random((1, ndim)).astype('float32')
matches: Matches = index.search(vector, 10)
ids = matches.keys.flatten()
print(matches)
# test.usearch大小: 10*1000*131072 =>2.2G (如果dtype='f32'=>4G+)
File "/home/ubuntu/anaconda3/envs/usearch/lib/python3.10/site-packages/watchdog/observers/inotify_c.py", line 428, in _raise_error
raise OSError(errno.ENOSPC, "inotify watch limit reached")
OSError: [Errno 28] inotify watch limit reached
这个错误表明在使用 watchdog
库时超过了 Linux 系统对 inotify 监视的文件数或目录数的限制。Linux 对于每个进程的 inotify 能够监视的文件和目录有一个限制,当达到这个限制时,会出现像上面的错误一样的问题。可以尝试增加系统对 inotify 的资源限制。可以通过修改 /etc/sysctl.conf
文件来增加 fs.inotify.max_user_watches
参数的值。例如:
```bash
sudo sysctl -w fs.inotify.max_user_watches=65536
```
然后,运行下面的命令使更改生效:
```bash
sudo sysctl -p
```
注意:增加监视数可能会对系统资源产生一些影响,因此请根据实际情况慎重调整。
///home/ubuntu/anaconda3/envs/usearch/lib/python3.10/site-packages/uform/__init__.py
def get_checkpoint(model_name, token) -> Tuple[str, Mapping, str]:
model_path = snapshot_download(repo_id=model_name, token=token)
config_path = f"{model_path}/torch_config.json"
state = torch.load(f"{model_path}/torch_weight.pt")
return config_path, state, f"{model_path}/tokenizer.json"
def get_model(model_name: str, token: Optional[str] = None) -> VLM:
config_path, state, tokenizer_path = get_checkpoint(model_name, token)
with open(config_path, "r") as f:
model = VLM(load(f), tokenizer_path)
model.image_encoder.load_state_dict(state["image_encoder"])
model.text_encoder.load_state_dict(state["text_encoder"])
return model.eval()
_model = get_model("你的下载路径")
def get_checkpoint(model_name, token) -> Tuple[str, Mapping, str]:
model_path = model_name#snapshot_download(repo_id=model_name, token=token)
config_path = f"{model_path}/torch_config.json"
state = torch.load(f"{model_path}/torch_weight.pt")
return config_path, state, f"{model_path}/tokenizer.json"
def get_model(model_name: str, token: Optional[str] = None) -> VLM:
config_path, state, tokenizer_path = get_checkpoint(model_name, token)
with open(config_path, "r") as f:
model = VLM(load(f), tokenizer_path)
model.image_encoder.load_state_dict(state["image_encoder"])
model.text_encoder.load_state_dict(state["text_encoder"])
return model.eval()
_datasets = {
name: _open_dataset(os.path.join("/home/ubuntu/userfile/***/Usearch/usearch-images-main/data", name))
for name in (
"unsplash-25k",
# "cc-3m",
# "laion-4m",
)
}
dataset_names: str = st.multiselect(
"Datasets",
[
dataset_unsplash_name,
# dataset_cc_name,
# dataset_laion_name,
],
[dataset_unsplash_name],#, dataset_cc_name],
format_func=lambda x: x.split(":")[0],
)
# uris: Strs = File(os.path.join(dir, "images.txt")).splitlines()
file_path = os.path.join(dir, "images.txt")
with open(file_path, 'r') as file:
uris = file.read().splitlines()