{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3346317,"sourceType":"datasetVersion","datasetId":1935874},{"sourceId":14268842,"sourceType":"datasetVersion","datasetId":9105661},{"sourceId":14326276,"sourceType":"datasetVersion","datasetId":9127744}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:50:29.552375Z","iopub.execute_input":"2025-12-29T01:50:29.552845Z","iopub.status.idle":"2025-12-29T01:50:29.685169Z","shell.execute_reply.started":"2025-12-29T01:50:29.552820Z","shell.execute_reply":"2025-12-29T01:50:29.684560Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!cp -r /kaggle/input/yolov5/yolov13-main /kaggle/working/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:50:29.686879Z","iopub.execute_input":"2025-12-29T01:50:29.687073Z","iopub.status.idle":"2025-12-29T01:50:32.749138Z","shell.execute_reply.started":"2025-12-29T01:50:29.687055Z","shell.execute_reply":"2025-12-29T01:50:32.748209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cd /kaggle/working/yolov13-main","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:50:32.750295Z","iopub.execute_input":"2025-12-29T01:50:32.750529Z","iopub.status.idle":"2025-12-29T01:50:32.756663Z","shell.execute_reply.started":"2025-12-29T01:50:32.750506Z","shell.execute_reply":"2025-12-29T01:50:32.756143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport yaml\n\n# 1. 定义你的yaml文件路径（替换为实际路径）\nYAML_PATH = \"/kaggle/input/ip102-yolov5/IP102_YOLOv5/ip102.yaml\"\n# 定义数据集根路径（yaml中path应指向的正确目录）\nDATA_ROOT = \"/kaggle/input/ip102-yolov5/IP102_YOLOv5\"\n\n# 2. 读取并修正yaml\nwith open(YAML_PATH, 'r') as f:\n    yaml_data = yaml.safe_load(f)\n\n# 修正核心参数：path用绝对路径，train/val用简短相对路径\nyaml_data['path'] = DATA_ROOT\nyaml_data['train'] = \"images/train\"\nyaml_data['val'] = \"images/val\"\n\n# 保存修正后的yaml到working目录（避免input只读问题）\nFIXED_YAML_PATH = \"/kaggle/working/ip102_fixed.yaml\"\nwith open(FIXED_YAML_PATH, 'w') as f:\n    yaml.safe_dump(yaml_data, f, sort_keys=False)\n\n# 3. 验证图片路径是否存在（关键：提前排查）\ntrain_img_path = os.path.join(DATA_ROOT, \"images/train\")\nval_img_path = os.path.join(DATA_ROOT, \"images/val\")\nprint(f\"训练集图片路径是否存在：{os.path.exists(train_img_path)}\")\nprint(f\"验证集图片路径是否存在：{os.path.exists(val_img_path)}\")\n\n# 若输出False，检查路径是否正确（比如images是否写成image，或train/val目录为空）\nif not os.path.exists(train_img_path):\n    raise Exception(f\"训练集路径不存在：{train_img_path}\")\nif not os.path.exists(val_img_path):\n    raise Exception(f\"验证集路径不存在：{val_img_path}\")\n\nprint(f\"\\n✅ 修正后的yaml文件已保存到：{FIXED_YAML_PATH}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:50:32.758114Z","iopub.execute_input":"2025-12-29T01:50:32.758297Z","iopub.status.idle":"2025-12-29T01:50:32.831291Z","shell.execute_reply.started":"2025-12-29T01:50:32.758282Z","shell.execute_reply":"2025-12-29T01:50:32.830530Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -U ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:50:32.831918Z","iopub.execute_input":"2025-12-29T01:50:32.832155Z","iopub.status.idle":"2025-12-29T01:51:58.198857Z","shell.execute_reply.started":"2025-12-29T01:50:32.832135Z","shell.execute_reply":"2025-12-29T01:51:58.198208Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 先检查GPU数量\nimport torch\nprint(f\"可用GPU数量: {torch.cuda.device_count()}\")  # 若输出1，说明只有1个GPU，无法用2进程","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:51:58.199775Z","iopub.execute_input":"2025-12-29T01:51:58.200063Z","iopub.status.idle":"2025-12-29T01:52:02.717131Z","shell.execute_reply.started":"2025-12-29T01:51:58.200039Z","shell.execute_reply":"2025-12-29T01:52:02.716427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip show ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:52:02.718125Z","iopub.execute_input":"2025-12-29T01:52:02.718711Z","iopub.status.idle":"2025-12-29T01:52:04.635035Z","shell.execute_reply.started":"2025-12-29T01:52:02.718689Z","shell.execute_reply":"2025-12-29T01:52:04.634312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -e .","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:52:04.636138Z","iopub.execute_input":"2025-12-29T01:52:04.636383Z","iopub.status.idle":"2025-12-29T01:52:14.426487Z","shell.execute_reply.started":"2025-12-29T01:52:04.636358Z","shell.execute_reply":"2025-12-29T01:52:14.425693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\nimport torch\nimport torch.distributed as dist\nimport os\nimport sys\n\n\n\nmodel = YOLO('yolov13s.pt')\n\n# 开始训练（核心：用修正后的yaml）\nresults = model.train(\n    data=\"/kaggle/working/ip102_fixed.yaml\",  # 改用working目录的修正yaml\n    epochs=30,\n    batch=32,\n    imgsz=640,\n    device=\"0,1\",\n    project='yolov13_training',\n    amp=True,\n    save_period=30,\n    patience=15,\n    pretrained=True,\n    workers=0,  \n    verbose=True,  # 打印详细日志\n)\n\n# 验证模型\nmetrics = model.val()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-29T01:52:14.427448Z","iopub.execute_input":"2025-12-29T01:52:14.427690Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\ntotal_metrics = {\n    \"总评价指标\": {\n        \"mAP@0.5\": round(float(metrics.box.map50), 4),  # 直接用属性，转float后取整 # 毕设目标≥0.7\n        \"mAP@0.5:0.95\": round(float(metrics.box.map), 4),\n        \"整体精确率(Precision)\": round(float(metrics.box.mp), 4),\n        \"整体召回率(Recall)\": round(float(metrics.box.mr), 4),\n        \"整体F1分数\": round(float(metrics.box.f1.mean()), 4),  # f1是数组，取平均后转float\n        \"类别数量\": metrics.box.nc,\n    }\n}\nSAVE_PATH=\"/kaggle/working/total_metrics.json\"\n# 保存为JSON\nwith open(SAVE_PATH, \"w\", encoding=\"utf-8\") as f:\n    json.dump(total_metrics, f, indent=4, ensure_ascii=False)\nprint(f\"总评价指标已保存到：{SAVE_PATH}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}