Nguyễn Lê Hoàng Phúc
commited on
Commit
·
4aade78
1
Parent(s):
4c6af41
Upload dataset as zip archives to avoid rate limits
Browse files- orida_dataset_loader.py +128 -0
- train.zip +3 -0
- validation.zip +3 -0
orida_dataset_loader.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datasets
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# --- Các hàm phụ trợ để đọc file chú thích ---
|
| 6 |
+
|
| 7 |
+
def parse_bbox_file(filepath):
|
| 8 |
+
"""Đọc file bbox và chuyển đổi thành list các số thực."""
|
| 9 |
+
if not os.path.exists(filepath):
|
| 10 |
+
return []
|
| 11 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 12 |
+
line = f.read().strip()
|
| 13 |
+
if not line:
|
| 14 |
+
return []
|
| 15 |
+
return [float(coord) for coord in line.split(',')]
|
| 16 |
+
|
| 17 |
+
def parse_points_file(filepath):
|
| 18 |
+
"""Đọc file points và chuyển đổi thành list của các list số nguyên."""
|
| 19 |
+
if not os.path.exists(filepath):
|
| 20 |
+
return []
|
| 21 |
+
points = []
|
| 22 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 23 |
+
for line in f:
|
| 24 |
+
line = line.strip()
|
| 25 |
+
if not line:
|
| 26 |
+
continue
|
| 27 |
+
coords = [int(p.strip()) for p in line.split(',')]
|
| 28 |
+
points.append(coords)
|
| 29 |
+
return points
|
| 30 |
+
|
| 31 |
+
# --- Lớp Builder chính ---
|
| 32 |
+
|
| 33 |
+
class ORiDaFactualCounterfactual(datasets.GeneratorBasedBuilder):
|
| 34 |
+
"""Dataset loader cho bộ dữ liệu ORiDa Factual/Counterfactual."""
|
| 35 |
+
|
| 36 |
+
def _info(self):
|
| 37 |
+
# Cập nhật lại cấu trúc (schema) để khớp với kiểu dữ liệu chính xác
|
| 38 |
+
return datasets.DatasetInfo(
|
| 39 |
+
description="Bộ dữ liệu ORiDa chứa các mẫu factual và counterfactual.",
|
| 40 |
+
features=datasets.Features({
|
| 41 |
+
'group_id': datasets.Value("string"),
|
| 42 |
+
'sample_id': datasets.Value("string"),
|
| 43 |
+
'type': datasets.ClassLabel(names=['factual_only', 'factual_counterfactual']),
|
| 44 |
+
'image': datasets.Image(),
|
| 45 |
+
'ground_truth': datasets.Image(),
|
| 46 |
+
'annotations': {
|
| 47 |
+
'bbox': datasets.Sequence(datasets.Value("float32")), # Danh sách số thực
|
| 48 |
+
'points': datasets.Sequence(datasets.Sequence(datasets.Value("int32"))), # Danh sách của các danh sách số nguyên
|
| 49 |
+
'masks': datasets.Image(),
|
| 50 |
+
'results_vis': datasets.Image(), # Thêm results_vis là ảnh
|
| 51 |
+
}
|
| 52 |
+
}),
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
def _split_generators(self, dl_manager):
|
| 56 |
+
# dl_manager sẽ tự động tìm và giải nén các file zip trong repo
|
| 57 |
+
train_path = dl_manager.extract("train.zip")
|
| 58 |
+
validation_path = dl_manager.extract("validation.zip")
|
| 59 |
+
|
| 60 |
+
# Đường dẫn bây giờ sẽ trỏ vào bên trong các thư mục đã được giải nén
|
| 61 |
+
return [
|
| 62 |
+
datasets.SplitGenerator(
|
| 63 |
+
name=datasets.Split.TRAIN,
|
| 64 |
+
gen_kwargs={"filepath": os.path.join(train_path, "train")}
|
| 65 |
+
),
|
| 66 |
+
datasets.SplitGenerator(
|
| 67 |
+
name=datasets.Split.VALIDATION,
|
| 68 |
+
gen_kwargs={"filepath": os.path.join(validation_path, "validation")}
|
| 69 |
+
),
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
def _generate_examples(self, filepath):
|
| 73 |
+
"""Hàm này sẽ duyệt qua thư mục và 'yield' từng mẫu dữ liệu."""
|
| 74 |
+
key = 0
|
| 75 |
+
for group_id in os.listdir(filepath):
|
| 76 |
+
group_path = os.path.join(filepath, group_id)
|
| 77 |
+
if not os.path.isdir(group_path): continue
|
| 78 |
+
|
| 79 |
+
ground_truth_path = os.path.join(group_path, f"{group_id}.png")
|
| 80 |
+
if not os.path.exists(ground_truth_path): continue
|
| 81 |
+
|
| 82 |
+
for type_name in ["factual_only", "factual_counterfactual"]:
|
| 83 |
+
type_path = os.path.join(group_path, type_name)
|
| 84 |
+
if not os.path.isdir(type_path): continue
|
| 85 |
+
|
| 86 |
+
for sample_id in os.listdir(type_path):
|
| 87 |
+
sample_path = os.path.join(type_path, sample_id)
|
| 88 |
+
if not os.path.isdir(sample_path): continue
|
| 89 |
+
|
| 90 |
+
images_path = os.path.join(sample_path, "images")
|
| 91 |
+
annotations_path = os.path.join(sample_path, "annotations")
|
| 92 |
+
if not os.path.isdir(images_path): continue
|
| 93 |
+
|
| 94 |
+
for image_filename in os.listdir(images_path):
|
| 95 |
+
if not image_filename.lower().endswith(('.png', '.jpg', '.jpeg')): continue
|
| 96 |
+
|
| 97 |
+
image_path = os.path.join(images_path, image_filename)
|
| 98 |
+
base_image_name = os.path.splitext(image_filename)[0]
|
| 99 |
+
|
| 100 |
+
# Xây dựng đường dẫn đầy đủ tới các file chú thích
|
| 101 |
+
bbox_file = os.path.join(annotations_path, "bbox", f"{base_image_name}.txt")
|
| 102 |
+
points_file = os.path.join(annotations_path, "points", f"{base_image_name}.txt")
|
| 103 |
+
# Thay đổi đuôi file mask và results_vis thành .jpg
|
| 104 |
+
mask_file = os.path.join(annotations_path, "masks", f"{base_image_name}.jpg")
|
| 105 |
+
results_vis_file = os.path.join(annotations_path, "results_vis", f"{base_image_name}.jpg")
|
| 106 |
+
|
| 107 |
+
# Gọi các hàm phụ trợ để đọc và xử lý dữ liệu
|
| 108 |
+
bbox_data = parse_bbox_file(bbox_file)
|
| 109 |
+
points_data = parse_points_file(points_file)
|
| 110 |
+
|
| 111 |
+
# Chỉ yield mẫu nếu các file ảnh cần thiết tồn tại
|
| 112 |
+
if not os.path.exists(mask_file) or not os.path.exists(results_vis_file):
|
| 113 |
+
continue
|
| 114 |
+
|
| 115 |
+
yield key, {
|
| 116 |
+
"group_id": group_id,
|
| 117 |
+
"sample_id": sample_id,
|
| 118 |
+
"type": type_name,
|
| 119 |
+
"image": image_path,
|
| 120 |
+
"ground_truth": ground_truth_path,
|
| 121 |
+
"annotations": {
|
| 122 |
+
"bbox": bbox_data,
|
| 123 |
+
"points": points_data,
|
| 124 |
+
"masks": mask_file,
|
| 125 |
+
"results_vis": results_vis_file,
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
key += 1
|
train.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e9e2200fbc06cce05ffb4ed596b4e0773a048dda704365c8f4c185b63dfc4bec
|
| 3 |
+
size 220839437
|
validation.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:544ffd775391f29dd8153fc8b03db832d191f2a64c294bd8cd5b38b3ca5289c4
|
| 3 |
+
size 6295080
|