NimaBoscarino commited on
Commit
67613a8
1 Parent(s): bd62bca

Create laion2b-en-vit_embeddings.py

Browse files
Files changed (1) hide show
  1. laion2b-en-vit_embeddings.py +56 -0
laion2b-en-vit_embeddings.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import datasets
3
+
4
+ from huggingface_hub import HfApi
5
+ api = HfApi()
6
+ repo_files = list(api.dataset_info(repo_id="laion/laion2b-en-vit-h-14-embeddings").siblings)
7
+ filenames = [x.rfilename for x in repo_files]
8
+ img_embs = [x for x in filenames if x.startswith("img_emb/")]
9
+
10
+
11
+ class LAIONEmbeddingsConfig(datasets.BuilderConfig):
12
+ def __init__(self, **kwargs):
13
+ super(LAIONEmbeddingsConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
14
+
15
+
16
+ class LAIONEmbeddings(datasets.GeneratorBasedBuilder):
17
+ VERSION = datasets.Version("1.0.0")
18
+
19
+ BUILDER_CONFIGS = [
20
+ LAIONEmbeddingsConfig()
21
+ ]
22
+
23
+ def _get_features(self) -> datasets.Features:
24
+ return datasets.Features({
25
+ "embedding": datasets.Sequence(datasets.Value("float32")),
26
+ })
27
+
28
+ def _info(self):
29
+ features = self._get_features()
30
+
31
+ return datasets.DatasetInfo(
32
+ features=features,
33
+ )
34
+
35
+ def _split_generators(self, dl_manager):
36
+ main_url = "https://huggingface.co/datasets/laion/laion2b-en-vit-h-14-embeddings/resolve/main/"
37
+ archive_paths = dl_manager.download([main_url + x for x in img_embs])
38
+
39
+ return [
40
+ datasets.SplitGenerator(
41
+ name=datasets.Split.TRAIN,
42
+ gen_kwargs={
43
+ "chunks": archive_paths,
44
+ "split": "train",
45
+ },
46
+ ),
47
+ ]
48
+
49
+ def _generate_examples(self, chunks, split):
50
+ for chunk in chunks:
51
+ file = np.DataSource().open(chunk)
52
+ data = np.load(file.name)
53
+ for example in data:
54
+ yield "", {
55
+ "embedding": example
56
+ }