{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:21:42.761200Z","iopub.execute_input":"2025-07-14T14:21:42.761404Z","iopub.status.idle":"2025-07-14T14:21:45.377104Z","shell.execute_reply.started":"2025-07-14T14:21:42.761386Z","shell.execute_reply":"2025-07-14T14:21:45.376249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install ultralytics opencv-python-headless","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:21:56.451715Z","iopub.execute_input":"2025-07-14T14:21:56.451970Z","iopub.status.idle":"2025-07-14T14:23:09.748182Z","shell.execute_reply.started":"2025-07-14T14:21:56.451950Z","shell.execute_reply":"2025-07-14T14:23:09.747360Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport pandas as pd\nfrom PIL import Image\nfrom ultralytics import YOLO\nfrom pathlib import Path\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:31:55.055792Z","iopub.execute_input":"2025-07-14T14:31:55.056425Z","iopub.status.idle":"2025-07-14T14:31:55.060333Z","shell.execute_reply.started":"2025-07-14T14:31:55.056399Z","shell.execute_reply":"2025-07-14T14:31:55.059521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_yaml = \"\"\"\ntrain: /kaggle/input/multi-class-object-detection-challenge/Dataset/train/images\nval: /kaggle/input/multi-class-object-detection-challenge/Dataset/val/images\n\nnc : 2\nnames : ['cheerios','soup']\n\"\"\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:33:27.815129Z","iopub.execute_input":"2025-07-14T14:33:27.815919Z","iopub.status.idle":"2025-07-14T14:33:27.819170Z","shell.execute_reply.started":"2025-07-14T14:33:27.815894Z","shell.execute_reply":"2025-07-14T14:33:27.818509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open('/kaggle/working/data.yaml', 'w') as f:\n    f.write(data_yaml)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:34:12.599421Z","iopub.execute_input":"2025-07-14T14:34:12.599724Z","iopub.status.idle":"2025-07-14T14:34:12.603818Z","shell.execute_reply.started":"2025-07-14T14:34:12.599699Z","shell.execute_reply":"2025-07-14T14:34:12.603151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = YOLO('yolov8n.pt')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:34:47.531682Z","iopub.execute_input":"2025-07-14T14:34:47.532415Z","iopub.status.idle":"2025-07-14T14:34:47.576030Z","shell.execute_reply.started":"2025-07-14T14:34:47.532388Z","shell.execute_reply":"2025-07-14T14:34:47.575429Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.train(\n    data = '/kaggle/working/data.yaml',\n    epochs = 50,\n    imgsz = 640,\n    batch = 8,\n    patience = 8,\n    device = 0,\n    optimizer = 'Adam',\n    lr0 = 1e-4,\n    weight_decay=0.0004,\n    mosaic=0.5,\n    mixup=0.1,\n    fliplr=0.5,\n    flipud=0.2,\n    translate=0.1,\n    scale=0.4,\n    shear=0.2,\n    perspective=0.001,\n    val=True,\n    seed=42\n    \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:37:05.809860Z","iopub.execute_input":"2025-07-14T14:37:05.810191Z","iopub.status.idle":"2025-07-14T14:48:43.312180Z","shell.execute_reply.started":"2025-07-14T14:37:05.810170Z","shell.execute_reply":"2025-07-14T14:48:43.311063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = YOLO('/kaggle/working/runs/detect/train/weights/best.pt')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:50:33.874565Z","iopub.execute_input":"2025-07-14T14:50:33.875294Z","iopub.status.idle":"2025-07-14T14:50:33.922909Z","shell.execute_reply.started":"2025-07-14T14:50:33.875257Z","shell.execute_reply":"2025-07-14T14:50:33.922120Z"}},"outputs":[],"execution_count":null},{"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 = []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:52:48.142512Z","iopub.execute_input":"2025-07-14T14:52:48.142799Z","iopub.status.idle":"2025-07-14T14:52:48.147604Z","shell.execute_reply.started":"2025-07-14T14:52:48.142777Z","shell.execute_reply":"2025-07-14T14:52:48.147014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for 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, device=0, verbose=False)\n    boxes = results[0].boxes\n\n    if boxes is not None and len(boxes) > 0:\n        prediction_parts = []\n        for box in boxes:\n            x_center, y_center, w, h = box.xywh[0].cpu().numpy()\n            cls = int(box.cls.item())\n            conf = float(box.conf.item())\n\n            # Normalize bbox values\n            x_center /= width\n            y_center /= height\n            w /= width\n            h /= height\n\n            prediction_parts.append(f\"{cls} {conf:.6f} {x_center:.6f} {y_center:.6f} {w:.6f} {h:.6f}\")\n\n        prediction_string = ' '.join(prediction_parts)\n    else:\n        prediction_string = 'no boxes'\n\n    results_list.append({\n        \"image_id\": os.path.splitext(img_name)[0],\n        \"prediction_string\": prediction_string\n    })\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:52:49.668294Z","iopub.execute_input":"2025-07-14T14:52:49.668855Z","iopub.status.idle":"2025-07-14T14:54:39.814651Z","shell.execute_reply.started":"2025-07-14T14:52:49.668829Z","shell.execute_reply":"2025-07-14T14:54:39.813971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame(results_list)\nsubmission.to_csv(\"submission09.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:54:47.042385Z","iopub.execute_input":"2025-07-14T14:54:47.042997Z","iopub.status.idle":"2025-07-14T14:54:47.053167Z","shell.execute_reply.started":"2025-07-14T14:54:47.042970Z","shell.execute_reply":"2025-07-14T14:54:47.052594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-14T14:54:51.424728Z","iopub.execute_input":"2025-07-14T14:54:51.425277Z","iopub.status.idle":"2025-07-14T14:54:51.444596Z","shell.execute_reply.started":"2025-07-14T14:54:51.425249Z","shell.execute_reply":"2025-07-14T14:54:51.443964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}