more configs
Browse files
glenda.py
CHANGED
|
@@ -71,18 +71,16 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 71 |
description="Contains images without visible pathology in relation to endometriosis (label = 'No-Pathology') and different endometriosis classes (label is exactly one of: 6.1.1.1_Endo-Peritoneum, 6.1.1.2_Endo-Ovar, 6.1.1.3_Endo-TIE, 6.1.1.4_Endo-Uterus).",
|
| 72 |
version=datasets.Version(f"{VERSION}.0"),
|
| 73 |
),
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# version=datasets.Version(f"{VERSION}.0"),
|
| 85 |
-
# ),
|
| 86 |
]
|
| 87 |
|
| 88 |
def _info(self):
|
|
@@ -106,7 +104,6 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 106 |
}
|
| 107 |
|
| 108 |
task_templates = None
|
| 109 |
-
supervised_keys = None
|
| 110 |
|
| 111 |
if self.config.name in ("binary_classification", "multiclass_classification"):
|
| 112 |
class_names = CLASS_NAMES[self.config.name]
|
|
@@ -120,6 +117,18 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 120 |
image_column="image", label_column="labels"
|
| 121 |
)
|
| 122 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
else:
|
| 124 |
raise NotImplementedError()
|
| 125 |
|
|
@@ -152,7 +161,14 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 152 |
for category in coco_annotations["categories"]
|
| 153 |
}
|
| 154 |
|
| 155 |
-
image_filepaths, image_metadata
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
for annotation, metadata in zip(
|
| 158 |
coco_annotations["annotations"],
|
|
@@ -185,9 +201,19 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 185 |
|
| 186 |
if self.config.name == "binary_classification":
|
| 187 |
_, positive_label_name = CLASS_NAMES[self.config.name]
|
| 188 |
-
|
| 189 |
elif self.config.name == "multiclass_classification":
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
else:
|
| 192 |
raise NotImplementedError()
|
| 193 |
|
|
@@ -214,6 +240,7 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 214 |
string=str(image_filename_with_parent_folder),
|
| 215 |
pattern=NO_PATHOLOGY_IMAGE_METADATA_REGEX,
|
| 216 |
)
|
|
|
|
| 217 |
try:
|
| 218 |
metadata.update(
|
| 219 |
{
|
|
@@ -223,14 +250,20 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 223 |
)
|
| 224 |
except AttributeError:
|
| 225 |
if match is None:
|
| 226 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 227 |
raise ValueError(image_filename_with_parent_folder)
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
| 234 |
# NOTE: Only defined for `endometriosis` images
|
| 235 |
metadata["case_id"] = None
|
| 236 |
image_metadata.append(metadata)
|
|
@@ -240,27 +273,64 @@ class GLENDA(datasets.GeneratorBasedBuilder):
|
|
| 240 |
"multiclass_classification",
|
| 241 |
):
|
| 242 |
negative_class_label_name, *_ = CLASS_NAMES[self.config.name]
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
return [
|
| 246 |
datasets.SplitGenerator(
|
| 247 |
name=datasets.Split.TRAIN,
|
| 248 |
gen_kwargs={
|
| 249 |
"image_filepaths": image_filepaths,
|
| 250 |
-
"labels": image_labels,
|
| 251 |
"metadata": image_metadata,
|
|
|
|
| 252 |
},
|
| 253 |
)
|
| 254 |
]
|
| 255 |
|
| 256 |
-
def _generate_examples(self,
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
"
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
description="Contains images without visible pathology in relation to endometriosis (label = 'No-Pathology') and different endometriosis classes (label is exactly one of: 6.1.1.1_Endo-Peritoneum, 6.1.1.2_Endo-Ovar, 6.1.1.3_Endo-TIE, 6.1.1.4_Endo-Uterus).",
|
| 72 |
version=datasets.Version(f"{VERSION}.0"),
|
| 73 |
),
|
| 74 |
+
datasets.BuilderConfig(
|
| 75 |
+
name="object_detection",
|
| 76 |
+
description="Contains images without visible pathology in relation to endometriosis and different endometriosis classes with corresponding COCO bounding box annotations.",
|
| 77 |
+
version=datasets.Version(f"{VERSION}.0"),
|
| 78 |
+
),
|
| 79 |
+
datasets.BuilderConfig(
|
| 80 |
+
name="instance_segmentation",
|
| 81 |
+
description="Contains images without visible pathology in relation to endometriosis and different endometriosis classes with COCO instance segmentation annotations.",
|
| 82 |
+
version=datasets.Version(f"{VERSION}.0"),
|
| 83 |
+
),
|
|
|
|
|
|
|
| 84 |
]
|
| 85 |
|
| 86 |
def _info(self):
|
|
|
|
| 104 |
}
|
| 105 |
|
| 106 |
task_templates = None
|
|
|
|
| 107 |
|
| 108 |
if self.config.name in ("binary_classification", "multiclass_classification"):
|
| 109 |
class_names = CLASS_NAMES[self.config.name]
|
|
|
|
| 117 |
image_column="image", label_column="labels"
|
| 118 |
)
|
| 119 |
]
|
| 120 |
+
elif self.config.name == "object_detection":
|
| 121 |
+
features["objects"] = {
|
| 122 |
+
"area": datasets.Value("int32"),
|
| 123 |
+
"bbox": datasets.Sequence(feature=datasets.Value("int32")),
|
| 124 |
+
"category": datasets.Value("string"),
|
| 125 |
+
"id": datasets.Value("int32"),
|
| 126 |
+
}
|
| 127 |
+
supervised_keys = (("image", "objects"),)
|
| 128 |
+
elif self.config.name == "instance_segmentation":
|
| 129 |
+
# features["segmentation"] = {
|
| 130 |
+
# }
|
| 131 |
+
supervised_keys = (("image", "objects"),)
|
| 132 |
else:
|
| 133 |
raise NotImplementedError()
|
| 134 |
|
|
|
|
| 161 |
for category in coco_annotations["categories"]
|
| 162 |
}
|
| 163 |
|
| 164 |
+
image_filepaths, image_metadata = [], []
|
| 165 |
+
|
| 166 |
+
if self.config.name in ("binary_classification", "multiclass_classification"):
|
| 167 |
+
label_name = "labels"
|
| 168 |
+
annotation_list = []
|
| 169 |
+
elif self.config.name == "object_detection":
|
| 170 |
+
label_name = "objects"
|
| 171 |
+
annotation_list = []
|
| 172 |
|
| 173 |
for annotation, metadata in zip(
|
| 174 |
coco_annotations["annotations"],
|
|
|
|
| 201 |
|
| 202 |
if self.config.name == "binary_classification":
|
| 203 |
_, positive_label_name = CLASS_NAMES[self.config.name]
|
| 204 |
+
annotation_list.append(positive_label_name)
|
| 205 |
elif self.config.name == "multiclass_classification":
|
| 206 |
+
annotation_list.append(category_id2_name[annotation["category_id"]])
|
| 207 |
+
elif self.config.name == "object_detection":
|
| 208 |
+
annotation_list.append({
|
| 209 |
+
"area": annotation["area"],
|
| 210 |
+
"bbox": annotation["bbox"],
|
| 211 |
+
"category": category_id2_name[annotation["category_id"]],
|
| 212 |
+
"id": annotation["category_id"],
|
| 213 |
+
})
|
| 214 |
+
elif self.config.name == "instance_segmentation":
|
| 215 |
+
raise ValueError(annotation)
|
| 216 |
+
raise NotImplementedError()
|
| 217 |
else:
|
| 218 |
raise NotImplementedError()
|
| 219 |
|
|
|
|
| 240 |
string=str(image_filename_with_parent_folder),
|
| 241 |
pattern=NO_PATHOLOGY_IMAGE_METADATA_REGEX,
|
| 242 |
)
|
| 243 |
+
|
| 244 |
try:
|
| 245 |
metadata.update(
|
| 246 |
{
|
|
|
|
| 250 |
)
|
| 251 |
except AttributeError:
|
| 252 |
if match is None:
|
| 253 |
+
print(
|
| 254 |
+
"Could not get metadata for: ",
|
| 255 |
+
image_filename_with_parent_folder,
|
| 256 |
+
)
|
| 257 |
raise ValueError(image_filename_with_parent_folder)
|
| 258 |
+
|
| 259 |
+
metadata.update(
|
| 260 |
+
{
|
| 261 |
+
"video_id": None,
|
| 262 |
+
"frame_id": None,
|
| 263 |
+
"from_seconds": None,
|
| 264 |
+
"to_seconds": None,
|
| 265 |
+
}
|
| 266 |
+
)
|
| 267 |
# NOTE: Only defined for `endometriosis` images
|
| 268 |
metadata["case_id"] = None
|
| 269 |
image_metadata.append(metadata)
|
|
|
|
| 273 |
"multiclass_classification",
|
| 274 |
):
|
| 275 |
negative_class_label_name, *_ = CLASS_NAMES[self.config.name]
|
| 276 |
+
annotation_list.append(negative_class_label_name)
|
| 277 |
+
elif self.config.name == "object_detection":
|
| 278 |
+
negative_category_id = 0
|
| 279 |
+
negative_class_label_name, *_ = CLASS_NAMES["multiclass_classification"]
|
| 280 |
+
annotation_list.append({
|
| 281 |
+
"area": None,
|
| 282 |
+
"bbox": [],
|
| 283 |
+
"category": negative_class_label_name,
|
| 284 |
+
"id": negative_category_id,
|
| 285 |
+
})
|
| 286 |
+
elif self.config.name == "instance_segmentation":
|
| 287 |
+
raise NotImplementedError()
|
| 288 |
+
else:
|
| 289 |
+
raise NotImplementedError()
|
| 290 |
+
|
| 291 |
|
| 292 |
return [
|
| 293 |
datasets.SplitGenerator(
|
| 294 |
name=datasets.Split.TRAIN,
|
| 295 |
gen_kwargs={
|
| 296 |
"image_filepaths": image_filepaths,
|
|
|
|
| 297 |
"metadata": image_metadata,
|
| 298 |
+
label_name: annotation_list,
|
| 299 |
},
|
| 300 |
)
|
| 301 |
]
|
| 302 |
|
| 303 |
+
def _generate_examples(self, **kwargs):
|
| 304 |
+
if self.config.name in ("binary_classification", "multiclass_classification"):
|
| 305 |
+
for example_id, (image_filepath, label, image_metadata) in enumerate(
|
| 306 |
+
zip(
|
| 307 |
+
kwargs["image_filepaths"],
|
| 308 |
+
kwargs["labels"],
|
| 309 |
+
kwargs["metadata"]
|
| 310 |
+
)
|
| 311 |
+
):
|
| 312 |
+
with open(image_filepath, "rb") as image_file:
|
| 313 |
+
yield example_id, {
|
| 314 |
+
"image": {"path": str(image_filepath), "bytes": image_file.read()},
|
| 315 |
+
"labels": label,
|
| 316 |
+
"metadata": image_metadata,
|
| 317 |
+
}
|
| 318 |
+
elif self.config.name == "object_detection":
|
| 319 |
+
for example_id, (image_filepath, objects, image_metadata) in enumerate(
|
| 320 |
+
zip(
|
| 321 |
+
kwargs["image_filepaths"],
|
| 322 |
+
kwargs["objects"],
|
| 323 |
+
kwargs["metadata"]
|
| 324 |
+
)
|
| 325 |
+
):
|
| 326 |
+
with open(image_filepath, "rb") as image_file:
|
| 327 |
+
yield example_id, {
|
| 328 |
+
"image": {"path": str(image_filepath), "bytes": image_file.read()},
|
| 329 |
+
"objects": objects,
|
| 330 |
+
"metadata": image_metadata,
|
| 331 |
+
}
|
| 332 |
+
else:
|
| 333 |
+
raise NotImplementedError()
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
datasets.load_dataset(__file__, name="instance_segmentation")
|