minwoosun commited on
Commit
daa5db2
1 Parent(s): c126fd1

Create CholecSeg8k.py for custom data loading

Browse files
Files changed (1) hide show
  1. CholecSeg8k.py +100 -0
CholecSeg8k.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+
4
+ _CITATION = ""
5
+ _DESCRIPTION = "CholecSeg8K dataset for semantic segmentation in laparoscopic cholecystectomy surgery."
6
+ _HOMEPAGE_URL = "https://www.kaggle.com/datasets/newslab/cholecseg8k"
7
+ _DATA_URL = "data/CholecSeg8k.zip"
8
+ _LICENSE= "cc-by-nc-sa-4.0"
9
+
10
+
11
+ class CholecSeg8KConfig(datasets.BuilderConfig):
12
+ """CholecSeg8K dataset for semantic segmentation in laparoscopic cholecystectomy surgery."""
13
+
14
+ def __init__(self, name, description, homepage, data_url):
15
+ """BuilderConfig for CholecSeg8k.
16
+ Args:
17
+ data_url: `string`, url to download the zip file from.
18
+ **kwargs: keyword arguments forwarded to super.
19
+ """
20
+ super(CholecSeg8KConfig, self).__init__(
21
+ name=self.name,
22
+ version=datasets.Version("1.0.0"),
23
+ description=self.description,
24
+ )
25
+ self.name = name
26
+ self.description = description
27
+ self.homepage = homepage
28
+ self.data_url = data_url
29
+
30
+
31
+ def _build_config(name):
32
+ return CholecSeg8KConfig(
33
+ name=name,
34
+ description=_DESCRIPTION,
35
+ homepage=_HOMEPAGE_URL,
36
+ data_url=_DATA_URL,
37
+ )
38
+
39
+
40
+ class CholecSeg8K(datasets.GeneratorBasedBuilder):
41
+
42
+ BUILDER_CONFIGS = [_build_config("all")]
43
+
44
+ def _info(self):
45
+ return datasets.DatasetInfo(
46
+ description=_DESCRIPTION,
47
+ features=datasets.Features(
48
+ {
49
+ "image": datasets.Image(),
50
+ "color_mask": datasets.Image(),
51
+ "watershed_mask": datasets.Image(),
52
+ "annotation_mask": datasets.Image(),
53
+ }
54
+ ),
55
+ supervised_keys=None,
56
+ homepage=_HOMEPAGE_URL,
57
+ citation=_CITATION,
58
+ license=_LICENSE,
59
+ )
60
+
61
+ def _split_generators(self, dl_manager):
62
+
63
+ datapath = dl_manager.download_and_extract(_DATA_URL)
64
+
65
+ return [
66
+ datasets.SplitGenerator(
67
+ name=datasets.Split.TRAIN,
68
+ gen_kwargs={"datapath": datapath},
69
+ ),
70
+ ]
71
+
72
+
73
+ def _generate_examples(self, datapath):
74
+ """Yields examples."""
75
+ key=0
76
+ datapath = os.path.join(datapath, "CholecSeg8k")
77
+
78
+ for video_folder in os.listdir(datapath):
79
+ video_folder_path = os.path.join(datapath, video_folder)
80
+
81
+ for clip_folder in os.listdir(video_folder_path):
82
+ clip_folder_path = os.path.join(video_folder_path, clip_folder)
83
+
84
+ for file in os.listdir(clip_folder_path):
85
+ if file.endswith("_endo.png"): # Check for endoscopic images
86
+ image_path = os.path.join(clip_folder_path, file)
87
+
88
+ # Construct paths for each mask type
89
+ base_filename = file.replace("_endo.png", "")
90
+ color_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_color_mask.png")
91
+ watershed_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_watershed_mask.png")
92
+ annotation_mask_path = os.path.join(clip_folder_path, f"{base_filename}_endo_mask.png")
93
+
94
+ yield key, {
95
+ "image": image_path,
96
+ "color_mask": color_mask_path,
97
+ "watershed_mask": watershed_mask_path,
98
+ "annotation_mask": annotation_mask_path,
99
+ }
100
+ key+=1