File size: 3,439 Bytes
fd35a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""TODO: Add a description here."""


import csv
import json
import os

import datasets
from safetensors import safe_open
import pandas as pd

# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {A great new dataset},
author={huggingface, Inc.
},
year={2022}
}
"""

_DESCRIPTION = """\
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""

# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URLS = {
    "image_ids": "https://huggingface.co/datasets/JLD/unsplash25k-image-embeddings/blob/main/data/image_ids.feather.zstd",
    "embeddings": "https://huggingface.co/datasets/JLD/unsplash25k-image-embeddings/blob/main/data/unsplash_embeddings.safetensors"
}

class Unsplash25kImageEmbeddingsDataset(datasets.GeneratorBasedBuilder):
    """_summary_

    Args:
        datasets (_type_): _description_
    """
    def _info(self):
        features = datasets.Features(
            {
                "image_id": datasets.Value("string"),
                "image_embedding": datasets.Features({'x': datasets.Array2D(shape=(1, 512), dtype='float16')})
            }
        )   
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
            # specify them. They'll be used if as_supervised=True in builder.as_dataset.
            # supervised_keys=("sentence", "label"),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        embeddings_path = dl_manager.download(_URLS["embeddings"])
        image_ids_path = dl_manager.download(_URLS["image_ids"])

        return [
            datasets.SplitGenerator(
                name=datasets.Split.ALL,
                gen_kwargs={
                    "embeddings_path": embeddings_path,
                    "image_ids": image_ids_path
                }
            )
        ]
    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, embeddings_path, image_ids_path):
        tensors = {}
        image_ids = pd.read_feather(image_ids_path, compression="zstd")
        with safe_open(embeddings_path, framework="pt", device="cpu") as f:
            for key in f.keys():
                tensors[key] = f.get_tensor(key)
        for num_id, image_id in enumerate(image_ids):
            yield {"image_id": image_id, "image_embedding": tensors["embeddings"][num_id]}