{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":84969,"databundleVersionId":10033515,"sourceType":"competition"},{"sourceId":10459045,"sourceType":"datasetVersion","datasetId":6474903},{"sourceId":139474,"sourceType":"modelInstanceVersion","modelInstanceId":118113,"modelId":141350}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"papermill":{"default_parameters":{},"duration":6568.674569,"end_time":"2024-12-04T19:34:39.227672","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-12-04T17:45:10.553103","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# CZII YOLO11 Training Baseline\n We created a training set adapted to YOLO from [the dataset baseline](https://www.kaggle.com/code/itsuki9180/czii-making-datasets-for-yolo).\n\nIn this notebook, we actually use it to train YOLO so that it can infer the xy coordinates of particles through 2D object detection.","metadata":{}},{"cell_type":"markdown","source":"# Install and Import modules","metadata":{}},{"cell_type":"code","source":"# import os\n\n# def print_directory_tree(path, indent_level=0):\n#     indent = '    ' * indent_level\n#     print(f\"{indent}{os.path.basename(path)}/\")\n#     if os.path.isdir(path):\n#         for item in sorted(os.listdir(path)):\n#             item_path = os.path.join(path, item)\n#             if os.path.isdir(item_path):\n#                 print_directory_tree(item_path, indent_level + 1)\n#             else:\n#                 print(f\"{'    ' * (indent_level + 1)}{item}\")\n\n# base_path = '/kaggle/input/czii-making-datasets-for-yolo-synthetic-data'\n# print_directory_tree(base_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-13T11:36:26.256178Z","iopub.execute_input":"2025-01-13T11:36:26.256437Z","iopub.status.idle":"2025-01-13T11:36:26.280226Z","shell.execute_reply.started":"2025-01-13T11:36:26.256408Z","shell.execute_reply":"2025-01-13T11:36:26.279433Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-13T11:36:26.281057Z","iopub.execute_input":"2025-01-13T11:36:26.281320Z","iopub.status.idle":"2025-01-13T11:36:37.054508Z","shell.execute_reply.started":"2025-01-13T11:36:26.281296Z","shell.execute_reply":"2025-01-13T11:36:37.053642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm import tqdm\nimport glob, os\nfrom ultralytics import YOLO","metadata":{"papermill":{"duration":0.026344,"end_time":"2024-12-04T17:45:50.629952","exception":false,"start_time":"2024-12-04T17:45:50.603608","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-13T11:36:37.056710Z","iopub.execute_input":"2025-01-13T11:36:37.057002Z","iopub.status.idle":"2025-01-13T11:36:40.898703Z","shell.execute_reply.started":"2025-01-13T11:36:37.056975Z","shell.execute_reply":"2025-01-13T11:36:40.897995Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Prepare to train and instance YOLOmodel","metadata":{}},{"cell_type":"code","source":"# Load a pretrained model\nmodel = YOLO(\"/kaggle/input/yolo11/pytorch/default/1/yolo11l.pt\") # load a pretrained model (recommended for training)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-13T11:36:40.899669Z","iopub.execute_input":"2025-01-13T11:36:40.900025Z","iopub.status.idle":"2025-01-13T11:36:42.236815Z","shell.execute_reply.started":"2025-01-13T11:36:40.899996Z","shell.execute_reply":"2025-01-13T11:36:42.236039Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Change yaml","metadata":{}},{"cell_type":"code","source":"!cp /kaggle/input/czii-synthetic/kaggle/working/czii_conf.yaml .","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-13T11:36:42.237865Z","iopub.execute_input":"2025-01-13T11:36:42.238183Z","iopub.status.idle":"2025-01-13T11:36:43.253731Z","shell.execute_reply.started":"2025-01-13T11:36:42.238148Z","shell.execute_reply":"2025-01-13T11:36:43.252715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!sed -i 's|path: .*|path: /kaggle/input/czii-synthetic/kaggle/working/datasets/czii_det2d|g' /kaggle/working/czii_conf.yaml","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-13T11:36:43.255308Z","iopub.execute_input":"2025-01-13T11:36:43.255648Z","iopub.status.idle":"2025-01-13T11:36:44.286617Z","shell.execute_reply.started":"2025-01-13T11:36:43.255606Z","shell.execute_reply":"2025-01-13T11:36:44.285427Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Let's train YOLO!","metadata":{}},{"cell_type":"code","source":"# Train the model\n_ = model.train(\n    data=\"/kaggle/working/czii_conf.yaml\",\n    epochs=100,\n    warmup_epochs=10,\n    optimizer='AdamW',\n    cos_lr=True,\n    lr0=3e-4,\n    lrf=0.03,\n    imgsz=640,\n    device=\"0\",\n    weight_decay=0.005,\n    batch=8,\n    scale=0,\n    flipud=0.5,\n    fliplr=0.5,\n    degrees=45,\n    shear=5,\n    mixup=0.2,\n    copy_paste=0.25,\n    seed=8620, # (｡•◡•｡)\n)","metadata":{"papermill":{"duration":6449.128203,"end_time":"2024-12-04T19:33:19.865363","exception":false,"start_time":"2024-12-04T17:45:50.73716","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-13T11:36:44.288311Z","iopub.execute_input":"2025-01-13T11:36:44.288723Z","execution_failed":"2025-01-13T11:41:46.512Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = YOLO(\"/kaggle/working/runs/detect/train/weights/best.pt\")\nmetrics = model.val(data=\"/kaggle/input/czii-yolo-datasets/czii_conf.yaml\", imgsz=640, batch=16, conf=0.25, iou=0.6, device=\"0\", save_json=True)  # no arguments needed, dataset and settings remembered\nprint(metrics.box.map)  # map50-95\nprint(metrics.box.map50)  # map50\nprint(metrics.box.map75)  # map75\nprint(metrics.box.maps)","metadata":{"papermill":{"duration":22.977007,"end_time":"2024-12-04T19:34:29.346505","exception":false,"start_time":"2024-12-04T19:34:06.369498","status":"completed"},"tags":[],"trusted":true,"execution":{"execution_failed":"2025-01-13T11:41:46.513Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Prediction example","metadata":{}},{"cell_type":"code","source":"results = model(\"/kaggle/input/czii-making-datasets-for-yolo-synthetic-data/datasets/czii_det2d/images/val/TS_5_4_920.png\")\nresults[0].show()","metadata":{"papermill":{"duration":0.759012,"end_time":"2024-12-04T19:34:30.516699","exception":false,"start_time":"2024-12-04T19:34:29.757687","status":"completed"},"tags":[],"trusted":true,"execution":{"execution_failed":"2025-01-13T11:41:46.513Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Continue to [Submission Baseline...](https://www.kaggle.com/code/itsuki9180/czii-yolo11-submission-baseline)","metadata":{}}]}