FGVC-Aircraft / FGVC-Aircraft.py
SaulLu's picture
simplify script
a51a586
raw
history blame
10.6 kB
# 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