Update Superimposed-Masked-Dataset.py
Browse files- Superimposed-Masked-Dataset.py +26 -33
Superimposed-Masked-Dataset.py
CHANGED
|
@@ -1,13 +1,26 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
import datasets
|
| 7 |
from datasets.tasks import ImageClassification
|
| 8 |
|
| 9 |
from .classes import IMAGENET2012_CLASSES
|
| 10 |
-
from io import BytesIO
|
| 11 |
|
| 12 |
|
| 13 |
_CITATION = """\
|
|
@@ -28,11 +41,6 @@ _DATA_URL = {
|
|
| 28 |
]
|
| 29 |
}
|
| 30 |
|
| 31 |
-
_MASK_DATA_URL = {
|
| 32 |
-
"smd_masks": [
|
| 33 |
-
f"https://huggingface.co/datasets/ariellee/Superimposed-Masked-Dataset/resolve/main/SMD_masks.tar.gz"
|
| 34 |
-
]
|
| 35 |
-
}
|
| 36 |
|
| 37 |
class SMD(datasets.GeneratorBasedBuilder):
|
| 38 |
VERSION = datasets.Version("1.0.0")
|
|
@@ -47,7 +55,6 @@ class SMD(datasets.GeneratorBasedBuilder):
|
|
| 47 |
{
|
| 48 |
"image": datasets.Image(),
|
| 49 |
"label": datasets.ClassLabel(names=list(IMAGENET2012_CLASSES.values())),
|
| 50 |
-
"segmentation": datasets.Sequence(datasets.Array2D(shape=(None, None), dtype="float32"))
|
| 51 |
}
|
| 52 |
),
|
| 53 |
homepage=_HOMEPAGE,
|
|
@@ -57,40 +64,26 @@ class SMD(datasets.GeneratorBasedBuilder):
|
|
| 57 |
|
| 58 |
def _split_generators(self, dl_manager):
|
| 59 |
"""Returns SplitGenerators."""
|
| 60 |
-
archives = dl_manager.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
return [
|
| 64 |
datasets.SplitGenerator(
|
| 65 |
-
name="SMD",
|
| 66 |
gen_kwargs={
|
| 67 |
-
"archives": archives["smd"],
|
| 68 |
-
"mask_archives": mask_archives["smd_masks"],
|
| 69 |
},
|
| 70 |
),
|
| 71 |
]
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
"""Yields examples."""
|
| 75 |
idx = 0
|
| 76 |
-
mask_files = {}
|
| 77 |
-
for mask_archive in mask_archives:
|
| 78 |
-
for path, file in dl_manager.iter_archive(mask_archive):
|
| 79 |
-
if path.endswith(".npy"):
|
| 80 |
-
mask_files[path] = np.load(BytesIO(file.read()))
|
| 81 |
-
|
| 82 |
for archive in archives:
|
| 83 |
-
for path, file in
|
| 84 |
if path.endswith(".png"):
|
| 85 |
synset_id = os.path.basename(os.path.dirname(path))
|
| 86 |
label = IMAGENET2012_CLASSES[synset_id]
|
| 87 |
-
|
| 88 |
-
mask_file_path = path.replace("_occluded.png", "_mask.npy")
|
| 89 |
-
segmentation_mask = mask_files.get(mask_file_path)
|
| 90 |
-
ex = {
|
| 91 |
-
"image": {"path": path, "bytes": file.read()},
|
| 92 |
-
"label": label,
|
| 93 |
-
"segmentation": segmentation_mask.tolist() # Convert numpy array to list
|
| 94 |
-
}
|
| 95 |
yield idx, ex
|
| 96 |
-
idx += 1
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 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
|
| 22 |
|
| 23 |
from .classes import IMAGENET2012_CLASSES
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
_CITATION = """\
|
|
|
|
| 41 |
]
|
| 42 |
}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
class SMD(datasets.GeneratorBasedBuilder):
|
| 46 |
VERSION = datasets.Version("1.0.0")
|
|
|
|
| 55 |
{
|
| 56 |
"image": datasets.Image(),
|
| 57 |
"label": datasets.ClassLabel(names=list(IMAGENET2012_CLASSES.values())),
|
|
|
|
| 58 |
}
|
| 59 |
),
|
| 60 |
homepage=_HOMEPAGE,
|
|
|
|
| 64 |
|
| 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", # "SMD (occluded IN-1K val set)"
|
| 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 |
+
ex = {"image": {"path": path, "bytes": file.read()}, "label": label}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
yield idx, ex
|
| 89 |
+
idx += 1
|