{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [Cell QC] one-image quick test\n",
    "import torch\n",
    "from mmengine.config import Config\n",
    "from mmengine.registry import init_default_scope\n",
    "from mmengine.runner import load_checkpoint\n",
    "from mmengine.dataset import pseudo_collate\n",
    "from mmdet.registry import MODELS, DATASETS\n",
    "\n",
    "CFG    = '/data/ltb/mmyolo/configs/it1_worker_helmet/250522_yolov8_fire_transfer_coco.py'\n",
    "WEIGHT = '/data/ltb/mmyolo/work_dir/yolov8_x_mask-refine_syncbn_fast_8xb16-500e_coco_20230217_120411-079ca8d1.pth'\n",
    "\n",
    "cfg = Config.fromfile(CFG)\n",
    "cfg.launcher = 'none'                         # 분산 OFF\n",
    "init_default_scope(cfg.default_scope)\n",
    "\n",
    "model = MODELS.build(cfg.model)\n",
    "load_checkpoint(model, WEIGHT, map_location='cpu', strict=False)\n",
    "model.cuda().eval()\n",
    "\n",
    "dataset = DATASETS.build(cfg.val_dataloader.dataset)\n",
    "\n",
    "for i in range(2):                            # 2장만 확인\n",
    "    single = dataset[i]\n",
    "    batch  = pseudo_collate([single])         # N=1 배치로 변환\n",
    "    with torch.no_grad():\n",
    "        result = model.test_step(batch)[0]    # DetDataSample\n",
    "    inst    = result.pred_instances           # ← 딕셔너리 X\n",
    "    bboxes  = inst.bboxes\n",
    "    scores  = inst.scores\n",
    "    print(f\"[{i}] #box={len(bboxes):3}   max_score={scores.max().item():.3f}\" if len(scores) else\n",
    "          f\"[{i}] #box=0   max_score=None\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [Cell 2] Annotation 검증 – id 범위, 배경 클래스 포함 여부\n",
    "import json, collections, os\n",
    "VAL_JSON = '/data/ai-hub/fire-dataset/106.화재_발생_예측_데이터(연기_동영상)/01.데이터/val.json'\n",
    "\n",
    "with open(VAL_JSON) as f:\n",
    "    coco = json.load(f)\n",
    "\n",
    "ids = [ann['category_id'] for ann in coco['annotations']]\n",
    "print(\"범위:\", min(ids), \"–\", max(ids), \"   고유 id 개수:\", len(set(ids)))\n",
    "print(\"배경 id 존재?\", 11 in set(ids))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [Cell 3] 이미지 경로 존재 여부 스캔 (10초 미만)\n",
    "import tqdm, os\n",
    "miss = []\n",
    "for im in tqdm.tqdm(coco['images']):\n",
    "    if not os.path.exists(im['file_name']):\n",
    "        miss.append(im['file_name'])\n",
    "print(\"#missing =\", len(miss))\n",
    "if miss: print(\"예시:\", miss[:5])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "250520_mmyolo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
