Datasets:
Download scripts/validate_coco_api.py from nnvff/Xray36-COCO-v1: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/datasets/nnvff/Xray36-COCO-v1/resolve/main/scripts/validate_coco_api.py
- Command line
-
hf download hf://datasets/nnvff/Xray36-COCO-v1/scripts/validate_coco_api.py
-
curl -L -o validate_coco_api.py https://huggingface.co/datasets/nnvff/Xray36-COCO-v1/resolve/main/scripts/validate_coco_api.py
3.45 kB
| #!/usr/bin/env python3 | |
| """Check official COCO loading and a synthetic bbox evaluation round trip.""" | |
| import gc | |
| import hashlib | |
| import importlib.metadata | |
| import json | |
| from datetime import datetime | |
| from pathlib import Path | |
| from zoneinfo import ZoneInfo | |
| from pycocotools.coco import COCO | |
| from pycocotools.cocoeval import COCOeval | |
| ROOT = Path(__file__).resolve().parents[1] | |
| def main(): | |
| results = [] | |
| for split in ('train', 'test'): | |
| path = ROOT / 'annotations' / f'instances_{split}.json' | |
| coco = COCO(str(path)) | |
| category_ids = sorted(coco.getCatIds()) | |
| assert category_ids == list(range(36)), category_ids | |
| assert len(coco.imgs) == len(coco.dataset['images']) | |
| assert len(coco.anns) == len(coco.dataset['annotations']) | |
| counts = {cid: len(coco.getAnnIds(catIds=[cid])) for cid in category_ids} | |
| assert all(counts.values()), counts | |
| assert sum(counts.values()) == len(coco.anns) | |
| for iid in [min(coco.imgs), max(coco.imgs)]: | |
| image = coco.loadImgs([iid])[0] | |
| assert not image['file_name'].startswith('images/') | |
| assert (ROOT / 'images' / image['file_name']).is_file() | |
| anns = coco.loadAnns(coco.getAnnIds(imgIds=[iid])) | |
| assert anns and all(a['image_id'] == iid for a in anns) | |
| # Use ground-truth boxes as synthetic detections to exercise the public | |
| # evaluator. These are an API compatibility check, not model predictions. | |
| sample_ids = sorted({min(coco.getImgIds(catIds=[cid])) for cid in category_ids}) | |
| sample_annotations = coco.loadAnns(coco.getAnnIds(imgIds=sample_ids)) | |
| predictions = [{'image_id': a['image_id'], 'category_id': a['category_id'], | |
| 'bbox': a['bbox'][:], 'score': 1.0} for a in sample_annotations] | |
| detections = coco.loadRes(predictions) | |
| evaluator = COCOeval(coco, detections, 'bbox') | |
| evaluator.params.imgIds = sample_ids | |
| evaluator.params.catIds = category_ids | |
| evaluator.evaluate() | |
| evaluator.accumulate() | |
| precision = evaluator.eval['precision'][:, :, :, 0, -1] | |
| valid_precision = precision[precision >= 0] | |
| assert len(valid_precision) and float(valid_precision.min()) > 0.999999 | |
| evaluation_flow = {'status': 'passed', 'input': 'synthetic ground-truth bbox detections', | |
| 'images': len(sample_ids), 'detections': len(predictions), | |
| 'categories': len(category_ids), 'model_evaluation': False} | |
| results.append({'split': split, 'images': len(coco.imgs), | |
| 'annotations': len(coco.anns), 'categories': len(category_ids), | |
| 'per_category': counts, 'evaluation_flow': evaluation_flow, | |
| 'coco_json_sha256': hashlib.sha256(path.read_bytes()).hexdigest()}) | |
| del evaluator, detections, coco | |
| gc.collect() | |
| report = {'status': 'passed', 'checked_at': datetime.now(ZoneInfo('Asia/Hong_Kong')).isoformat(), | |
| 'pycocotools_version': importlib.metadata.version('pycocotools'), | |
| 'scope': 'Public COCO loading, indexing, lookups and synthetic bbox evaluation flow; no model training or performance evaluation.', | |
| 'splits': results} | |
| (ROOT / 'reports' / 'coco_api_validation.json').write_text(json.dumps(report, indent=2) + '\n') | |
| print(json.dumps(report)) | |
| if __name__ == '__main__': | |
| main() | |