File size: 3,163 Bytes
8b74b04
 
 
 
 
 
 
 
 
 
 
97cc82c
8b74b04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6255c84
8b74b04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8

"""MNIST Point Cloud Data Set"""


import struct

import numpy as np

import datasets

_VERSION = "0.0.3"

# _CITATION = """\
# @article{lecun2010mnist,
#   title={MNIST handwritten digit database},
#   author={LeCun, Yann and Cortes, Corinna and Burges, CJ},
#   journal={ATT Labs [Online]. Available: http://yann.lecun.com/exdb/mnist},
#   volume={2},
#   year={2010}
# }
# """

_DESCRIPTION = """\
The MNIST dataset consists of 70,000 28x28 black-and-white points in 10 classes (one for each digits), with 7,000
points per class. There are 60,000 training points and 10,000 test points.
"""

_URL = f"https://huggingface.co/datasets/cgarciae/point-cloud-mnist/resolve/{_VERSION}/data/"
_URLS = {
    "train_points": "X_train.npy",
    "train_labels": "y_train.npy",
    "test_points": "X_test.npy",
    "test_labels": "y_test.npy",
}


class MNIST(datasets.GeneratorBasedBuilder):
    """MNIST Data Set"""

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="point-cloud-mnist",
            version=datasets.Version(_VERSION),
            description=_DESCRIPTION,
        )
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "points": datasets.Array2D(shape=(351, 3), dtype="uint8"),
                    "label": datasets.features.ClassLabel(
                        names=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
                    ),
                }
            ),
            supervised_keys=("points", "label"),
            # homepage="http://yann.lecun.com/exdb/mnist/",
            # citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        urls_to_download = {key: _URL + fname for key, fname in _URLS.items()}
        downloaded_files = dl_manager.download_and_extract(urls_to_download)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": [
                        downloaded_files["train_points"],
                        downloaded_files["train_labels"],
                    ],
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": [
                        downloaded_files["test_points"],
                        downloaded_files["test_labels"],
                    ],
                    "split": "test",
                },
            ),
        ]

    def _generate_examples(self, filepath, split):
        """This function returns the examples in the raw form."""
        # points
        with open(filepath[0], "rb") as f:
            points = np.load(f)

        # Labels
        with open(filepath[1], "rb") as f:
            labels = np.load(f)

        for idx in range(len(points)):
            yield (
                idx,
                {
                    "points": points[idx],
                    "label": str(labels[idx]),
                },
            )