# 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. """Oxford_IIT pet loading script.""" import xml.etree.ElementTree as ET from pathlib import Path import datasets _CITATION = """\ @InProceedings{parkhi12a, author = "Parkhi, O. M. and Vedaldi, A. and Zisserman, A. and Jawahar, C.~V.", title = "Cats and Dogs", booktitle = "IEEE Conference on Computer Vision and Pattern Recognition", year = "2012", } """ _DESCRIPTION = """\ 37 category pet dataset with roughly 200 images for each class. The images have a large variations in scale, pose and lighting. All images have an associated ground truth annotation of breed, head ROI, and pixel level trimap segmentation. """ _HOMEPAGE = "https://www.robots.ox.ac.uk/~vgg/data/pets/" _LICENSE = "CC BY-SA 4.0" # TODO: Add link to the official dataset URLs here # The HuggingFace Datasets library doesn't host the datasets but only points to the original files. # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method) _URLS = { "images": "https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz", "annotations": "https://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz", } _LABEL_CLASSES = [ "Abyssinian", "american_bulldog", "american_pit_bull_terrier", "basset_hound", "beagle", "Bengal", "Birman", "Bombay", "boxer", "British_Shorthair", "chihuahua", "Egyptian_Mau", "english_cocker_spaniel", "english_setter", "german_shorthaired", "great_pyrenees", "havanese", "japanese_chin", "keeshond", "leonberger", "Maine_Coon", "miniature_pinscher", "newfoundland", "Persian", "pomeranian", "pug", "Ragdoll", "Russian_Blue", "saint_bernard", "samoyed", "scottish_terrier", "shiba_inu", "Siamese", "Sphynx", "staffordshire_bull_terrier", "wheaten_terrier", "yorkshire_terrier", ] _SPECIES_CLASSES = ["Cat", "Dog"] # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case class NewDataset(datasets.GeneratorBasedBuilder): """TODO: Short description of my dataset.""" VERSION = datasets.Version("1.0.0") def _info(self): features = datasets.Features( { "image": datasets.Image(), "label": datasets.features.ClassLabel(names=_LABEL_CLASSES), "species": datasets.features.ClassLabel(names=_SPECIES_CLASSES), "segmentation_mask": datasets.Image(), # "bbox": datasets.features.Array2D(shape=(1, 4), dtype="int64"), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): urls = _URLS data_dir = dl_manager.download_and_extract(urls) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "images_dir": Path(data_dir["images"]) / "images", "annotations_dir": Path(data_dir["annotations"]) / "annotations", "images_list": Path(data_dir["annotations"]) / "annotations" / "trainval.txt", }, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "images_dir": Path(data_dir["images"]) / "images", "annotations_dir": Path(data_dir["annotations"]) / "annotations", "images_list": Path(data_dir["annotations"]) / "annotations" / "test.txt", }, ), ] # Not used because missing xml file for Abyssinian_104 # def _get_data_from_xml_file(self, xlm_annotation_file): # # From https://huggingface.co/datasets/fuliucansheng/pascal_voc/blob/main/pascal_voc.py # anno_tree = ET.parse(xlm_annotation_file) # objects = [] # for obj in anno_tree.findall("./object"): # info = { # "class": obj.findall("./name")[0].text, # "bbox": [[ # int(float(obj.findall("./bndbox/xmin")[0].text)), # int(float(obj.findall("./bndbox/ymin")[0].text)), # int(float(obj.findall("./bndbox/xmax")[0].text)), # int(float(obj.findall("./bndbox/ymax")[0].text)),] # ], # } # if obj.findall("./pose"): # info["pose"] = obj.findall("./pose")[0].text # if obj.findall("./truncated"): # info["truncated"] = int(obj.findall("./truncated")[0].text) # if obj.findall("./difficult"): # info["difficult"] = int(obj.findall("./difficult")[0].text) # else: # info["difficult"] = 0 # if obj.findall("./occluded"): # info["occluded"] = int(obj.findall("./occluded")[0].text) # if obj.findall("./actions"): # info["action"] = [ # action.tag # for action in obj.findall("./actions/") # if int(action.text) == 1 # ][0] # objects.append(info) # print(len(objects)) # return objects def _generate_examples(self, images_dir, annotations_dir, images_list): bounding_box_dir = annotations_dir / "xmls" trimaps_dir = annotations_dir / "trimaps" with open(images_list, encoding="utf-8") as f: for row in f: image_name, label, species, _ = row.strip().split(" ") trimap_name = f"{image_name}.png" # bbox_name = f"{image_name}.xml" image_name = f"{image_name}.jpg" label = _LABEL_CLASSES[int(label) - 1] species = _SPECIES_CLASSES[int(species) - 1] # objects = self._get_data_from_xml_file(str(bounding_box_dir / bbox_name)) record = { "image": str(images_dir / image_name), "label": label, "species": species, "segmentation_mask": str(trimaps_dir / trimap_name), # "bbox": [[ymin, ymax, xmin, xmax]] } yield image_name, record