{"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":107469,"databundleVersionId":13024000,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics > /dev/null","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T08:07:56.765689Z","iopub.execute_input":"2025-07-16T08:07:56.766263Z","iopub.status.idle":"2025-07-16T08:09:12.852620Z","shell.execute_reply.started":"2025-07-16T08:07:56.766238Z","shell.execute_reply":"2025-07-16T08:09:12.851716Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 𝙞𝙢𝙥𝙤𝙧𝙩 𝙡𝙞𝙗𝙨","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom PIL import Image\nfrom pathlib import Path\nfrom ultralytics import YOLO","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T08:09:12.854118Z","iopub.execute_input":"2025-07-16T08:09:12.854363Z","iopub.status.idle":"2025-07-16T08:09:17.253659Z","shell.execute_reply.started":"2025-07-16T08:09:12.854339Z","shell.execute_reply":"2025-07-16T08:09:17.253019Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 𝙘𝙧𝙚𝙖𝙩𝙚 𝙮𝙖𝙢𝙡 𝙘𝙤𝙣𝙛𝙞𝙜𝙪𝙧𝙖𝙩𝙞𝙤𝙣","metadata":{}},{"cell_type":"code","source":"data_yaml = \"\"\"\ntrain: /kaggle/input/multi-class-object-detection-challenge/Starter_Dataset/train/images\nval: /kaggle/input/multi-class-object-detection-challenge/Starter_Dataset/val/images\n\nnc: 2\nnames: ['cheerios', 'soup']\n\"\"\"\n\nwith open('/kaggle/working/data.yaml', 'w') as file:\n    file.write(data_yaml)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T08:09:17.254392Z","iopub.execute_input":"2025-07-16T08:09:17.254827Z","iopub.status.idle":"2025-07-16T08:09:17.259079Z","shell.execute_reply.started":"2025-07-16T08:09:17.254790Z","shell.execute_reply":"2025-07-16T08:09:17.258157Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 𝙇𝙚𝙖𝙧𝙣 𝙔𝙊𝙇𝙊𝙫12 𝙢𝙤𝙙𝙚𝙡","metadata":{}},{"cell_type":"code","source":"model = YOLO(\"yolo12m.pt\")\ndata_yaml = '/kaggle/working/data.yaml'\n\nresults = model.train(\n    data=data_yaml,\n    pretrained=True,\n    epochs=150,\n    batch=8,\n    imgsz=960,\n    device=[0, 1],\n    patience=20,\n    lr0=0.0001,\n    lrf=0.02,\n    optimizer=\"Adam\",\n    weight_decay=0.0004,\n    cos_lr=True,\n    dropout=0.3,\n    label_smoothing=0.01,\n    mosaic=0.5,\n    mixup=0.15,\n    copy_paste=0.1,\n    fliplr=0.5,\n    flipud=0.4,\n    hsv_h=0.02,\n    hsv_s=0.2,\n    hsv_v=0.4,\n    translate=0.2,\n    scale=0.5,\n    shear=0.2,\n    perspective=0.007,\n    val=True,\n    workers=8,\n    seed=6\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-16T08:11:21.184556Z","iopub.execute_input":"2025-07-16T08:11:21.184856Z","iopub.status.idle":"2025-07-16T08:13:32.860786Z","shell.execute_reply.started":"2025-07-16T08:11:21.184829Z","shell.execute_reply":"2025-07-16T08:13:32.859581Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 𝙎𝙝𝙤𝙬 𝙞𝙢𝙖𝙜𝙚","metadata":{}},{"cell_type":"code","source":"img_path = '/kaggle/input/multi-class-object-detection-challenge/testImages/images/IMG_8656.png'\nmodel = YOLO('/kaggle/working/runs/detect/train/weights/best.pt')\nresults = model(img_path)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-13T19:42:24.675707Z","iopub.status.idle":"2025-07-13T19:42:24.676007Z","shell.execute_reply.started":"2025-07-13T19:42:24.675854Z","shell.execute_reply":"2025-07-13T19:42:24.675869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result_img = results[0].plot()\nplt.imshow(result_img)\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-13T19:42:24.676476Z","iopub.status.idle":"2025-07-13T19:42:24.676784Z","shell.execute_reply.started":"2025-07-13T19:42:24.676628Z","shell.execute_reply":"2025-07-13T19:42:24.676642Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 𝙋𝙧𝙚𝙙𝙞𝙘𝙩","metadata":{}},{"cell_type":"code","source":"test_images_dir = '/kaggle/input/multi-class-object-detection-challenge/testImages/images'\nimage_files = [f for f in os.listdir(test_images_dir) if f.endswith(('.jpg', '.png'))]\n\nresults_list = []\n\nfor img_name in image_files:\n    img_path = os.path.join(test_images_dir, img_name)\n\n    with Image.open(img_path) as img:\n        width, height = img.size\n\n    results = model.predict(img_path, conf=0.0001, device='0', verbose=False)\n    boxes = results[0].boxes\n    \n    if boxes is not None and len(boxes) > 0:\n        prediction_strings = []\n        for box in boxes:\n            box_data = box.xywh[0].cpu().numpy()\n            \n            cls = int(box.cls.item())\n            conf = box.conf.item()\n\n            x_center = box_data[0] / width\n            y_center = box_data[1] / height\n            w = box_data[2] / width\n            h = box_data[3] / height\n            \n            prediction_strings.append(f\"{cls} {conf:.6f} {x_center:.6f} {y_center:.6f} {w:.6f} {h:.6f}\")\n        \n        prediction_str = \" \".join(prediction_strings)\n    else:\n        prediction_str = \"no boxes\"\n    \n    results_list.append({\n        \"image_id\": os.path.splitext(img_name)[0],\n        \"prediction_string\": prediction_str\n    })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-13T19:42:24.677226Z","iopub.status.idle":"2025-07-13T19:42:24.677556Z","shell.execute_reply.started":"2025-07-13T19:42:24.677390Z","shell.execute_reply":"2025-07-13T19:42:24.677404Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 𝙨𝙖𝙫𝙚 𝙨𝙪𝙗𝙢𝙞𝙨𝙨𝙞𝙤𝙣","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(results_list)\nsubmission.to_csv('submission.csv', index=False)\nsubmission.sample(15)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-13T19:42:24.677995Z","iopub.status.idle":"2025-07-13T19:42:24.679288Z","shell.execute_reply.started":"2025-07-13T19:42:24.678137Z","shell.execute_reply":"2025-07-13T19:42:24.678152Z"}},"outputs":[],"execution_count":null}]}