Create Oxford102FlowerDataset.py
Browse files- Oxford102FlowerDataset.py +188 -0
Oxford102FlowerDataset.py
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Oxford 102 flower loading script."""
|
2 |
+
import csv
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
import datasets
|
8 |
+
import scipy.io
|
9 |
+
|
10 |
+
_DESCRIPTION = """\
|
11 |
+
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.
|
12 |
+
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.
|
13 |
+
"""
|
14 |
+
|
15 |
+
|
16 |
+
_HOMEPAGE = "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/"
|
17 |
+
|
18 |
+
# TODO: Add the licence for the dataset here if you can find it
|
19 |
+
_LICENSE = ""
|
20 |
+
|
21 |
+
_NAMES = [
|
22 |
+
"pink primrose",
|
23 |
+
"hard-leaved pocket orchid",
|
24 |
+
"canterbury bells",
|
25 |
+
"sweet pea",
|
26 |
+
"english marigold",
|
27 |
+
"tiger lily",
|
28 |
+
"moon orchid",
|
29 |
+
"bird of paradise",
|
30 |
+
"monkshood",
|
31 |
+
"globe thistle",
|
32 |
+
"snapdragon",
|
33 |
+
"colt's foot",
|
34 |
+
"king protea",
|
35 |
+
"spear thistle",
|
36 |
+
"yellow iris",
|
37 |
+
"globe-flower",
|
38 |
+
"purple coneflower",
|
39 |
+
"peruvian lily",
|
40 |
+
"balloon flower",
|
41 |
+
"giant white arum lily",
|
42 |
+
"fire lily",
|
43 |
+
"pincushion flower",
|
44 |
+
"fritillary",
|
45 |
+
"red ginger",
|
46 |
+
"grape hyacinth",
|
47 |
+
"corn poppy",
|
48 |
+
"prince of wales feathers",
|
49 |
+
"stemless gentian",
|
50 |
+
"artichoke",
|
51 |
+
"sweet william",
|
52 |
+
"carnation",
|
53 |
+
"garden phlox",
|
54 |
+
"love in the mist",
|
55 |
+
"mexican aster",
|
56 |
+
"alpine sea holly",
|
57 |
+
"ruby-lipped cattleya",
|
58 |
+
"cape flower",
|
59 |
+
"great masterwort",
|
60 |
+
"siam tulip",
|
61 |
+
"lenten rose",
|
62 |
+
"barbeton daisy",
|
63 |
+
"daffodil",
|
64 |
+
"sword lily",
|
65 |
+
"poinsettia",
|
66 |
+
"bolero deep blue",
|
67 |
+
"wallflower",
|
68 |
+
"marigold",
|
69 |
+
"buttercup",
|
70 |
+
"oxeye daisy",
|
71 |
+
"common dandelion",
|
72 |
+
"petunia",
|
73 |
+
"wild pansy",
|
74 |
+
"primula",
|
75 |
+
"sunflower",
|
76 |
+
"pelargonium",
|
77 |
+
"bishop of llandaff",
|
78 |
+
"gaura",
|
79 |
+
"geranium",
|
80 |
+
"orange dahlia",
|
81 |
+
"pink-yellow dahlia?",
|
82 |
+
"cautleya spicata",
|
83 |
+
"japanese anemone",
|
84 |
+
"black-eyed susan",
|
85 |
+
"silverbush",
|
86 |
+
"californian poppy",
|
87 |
+
"osteospermum",
|
88 |
+
"spring crocus",
|
89 |
+
"bearded iris",
|
90 |
+
"windflower",
|
91 |
+
"tree poppy",
|
92 |
+
"gazania",
|
93 |
+
"azalea",
|
94 |
+
"water lily",
|
95 |
+
"rose",
|
96 |
+
"thorn apple",
|
97 |
+
"morning glory",
|
98 |
+
"passion flower",
|
99 |
+
"lotus",
|
100 |
+
"toad lily",
|
101 |
+
"anthurium",
|
102 |
+
"frangipani",
|
103 |
+
"clematis",
|
104 |
+
"hibiscus",
|
105 |
+
"columbine",
|
106 |
+
"desert-rose",
|
107 |
+
"tree mallow",
|
108 |
+
"magnolia",
|
109 |
+
"cyclamen",
|
110 |
+
"watercress",
|
111 |
+
"canna lily",
|
112 |
+
"hippeastrum",
|
113 |
+
"bee balm",
|
114 |
+
"ball moss",
|
115 |
+
"foxglove",
|
116 |
+
"bougainvillea",
|
117 |
+
"camellia",
|
118 |
+
"mallow",
|
119 |
+
"mexican petunia",
|
120 |
+
"bromelia",
|
121 |
+
"blanket flower",
|
122 |
+
"trumpet creeper",
|
123 |
+
"blackberry lily",
|
124 |
+
]
|
125 |
+
|
126 |
+
_URLS = {
|
127 |
+
"images": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz",
|
128 |
+
"labels": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/imagelabels.mat",
|
129 |
+
"setids": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/setid.mat",
|
130 |
+
# "segmentations": "https://www.robots.ox.ac.uk/~vgg/data/flowers/102/102segmentations.tgz" #todo
|
131 |
+
}
|
132 |
+
|
133 |
+
|
134 |
+
class Oxford102FlowerDataset(datasets.GeneratorBasedBuilder):
|
135 |
+
"""Oxford 102 flower dataset."""
|
136 |
+
VERSION = datasets.Version("1.0.0")
|
137 |
+
|
138 |
+
def _info(self):
|
139 |
+
features = datasets.Features(
|
140 |
+
{
|
141 |
+
"image": datasets.Image(),
|
142 |
+
"label": datasets.features.ClassLabel(names=_NAMES),
|
143 |
+
}
|
144 |
+
)
|
145 |
+
|
146 |
+
return datasets.DatasetInfo(
|
147 |
+
description=_DESCRIPTION,
|
148 |
+
features=features,
|
149 |
+
homepage=_HOMEPAGE,
|
150 |
+
license=_LICENSE,
|
151 |
+
)
|
152 |
+
|
153 |
+
def _split_generators(self, dl_manager):
|
154 |
+
data_dir = dl_manager.download_and_extract(_URLS)
|
155 |
+
|
156 |
+
gen_kwargs_commun = {
|
157 |
+
"images_dir": Path(data_dir["images"]) / "jpg",
|
158 |
+
"labels_path": Path(data_dir["labels"]),
|
159 |
+
"setids_path": Path(data_dir["setids"]),
|
160 |
+
}
|
161 |
+
return [
|
162 |
+
datasets.SplitGenerator(
|
163 |
+
name=datasets.Split.TRAIN,
|
164 |
+
gen_kwargs={"split": "trnid", **gen_kwargs_commun},
|
165 |
+
),
|
166 |
+
datasets.SplitGenerator(
|
167 |
+
name=datasets.Split.VALIDATION,
|
168 |
+
gen_kwargs={"split": "valid", **gen_kwargs_commun},
|
169 |
+
),
|
170 |
+
datasets.SplitGenerator(
|
171 |
+
name=datasets.Split.TEST,
|
172 |
+
gen_kwargs={"split": "tstid", **gen_kwargs_commun},
|
173 |
+
),
|
174 |
+
]
|
175 |
+
|
176 |
+
def _generate_examples(self, images_dir, labels_path, setids_path, split):
|
177 |
+
with open(labels_path, "rb") as f:
|
178 |
+
labels = scipy.io.loadmat(f)["labels"][0]
|
179 |
+
with open(setids_path, "rb") as f:
|
180 |
+
examples = scipy.io.loadmat(f)[split][0]
|
181 |
+
|
182 |
+
for image_id in examples:
|
183 |
+
file_name = f"image_{image_id:05d}.jpg"
|
184 |
+
record = {
|
185 |
+
"image": str(images_dir / file_name),
|
186 |
+
"label": _NAMES[labels[image_id - 1] - 1],
|
187 |
+
}
|
188 |
+
yield file_name, record
|