mscoco_jsonl_full / mscoco_jsonl_full.py
pain's picture
Update mscoco_jsonl_full.py
af5b399
import datasets
import pickle
import json
class EmbeddingsDatasetConfig(datasets.BuilderConfig):
def __init__(self, image_base_m, embeddingSize, **kwargs):
super(EmbeddingsDatasetConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
self.data_urls = {
"train" :"Vit_B_32_train_full_mapping.jsonl",
"dev": "Vit_B_32_val_full_mapping.jsonl",
"test": "Vit_B_32_test_full_mapping.jsonl"
}
self.image_base_m = image_base_m
self.embeddingSize = embeddingSize
class ImageCaptionsEmbeddings(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
EmbeddingsDatasetConfig(
name="Vit-B-32",
image_base_m="Vit-B-32-2",
embeddingSize=512,
),
]
DEFAULT_CONFIG_NAME = "Vit-B-32"
def _info(self):
return datasets.DatasetInfo(
description='test',
features=datasets.Features(
{
"id": datasets.Value('string'),
"embedding": datasets.Array2D((1, self.config.embeddingSize), 'float32'),
"en_caption": datasets.Value('string'),
"ar_caption": datasets.Value('string'),
}
),
)
def _split_generators(self, dl_manager):
urls_to_download = self.config.data_urls
downloaded_files = dl_manager.download_and_extract(urls_to_download)
# return [
# datasets.SplitGenerator(name=datasets.Split.TRAIN,
# gen_kwargs={
# "data_files": [downloaded_files],
# })
# ]
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": [downloaded_files["train"]]}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": [downloaded_files["dev"]]}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": [downloaded_files["test"]]}),
]
def _generate_examples(self, filepath):
for data_file in filepath:
with open(data_file, "r") as input_file:
for line in input_file:
json_data = json.loads(line)
yield json_data["id"], {
"id": json_data["id"],
"embedding": json_data["embedding"],
"en_caption": json_data["en_caption"],
"ar_caption": json_data["ar_caption"],
}