Commit
·
690248f
1
Parent(s):
dc22be6
Create test_dataset.py
Browse files- test_dataset.py +94 -0
test_dataset.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2021 The HuggingFace Datasets Authors and the current dataset script contributor.
|
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 |
+
"""chensu test animal classification dataset with images of cats and dogs"""
|
16 |
+
|
17 |
+
import os
|
18 |
+
|
19 |
+
import datasets
|
20 |
+
from datasets import load_dataset
|
21 |
+
from datasets.tasks import ImageClassification
|
22 |
+
|
23 |
+
|
24 |
+
_HOMEPAGE = "https://oss.console.aliyun.com/bucket/oss-cn-beijing/340788/object"
|
25 |
+
|
26 |
+
_CITATION = """\
|
27 |
+
@ONLINE {
|
28 |
+
author="chensu"
|
29 |
+
}
|
30 |
+
"""
|
31 |
+
|
32 |
+
_DESCRIPTION = """\
|
33 |
+
This is a test dataset used to demonstrate the process of creating a hugging face dataset
|
34 |
+
"""
|
35 |
+
|
36 |
+
_URL = "https://340788.oss-cn-beijing.aliyuncs.com/animal%20classification.zip"
|
37 |
+
|
38 |
+
_NAMES = ["cat", "dog"]
|
39 |
+
|
40 |
+
|
41 |
+
class TestDataset(datasets.GeneratorBasedBuilder):
|
42 |
+
"""Test classification dataset."""
|
43 |
+
VERSION = datasets.Version("1.1.0")
|
44 |
+
BUILDER_CONFIGS = [
|
45 |
+
datasets.BuilderConfig(name="cat", version=VERSION,
|
46 |
+
description="This part of my cat dataset"),
|
47 |
+
datasets.BuilderConfig(name="dog", version=VERSION,
|
48 |
+
description="This part of my dog dataset"),
|
49 |
+
]
|
50 |
+
|
51 |
+
def _info(self):
|
52 |
+
return datasets.DatasetInfo(
|
53 |
+
description=_DESCRIPTION,
|
54 |
+
features=datasets.Features(
|
55 |
+
{
|
56 |
+
"image_file_path": datasets.Value("string"),
|
57 |
+
"image": datasets.Image(),
|
58 |
+
"labels": datasets.features.ClassLabel(names=_NAMES),
|
59 |
+
}
|
60 |
+
),
|
61 |
+
supervised_keys=("image", "labels"),
|
62 |
+
homepage=_HOMEPAGE,
|
63 |
+
citation=_CITATION,
|
64 |
+
task_templates=[ImageClassification(image_column="image", label_column="labels")],
|
65 |
+
)
|
66 |
+
|
67 |
+
def _split_generators(self, dl_manager):
|
68 |
+
data_dir = dl_manager.download_and_extract(_URL)
|
69 |
+
sub_set = self.config.name
|
70 |
+
return [
|
71 |
+
datasets.SplitGenerator(
|
72 |
+
name=datasets.Split.TRAIN,
|
73 |
+
gen_kwargs={
|
74 |
+
"files": dl_manager.iter_files(os.path.join(data_dir, sub_set, "train")), # oss://.../cat/train
|
75 |
+
},
|
76 |
+
),
|
77 |
+
datasets.SplitGenerator(
|
78 |
+
name=datasets.Split.TEST,
|
79 |
+
gen_kwargs={
|
80 |
+
"files": dl_manager.iter_files(os.path.join(data_dir, sub_set, "test")),# oss://.../cat/test
|
81 |
+
},
|
82 |
+
),
|
83 |
+
]
|
84 |
+
|
85 |
+
|
86 |
+
def _generate_examples(self, files):
|
87 |
+
for i, path in enumerate(files):
|
88 |
+
file_name = os.path.basename(path)
|
89 |
+
if file_name.endswith(".jpeg"):
|
90 |
+
yield i, {
|
91 |
+
"image_file_path": path,
|
92 |
+
"image": path,
|
93 |
+
"labels": os.path.basename(os.path.dirname(path)).lower(),
|
94 |
+
}
|