leonleyang commited on
Commit
0f3e0a1
·
verified ·
1 Parent(s): 13f6937

Create dtd.py

Browse files
Files changed (1) hide show
  1. dtd.py +90 -0
dtd.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tarfile
2
+ from io import BytesIO
3
+ from PIL import Image
4
+ from tqdm import tqdm
5
+ import datasets
6
+
7
+
8
+ class DTD(datasets.GeneratorBasedBuilder):
9
+ """Describable Textures Dataset (DTD)
10
+
11
+ DTD is a texture database, consisting of 5640 images, organized according to a list of 47 terms (categories)
12
+ inspired from human perception. There are 120 images for each category. Image sizes range between 300x300 and
13
+ 640x640, and the images contain at least 90% of the surface representing the category attribute. The images were
14
+ collected from Google and Flickr by entering our proposed attributes and related terms as search queries. The images
15
+ were annotated using Amazon Mechanical Turk in several iterations. For each image we provide key attribute (main
16
+ category) and a list of joint attributes.
17
+
18
+ The data is split in three equal parts, in train, validation and test, 40 images per class, for each split. We
19
+ provide the ground truth annotation for both key and joint attributes, as well as the 10 splits of the data we used
20
+ for evaluation.
21
+ """
22
+
23
+ VERSION = datasets.Version("1.0.0")
24
+
25
+ def _info(self):
26
+ return datasets.DatasetInfo(
27
+ description="""Describing Textures in the Wild (DTD) is a dataset for texture classification.
28
+ It contains 5640 images organized into 47 categories.""",
29
+ features=datasets.Features(
30
+ {
31
+ "image": datasets.Image(),
32
+ "label": datasets.ClassLabel(names=[
33
+ "banded", "blotchy", "braided", "bubbly", "bumpy", "chequered", "cobwebbed",
34
+ "cracked", "crosshatched", "crystalline", "dotted", "fibrous", "flecked",
35
+ "freckled", "frilly", "gauzy", "grid", "grooved", "honeycombed", "interlaced",
36
+ "knitted", "lacelike", "lined", "marbled", "matted", "meshed", "paisley",
37
+ "perforated", "pitted", "pleated", "polka-dotted", "porous", "potholed", "scaly",
38
+ "smeared", "spiralled", "sprinkled", "stained", "stratified", "striped",
39
+ "studded", "swirly", "veined", "waffled", "woven", "wrinkled", "zigzagged"
40
+ ])
41
+ }
42
+ ),
43
+ supervised_keys=("image", "label"),
44
+ homepage="https://www.robots.ox.ac.uk/~vgg/data/dtd/",
45
+ citation="""@InProceedings{cimpoi14describing,
46
+ Author = {M. Cimpoi and S. Maji and I. Kokkinos and S. Mohamed and and A. Vedaldi},
47
+ Title = {Describing Textures in the Wild},
48
+ Booktitle = {Proceedings of the {IEEE} Conf. on Computer Vision and Pattern Recognition ({CVPR})},
49
+ Year = {2014}}""",
50
+ )
51
+
52
+ def _split_generators(self, dl_manager):
53
+ archive_path = dl_manager.download(
54
+ "https://www.robots.ox.ac.uk/~vgg/data/dtd/download/dtd-r1.0.1.tar.gz"
55
+ )
56
+
57
+ return [
58
+ datasets.SplitGenerator(
59
+ name=datasets.Split.TRAIN,
60
+ gen_kwargs={"archive_path": archive_path, "split": "train"},
61
+ ),
62
+ datasets.SplitGenerator(
63
+ name=datasets.Split.VALIDATION,
64
+ gen_kwargs={"archive_path": archive_path, "split": "val"},
65
+ ),
66
+ datasets.SplitGenerator(
67
+ name=datasets.Split.TEST,
68
+ gen_kwargs={"archive_path": archive_path, "split": "test"},
69
+ ),
70
+ ]
71
+
72
+ def _generate_examples(self, archive_path, split):
73
+ with tarfile.open(archive_path, "r:gz") as tar:
74
+ split_file = f"dtd/labels/{split}1.txt"
75
+ file_names = self._read_split_file(tar, split_file)
76
+
77
+ for idx, file_name in enumerate(tqdm(file_names, desc=f"Processing {split} split")):
78
+ member = tar.getmember(f"dtd/images/{file_name}")
79
+ file = tar.extractfile(member)
80
+ image = Image.open(BytesIO(file.read())).convert("RGB")
81
+
82
+ yield idx, {
83
+ "image": image,
84
+ "label": file_name.split("/")[0],
85
+ }
86
+
87
+ def _read_split_file(self, tar, split_file):
88
+ """Helper function to read split file from the tar archive."""
89
+ split_content = tar.extractfile(split_file).read().decode("utf-8")
90
+ return split_content.splitlines()