File size: 2,858 Bytes
72d3afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hugging Face `datasets` loading script for the local ModelNet10 layout (CSV + ModelNet10/*.off)."""

import csv
import os

from datasets import (
    BuilderConfig,
    DatasetInfo,
    Features,
    GeneratorBasedBuilder,
    Split,
    SplitGenerator,
    Value,
    Version,
)


class ModelNet10(GeneratorBasedBuilder):
    """Build train/test splits from metadata_modelnet10.csv next to the ModelNet10 mesh folder."""

    VERSION = Version("1.0.0")
    BUILDER_CONFIGS = [BuilderConfig(name="default", version=VERSION)]
    DEFAULT_CONFIG_NAME = "default"

    def _info(self):
        return DatasetInfo(
            description=(
                "ModelNet10: 3D mesh classification subset (10 categories). "
                "Each row references an OFF file under ModelNet10/ and metadata_modelnet10.csv at the dataset root."
            ),
            features=Features(
                {
                    "object_id": Value("string"),
                    "class": Value("string"),
                    "split": Value("string"),
                    "file_path": Value("string"),
                }
            ),
        )

    def _split_generators(self, dl_manager):
        if self.config.data_dir is not None:
            data_dir = os.path.abspath(os.path.expanduser(self.config.data_dir))
            if not os.path.isdir(data_dir):
                raise FileNotFoundError(f"data_dir is not a directory: {data_dir}")
        elif self.config.data_files is not None:
            data_dir = dl_manager.download_and_extract(self.config.data_files)
        else:
            raise ValueError(
                "Pass data_dir=... (folder containing metadata_modelnet10.csv and ModelNet10/) "
                "or data_files=... when calling load_dataset."
            )

        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={"split": "train", "data_dir": data_dir},
            ),
            SplitGenerator(
                name=Split.TEST,
                gen_kwargs={"split": "test", "data_dir": data_dir},
            ),
        ]

    def _generate_examples(self, split, data_dir):
        csv_path = os.path.join(data_dir, "metadata_modelnet10.csv")
        if not os.path.isfile(csv_path):
            raise FileNotFoundError(f"Missing metadata CSV: {csv_path}")

        with open(csv_path, newline="", encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for row in reader:
                if row["split"] != split:
                    continue
                object_id = row["object_id"]
                yield object_id, {
                    "object_id": object_id,
                    "class": row["class"],
                    "split": row["split"],
                    "file_path": os.path.join(data_dir, "ModelNet10", row["object_path"]),
                }