{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":117682,"databundleVersionId":15062069},{"sourceType":"datasetVersion","sourceId":14597686,"datasetId":9324423,"databundleVersionId":15432883}],"dockerImageVersionId":31260,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<!-- Python beginner 2025/12/24, for personal archive and reference -->","metadata":{}},{"cell_type":"code","source":"!pip install /kaggle/input/my-imagecodecs/*.whl --no-index --find-links=/kaggle/input/my-imagecodecs/\n\n\nimport os\nimport zipfile\nimport pandas as pd\nimport numpy as np\nimport tifffile\nimport torch\nfrom tqdm import tqdm\n\n# =========================\n# 1. 설정 및 경로\n# =========================\nINPUT_DIR = \"/kaggle/input/vesuvius-challenge-surface-detection\"\nTEST_CSV = os.path.join(INPUT_DIR, \"test.csv\")\nTEST_IMG_DIR = os.path.join(INPUT_DIR, \"test_images\")\nOUTPUT_ZIP = \"submission.zip\"\n\ntest_df = pd.read_csv(TEST_CSV)\n\ntif_files = []\n\nprint(f\"🚀 Starting inference for {len(test_df)} images...\")\n\n# =========================\n# 2. 더미 마스크 생성 (Shape 정확히 맞춤)\n# =========================\nwith torch.no_grad():\n    for _, row in tqdm(test_df.iterrows(), total=len(test_df)):\n        img_id = str(row[\"id\"])\n        img_path = os.path.join(TEST_IMG_DIR, f\"{img_id}.tif\")\n\n        # 🔥 핵심 1: 실제 test tif shape 읽기\n        with tifffile.TiffFile(img_path) as tif:\n            vol = tif.asarray()   # (Z, H, W)\n\n        # 🔥 핵심 2: 동일 shape + 동일 dtype\n        mask_bin = np.zeros_like(vol, dtype=np.uint8)\n\n        # 🔥 핵심 3: 완전 0 방지 → 중앙 1픽셀만 켜기\n        z, h, w = mask_bin.shape\n        mask_bin[z // 2, h // 2, w // 2] = 1\n\n        # 규칙: 파일명은 [image_id].tif\n        file_name = f\"{img_id}.tif\"\n\n        tifffile.imwrite(file_name, mask_bin)\n        tif_files.append(file_name)\n\n# =========================\n# 3. submission.zip 생성\n# =========================\nwith zipfile.ZipFile(OUTPUT_ZIP, \"w\", zipfile.ZIP_DEFLATED) as submission_zip:\n    for f in tif_files:\n        submission_zip.write(f, arcname=f)\n        os.remove(f)  # 용량 확보용\n\nprint(f\"✅ {OUTPUT_ZIP} 생성 완료!\")\n\n# =========================\n# 4. 최종 검증 로그\n# =========================\nprint(\"\\n--- FINAL CHECK ---\")\nprint(\"CSV rows:\", len(test_df))\nprint(\"ZIP files:\", len(zipfile.ZipFile(OUTPUT_ZIP).namelist()))\nprint(\"First 5 zip entries:\", zipfile.ZipFile(OUTPUT_ZIP).namelist()[:5])\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}