Create cats_vs_dogs_sample.py
Browse files- cats_vs_dogs_sample.py +78 -0
cats_vs_dogs_sample.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Sample of the Microsoft Cats vs. Dogs dataset"""
|
16 |
+
|
17 |
+
from pathlib import Path
|
18 |
+
from typing import List
|
19 |
+
|
20 |
+
import datasets
|
21 |
+
from datasets.tasks import ImageClassification
|
22 |
+
|
23 |
+
|
24 |
+
logger = datasets.logging.get_logger(__name__)
|
25 |
+
|
26 |
+
_URL = "https://huggingface.co/datasets/hf-internal-testing/cats_vs_dogs_sample/raw/main/cats_and_dogs_sample.zip"
|
27 |
+
|
28 |
+
_HOMEPAGE = "https://www.microsoft.com/en-us/download/details.aspx?id=54765"
|
29 |
+
|
30 |
+
_DESCRIPTION = "A 50 image sample of microsoft's cats vs. dogs dataset for unit testing."
|
31 |
+
|
32 |
+
_CITATION = """\
|
33 |
+
@Inproceedings (Conference){asirra-a-captcha-that-exploits-interest-aligned-manual-image-categorization,
|
34 |
+
author = {Elson, Jeremy and Douceur, John (JD) and Howell, Jon and Saul, Jared},
|
35 |
+
title = {Asirra: A CAPTCHA that Exploits Interest-Aligned Manual Image Categorization},
|
36 |
+
booktitle = {Proceedings of 14th ACM Conference on Computer and Communications Security (CCS)},
|
37 |
+
year = {2007},
|
38 |
+
month = {October},
|
39 |
+
publisher = {Association for Computing Machinery, Inc.},
|
40 |
+
url = {https://www.microsoft.com/en-us/research/publication/asirra-a-captcha-that-exploits-interest-aligned-manual-image-categorization/},
|
41 |
+
edition = {Proceedings of 14th ACM Conference on Computer and Communications Security (CCS)},
|
42 |
+
}
|
43 |
+
"""
|
44 |
+
|
45 |
+
|
46 |
+
class CatsVsDogs(datasets.GeneratorBasedBuilder):
|
47 |
+
def _info(self):
|
48 |
+
return datasets.DatasetInfo(
|
49 |
+
description=_DESCRIPTION,
|
50 |
+
features=datasets.Features(
|
51 |
+
{
|
52 |
+
"image_file_path": datasets.Value("string"),
|
53 |
+
"labels": datasets.features.ClassLabel(names=["cat", "dog"]),
|
54 |
+
}
|
55 |
+
),
|
56 |
+
supervised_keys=("image_file_path", "labels"),
|
57 |
+
task_templates=[
|
58 |
+
ImageClassification(
|
59 |
+
image_file_path_column="image_file_path", label_column="labels", labels=["cat", "dog"]
|
60 |
+
)
|
61 |
+
],
|
62 |
+
homepage=_HOMEPAGE,
|
63 |
+
citation=_CITATION,
|
64 |
+
)
|
65 |
+
|
66 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
67 |
+
images_path = Path(dl_manager.download_and_extract(_URL)) / "PetImagesSample"
|
68 |
+
return [
|
69 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"images_path": images_path}),
|
70 |
+
]
|
71 |
+
|
72 |
+
def _generate_examples(self, images_path):
|
73 |
+
logger.info("generating examples from = %s", images_path)
|
74 |
+
for i, filepath in enumerate(images_path.glob("**/*.jpg")):
|
75 |
+
yield str(i), {
|
76 |
+
"image_file_path": str(filepath),
|
77 |
+
"labels": filepath.parent.name.lower(),
|
78 |
+
}
|