cassiekang commited on
Commit
35ac0c6
1 Parent(s): 6733cdb

Upload cub200_dataset.py

Browse files
Files changed (1) hide show
  1. cub200_dataset.py +79 -0
cub200_dataset.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """cub200_dataset.py
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1qC5RnFLP3_9X50ripGf5YtfXnugxBj2m
8
+ """
9
+
10
+ from PIL import Image
11
+ import os
12
+ import pandas as pd
13
+ from datasets import DatasetDict, DatasetInfo, Features, Value, Sequence, Image, SplitGenerator, GeneratorBasedBuilder, Version
14
+
15
+ _CITATION = """\
16
+ @techreport{WahCUB_200_2011,
17
+ Title = {The Caltech-UCSD Birds-200-2011 Dataset},
18
+ Author = {Wah, C. and Branson, S. and Welinder, P. and Perona, P. and Belongie, S.},
19
+ Year = {2011},
20
+ Institution = {California Institute of Technology},
21
+ Number = {CNS-TR-2011-001}
22
+ }
23
+ """
24
+
25
+ _DESCRIPTION = """\
26
+ 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.
27
+ """
28
+
29
+ _HOMEPAGE = "http://www.vision.caltech.edu/visipedia/CUB-200-2011.html"
30
+
31
+ _DATASET_PATH = "/content/drive/My Drive/cub200/CUB_200_2011"
32
+
33
+ class CUB2002011(datasets.GeneratorBasedBuilder):
34
+ """CUB-200-2011 dataset for bird species image classification."""
35
+
36
+ # Version of the dataset
37
+ VERSION = datasets.Version("1.0.0")
38
+
39
+ # Define the features of the dataset, including the image and the label
40
+ def _info(self):
41
+ return datasets.DatasetInfo(
42
+ description="CUB-200-2011 is an image dataset with photos of 200 bird species.",
43
+ features=datasets.Features({
44
+ "image": datasets.Image(),
45
+ "label": datasets.ClassLabel(names=[f"species_{i:03d}" for i in range(1, 201)]),
46
+ }),
47
+ supervised_keys=("image", "label"),
48
+ homepage="http://www.vision.caltech.edu/visipedia/CUB-200-2011.html",
49
+ citation="""@techreport{WahCUB_200_2011,
50
+ Title = {The Caltech-UCSD Birds-200-2011 Dataset},
51
+ Author = {Wah, C. and Branson, S. and Welinder, P. and Perona, P. and Belongie, S.},
52
+ Year = {2011},
53
+ Institution = {California Institute of Technology},
54
+ Number = {CNS-TR-2011-001}
55
+ }"""
56
+ )
57
+
58
+ # Specify the dataset splits
59
+ def _split_generators(self, dl_manager):
60
+ # Assuming the dataset is pre-downloaded
61
+ dl_manager = DownloadManager.download_and_extract("https://data.caltech.edu/records/65de6-vp158/files/CUB_200_2011.tgz")
62
+ return [
63
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"data_dir": data_dir, "split": "train"}),
64
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"data_dir": data_dir, "split": "test"}),
65
+ ]
66
+
67
+ # Generate examples from the dataset directory
68
+ def _generate_examples(self, data_dir, split):
69
+ # Implement logic to iterate over the dataset and yield examples
70
+ # For simplicity, assuming all images are in the 'images' folder and split is ignored
71
+ species_dirs = [p for p in (data_dir / "images").iterdir() if p.is_dir()]
72
+ for species_dir in species_dirs:
73
+ species_label = species_dir.name
74
+ for image_path in species_dir.glob("*.jpg"):
75
+ # The key can be whatever unique identifier you choose; here we use the image path
76
+ yield image_path.stem, {
77
+ "image": str(image_path),
78
+ "label": species_label,
79
+ }