"""Micro-OD: A few-shot microscopy object detection benchmark.""" import json import os import datasets _DESCRIPTION = """\ Micro-OD is a few-shot microscopy object detection benchmark spanning four biological imaging domains: BBBC (malaria parasite detection), BCCD (blood cell counting), LIVECell (RatC6 live-cell imaging), and NIH-3T3 (mouse fibroblast imaging). Two splits are provided: - example: 10 images per sub-dataset (40 total) — the few-shot support set. - test: 53 images per sub-dataset (212 total) — the evaluation query set. """ # Sorted alphabetically; used as ClassLabel names. ALL_CLASSES = [ "Gametocyte Cells", "Platelets", "Polygonal Cells", "Red Blood Cells", "Ring Cells", "Round Cells", "Schizont Cells", "Spindle Cells", "Trophozoite Cells", "White Blood Cells", ] SUBDATASETS = ["BBBC", "BCCD", "LIVECell", "NIH-3T3"] class MicroOD(datasets.GeneratorBasedBuilder): """Micro-OD dataset loader.""" VERSION = datasets.Version("1.0.0") def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "image": datasets.Image(), "image_id": datasets.Value("string"), "subdataset": datasets.Value("string"), "objects": datasets.Sequence( feature={ # COCO format: [x_min, y_min, width, height] "bbox": datasets.Sequence( datasets.Value("float32"), length=4 ), "label": datasets.ClassLabel(names=ALL_CLASSES), "category": datasets.Value("string"), } ), } ), ) def _split_generators(self, dl_manager): data_dir = os.path.dirname(os.path.abspath(__file__)) return [ datasets.SplitGenerator( name=datasets.splits.NamedSplit("example"), gen_kwargs={"split_path": os.path.join(data_dir, "example")}, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"split_path": os.path.join(data_dir, "test")}, ), ] def _generate_examples(self, split_path): idx = 0 for subdataset in SUBDATASETS: annotation_path = os.path.join( split_path, subdataset, "annotation.jsonl" ) image_base = os.path.join(split_path, subdataset) with open(annotation_path, encoding="utf-8") as f: for line in f: line = line.strip() if not line: continue entry = json.loads(line) image_path = os.path.join(image_base, entry["image_path"]) bboxes, labels, categories = [], [], [] for category, boxes in entry["bbox"].items(): for box in boxes: x_min, y_min = box[0] x_max, y_max = box[1] # Convert [[x_min,y_min],[x_max,y_max]] → COCO [x,y,w,h] bboxes.append( [ float(x_min), float(y_min), float(x_max - x_min), float(y_max - y_min), ] ) labels.append(ALL_CLASSES.index(category)) categories.append(category) yield idx, { "image": image_path, "image_id": f"{subdataset}/{entry['image_path']}", "subdataset": subdataset, "objects": { "bbox": bboxes, "label": labels, "category": categories, }, } idx += 1