Update Superimposed-Masked-Dataset.py
Browse files- Superimposed-Masked-Dataset.py +25 -23
Superimposed-Masked-Dataset.py
CHANGED
@@ -1,21 +1,7 @@
|
|
1 |
-
#
|
2 |
-
# Copyright 2022 the HuggingFace Datasets Authors.
|
3 |
-
#
|
4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
-
# you may not use this file except in compliance with the License.
|
6 |
-
# You may obtain a copy of the License at
|
7 |
-
#
|
8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
-
#
|
10 |
-
# Unless required by applicable law or agreed to in writing, software
|
11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
-
# See the License for the specific language governing permissions and
|
14 |
-
# limitations under the License.
|
15 |
-
|
16 |
-
# This script was modified from the imagenet-1k HF dataset repo
|
17 |
|
18 |
import os
|
|
|
19 |
|
20 |
import datasets
|
21 |
from datasets.tasks import ImageClassification
|
@@ -41,6 +27,11 @@ _DATA_URL = {
|
|
41 |
]
|
42 |
}
|
43 |
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
class SMD(datasets.GeneratorBasedBuilder):
|
46 |
VERSION = datasets.Version("1.0.0")
|
@@ -55,6 +46,7 @@ class SMD(datasets.GeneratorBasedBuilder):
|
|
55 |
{
|
56 |
"image": datasets.Image(),
|
57 |
"label": datasets.ClassLabel(names=list(IMAGENET2012_CLASSES.values())),
|
|
|
58 |
}
|
59 |
),
|
60 |
homepage=_HOMEPAGE,
|
@@ -65,25 +57,35 @@ class SMD(datasets.GeneratorBasedBuilder):
|
|
65 |
def _split_generators(self, dl_manager):
|
66 |
"""Returns SplitGenerators."""
|
67 |
archives = dl_manager.download(_DATA_URL)
|
|
|
68 |
|
69 |
return [
|
70 |
datasets.SplitGenerator(
|
71 |
-
name="SMD",
|
72 |
gen_kwargs={
|
73 |
"archives": [dl_manager.iter_archive(archive) for archive in archives["smd"]],
|
|
|
74 |
},
|
75 |
),
|
76 |
]
|
77 |
|
78 |
-
|
79 |
-
def _generate_examples(self, archives):
|
80 |
"""Yields examples."""
|
81 |
idx = 0
|
82 |
-
for archive in archives:
|
|
|
83 |
for path, file in archive:
|
84 |
if path.endswith(".png"):
|
85 |
synset_id = os.path.basename(os.path.dirname(path))
|
86 |
label = IMAGENET2012_CLASSES[synset_id]
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This script was modified from the imagenet-1k HF dataset repo: https://huggingface.co/datasets/imagenet-1k
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
import os
|
4 |
+
import numpy as np
|
5 |
|
6 |
import datasets
|
7 |
from datasets.tasks import ImageClassification
|
|
|
27 |
]
|
28 |
}
|
29 |
|
30 |
+
_MASK_DATA_URL = {
|
31 |
+
"smd_masks": [
|
32 |
+
f"https://huggingface.co/datasets/ariellee/Superimposed-Masked-Dataset/resolve/main/SMD_masks.tar.gz"
|
33 |
+
]
|
34 |
+
}
|
35 |
|
36 |
class SMD(datasets.GeneratorBasedBuilder):
|
37 |
VERSION = datasets.Version("1.0.0")
|
|
|
46 |
{
|
47 |
"image": datasets.Image(),
|
48 |
"label": datasets.ClassLabel(names=list(IMAGENET2012_CLASSES.values())),
|
49 |
+
"segmentation": datasets.Sequence(datasets.Array2D(shape=(None, None), dtype="float32"))
|
50 |
}
|
51 |
),
|
52 |
homepage=_HOMEPAGE,
|
|
|
57 |
def _split_generators(self, dl_manager):
|
58 |
"""Returns SplitGenerators."""
|
59 |
archives = dl_manager.download(_DATA_URL)
|
60 |
+
mask_archives = dl_manager.download(_MASK_DATA_URL)
|
61 |
|
62 |
return [
|
63 |
datasets.SplitGenerator(
|
64 |
+
name="SMD",
|
65 |
gen_kwargs={
|
66 |
"archives": [dl_manager.iter_archive(archive) for archive in archives["smd"]],
|
67 |
+
"mask_archives": [dl_manager.iter_archive(archive) for archive in mask_archives["smd_masks"]],
|
68 |
},
|
69 |
),
|
70 |
]
|
71 |
|
72 |
+
def _generate_examples(self, archives, mask_archives):
|
|
|
73 |
"""Yields examples."""
|
74 |
idx = 0
|
75 |
+
for archive, mask_archive in zip(archives, mask_archives):
|
76 |
+
mask_files = {path: np.load(file) for path, file in mask_archive if path.endswith(".npy")}
|
77 |
for path, file in archive:
|
78 |
if path.endswith(".png"):
|
79 |
synset_id = os.path.basename(os.path.dirname(path))
|
80 |
label = IMAGENET2012_CLASSES[synset_id]
|
81 |
+
|
82 |
+
mask_file_path = path.replace(".png", "_mask.npy")
|
83 |
+
segmentation_mask = mask_files.get(mask_file_path, None)
|
84 |
+
if segmentation_mask is not None:
|
85 |
+
ex = {
|
86 |
+
"image": {"path": path, "bytes": file.read()},
|
87 |
+
"label": label,
|
88 |
+
"segmentation": segmentation_mask.tolist() # Convert numpy array to list
|
89 |
+
}
|
90 |
+
yield idx, ex
|
91 |
+
idx += 1
|