mikewang commited on
Commit
650d54e
·
1 Parent(s): 510b2aa

upload dataset

Browse files
Files changed (2) hide show
  1. AwA2.py +237 -0
  2. README.md +43 -0
AwA2.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
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
+
15
+ """Animals with Attributes v2 (AwA2)"""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+ _CITATION = """\
25
+ @article{xian2018zero,
26
+ title={Zero-shot learning—a comprehensive evaluation of the good, the bad and the ugly},
27
+ author={Xian, Yongqin and Lampert, Christoph H and Schiele, Bernt and Akata, Zeynep},
28
+ journal={IEEE transactions on pattern analysis and machine intelligence},
29
+ volume={41},
30
+ number={9},
31
+ pages={2251--2265},
32
+ year={2018},
33
+ publisher={IEEE}
34
+ }
35
+ """
36
+
37
+ # TODO: Add description of the dataset here
38
+ # You can copy an official description
39
+ _DESCRIPTION = """\
40
+ **Homepage:** https://cvml.ista.ac.at/AwA2/
41
+
42
+ **IMPORTANT NOTES**
43
+ - This HF dataset loads the instances with class-level annotations.
44
+ - Images and License can be downloaded from: https://cvml.ista.ac.at/AwA2/AwA2-data.zip
45
+ """
46
+
47
+ # TODO: Add a link to an official homepage for the dataset here
48
+ _HOMEPAGE = "ttps://cvml.ista.ac.at/AwA2/"
49
+
50
+ # TODO: Add the licence for the dataset here if you can find it
51
+ _LICENSE = ""
52
+
53
+ # TODO: Add link to the official dataset URLs here
54
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
55
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
56
+ # _URLS = {
57
+ # # "first_domain": "https://huggingface.co/great-new-dataset-first_domain.zip",
58
+ # # "second_domain": "https://huggingface.co/great-new-dataset-second_domain.zip",
59
+ # }
60
+
61
+ _URLS = {
62
+ "data": "https://cvml.ista.ac.at/AwA2/AwA2-data.zip", # including images
63
+ # "annotation": "https://cvml.ista.ac.at/AwA2/AwA2-base.zip",
64
+ # "features": "http://cvml.ist.ac.at/AwA2/AwA2-features.zip",
65
+ }
66
+
67
+
68
+ def _load_AwA2_dataset(datadir):
69
+ image_dir = os.path.join(datadir, "JPEGImages")
70
+ classes_path = os.path.join(datadir, "classes.txt")
71
+ predicates_path = os.path.join(datadir, "predicates.txt")
72
+ annotation_binary = os.path.join(datadir, "predicate-matrix-binary.txt")
73
+ annotation_continuous = os.path.join(datadir, "predicate-matrix-continuous.txt")
74
+
75
+ # load classes
76
+ classes = []
77
+ with open(classes_path, "r") as f:
78
+ for line in f:
79
+ classes.append(line.split("\t")[1].strip())
80
+ # load predicates
81
+ predicates = []
82
+ with open(predicates_path, "r") as f:
83
+ for line in f:
84
+ predicates.append(line.split("\t")[1].strip())
85
+
86
+ # class to annotation binary
87
+ annotation_binary_list = []
88
+ with open(annotation_binary, "r") as f:
89
+ for line in f:
90
+ ann = [int(x) for x in line.strip().split(" ")]
91
+ assert len(ann) == len(predicates)
92
+ annotation_binary_list.append(ann)
93
+ class_to_annotation_binary = dict(zip(classes, annotation_binary_list))
94
+ # class to annotation continuous
95
+ annotation_continuous_list = []
96
+ with open(annotation_continuous, "r") as f:
97
+ for line in f:
98
+ ann = [float(x) for x in line.strip().split(" ") if x != ""]
99
+ assert len(ann) == len(predicates)
100
+ annotation_continuous_list.append(ann)
101
+ class_to_annotation_continuous = dict(zip(classes, annotation_continuous_list))
102
+
103
+ print("classes:", len(classes), classes)
104
+ print("attribute types:", len(predicates), predicates)
105
+
106
+ data = []
107
+ # list all images in image dir
108
+ for class_type in os.listdir(image_dir):
109
+ image_class_dir = os.path.join(image_dir, class_type)
110
+ for img_name in os.listdir(image_class_dir):
111
+ data.append(
112
+ {
113
+ "image_id": img_name,
114
+ "image_path": os.path.join(image_class_dir, img_name),
115
+ "class": class_type,
116
+ "attributes_binary": class_to_annotation_binary[class_type],
117
+ "attributes_continuous": class_to_annotation_continuous[class_type],
118
+ "attribute_types": predicates
119
+ }
120
+ )
121
+ return data
122
+
123
+
124
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
125
+ class AwA2(datasets.GeneratorBasedBuilder):
126
+ """TODO: Short description of my dataset."""
127
+
128
+ VERSION = datasets.Version("1.0.0")
129
+
130
+ # This is an example of a dataset with multiple configurations.
131
+ # If you don't want/need to define several sub-sets in your dataset,
132
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
133
+
134
+ # If you need to make complex sub-parts in the datasets with configurable options
135
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
136
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
137
+
138
+ # You will be able to load one or the other configurations in the following list with
139
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
140
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
141
+ # BUILDER_CONFIGS = [
142
+ # datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
143
+ # datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
144
+ # ]
145
+
146
+ # DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
147
+
148
+ def _info(self):
149
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
150
+ # if self.config.name == "first_domain": # This is the name of the configuration selected in BUILDER_CONFIGS above
151
+ # features = datasets.Features(
152
+ # {
153
+ # "sentence": datasets.Value("string"),
154
+ # "option1": datasets.Value("string"),
155
+ # "answer": datasets.Value("string")
156
+ # # These are the features of your dataset like images, labels ...
157
+ # }
158
+ # )
159
+ # else: # This is an example to show how to have different features for "first_domain" and "second_domain"
160
+ # features = datasets.Features(
161
+ # {
162
+ # "sentence": datasets.Value("string"),
163
+ # "option2": datasets.Value("string"),
164
+ # "second_domain_answer": datasets.Value("string")
165
+ # # These are the features of your dataset like images, labels ...
166
+ # }
167
+ # )
168
+
169
+ features = datasets.Features(
170
+ {
171
+ "image_id": datasets.Value("string"),
172
+ "image_path": datasets.Value("string"),
173
+ "class": datasets.Value("string"),
174
+ "attributes_binary": datasets.features.Sequence(datasets.Value("int32")),
175
+ "attributes_continuous": datasets.features.Sequence(datasets.Value("float32")),
176
+ "attribute_types": datasets.features.Sequence(datasets.Value("string")),
177
+ }
178
+ )
179
+
180
+ return datasets.DatasetInfo(
181
+ # This is the description that will appear on the datasets page.
182
+ description=_DESCRIPTION,
183
+ # This defines the different columns of the dataset and their types
184
+ features=features, # Here we define them above because they are different between the two configurations
185
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
186
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
187
+ # supervised_keys=("sentence", "label"),
188
+ # Homepage of the dataset for documentation
189
+ homepage=_HOMEPAGE,
190
+ # License for the dataset if available
191
+ license=_LICENSE,
192
+ # Citation for the dataset
193
+ citation=_CITATION,
194
+ )
195
+
196
+ def _split_generators(self, dl_manager):
197
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
198
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
199
+
200
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
201
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
202
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
203
+
204
+ downloaded_files = dl_manager.download_and_extract(_URLS)
205
+
206
+ # downloaded_files = {
207
+ # "data": "/shared/nas/data/m1/shared-resource/vision-language/data/raw/AwA2/Animals_with_Attributes2",
208
+ # }
209
+
210
+ print("downloaded_files: ", downloaded_files)
211
+ return [
212
+ datasets.SplitGenerator(
213
+ name=datasets.Split.TRAIN,
214
+ # These kwargs will be passed to _generate_examples
215
+ gen_kwargs={
216
+ "filepath": downloaded_files["data"],
217
+ "split": "train",
218
+ },
219
+ )
220
+ ]
221
+
222
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
223
+ def _generate_examples(self, filepath, split):
224
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
225
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
226
+
227
+ data = _load_AwA2_dataset(filepath)
228
+
229
+ for key, row in enumerate(data):
230
+ yield key, {
231
+ "image_id": row["image_id"],
232
+ "image_path": row["image_path"],
233
+ "class": row["class"],
234
+ "attributes_binary": row["attributes_binary"],
235
+ "attributes_continuous": row["attributes_continuous"],
236
+ "attribute_types": row["attribute_types"],
237
+ }
README.md ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pretty_name: 'Animals with Attributes v2 (AwA2)'
3
+ language:
4
+ - en
5
+ ---
6
+ # Dataset Card for Visual Attributes in the Wild (VAW)
7
+
8
+ ## Dataset Description
9
+
10
+ **Homepage:** https://cvml.ista.ac.at/AwA2/
11
+
12
+ **IMPORTANT NOTES**
13
+ - This HF dataset downloads the images, and loads the image instances with class-level annotations.
14
+ - The "train" split in this HF dataset contains all the images. For the original proposed splits and the proposed splits version 2.0, please refer to [here](https://www.mpi-inf.mpg.de/departments/computer-vision-and-machine-learning/research/zero-shot-learning/zero-shot-learning-the-good-the-bad-and-the-ugly/).
15
+ - Images and License can be downloaded from: https://cvml.ista.ac.at/AwA2/AwA2-data.zip
16
+
17
+
18
+ **Paper Citation:**
19
+ ```
20
+ @article{xian2018zero,
21
+ title={Zero-shot learning—a comprehensive evaluation of the good, the bad and the ugly},
22
+ author={Xian, Yongqin and Lampert, Christoph H and Schiele, Bernt and Akata, Zeynep},
23
+ journal={IEEE transactions on pattern analysis and machine intelligence},
24
+ volume={41},
25
+ number={9},
26
+ pages={2251--2265},
27
+ year={2018},
28
+ publisher={IEEE}
29
+ }
30
+ ```
31
+
32
+ ## Dataset Summary
33
+ This dataset provides a platform to benchmark transfer-learning algorithms, in particular attribute base classification and zero-shot learning [1]. It can act as a drop-in replacement to the original Animals with Attributes (AwA) dataset [2,3], as it has the same class structure and almost the same characteristics.
34
+ It consists of 37322 images of 50 animals classes with pre-extracted feature representations for each image. The classes are aligned with Osherson's classical class/attribute matrix [3,4], thereby providing 85 numeric attribute values for each class. Using the shared attributes, it is possible to transfer information between different classes.
35
+ The image data was collected from public sources, such as Flickr, in 2016. In the process we made sure to only include images that are licensed for free use and redistribution, please see the archive for the individual license files. If the dataset contains an image for which you hold the copyright and that was not licensed freely, please contact us at , so we can remove it from the collection.
36
+
37
+ **References**
38
+
39
+ [1] Y. Xian, C. H. Lampert, B. Schiele, Z. Akata. "Zero-Shot Learning - A Comprehensive Evaluation of the Good, the Bad and the Ugly", IEEE Transactions on Pattern Analysis and Machine Intelligence (T-PAMI) 40(8), 2018. (arXiv:1707.00600 [cs.CV])
40
+ [2] C. H. Lampert, H. Nickisch, and S. Harmeling. "Learning To Detect Unseen Object Classes by Between-Class Attribute Transfer". In CVPR, 2009
41
+ [3] C. H. Lampert, H. Nickisch, and S. Harmeling. "Attribute-Based Classification for Zero-Shot Visual Object Categorization". IEEE T-PAMI, 2013
42
+ [4] D. N. Osherson, J. Stern, O. Wilkie, M. Stob, and E. E. Smith. "Default probability". Cognitive Science, 15(2), 1991.
43
+ [5] C. Kemp, J. B. Tenenbaum, T. L. Griffiths, T. Yamada, and N. Ueda. "Learning systems of concepts with an infinite relational model". In AAAI, 2006.