File size: 10,619 Bytes
159348b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a51a586
 
 
 
 
 
 
 
 
 
 
159348b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a51a586
 
159348b
 
a51a586
159348b
 
 
 
a51a586
159348b
 
a51a586
159348b
 
 
 
a51a586
 
 
 
 
 
 
 
 
159348b
 
 
 
 
 
 
 
a51a586
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Copyright 2020 The HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""FGVC Aircraft loading script."""


import csv
import json
import os
from pathlib import Path

import datasets

_CITATION = """\
@techreport{maji13fine-grained,
   title         = {Fine-Grained Visual Classification of Aircraft},
   author        = {S. Maji and J. Kannala and E. Rahtu
                    and M. Blaschko and A. Vedaldi},
   year          = {2013},
   archivePrefix = {arXiv},
   eprint        = {1306.5151},
   primaryClass  = "cs-cv",
}
"""

_DESCRIPTION = """\
The dataset contains 10,200 images of aircraft, with 100 images for each of 102 different aircraft model variants, most of which are airplanes. The (main) aircraft in each image is annotated with a tight bounding box and a hierarchical airplane model label.

Aircraft models are organized in a four-levels hierarchy. The four levels, from finer to coarser, are:

    Model, e.g. Boeing 737-76J. Since certain models are nearly visually indistinguishable, this level is not used in the evaluation.
    Variant, e.g. Boeing 737-700. A variant collapses all the models that are visually indistinguishable into one class. The dataset comprises 102 different variants.
    Family, e.g. Boeing 737. The dataset comprises 70 different families.
    Manufacturer, e.g. Boeing. The dataset comprises 41 different manufacturers.

The data is divided into three equally-sized training, validation and test subsets. The first two sets can be used for development, and the latter should be used for final evaluation only. 
"""

_HOMEPAGE = "https://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/"

_LICENSE = "exclusively for non-commercial research purposes"

_URL = "https://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/archives/fgvc-aircraft-2013b.tar.gz"


_FAMILIES = [
    "A300",
    "A310",
    "A320",
    "A330",
    "A340",
    "A380",
    "ATR-42",
    "ATR-72",
    "An-12",
    "BAE 146",
    "BAE-125",
    "Beechcraft 1900",
    "Boeing 707",
    "Boeing 717",
    "Boeing 727",
    "Boeing 737",
    "Boeing 747",
    "Boeing 757",
    "Boeing 767",
    "Boeing 777",
    "C-130",
    "C-47",
    "CRJ-200",
    "CRJ-700",
    "Cessna 172",
    "Cessna 208",
    "Cessna Citation",
    "Challenger 600",
    "DC-10",
    "DC-3",
    "DC-6",
    "DC-8",
    "DC-9",
    "DH-82",
    "DHC-1",
    "DHC-6",
    "DR-400",
    "Dash 8",
    "Dornier 328",
    "EMB-120",
    "Embraer E-Jet",
    "Embraer ERJ 145",
    "Embraer Legacy 600",
    "Eurofighter Typhoon",
    "F-16",
    "F/A-18",
    "Falcon 2000",
    "Falcon 900",
    "Fokker 100",
    "Fokker 50",
    "Fokker 70",
    "Global Express",
    "Gulfstream",
    "Hawk T1",
    "Il-76",
    "King Air",
    "L-1011",
    "MD-11",
    "MD-80",
    "MD-90",
    "Metroliner",
    "PA-28",
    "SR-20",
    "Saab 2000",
    "Saab 340",
    "Spitfire",
    "Tornado",
    "Tu-134",
    "Tu-154",
    "Yak-42",
]


_MANUFACTURERS = [
    "ATR",
    "Airbus",
    "Antonov",
    "Beechcraft",
    "Boeing",
    "Bombardier Aerospace",
    "British Aerospace",
    "Canadair",
    "Cessna",
    "Cirrus Aircraft",
    "Dassault Aviation",
    "Dornier",
    "Douglas Aircraft Company",
    "Embraer",
    "Eurofighter",
    "Fairchild",
    "Fokker",
    "Gulfstream Aerospace",
    "Ilyushin",
    "Lockheed Corporation",
    "Lockheed Martin",
    "McDonnell Douglas",
    "Panavia",
    "Piper",
    "Robin",
    "Saab",
    "Supermarine",
    "Tupolev",
    "Yakovlev",
    "de Havilland",
]


_VARIANTS = [
    "707-320",
    "727-200",
    "737-200",
    "737-300",
    "737-400",
    "737-500",
    "737-600",
    "737-700",
    "737-800",
    "737-900",
    "747-100",
    "747-200",
    "747-300",
    "747-400",
    "757-200",
    "757-300",
    "767-200",
    "767-300",
    "767-400",
    "777-200",
    "777-300",
    "A300B4",
    "A310",
    "A318",
    "A319",
    "A320",
    "A321",
    "A330-200",
    "A330-300",
    "A340-200",
    "A340-300",
    "A340-500",
    "A340-600",
    "A380",
    "ATR-42",
    "ATR-72",
    "An-12",
    "BAE 146-200",
    "BAE 146-300",
    "BAE-125",
    "Beechcraft 1900",
    "Boeing 717",
    "C-130",
    "C-47",
    "CRJ-200",
    "CRJ-700",
    "CRJ-900",
    "Cessna 172",
    "Cessna 208",
    "Cessna 525",
    "Cessna 560",
    "Challenger 600",
    "DC-10",
    "DC-3",
    "DC-6",
    "DC-8",
    "DC-9-30",
    "DH-82",
    "DHC-1",
    "DHC-6",
    "DHC-8-100",
    "DHC-8-300",
    "DR-400",
    "Dornier 328",
    "E-170",
    "E-190",
    "E-195",
    "EMB-120",
    "ERJ 135",
    "ERJ 145",
    "Embraer Legacy 600",
    "Eurofighter Typhoon",
    "F-16A/B",
    "F/A-18",
    "Falcon 2000",
    "Falcon 900",
    "Fokker 100",
    "Fokker 50",
    "Fokker 70",
    "Global Express",
    "Gulfstream IV",
    "Gulfstream V",
    "Hawk T1",
    "Il-76",
    "L-1011",
    "MD-11",
    "MD-80",
    "MD-87",
    "MD-90",
    "Metroliner",
    "Model B200",
    "PA-28",
    "SR-20",
    "Saab 2000",
    "Saab 340",
    "Spitfire",
    "Tornado",
    "Tu-134",
    "Tu-154",
    "Yak-42",
]


def parse_annotations(annotations, join_labels=True):
    annotations = [annotation.strip().split() for annotation in annotations]
    if join_labels:
        return {
            annotation[0].strip(): " ".join(annotation[1:])
            for annotation in annotations
        }
    else:
        return {annotation[0].strip(): annotation[1:] for annotation in annotations}


class FGVCAircraft(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    # # This is an example of a dataset with multiple configurations.
    # # If you don't want/need to define several sub-sets in your dataset,
    # # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.

    # # If you need to make complex sub-parts in the datasets with configurable options
    # # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
    # # BUILDER_CONFIG_CLASS = MyBuilderConfig

    # # You will be able to load one or the other configurations in the following list with
    # # data = datasets.load_dataset('my_dataset', 'first_domain')
    # # data = datasets.load_dataset('my_dataset', 'second_domain')
    # BUILDER_CONFIGS = [
    #     datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
    #     datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
    # ]

    # DEFAULT_CONFIG_NAME = "first_domain"  # It's not mandatory to have a default configuration. Just use one if it make sense.

    def _info(self):
        features = datasets.Features(
            {
                "image": datasets.Image(),
                "bbox": {
                    "ymin": datasets.features.Value(dtype="int64"),
                    "xmin": datasets.features.Value(dtype="int64"),
                    "ymax": datasets.features.Value(dtype="int64"),
                    "xmax": datasets.features.Value(dtype="int64"),
                },
                "family": datasets.features.ClassLabel(names=_FAMILIES),
                "manufacturer": datasets.features.ClassLabel(names=_MANUFACTURERS),
                "variant": datasets.features.ClassLabel(names=_VARIANTS),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_dir = dl_manager.download_and_extract(_URL)
        data_dir = Path(data_dir) / "fgvc-aircraft-2013b" / "data"
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images_dir": data_dir / "images",
                    "annotations_dir": data_dir,
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "images_dir": data_dir / "images",
                    "annotations_dir": data_dir,
                    "split": "val",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "images_dir": data_dir / "images",
                    "annotations_dir": data_dir,
                    "split": "test",
                },
            ),
        ]

    def _generate_examples(self, images_dir, annotations_dir, split):
        image_ids = open(annotations_dir / f"images_{split}.txt").readlines()
        image_ids = [image_id.strip() for image_id in image_ids]

        families = open(annotations_dir / f"images_family_{split}.txt").readlines()
        families = parse_annotations(families)

        manufacturers = open(
            annotations_dir / f"images_manufacturer_{split}.txt"
        ).readlines()
        manufacturers = parse_annotations(manufacturers)

        variants = open(annotations_dir / f"images_variant_{split}.txt").readlines()
        variants = parse_annotations(variants)

        bounding_boxes = open(
            os.path.join(annotations_dir, "images_box.txt")
        ).readlines()

        bounding_boxes = parse_annotations(bounding_boxes, join_labels=False)

        for image_id in image_ids:
            full_path = images_dir / f"{image_id}.jpg"
            family = families[image_id]
            manufacturer = manufacturers[image_id]
            variant = variants[image_id]
            xmin, ymin, xmax, ymax = list(map(int, bounding_boxes[image_id]))

            record = {
                "image": str(full_path),
                "bbox": {"ymin": ymin, "xmin": xmin, "ymax": ymax, "xmax": xmax},
                "family": family,
                "manufacturer": manufacturer,
                "variant": variant,
            }
            yield image_id, record