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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 The HuggingFace Datasets Authors.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Oxford 102 flower loading script."""
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
import csv
|
| 18 |
+
import json
|
| 19 |
+
import os
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
|
| 22 |
+
import datasets
|
| 23 |
+
import scipy.io
|
| 24 |
+
|
| 25 |
+
_CITATION = """\
|
| 26 |
+
@InProceedings{Nilsback08,
|
| 27 |
+
author = "Nilsback, M-E. and Zisserman, A.",
|
| 28 |
+
title = "Automated Flower Classification over a Large Number of Classes",
|
| 29 |
+
booktitle = "Proceedings of the Indian Conference on Computer Vision, Graphics and Image Processing",
|
| 30 |
+
year = "2008",
|
| 31 |
+
month = "Dec"
|
| 32 |
+
}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
_DESCRIPTION = """\
|
| 36 |
+
Oxford 102 flower dataset is a 102 category dataset, consisting of 102 flower categories. The flowers chosen to be flower commonly occuring in the United Kingdom. Each class consists of between 40 and 258 images. The details of the categories and the number of images for each class can be found on this category statistics page.
|
| 37 |
+
|
| 38 |
+
The images have large scale, pose and light variations. In addition, there are categories that have large variations within the category and several very similar categories. The dataset is visualized using isomap with shape and colour features.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
_HOMEPAGE = "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/"
|
| 43 |
+
|
| 44 |
+
# TODO: Add the licence for the dataset here if you can find it
|
| 45 |
+
_LICENSE = ""
|
| 46 |
+
|
| 47 |
+
_NAMES = [
|
| 48 |
+
"pink primrose",
|
| 49 |
+
"hard-leaved pocket orchid",
|
| 50 |
+
"canterbury bells",
|
| 51 |
+
"sweet pea",
|
| 52 |
+
"english marigold",
|
| 53 |
+
"tiger lily",
|
| 54 |
+
"moon orchid",
|
| 55 |
+
"bird of paradise",
|
| 56 |
+
"monkshood",
|
| 57 |
+
"globe thistle",
|
| 58 |
+
"snapdragon",
|
| 59 |
+
"colt's foot",
|
| 60 |
+
"king protea",
|
| 61 |
+
"spear thistle",
|
| 62 |
+
"yellow iris",
|
| 63 |
+
"globe-flower",
|
| 64 |
+
"purple coneflower",
|
| 65 |
+
"peruvian lily",
|
| 66 |
+
"balloon flower",
|
| 67 |
+
"giant white arum lily",
|
| 68 |
+
"fire lily",
|
| 69 |
+
"pincushion flower",
|
| 70 |
+
"fritillary",
|
| 71 |
+
"red ginger",
|
| 72 |
+
"grape hyacinth",
|
| 73 |
+
"corn poppy",
|
| 74 |
+
"prince of wales feathers",
|
| 75 |
+
"stemless gentian",
|
| 76 |
+
"artichoke",
|
| 77 |
+
"sweet william",
|
| 78 |
+
"carnation",
|
| 79 |
+
"garden phlox",
|
| 80 |
+
"love in the mist",
|
| 81 |
+
"mexican aster",
|
| 82 |
+
"alpine sea holly",
|
| 83 |
+
"ruby-lipped cattleya",
|
| 84 |
+
"cape flower",
|
| 85 |
+
"great masterwort",
|
| 86 |
+
"siam tulip",
|
| 87 |
+
"lenten rose",
|
| 88 |
+
"barbeton daisy",
|
| 89 |
+
"daffodil",
|
| 90 |
+
"sword lily",
|
| 91 |
+
"poinsettia",
|
| 92 |
+
"bolero deep blue",
|
| 93 |
+
"wallflower",
|
| 94 |
+
"marigold",
|
| 95 |
+
"buttercup",
|
| 96 |
+
"oxeye daisy",
|
| 97 |
+
"common dandelion",
|
| 98 |
+
"petunia",
|
| 99 |
+
"wild pansy",
|
| 100 |
+
"primula",
|
| 101 |
+
"sunflower",
|
| 102 |
+
"pelargonium",
|
| 103 |
+
"bishop of llandaff",
|
| 104 |
+
"gaura",
|
| 105 |
+
"geranium",
|
| 106 |
+
"orange dahlia",
|
| 107 |
+
"pink-yellow dahlia?",
|
| 108 |
+
"cautleya spicata",
|
| 109 |
+
"japanese anemone",
|
| 110 |
+
"black-eyed susan",
|
| 111 |
+
"silverbush",
|
| 112 |
+
"californian poppy",
|
| 113 |
+
"osteospermum",
|
| 114 |
+
"spring crocus",
|
| 115 |
+
"bearded iris",
|
| 116 |
+
"windflower",
|
| 117 |
+
"tree poppy",
|
| 118 |
+
"gazania",
|
| 119 |
+
"azalea",
|
| 120 |
+
"water lily",
|
| 121 |
+
"rose",
|
| 122 |
+
"thorn apple",
|
| 123 |
+
"morning glory",
|
| 124 |
+
"passion flower",
|
| 125 |
+
"lotus",
|
| 126 |
+
"toad lily",
|
| 127 |
+
"anthurium",
|
| 128 |
+
"frangipani",
|
| 129 |
+
"clematis",
|
| 130 |
+
"hibiscus",
|
| 131 |
+
"columbine",
|
| 132 |
+
"desert-rose",
|
| 133 |
+
"tree mallow",
|
| 134 |
+
"magnolia",
|
| 135 |
+
"cyclamen",
|
| 136 |
+
"watercress",
|
| 137 |
+
"canna lily",
|
| 138 |
+
"hippeastrum",
|
| 139 |
+
"bee balm",
|
| 140 |
+
"ball moss",
|
| 141 |
+
"foxglove",
|
| 142 |
+
"bougainvillea",
|
| 143 |
+
"camellia",
|
| 144 |
+
"mallow",
|
| 145 |
+
"mexican petunia",
|
| 146 |
+
"bromelia",
|
| 147 |
+
"blanket flower",
|
| 148 |
+
"trumpet creeper",
|
| 149 |
+
"blackberry lily",
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
_URLS = {
|
| 153 |
+
"images": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz",
|
| 154 |
+
"labels": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/imagelabels.mat",
|
| 155 |
+
"setids": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/setid.mat",
|
| 156 |
+
# "segmentations": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102segmentations.tgz" #todo
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
class Oxford102FlowerDataset(datasets.GeneratorBasedBuilder):
|
| 161 |
+
|
| 162 |
+
VERSION = datasets.Version("1.0.0")
|
| 163 |
+
|
| 164 |
+
def _info(self):
|
| 165 |
+
features = datasets.Features(
|
| 166 |
+
{
|
| 167 |
+
"image": datasets.Image(),
|
| 168 |
+
"label": datasets.features.ClassLabel(names=_NAMES),
|
| 169 |
+
}
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
return datasets.DatasetInfo(
|
| 173 |
+
description=_DESCRIPTION,
|
| 174 |
+
features=features,
|
| 175 |
+
homepage=_HOMEPAGE,
|
| 176 |
+
license=_LICENSE,
|
| 177 |
+
citation=_CITATION,
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
def _split_generators(self, dl_manager):
|
| 181 |
+
data_dir = dl_manager.download_and_extract(_URLS)
|
| 182 |
+
|
| 183 |
+
gen_kwargs_commun = {
|
| 184 |
+
"images_dir": Path(data_dir["images"]) / "jpg",
|
| 185 |
+
"labels_path": Path(data_dir["labels"]),
|
| 186 |
+
"setids_path": Path(data_dir["setids"]),
|
| 187 |
+
}
|
| 188 |
+
return [
|
| 189 |
+
datasets.SplitGenerator(
|
| 190 |
+
name=datasets.Split.TRAIN,
|
| 191 |
+
gen_kwargs={"split": "trnid", **gen_kwargs_commun},
|
| 192 |
+
),
|
| 193 |
+
datasets.SplitGenerator(
|
| 194 |
+
name=datasets.Split.VALIDATION,
|
| 195 |
+
gen_kwargs={"split": "valid", **gen_kwargs_commun},
|
| 196 |
+
),
|
| 197 |
+
datasets.SplitGenerator(
|
| 198 |
+
name=datasets.Split.TEST,
|
| 199 |
+
gen_kwargs={"split": "tstid", **gen_kwargs_commun},
|
| 200 |
+
),
|
| 201 |
+
]
|
| 202 |
+
|
| 203 |
+
def _generate_examples(self, images_dir, labels_path, setids_path, split):
|
| 204 |
+
with open(labels_path, "rb") as f:
|
| 205 |
+
labels = scipy.io.loadmat(f)["labels"][0]
|
| 206 |
+
with open(setids_path, "rb") as f:
|
| 207 |
+
examples = scipy.io.loadmat(f)[split][0]
|
| 208 |
+
|
| 209 |
+
for image_id in examples:
|
| 210 |
+
file_name = f"image_{image_id:05d}.jpg"
|
| 211 |
+
record = {
|
| 212 |
+
"image": str(images_dir / file_name),
|
| 213 |
+
"label": _NAMES[labels[image_id - 1] - 1],
|
| 214 |
+
}
|
| 215 |
+
yield file_name, record
|