|
import os
|
|
import csv
|
|
from PIL import Image
|
|
import datasets
|
|
|
|
|
|
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):
|
|
|
|
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
|
|
|