Datasets:
bentrevett
commited on
Commit
•
4b3cb3f
1
Parent(s):
40e114d
Create script.py
Browse files
script.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This is the script used to create the dataset from the downloaded/extracted dataset @ https://www.vision.caltech.edu/datasets/cub_200_2011/
|
3 |
+
"""
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
from pathlib import Path
|
7 |
+
import shutil
|
8 |
+
import json
|
9 |
+
|
10 |
+
index_to_path = (
|
11 |
+
Path("CUB_200_2011/CUB_200_2011/images.txt")
|
12 |
+
.read_text()
|
13 |
+
.strip()
|
14 |
+
.split("\n")
|
15 |
+
)
|
16 |
+
index_to_path = [Path("CUB_200_2011/CUB_200_2011/images") / Path(x.split(" ")[-1]) for x in index_to_path]
|
17 |
+
|
18 |
+
index_to_split = (
|
19 |
+
Path("CUB_200_2011/CUB_200_2011/train_test_split.txt")
|
20 |
+
.read_text()
|
21 |
+
.strip()
|
22 |
+
.split("\n")
|
23 |
+
)
|
24 |
+
index_to_split = ["train" if x.split(" ")[-1] == "1" else "test" for x in index_to_split]
|
25 |
+
|
26 |
+
index_to_bbox = (
|
27 |
+
Path("CUB_200_2011/CUB_200_2011/bounding_boxes.txt")
|
28 |
+
.read_text()
|
29 |
+
.strip()
|
30 |
+
.split("\n")
|
31 |
+
)
|
32 |
+
|
33 |
+
def convert_bbox(bbox):
|
34 |
+
# From: x0, y0, width, height
|
35 |
+
# To: x0, y0, x1, y1
|
36 |
+
new_bbox = [bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]]
|
37 |
+
return new_bbox
|
38 |
+
|
39 |
+
index_to_bbox = [[float(i) for i in x.split(" ")[1:]] for x in index_to_bbox]
|
40 |
+
index_to_bbox = [convert_bbox(bbox) for bbox in index_to_bbox]
|
41 |
+
|
42 |
+
data_dir = Path("data")
|
43 |
+
train_dir = Path("data") / Path("train")
|
44 |
+
test_dir = Path("data") / Path("test")
|
45 |
+
train_dir.mkdir(parents=True, exist_ok=True)
|
46 |
+
test_dir.mkdir(parents=True, exist_ok=True)
|
47 |
+
|
48 |
+
metadata = []
|
49 |
+
|
50 |
+
for path, split, bbox in zip(index_to_path, index_to_split, index_to_bbox):
|
51 |
+
class_dir, file_name = path.parts[-2:]
|
52 |
+
dir = train_dir / class_dir if split == "train" else test_dir / class_dir
|
53 |
+
dir.mkdir(parents=True, exist_ok=True)
|
54 |
+
class_file_path = Path("/".join([class_dir, file_name]))
|
55 |
+
destination_path = train_dir / class_file_path if split == "train" else test_dir / class_file_path
|
56 |
+
metadata_file_name = Path("/".join(destination_path.parts[1:]))
|
57 |
+
x_min, y_min, x_max, y_max = bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]
|
58 |
+
metadata.append({"file_name": str(metadata_file_name), "bbox": bbox})
|
59 |
+
shutil.copy(path, destination_path)
|
60 |
+
|
61 |
+
with open("data/metadata.jsonl", "w") as f:
|
62 |
+
for md in metadata:
|
63 |
+
f.write(f"{json.dumps(md)}\n")
|
64 |
+
|
65 |
+
dataset = datasets.load_dataset("imagefolder", data_dir="data", drop_labels=False)
|
66 |
+
|
67 |
+
dataset.push_to_hub("bentrevett/cub-200-2011")
|