Create cub200.py
Browse files
cub200.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
from PIL import Image
|
3 |
+
import pandas as pd
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
|
7 |
+
class CUB200(datasets.GeneratorBasedBuilder):
|
8 |
+
"""Caltech-UCSD Birds-200-2011 (CUB-200-2011) Dataset"""
|
9 |
+
|
10 |
+
VERSION = datasets.Version("1.0.0")
|
11 |
+
|
12 |
+
def _info(self):
|
13 |
+
return datasets.DatasetInfo(
|
14 |
+
description="""The Caltech-UCSD Birds-200-2011 dataset consists of 11,788 images of 200 bird species.""",
|
15 |
+
features=datasets.Features(
|
16 |
+
{
|
17 |
+
"image": datasets.Image(),
|
18 |
+
"label": datasets.ClassLabel(names=self._labels())
|
19 |
+
}
|
20 |
+
),
|
21 |
+
supervised_keys=("image", "label"),
|
22 |
+
homepage="https://www.vision.caltech.edu/datasets/cub_200_2011/",
|
23 |
+
citation="""@techreport{WahCUB_200_2011,
|
24 |
+
Title = {The Caltech-UCSD Birds-200-2011 Dataset},
|
25 |
+
Author = {Wah, C. and Branson, S. and Welinder, P. and Perona, P. and Belongie, S.},
|
26 |
+
Year = {2011},
|
27 |
+
Institution = {California Institute of Technology},
|
28 |
+
Number = {CNS-TR-2011-001}}"""
|
29 |
+
)
|
30 |
+
|
31 |
+
def _split_generators(self, dl_manager):
|
32 |
+
# Download and extract in a single step
|
33 |
+
extracted_path = dl_manager.download_and_extract("https://data.caltech.edu/records/65de6-vp158/files/CUB_200_2011.tgz?download=1")
|
34 |
+
data_dir = Path(extracted_path) / "CUB_200_2011"
|
35 |
+
|
36 |
+
return [
|
37 |
+
datasets.SplitGenerator(
|
38 |
+
name=datasets.Split.TRAIN,
|
39 |
+
gen_kwargs={"data_dir": data_dir, "split": "train"},
|
40 |
+
),
|
41 |
+
datasets.SplitGenerator(
|
42 |
+
name=datasets.Split.TEST,
|
43 |
+
gen_kwargs={"data_dir": data_dir, "split": "test"},
|
44 |
+
)
|
45 |
+
]
|
46 |
+
|
47 |
+
def _generate_examples(self, data_dir, split):
|
48 |
+
"""Generate examples from the extracted directory."""
|
49 |
+
# Paths to metadata files in the extracted directory
|
50 |
+
image_labels_path = data_dir / "image_class_labels.txt"
|
51 |
+
image_paths_path = data_dir / "images.txt"
|
52 |
+
train_test_split_path = data_dir / "train_test_split.txt"
|
53 |
+
|
54 |
+
# Load metadata
|
55 |
+
images_df = pd.read_csv(image_paths_path, sep='\s+', header=None, names=["image_id", "file_path"])
|
56 |
+
labels_df = pd.read_csv(image_labels_path, sep='\s+', header=None, names=["image_id", "label"])
|
57 |
+
split_df = pd.read_csv(train_test_split_path, sep='\s+', header=None, names=["image_id", "is_training"])
|
58 |
+
|
59 |
+
# Merge metadata into a single DataFrame
|
60 |
+
data_df = images_df.merge(labels_df, on="image_id").merge(split_df, on="image_id")
|
61 |
+
data_df["label"] -= 1 # Zero-index the labels
|
62 |
+
|
63 |
+
# Filter by the specified split
|
64 |
+
is_training_split = 1 if split == "train" else 0
|
65 |
+
split_data = data_df[data_df["is_training"] == is_training_split]
|
66 |
+
|
67 |
+
# Generate examples
|
68 |
+
for _, row in split_data.iterrows():
|
69 |
+
image_path = data_dir / "images" / row['file_path']
|
70 |
+
label = row["label"]
|
71 |
+
|
72 |
+
# Load the image
|
73 |
+
with open(image_path, "rb") as img_file:
|
74 |
+
image = Image.open(img_file).convert("RGB")
|
75 |
+
yield row["image_id"], {
|
76 |
+
"image": image,
|
77 |
+
"label": label,
|
78 |
+
}
|
79 |
+
|
80 |
+
@staticmethod
|
81 |
+
def _labels():
|
82 |
+
return [
|
83 |
+
"Black_footed_Albatross", "Laysan_Albatross", "Sooty_Albatross", "Groove_billed_Ani",
|
84 |
+
"Crested_Auklet", "Least_Auklet", "Parakeet_Auklet", "Rhinoceros_Auklet", "Brewer_Blackbird",
|
85 |
+
"Red_winged_Blackbird", "Rusty_Blackbird", "Yellow_headed_Blackbird", "Bobolink",
|
86 |
+
"Indigo_Bunting", "Lazuli_Bunting", "Painted_Bunting", "Cardinal", "Spotted_Catbird",
|
87 |
+
"Gray_Catbird", "Yellow_breasted_Chat", "Eastern_Towhee", "Chuck_will_Widow",
|
88 |
+
"Brandt_Cormorant", "Red_faced_Cormorant", "Pelagic_Cormorant", "Bronzed_Cowbird",
|
89 |
+
"Shiny_Cowbird", "Brown_Creeper", "American_Crow", "Fish_Crow", "Black_billed_Cuckoo",
|
90 |
+
"Mangrove_Cuckoo", "Yellow_billed_Cuckoo", "Gray_crowned_Rosy_Finch", "Purple_Finch",
|
91 |
+
"Northern_Flicker", "Acadian_Flycatcher", "Great_Crested_Flycatcher", "Least_Flycatcher",
|
92 |
+
"Olive_sided_Flycatcher", "Scissor_tailed_Flycatcher", "Vermilion_Flycatcher",
|
93 |
+
"Yellow_bellied_Flycatcher", "Frigatebird", "Northern_Fulmar", "Gadwall", "American_Goldfinch",
|
94 |
+
"European_Goldfinch", "Boat_tailed_Grackle", "Eared_Grebe", "Horned_Grebe",
|
95 |
+
"Pied_billed_Grebe", "Western_Grebe", "Blue_Grosbeak", "Evening_Grosbeak", "Pine_Grosbeak",
|
96 |
+
"Rose_breasted_Grosbeak", "Pigeon_Guillemot", "California_Gull", "Glaucous_winged_Gull",
|
97 |
+
"Heermann_Gull", "Herring_Gull", "Ivory_Gull", "Ring_billed_Gull", "Slaty_backed_Gull",
|
98 |
+
"Western_Gull", "Anna_Hummingbird", "Ruby_throated_Hummingbird", "Rufous_Hummingbird",
|
99 |
+
"Green_Violetear", "Long_tailed_Jaeger", "Pomarine_Jaeger", "Blue_Jay", "Florida_Jay",
|
100 |
+
"Green_Jay", "Dark_eyed_Junco", "Tropical_Kingbird", "Gray_Kingbird", "Belted_Kingfisher",
|
101 |
+
"Green_Kingfisher", "Pied_Kingfisher", "Ringed_Kingfisher", "White_breasted_Kingfisher",
|
102 |
+
"Red_legged_Kittiwake", "Horned_Lark", "Pacific_Loon", "Mallard", "Western_Meadowlark",
|
103 |
+
"Hooded_Merganser", "Red_breasted_Merganser", "Mockingbird", "Nighthawk", "Clark_Nutcracker",
|
104 |
+
"White_breasted_Nuthatch", "Baltimore_Oriole", "Hooded_Oriole", "Orchard_Oriole",
|
105 |
+
"Scott_Oriole", "Ovenbird", "Brown_Pelican", "White_Pelican", "Western_Wood_Pewee",
|
106 |
+
"Sayornis", "American_Pipit", "Whip_poor_Will", "Horned_Puffin", "Common_Raven",
|
107 |
+
"White_necked_Raven", "American_Redstart", "Geococcyx", "Loggerhead_Shrike",
|
108 |
+
"Great_Grey_Shrike", "Baird_Sparrow", "Black_throated_Sparrow", "Brewer_Sparrow",
|
109 |
+
"Chipping_Sparrow", "Clay_colored_Sparrow", "House_Sparrow", "Field_Sparrow",
|
110 |
+
"Fox_Sparrow", "Grasshopper_Sparrow", "Harris_Sparrow", "Henslow_Sparrow",
|
111 |
+
"Le_Conte_Sparrow", "Lincoln_Sparrow", "Nelson_Sharp_tailed_Sparrow", "Savannah_Sparrow",
|
112 |
+
"Seaside_Sparrow", "Song_Sparrow", "Tree_Sparrow", "Vesper_Sparrow",
|
113 |
+
"White_crowned_Sparrow", "White_throated_Sparrow", "Cape_Glossy_Starling",
|
114 |
+
"Bank_Swallow", "Barn_Swallow", "Cliff_Swallow", "Tree_Swallow", "Scarlet_Tanager",
|
115 |
+
"Summer_Tanager", "Arctic_Tern", "Black_Tern", "Caspian_Tern", "Common_Tern",
|
116 |
+
"Elegant_Tern", "Forster_Tern", "Least_Tern", "Green_tailed_Towhee", "Brown_Thrasher",
|
117 |
+
"Sage_Thrasher", "Black_capped_Vireo", "Blue_headed_Vireo", "Philadelphia_Vireo",
|
118 |
+
"Red_eyed_Vireo", "Warbling_Vireo", "White_eyed_Vireo", "Yellow_throated_Vireo",
|
119 |
+
"Bay_breasted_Warbler", "Black_and_white_Warbler", "Black_throated_Blue_Warbler",
|
120 |
+
"Blue_winged_Warbler", "Canada_Warbler", "Cape_May_Warbler", "Cerulean_Warbler",
|
121 |
+
"Chestnut_sided_Warbler", "Golden_winged_Warbler", "Hooded_Warbler", "Kentucky_Warbler",
|
122 |
+
"Magnolia_Warbler", "Mourning_Warbler", "Myrtle_Warbler", "Nashville_Warbler",
|
123 |
+
"Orange_crowned_Warbler", "Palm_Warbler", "Pine_Warbler", "Prairie_Warbler",
|
124 |
+
"Prothonotary_Warbler", "Swainson_Warbler", "Tennessee_Warbler", "Wilson_Warbler",
|
125 |
+
"Worm_eating_Warbler", "Yellow_Warbler", "Northern_Waterthrush", "Louisiana_Waterthrush",
|
126 |
+
"Bohemian_Waxwing", "Cedar_Waxwing", "American_Three_toed_Woodpecker",
|
127 |
+
"Pileated_Woodpecker", "Red_bellied_Woodpecker", "Red_cockaded_Woodpecker",
|
128 |
+
"Red_headed_Woodpecker", "Downy_Woodpecker", "Bewick_Wren", "Cactus_Wren",
|
129 |
+
"Carolina_Wren", "House_Wren", "Marsh_Wren", "Rock_Wren", "Winter_Wren",
|
130 |
+
"Common_Yellowthroat"
|
131 |
+
]
|