# -*- coding: utf-8 -*- """cub200_dataset.py Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1qC5RnFLP3_9X50ripGf5YtfXnugxBj2m """ from PIL import Image import os import pandas as pd from datasets import DatasetDict, DatasetInfo, Features, Value, Sequence, Image, SplitGenerator, GeneratorBasedBuilder, Version _CITATION = """\ @techreport{WahCUB_200_2011, Title = {The Caltech-UCSD Birds-200-2011 Dataset}, Author = {Wah, C. and Branson, S. and Welinder, P. and Perona, P. and Belongie, S.}, Year = {2011}, Institution = {California Institute of Technology}, Number = {CNS-TR-2011-001} } """ _DESCRIPTION = """\ The CUB-200-2011 dataset contains 11,788 photos of 200 bird species. Each photo comes with detailed annotations, including part locations, bounding boxes, and attributes for studying fine-grained visual categorization. """ _HOMEPAGE = "http://www.vision.caltech.edu/visipedia/CUB-200-2011.html" _DATASET_PATH = "/content/drive/My Drive/cub200/CUB_200_2011" class CUB2002011(datasets.GeneratorBasedBuilder): """CUB-200-2011 dataset for bird species image classification.""" # Version of the dataset VERSION = datasets.Version("1.0.0") # Define the features of the dataset, including the image and the label def _info(self): return datasets.DatasetInfo( description="CUB-200-2011 is an image dataset with photos of 200 bird species.", features=datasets.Features({ "image": datasets.Image(), "label": datasets.ClassLabel(names=[f"species_{i:03d}" for i in range(1, 201)]), }), supervised_keys=("image", "label"), homepage="http://www.vision.caltech.edu/visipedia/CUB-200-2011.html", citation="""@techreport{WahCUB_200_2011, Title = {The Caltech-UCSD Birds-200-2011 Dataset}, Author = {Wah, C. and Branson, S. and Welinder, P. and Perona, P. and Belongie, S.}, Year = {2011}, Institution = {California Institute of Technology}, Number = {CNS-TR-2011-001} }""" ) # Specify the dataset splits def _split_generators(self, dl_manager): # Assuming the dataset is pre-downloaded dl_manager = DownloadManager.download_and_extract("https://data.caltech.edu/records/65de6-vp158/files/CUB_200_2011.tgz") return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"data_dir": data_dir, "split": "train"}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"data_dir": data_dir, "split": "test"}), ] # Generate examples from the dataset directory def _generate_examples(self, data_dir, split): # Implement logic to iterate over the dataset and yield examples # For simplicity, assuming all images are in the 'images' folder and split is ignored species_dirs = [p for p in (data_dir / "images").iterdir() if p.is_dir()] for species_dir in species_dirs: species_label = species_dir.name for image_path in species_dir.glob("*.jpg"): # The key can be whatever unique identifier you choose; here we use the image path yield image_path.stem, { "image": str(image_path), "label": species_label, }