JLD commited on
Commit
fd35a16
1 Parent(s): fb27d7d

Add script to load dataset

Browse files
Files changed (1) hide show
  1. unsplash25k_image_embeddings.py +92 -0
unsplash25k_image_embeddings.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """TODO: Add a description here."""
2
+
3
+
4
+ import csv
5
+ import json
6
+ import os
7
+
8
+ import datasets
9
+ from safetensors import safe_open
10
+ import pandas as pd
11
+
12
+ # TODO: Add BibTeX citation
13
+ # Find for instance the citation on arxiv or on the dataset repo/website
14
+ _CITATION = """\
15
+ @InProceedings{huggingface:dataset,
16
+ title = {A great new dataset},
17
+ author={huggingface, Inc.
18
+ },
19
+ year={2022}
20
+ }
21
+ """
22
+
23
+ _DESCRIPTION = """\
24
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
25
+ """
26
+
27
+ # TODO: Add a link to an official homepage for the dataset here
28
+ _HOMEPAGE = ""
29
+
30
+ # TODO: Add the licence for the dataset here if you can find it
31
+ _LICENSE = ""
32
+
33
+ # TODO: Add link to the official dataset URLs here
34
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
35
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
36
+ _URLS = {
37
+ "image_ids": "https://huggingface.co/datasets/JLD/unsplash25k-image-embeddings/blob/main/data/image_ids.feather.zstd",
38
+ "embeddings": "https://huggingface.co/datasets/JLD/unsplash25k-image-embeddings/blob/main/data/unsplash_embeddings.safetensors"
39
+ }
40
+
41
+ class Unsplash25kImageEmbeddingsDataset(datasets.GeneratorBasedBuilder):
42
+ """_summary_
43
+
44
+ Args:
45
+ datasets (_type_): _description_
46
+ """
47
+ def _info(self):
48
+ features = datasets.Features(
49
+ {
50
+ "image_id": datasets.Value("string"),
51
+ "image_embedding": datasets.Features({'x': datasets.Array2D(shape=(1, 512), dtype='float16')})
52
+ }
53
+ )
54
+ return datasets.DatasetInfo(
55
+ # This is the description that will appear on the datasets page.
56
+ description=_DESCRIPTION,
57
+ # This defines the different columns of the dataset and their types
58
+ features=features, # Here we define them above because they are different between the two configurations
59
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
60
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
61
+ # supervised_keys=("sentence", "label"),
62
+ # Homepage of the dataset for documentation
63
+ homepage=_HOMEPAGE,
64
+ # License for the dataset if available
65
+ license=_LICENSE,
66
+ # Citation for the dataset
67
+ citation=_CITATION,
68
+ )
69
+
70
+ def _split_generators(self, dl_manager):
71
+ embeddings_path = dl_manager.download(_URLS["embeddings"])
72
+ image_ids_path = dl_manager.download(_URLS["image_ids"])
73
+
74
+ return [
75
+ datasets.SplitGenerator(
76
+ name=datasets.Split.ALL,
77
+ gen_kwargs={
78
+ "embeddings_path": embeddings_path,
79
+ "image_ids": image_ids_path
80
+ }
81
+ )
82
+ ]
83
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
84
+ def _generate_examples(self, embeddings_path, image_ids_path):
85
+ tensors = {}
86
+ image_ids = pd.read_feather(image_ids_path, compression="zstd")
87
+ with safe_open(embeddings_path, framework="pt", device="cpu") as f:
88
+ for key in f.keys():
89
+ tensors[key] = f.get_tensor(key)
90
+ for num_id, image_id in enumerate(image_ids):
91
+ yield {"image_id": image_id, "image_embedding": tensors["embeddings"][num_id]}
92
+