SaulLu commited on
Commit
9deff5c
·
1 Parent(s): 670d25e

add loading script

Browse files
Files changed (1) hide show
  1. DTD_Describable-Textures-Dataset.py +166 -0
DTD_Describable-Textures-Dataset.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """DTD loading script."""
15
+
16
+
17
+ from pathlib import Path
18
+
19
+ import datasets
20
+
21
+ _CITATION = """\
22
+ @InProceedings{cimpoi14describing,
23
+ Author = {M. Cimpoi and S. Maji and I. Kokkinos and S. Mohamed and and A. Vedaldi},
24
+ Title = {Describing Textures in the Wild},
25
+ Booktitle = {Proceedings of the {IEEE} Conf. on Computer Vision and Pattern Recognition ({CVPR})},
26
+ Year = {2014}}
27
+ """
28
+
29
+ _DESCRIPTION = """\
30
+ DTD is a texture database, consisting of 5640 images, organized according to a list of 47 terms (categories) inspired from human perception. There are 120 images for each category. Image sizes range between 300x300 and 640x640, and the images contain at least 90% of the surface representing the category attribute. The images were collected from Google and Flickr by entering our proposed attributes and related terms as search queries. The images were annotated using Amazon Mechanical Turk in several iterations. For each image we provide key attribute (main category) and a list of joint attributes.
31
+ """
32
+
33
+ _HOMEPAGE = "https://www.robots.ox.ac.uk/%7Evgg/data/dtd/index.html"
34
+
35
+
36
+ _LICENSE = ""
37
+
38
+ _URL = "https://www.robots.ox.ac.uk/~vgg/data/dtd/download/dtd-r1.0.1.tar.gz"
39
+
40
+ _NAMES = [
41
+ "banded",
42
+ "blotchy",
43
+ "braided",
44
+ "bubbly",
45
+ "bumpy",
46
+ "chequered",
47
+ "cobwebbed",
48
+ "cracked",
49
+ "crosshatched",
50
+ "crystalline",
51
+ "dotted",
52
+ "fibrous",
53
+ "flecked",
54
+ "freckled",
55
+ "frilly",
56
+ "gauzy",
57
+ "grid",
58
+ "grooved",
59
+ "honeycombed",
60
+ "interlaced",
61
+ "knitted",
62
+ "lacelike",
63
+ "lined",
64
+ "marbled",
65
+ "matted",
66
+ "meshed",
67
+ "paisley",
68
+ "perforated",
69
+ "pitted",
70
+ "pleated",
71
+ "polka-dotted",
72
+ "porous",
73
+ "potholed",
74
+ "scaly",
75
+ "smeared",
76
+ "spiralled",
77
+ "sprinkled",
78
+ "stained",
79
+ "stratified",
80
+ "striped",
81
+ "studded",
82
+ "swirly",
83
+ "veined",
84
+ "waffled",
85
+ "woven",
86
+ "wrinkled",
87
+ "zigzagged",
88
+ ]
89
+
90
+
91
+ class DTDDataset(datasets.GeneratorBasedBuilder):
92
+
93
+ VERSION = datasets.Version("1.0.0")
94
+
95
+ BUILDER_CONFIGS = [
96
+ datasets.BuilderConfig(
97
+ name=f"partition_{idx}",
98
+ version=datasets.Version("1.0.0"),
99
+ description=f"DTD defines 10-fold CV partitions. This part of the dataset covers the {idx} fold.",
100
+ )
101
+ for idx in range(1, 11)
102
+ ]
103
+
104
+ def _info(self):
105
+ features = datasets.Features(
106
+ {
107
+ "image": datasets.Image(),
108
+ "label": datasets.features.ClassLabel(names=_NAMES),
109
+ }
110
+ )
111
+
112
+ return datasets.DatasetInfo(
113
+ description=_DESCRIPTION,
114
+ features=features,
115
+ homepage=_HOMEPAGE,
116
+ license=_LICENSE,
117
+ citation=_CITATION,
118
+ )
119
+
120
+ def _split_generators(self, dl_manager):
121
+ data_dir = dl_manager.download_and_extract(_URL)
122
+ partition_id = self.config.name.split("_")[1]
123
+ images_dir = Path(data_dir) / "dtd" / "images"
124
+ labels_dir = Path(data_dir) / "dtd" / "labels"
125
+ return [
126
+ datasets.SplitGenerator(
127
+ name=datasets.Split.TRAIN,
128
+ gen_kwargs={
129
+ "images_dir": images_dir,
130
+ "labels_dir": labels_dir,
131
+ "partition_id": partition_id,
132
+ "split": "train",
133
+ },
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={
138
+ "images_dir": images_dir,
139
+ "labels_dir": labels_dir,
140
+ "partition_id": partition_id,
141
+ "split": "val",
142
+ },
143
+ ),
144
+ datasets.SplitGenerator(
145
+ name=datasets.Split.TEST,
146
+ gen_kwargs={
147
+ "images_dir": images_dir,
148
+ "labels_dir": labels_dir,
149
+ "partition_id": partition_id,
150
+ "split": "test",
151
+ },
152
+ ),
153
+ ]
154
+
155
+ def _generate_examples(self, images_dir, labels_dir, partition_id, split):
156
+ with open(
157
+ labels_dir / f"{split}{partition_id}.txt", "r", encoding="utf-8"
158
+ ) as split_file:
159
+ for line in split_file:
160
+ fname = line.strip()
161
+ label = fname.split("/")[0]
162
+ record = {
163
+ "image": str(images_dir / fname),
164
+ "label": label,
165
+ }
166
+ yield fname, record