unsplash25k-image-embeddings / unsplash25k-image-embeddings.py
JLD's picture
Correct bug with image ID loaded
800e5d6
"""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/resolve/main/data/image_ids.feather.zstd",
"embeddings": "https://huggingface.co/datasets/JLD/unsplash25k-image-embeddings/resolve/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')})
"image_embedding": datasets.Array2D(shape=(1, 512), dtype='float32')
}
)
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.TRAIN,
gen_kwargs={
"embeddings_path": embeddings_path,
"image_ids_path": 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)
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 in range(len(image_ids)):
yield num_id, {"image_id": image_ids.iloc[num_id].image_id, "image_embedding": tensors["embeddings"][num_id].unsqueeze(0)}