File size: 2,808 Bytes
edbad79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datasets

# Constants for your dataset
_DESCRIPTION = """\
This dataset includes images with associated IDs, titles, and URLs. There are two types of images: 'Listing Image' and 'Search-image'.
"""
_LABEL_MAP = {
    'Listing Image': 'listing_image',
    'Search-image': 'search_image',
}

class MyDatasetConfig(datasets.BuilderConfig):
    """BuilderConfig for MyDataset."""

    def __init__(self, **kwargs):
        """BuilderConfig for MyDataset.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(MyDatasetConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)


class MyDataset(datasets.GeneratorBasedBuilder):
    """My custom dataset."""

    BUILDER_CONFIGS = [
        MyDatasetConfig(
            name="default",
            description="This version of the dataset contains two types of images with metadata.",
        )
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "listing_title": datasets.Value("string"),
                    "url": datasets.Value("string"),
                    "listing_image": datasets.Image(),
                    "search_image": datasets.Image(),
                }
            ),
            supervised_keys=None,
            homepage="Your dataset homepage here",
            license="Your dataset's license here",
            citation="Your dataset's citation here",
        )

    def _split_generators(self, dl_manager):
        # You would have a way to access and download your data, for example, from a Google Cloud Storage Bucket
        # For simplicity, we are assuming your data is already downloaded and accessible
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "datapath": "path_to_your_downloaded_data",
                },
            )
        ]

    def _generate_examples(self, datapath):
        # Here you will write the logic to read your dataset's contents
        # For example, let's say you have a CSV file with all the metadata and the links to the images
        # You would read the CSV file and for each row, yield the following:
        with open(datapath, encoding="utf-8") as csv_file:
            reader = csv.DictReader(csv_file)
            for idx, row in enumerate(reader):
                yield idx, {
                    "id": row['id'],
                    "listing_title": row['listing-title'],
                    "url": row['url'],
                    "listing_image": row['Listing Image'],
                    "search_image": row['Search-image'],
                }