File size: 2,636 Bytes
ced594a
 
 
 
 
 
 
 
a3728b4
5a3528f
 
 
a3728b4
 
ced594a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af5b399
 
 
ced594a
 
 
 
 
 
 
 
a3728b4
f3052c8
 
 
 
 
 
ced594a
759dd7b
 
 
 
 
 
 
 
68570a4
ced594a
 
 
 
 
 
 
 
a05847b
 
adaf059
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
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"],
                    }