File size: 7,246 Bytes
604734f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import csv
from PIL import Image
import datasets

# Define configurations for each flavor.
BUILDER_CONFIGS = [
    datasets.BuilderConfig(
        name="sound_baseline",
        description="Physical dataset: baseline variant",
        data_dir="./physicsgen/urban_sound_25k_baseline"
    ),
    datasets.BuilderConfig(
        name="sound_reflection",
        description="Physical dataset: reflection variant",
        data_dir="./physicsgen/urban_sound_25k_reflection"
    ),
    datasets.BuilderConfig(
        name="sound_diffraction",
        description="Physical dataset: reflection variant",
        data_dir="./physicsgen/urban_sound_25k_diffraction"
    ),
    datasets.BuilderConfig(
        name="sound_combined",
        description="Physical dataset: reflection variant",
        data_dir="./physicsgen/urban_sound_25k_combined"
    ),
    datasets.BuilderConfig(
        name="lens_p1",
        description="Distortion dataset variant",
        data_dir="./physicsgen/lens_distortion_p1"
    ),
    datasets.BuilderConfig(
        name="lens_p2",
        description="Distortion dataset variant",
        data_dir="./physicsgen/lens_distortion_p2"
    ),
    datasets.BuilderConfig(
        name="ball_roll",
        description="Double image dataset variant",
        data_dir="./physicsgen/ball_roll"
    ),
    datasets.BuilderConfig(
        name="ball_bounce",
        description="Double image dataset variant",
        data_dir="./physicsgen/ball_bounce"
    ),
]

class MyPhysicalDataset(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = BUILDER_CONFIGS
    VERSION = datasets.Version("1.0.2")

    def _info(self):
        if self.config.name in ["sound_baseline", "sound_reflection", "sound_diffraction", "sound_combined"]:
            features = datasets.Features({
                "lat": datasets.Value("float"),
                "long": datasets.Value("float"),
                "db": datasets.Value("string"),
                "soundmap": datasets.Image(),
                "osm": datasets.Image(),
                "temperature": datasets.Value("int32"),
                "humidity": datasets.Value("int32"),
                "yaw": datasets.Value("float"),
                "sample_id": datasets.Value("int32"),
                "soundmap_512": datasets.Image(),
            })
        elif self.config.name in ["lens_p1", "lens_p2"]:
            features = datasets.Features({
                "label_path": datasets.Value("string"),
                "fx": datasets.Value("float"),
                "k1": datasets.Value("float"),
                "k2": datasets.Value("float"),
                "k3": datasets.Value("float"),
                "p1": datasets.Value("float"),
                "p2": datasets.Value("float"),
                "cx": datasets.Value("float"),
                "distortion_path": datasets.Value("string"),
            })
        elif self.config.name in ["ball_roll", "ball_bounce"]:
            features = datasets.Features({
                "ImgName": datasets.Value("string"),
                "StartHeight": datasets.Value("int32"),
                "GroundIncli": datasets.Value("float"),
                "InputTime": datasets.Value("int32"),
                "TargetTime": datasets.Value("int32"),
                "input_image": datasets.Image(),
                "target_image": datasets.Image(),
            })
        else:
            raise ValueError(f"Unknown config name: {self.config.name}")
        return datasets.DatasetInfo(
            description="Multiple variant physical tasks dataset.",
            features=features,
        )

    def _split_generators(self, dl_manager):
        data_dir = self.config.data_dir
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"split_dir": os.path.join(data_dir, "train")},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"split_dir": os.path.join(data_dir, "test")},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"split_dir": os.path.join(data_dir, "eval")},
            ),
        ]

    def _generate_examples(self, split_dir):
        if self.config.name in ["sound_baseline", "sound_reflection", "sound_diffraction", "sound_combined"]:
            csv_path = os.path.join(split_dir, "meta_data.csv")
            with open(csv_path, encoding="utf-8") as f:
                reader = csv.DictReader(f)
                for idx, row in enumerate(reader):
                    row["soundmap"] = os.path.join(split_dir, row["soundmap"])
                    row["osm"] = os.path.join(split_dir, row["osm"])
                    row["soundmap_512"] = os.path.join(split_dir, row["soundmap_512"])
                    row["lat"] = float(row["lat"])
                    row["long"] = float(row["long"])
                    row["temperature"] = int(row["temperature"])
                    row["humidity"] = int(row["humidity"])
                    row["sample_id"] = int(row["sample_id"])
                    row["yaw"] = float(row["yaw"]) if row["yaw"] else 0.0
                    yield idx, row

        elif self.config.name in ["lens_p1", "lens_p2"]:
            csv_path = os.path.join(split_dir, "meta_data.csv")
            with open(csv_path, encoding="utf-8") as f:
                reader = csv.DictReader(f)
                for idx, row in enumerate(reader):
                    row["label_path"] = str(row["label_path"])
                    row["distortion_path"] = str(row["distortion_path"])
                    row["fx"] = float(row["fx"])
                    row["k1"] = float(row["k1"])
                    row["k2"] = float(row["k2"])
                    row["k3"] = float(row["k3"])
                    row["p1"] = float(row["p1"])
                    row["p2"] = float(row["p2"])
                    row["cx"] = float(row["cx"])
                    yield idx, row

        elif self.config.name in ["ball_roll", "ball_bounce"]:
            csv_path = os.path.join(split_dir, "meta_data.csv")
            with open(csv_path, encoding="utf-8") as f:
                reader = csv.DictReader(f)
                for idx, row in enumerate(reader):
                    # Construct image path from ImgName, e.g., "DoubleImg_0.jpg"
                    image_filename = "DoubleImg_" + row["ImgName"] + ".jpg"
                    input_image_path = os.path.join(split_dir, "x", image_filename)
                    target_image_path = os.path.join(split_dir, "y", image_filename)
                    row["input_image"] = input_image_path
                    row["target_image"] = target_image_path
                    row["ImgName"] = row["ImgName"]
                    row["StartHeight"] = int(row["StartHeight"])
                    row["GroundIncli"] = float(row["GroundIncli"])
                    row["InputTime"] = int(row["InputTime"])
                    row["TargetTime"] = int(row["TargetTime"])
                    yield idx, row