Datasets:

File size: 8,702 Bytes
ca1fdb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5571213
 
84c942e
5571213
 
84c942e
5571213
 
84c942e
5571213
 
84c942e
5571213
 
84c942e
5571213
 
 
 
 
ca1fdb0
 
 
 
 
 
 
 
 
 
 
 
 
5571213
 
 
 
ca1fdb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5571213
 
b8fb613
ee813ef
fdda5ec
 
5571213
ca1fdb0
 
 
 
 
5571213
ca1fdb0
5571213
 
ca1fdb0
 
 
 
 
 
 
 
 
 
 
5571213
ca1fdb0
 
 
5571213
 
 
 
 
 
 
 
 
 
 
 
 
 
ca1fdb0
 
 
5571213
ca1fdb0
 
 
5571213
ca1fdb0
 
 
 
 
fdda5ec
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# coding=utf-8
"""Snacks Data Set"""

import os
import json

import datasets
from datasets.tasks import ImageClassification
from sklearn.model_selection import train_test_split

_CITATION = """
@misc{helff2023vlol,
      title={V-LoL: A Diagnostic Dataset for Visual Logical Learning}, 
      author={Lukas Helff and Wolfgang Stammer and Hikaru Shindo and Devendra Singh Dhami and Kristian Kersting},
      journal={Dataset available from https://sites.google.com/view/v-lol},
      year={2023},
      eprint={2306.07743},
      archivePrefix={arXiv},
      primaryClass={cs.AI}
}

"""

_DESCRIPTION = "This is a diagnostic dataset for visual logical learning. " \
               "It consists of 2D images of trains, where each train is either going eastbound or westbound. " \
               "The trains are composed of multiple wagons, which are composed of multiple properties. " \
               "The task is to predict the direction of the train. " \
               "The dataset is designed to test the ability of machine learning models to learn logical rules from visual input."

_HOMEPAGE = "https://huggingface.co/datasets/LukasHug/v-lol-trains/"

_LICENSE = "cc-by-4.0"
_IMAGES_URL = "https://huggingface.co/datasets/LukasHug/v-lol-trains/resolve/main/data"
_DIR = _IMAGES_URL
# _URL_DATA = {
#     "V-LoL-Trains-TheoryX": f"{_DIR}/V-LoL-Trains-TheoryX.zip",
#     "V-LoL-Trains-Numerical": f"{_DIR}/V-LoL-Trains-Numerical.zip",
#     "V-LoL-Trains-Complex": f"{_DIR}/V-LoL-Trains-Complex.zip",
#     "V-LoL-Blocks-TheoryX": f"{_DIR}/V-LoL-Blocks-TheoryX.zip",
#     "V-LoL-Blocks-Numerical": f"{_DIR}/V-LoL-Blocks-Numerical.zip",
#     "V-LoL-Blocks-Complex": f"{_DIR}/V-LoL-Blocks-Complex.zip",
#     "V-LoL-Trains-TheoryX-len7": f"{_DIR}/V-LoL-Trains-TheoryX-len7.zip",
#     "V-LoL-Trains-Numerical-len7": f"{_DIR}/V-LoL-Trains-Numerical-len7.zip",
#     "V-LoL-Trains-Complex-len7": f"{_DIR}/V-LoL-Trains-Complex-len7.zip",
#     "V-LoL-Random-Blocks-TheoryX": f"{_DIR}/V-LoL-Random-Blocks-TheoryX.zip",
#     "V-LoL-Random-Trains-TheoryX": f"{_DIR}/V-LoL-Random-Trains-TheoryX.zip",
# }
_URL_DATA = {
    "V-LoL-Trains-TheoryX": f"{_DIR}/Trains_theoryx_MichalskiTrains_base_scene_len_2-4.zip",
    "V-LoL-Trains-Numerical": f"{_DIR}/Trains_numerical_MichalskiTrains_base_scene_len_2-4.zip",
    "V-LoL-Trains-Complex": f"{_DIR}/Trains_complex_MichalskiTrains_base_scene_len_2-4.zip",
    "V-LoL-Blocks-TheoryX": f"{_DIR}/SimpleObjects_theoryx_MichalskiTrains_base_scene_len_2-4.zip",
    "V-LoL-Blocks-Numerical": f"{_DIR}/SimpleObjects_numerical_MichalskiTrains_base_scene_len_2-4.zip",
    "V-LoL-Blocks-Complex": f"{_DIR}/SimpleObjects_complex_MichalskiTrains_base_scene_len_2-4.zip",
    "V-LoL-Trains-TheoryX-len7":
        {'train': f"{_DIR}/Trains_theoryx_MichalskiTrains_base_scene_len_2-4.zip",
         'test': f"{_DIR}/Trains_theoryx_MichalskiTrains_base_scene_len_7-7.zip"},
    "V-LoL-Trains-Numerical-len7":
        {'train': f"{_DIR}/Trains_numerical_MichalskiTrains_base_scene_len_2-4.zip",
         'test': f"{_DIR}/Trains_numerical_MichalskiTrains_base_scene_len_7-7.zip"},
    "V-LoL-Trains-Complex-len7":
        {'train': f"{_DIR}/Trains_complex_MichalskiTrains_base_scene_len_2-4.zip",
         'test': f"{_DIR}/Trains_complex_MichalskiTrains_base_scene_len_7-7.zip"},
    "V-LoL-Random-Blocks-TheoryX":
        {'train': f"{_DIR}/SimpleObjects_theoryx_MichalskiTrains_base_scene_len_2-4.zip",
         'test': f"{_DIR}/SimpleObjects_theoryx_RandomTrains_base_scene_len_2-4.zip"},
    "V-LoL-Random-Trains-TheoryX":
        {'train': f"{_DIR}/Trains_theoryx_MichalskiTrains_base_scene_len_2-4.zip",
         'test': f"{_DIR}/Trains_theoryx_RandomTrains_base_scene_len_2-4.zip"},
    #     "V-LoL-Trains-TheoryX-len7": f"{_DIR}/Trains_theoryx_MichalskiTrains_base_scene_len_7.zip",
    #     "V-LoL-Trains-Numerical-len7": f"{_DIR}/Trains_numerical_MichalskiTrains_base_scene_len_7.zip",
    #     "V-LoL-Trains-Complex-len7": f"{_DIR}/Trains_complex_MichalskiTrains_base_scene_len_7.zip",
    #     "V-LoL-Random-Blocks-TheoryX": f"{_DIR}/SimpleObjects_theoryx_RandomTrains_base_scene_len_2-4.zip",
    #     "V-LoL-Random-Trains-TheoryX": f"{_DIR}/Trains_theoryx_RandomTrains_base_scene_len_2-4.zip",
}

_NAMES = ["westbound", "eastbound"]
class VLoLConfig(datasets.BuilderConfig):
    """Builder Config for Food-101"""

    def __init__(self, data_url, **kwargs):
        """BuilderConfig for Food-101.
        Args:
          metadata_urls: dictionary with keys 'train' and 'validation' containing the archive metadata URLs
          **kwargs: keyword arguments forwarded to super.
        """
        super(VLoLConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
        if isinstance(data_url, dict):
            self.metadata_urls = data_url
        else:
            self.metadata_urls = {'train': data_url, 'test': None}


class vloltrains(datasets.GeneratorBasedBuilder):
    '''v-lol-trains Data Set'''

    BUILDER_CONFIGS = [
        VLoLConfig(
            name=name,
            description=name,
            data_url=data_url,
        ) for name, data_url in _URL_DATA.items()
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.features.ClassLabel(names=_NAMES),
                }
            ),
            supervised_keys=("image", "label"),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE,
            task_templates=ImageClassification(image_column="image", label_column="label"),
        )

    def get_data(self, dl_manager, url):
        archive_path = os.path.join(dl_manager.download_and_extract(url), url.split('/')[-1].split('.')[0])
        # print containg folders
        print(os.listdir(archive_path))
        image_dir = os.path.join(archive_path, "images")
        metadata_pth = os.path.join(archive_path, "all_scenes", "all_scenes.json")
        images, y, trains, masks = [], [], [], []
        # ds settings
        # load data
        with open(metadata_pth, 'r') as f:
            all_scenes = json.load(f)
            for scene in all_scenes['scenes']:
                images.append(scene['image_filename'])
                train = scene['train']
                y.append(int(train.split(' ')[0] == 'east'))
                # depths.append(scene['depth_map_filename'])
                # if 'train' in scene:
                #     # new json data format
                #     train = scene['train']
                #     l = train.split(' ')
                #     y = l[0]
                #     y = int(l[0] == 'east')
                #     train = MichalskiTrain.from_text(train, train_vis)
                # else:
                #     # old json data format
                #     train = scene['m_train']
                #     train = jsonpickle.decode(train)
                #     # trains.append(train.replace('michalski_trains.m_train.', 'm_train.'))
                #     # text = train.to_txt()
                #     # t1 = MichalskiTrain.from_text(text, train_vis)
                # lab = int(train.get_label() == 'east')
                # y.append(lab)
                # trains.append(train)
                # masks.append(scene['car_masks'])
        return image_dir, y, images

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        if self.config.metadata_urls['test'] is None:
            image_dir, y, images = self.get_data(dl_manager, self.config.metadata_urls['train'])
            image_dir_train, image_dir_test = image_dir, image_dir
            y_train, y_test, images_train, images_test = train_test_split(y, images, test_size=0.2, random_state=0)
        else:
            image_dir_train, y_train, images_train = self.get_data(dl_manager, self.config.metadata_urls['train'])
            image_dir_test, y_test, images_test = self.get_data(dl_manager, self.config.metadata_urls['test'])
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"image_dir": image_dir_train, "labels": y_train, "images": images_train}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"image_dir": image_dir_test, "labels": y_test, "images": images_test}
            ),
        ]

    def _generate_examples(self, image_dir, labels, images):
        for i, (image, label) in enumerate(zip(images, labels)):
            yield i, {"image": os.path.join(image_dir, image), "label": label}