forked from erikbern/ann-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 1
Included laion400m image embeddings dataset generator and links #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
filipecosta90
wants to merge
1
commit into
multiclient_tool
Choose a base branch
from
laion-400m-img-emb
base: multiclient_tool
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,7 @@ venv | |
|
||
.idea | ||
.vscode | ||
|
||
*.hdf5 | ||
*.npy | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from ann_benchmarks.algorithms.bruteforce import BruteForceBLAS | ||
import numpy as np | ||
import click | ||
import h5py | ||
from joblib import Parallel, delayed | ||
import multiprocessing | ||
import tqdm | ||
from urllib.request import urlretrieve | ||
import sklearn.model_selection | ||
import os | ||
import wget | ||
import sys | ||
|
||
|
||
def download(src, dst): | ||
if not os.path.exists(dst): | ||
# TODO: should be atomic | ||
print("downloading %s -> %s..." % (src, dst)) | ||
wget.download(src, dst) | ||
|
||
|
||
def calc_i(i, x, bf, test, neighbors, distances, count): | ||
if i % 1000 == 0: | ||
print("%d/%d..." % (i, len(test))) | ||
res = list(bf.query_with_distances(x, count)) | ||
res.sort(key=lambda t: t[-1]) | ||
neighbors[i] = [j for j, _ in res] | ||
distances[i] = [d for _, d in res] | ||
|
||
|
||
def calc(bf, test, neighbors, distances, count): | ||
Parallel(n_jobs=multiprocessing.cpu_count(), require="sharedmem")( | ||
delayed(calc_i)(i, x, bf, test, neighbors, distances, count) | ||
for i, x in enumerate(test) | ||
) | ||
|
||
|
||
def human_format(num): | ||
magnitude = 0 | ||
while abs(num) >= 1000: | ||
magnitude += 1 | ||
num /= 1000.0 | ||
# add more suffixes if you need them | ||
return "%.0f%s" % (num, ["", "K", "M", "G", "T", "P"][magnitude]) | ||
|
||
|
||
def write_output(train, test, fn, distance, point_type="float", count=100): | ||
f = h5py.File(fn, "w") | ||
f.attrs["type"] = "dense" | ||
f.attrs["distance"] = distance | ||
f.attrs["dimension"] = len(train[0]) | ||
f.attrs["point_type"] = point_type | ||
print("train size: %9d * %4d" % train.shape) | ||
print("test size: %9d * %4d" % test.shape) | ||
f.create_dataset("train", (len(train), len(train[0])), dtype=train.dtype)[:] = train | ||
f.create_dataset("test", (len(test), len(test[0])), dtype=test.dtype)[:] = test | ||
neighbors = f.create_dataset("neighbors", (len(test), count), dtype="i") | ||
distances = f.create_dataset("distances", (len(test), count), dtype="f") | ||
bf = BruteForceBLAS(distance, precision=train.dtype) | ||
|
||
bf.fit(train) | ||
calc(bf, test, neighbors, distances, count) | ||
f.close() | ||
|
||
|
||
@click.command() | ||
@click.option("--train_size", default=1000000, help="Train size.") | ||
@click.option("--test_size", default=10000, help="Test size.") | ||
@click.option("--distance", default="angular", help="distance metric.") | ||
def create_ds(train_size, test_size, distance): | ||
dim = 512 | ||
total_vecs = train_size + test_size | ||
file_limit = 409 | ||
vector_limit = 400 * 1000000 | ||
if total_vecs > vector_limit: | ||
print("vector limit is larger than the dataset") | ||
sys.exit(1) | ||
pos = 0 | ||
print( | ||
f"generating train set of size {train_size} and test set of size {test_size}. Fetching {total_vecs} embeddings." | ||
) | ||
X = np.zeros((total_vecs, dim), dtype=np.float32) | ||
|
||
pbar = tqdm.tqdm(total=total_vecs) | ||
file_n = 0 | ||
while pos < total_vecs: | ||
filename = f"img_emb_{file_n}.npy" | ||
url = f"https://the-eye.eu/public/AI/cah/laion400m-met-release/laion400m-embeddings/images/{filename}" | ||
download(url, filename) | ||
img_emb = np.load(filename) | ||
for row in img_emb: | ||
X[pos] = row.astype(np.float32) | ||
pbar.update(1) | ||
pos = pos + 1 | ||
if pos >= total_vecs: | ||
break | ||
file_n = file_n + 1 | ||
if file_n > file_limit: | ||
print("vector limit is larger than the dataset") | ||
sys.exit(1) | ||
|
||
print("Splitting %d*%d into train/test" % (X.shape[0], X.shape[1])) | ||
X_train, X_test = sklearn.model_selection.train_test_split( | ||
X, test_size=test_size, random_state=1 | ||
) | ||
|
||
human_size = human_format(train_size) | ||
write_output( | ||
train=np.array(X_train), | ||
test=np.array(X_test), | ||
fn=f"laion-img-emb-{dim}-{human_size}-{distance}.hdf5", | ||
distance=distance, | ||
point_type="float", | ||
count=100, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
create_ds() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ pymilvus==2.0.2 | |
pinecone-client==2.0.11 | ||
redis==4.3.2 | ||
elasticsearch==8.3.1 | ||
datasets==2.14.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ scipy==1.5.4 | |
scikit-learn | ||
jinja2==2.10.1 | ||
pandas==1.1.5 | ||
datasets==2.14.5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are 2 packages missing in the requirements.txt files:
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While creating hdf5 files I encountered an issue:
The system that I was using is Ubuntu 22.04 with 144 cpu_count and python3.8.10. I was able to workaround this error with hardcoding the n_jobs to 64.