Upload folder using huggingface_hub
Browse files- kvasir-points_datasets_script.py +143 -0
kvasir-points_datasets_script.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
"""Dataset for filtered Kvasir-instrument and Hyper-Kvasir with bounding boxes."""
|
16 |
+
|
17 |
+
import os
|
18 |
+
import json
|
19 |
+
from PIL import Image
|
20 |
+
import datasets
|
21 |
+
|
22 |
+
import os
|
23 |
+
import json
|
24 |
+
import pandas as pd
|
25 |
+
import hashlib
|
26 |
+
|
27 |
+
|
28 |
+
cal_mid = lambda bx: [[[float(box['xmin'] + box['xmax']) / 2, float(box['ymin'] + box['ymax']) / 2] for box in bx]]
|
29 |
+
|
30 |
+
|
31 |
+
def cal_sha256(file_path): return hashlib.sha256(
|
32 |
+
open(file_path, 'rb').read()).hexdigest()
|
33 |
+
|
34 |
+
|
35 |
+
hyper_label_img_path = '/global/D1/projects/HOST/Datasets/hyper-kvasir/labeled-images/image-labels.csv'
|
36 |
+
|
37 |
+
hyper_df = pd.read_csv(hyper_label_img_path)
|
38 |
+
|
39 |
+
hyper_seg_img_path = '/global/D1/projects/HOST/Datasets/hyper-kvasir/segmented-images/bounding-boxes.json'
|
40 |
+
hyper_seg_img_base_path = "/global/D1/projects/HOST/Datasets/hyper-kvasir/segmented-images/images"
|
41 |
+
|
42 |
+
instr_seg_img_path = '/global/D1/projects/HOST/Datasets/kvasir-instrument/bboxes.json'
|
43 |
+
instr_seg_img_base_path = '/global/D1/projects/HOST/Datasets/kvasir-instrument/images/'
|
44 |
+
|
45 |
+
hyper_seg_imgs = json.load(open(hyper_seg_img_path))
|
46 |
+
instr_seg_imgs = json.load(open(instr_seg_img_path))
|
47 |
+
|
48 |
+
_CITATION = """\
|
49 |
+
@article{kvasir,
|
50 |
+
title={Kvasir-instrument and Hyper-Kvasir datasets for bounding box annotations},
|
51 |
+
author={Sushant Gautam and collaborators},
|
52 |
+
year={2024}
|
53 |
+
}
|
54 |
+
"""
|
55 |
+
|
56 |
+
_DESCRIPTION = """
|
57 |
+
Filtered Kvasir-instrument and Hyper-Kvasir datasets with bounding boxes for medical imaging tasks.
|
58 |
+
Each entry contains images, bounding box coordinates, and additional metadata.
|
59 |
+
"""
|
60 |
+
|
61 |
+
_HOMEPAGE = "https://example.com/kvasir-hyper-bbox"
|
62 |
+
|
63 |
+
_LICENSE = "CC BY-NC 4.0"
|
64 |
+
|
65 |
+
_URLS = {
|
66 |
+
"filtered_data": "https://example.com/kvasir-hyper-bbox-dataset.zip"
|
67 |
+
}
|
68 |
+
|
69 |
+
|
70 |
+
class KvasirHyperBBox(datasets.GeneratorBasedBuilder):
|
71 |
+
"""Dataset for Kvasir-instrument and Hyper-Kvasir with bounding boxes."""
|
72 |
+
|
73 |
+
VERSION = datasets.Version("1.0.0")
|
74 |
+
|
75 |
+
BUILDER_CONFIGS = [
|
76 |
+
datasets.BuilderConfig(
|
77 |
+
name="bbox_dataset",
|
78 |
+
version=VERSION,
|
79 |
+
description="Dataset with bounding box annotations."
|
80 |
+
)
|
81 |
+
]
|
82 |
+
|
83 |
+
DEFAULT_CONFIG_NAME = "bbox_dataset"
|
84 |
+
|
85 |
+
def _info(self):
|
86 |
+
features = datasets.Features({
|
87 |
+
"image_data": datasets.Image(),
|
88 |
+
"image_sha256": datasets.Value("string"),
|
89 |
+
"points": datasets.Sequence(datasets.Sequence(datasets.Sequence(datasets.Value("float32")))),
|
90 |
+
"count": datasets.Value("int64"),
|
91 |
+
"label": datasets.Value("string"),
|
92 |
+
"collection_method": datasets.Value("string"),
|
93 |
+
"classification": datasets.Value("string"),
|
94 |
+
"organ": datasets.Value("string")
|
95 |
+
})
|
96 |
+
|
97 |
+
return datasets.DatasetInfo(
|
98 |
+
description=_DESCRIPTION,
|
99 |
+
homepage=_HOMEPAGE,
|
100 |
+
license=_LICENSE,
|
101 |
+
citation=_CITATION,
|
102 |
+
features=features
|
103 |
+
)
|
104 |
+
|
105 |
+
def _split_generators(self, dl_manager):
|
106 |
+
return [
|
107 |
+
datasets.SplitGenerator(
|
108 |
+
name=datasets.Split.TRAIN,
|
109 |
+
gen_kwargs={},
|
110 |
+
)
|
111 |
+
]
|
112 |
+
|
113 |
+
def _generate_examples(self):
|
114 |
+
for key, entry in hyper_seg_imgs.items():
|
115 |
+
img_path = os.path.join(hyper_seg_img_base_path, f"{key}.jpg")
|
116 |
+
hyper_entry = hyper_df.loc[hyper_df['Video file'] == key].iloc[0]
|
117 |
+
yield key, {
|
118 |
+
"image_data": open(img_path, 'rb').read(),
|
119 |
+
"image_sha256": cal_sha256(img_path),
|
120 |
+
"points": cal_mid(entry['bbox']),
|
121 |
+
"count": len(entry['bbox']),
|
122 |
+
"label": hyper_entry.Finding,
|
123 |
+
"collection_method": 'counting',
|
124 |
+
"classification": hyper_entry.Classification,
|
125 |
+
"organ": hyper_entry.Organ
|
126 |
+
}
|
127 |
+
|
128 |
+
for key, entry in instr_seg_imgs.items():
|
129 |
+
img_path = os.path.join(instr_seg_img_base_path, f"{key}.jpg")
|
130 |
+
assert len(cal_mid(entry['bbox'])) > 0
|
131 |
+
yield key, {
|
132 |
+
"image_data": open(img_path, 'rb').read(),
|
133 |
+
"image_sha256": cal_sha256(img_path),
|
134 |
+
"points": cal_mid(entry['bbox']),
|
135 |
+
"count": len(entry['bbox']),
|
136 |
+
"label": "instrument",
|
137 |
+
"collection_method": "counting",
|
138 |
+
"classification": "instrument",
|
139 |
+
"organ": "instrument"
|
140 |
+
}
|
141 |
+
|
142 |
+
#datasets-cli test /global/D1/projects/HOST/Datasets/hyper-kvasir/sushant-experiments/kvasir-points_datasets_script.py --save_info --all_configs --trust_remote_code
|
143 |
+
# huggingface-cli upload kvasir-points . . --repo-type dataset
|