{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":"nvidiaTeslaT4","dataSources":[{"sourceId":85240,"databundleVersionId":9622164,"sourceType":"competition"}],"dockerImageVersionId":30787,"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":"2024-12-02T09:30:32.520456Z","iopub.execute_input":"2024-12-02T09:30:32.521512Z","iopub.status.idle":"2024-12-02T09:30:49.430785Z","shell.execute_reply.started":"2024-12-02T09:30:32.521446Z","shell.execute_reply":"2024-12-02T09:30:49.428216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install ultralytics pandas pyyaml","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:35:20.496603Z","iopub.execute_input":"2024-12-02T09:35:20.497035Z","iopub.status.idle":"2024-12-02T09:35:30.917791Z","shell.execute_reply.started":"2024-12-02T09:35:20.496993Z","shell.execute_reply":"2024-12-02T09:35:30.9165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom pathlib import Path\nfrom ultralytics import YOLO","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:35:37.411739Z","iopub.execute_input":"2024-12-02T09:35:37.41212Z","iopub.status.idle":"2024-12-02T09:35:41.169187Z","shell.execute_reply.started":"2024-12-02T09:35:37.412087Z","shell.execute_reply":"2024-12-02T09:35:41.168414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/images\"\ntrain_labels = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/labels\"\ntest_images = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images\"\noutput_dir = Path(\"results\")\noutput_dir.mkdir(exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:43:49.120585Z","iopub.execute_input":"2024-12-02T09:43:49.121615Z","iopub.status.idle":"2024-12-02T09:43:49.127999Z","shell.execute_reply.started":"2024-12-02T09:43:49.121555Z","shell.execute_reply":"2024-12-02T09:43:49.12679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_dir = Path(\"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/labels\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:45:44.001081Z","iopub.execute_input":"2024-12-02T09:45:44.00149Z","iopub.status.idle":"2024-12-02T09:45:44.005838Z","shell.execute_reply.started":"2024-12-02T09:45:44.001453Z","shell.execute_reply":"2024-12-02T09:45:44.004863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_ids = set()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:44:03.359223Z","iopub.execute_input":"2024-12-02T09:44:03.359548Z","iopub.status.idle":"2024-12-02T09:44:03.363981Z","shell.execute_reply.started":"2024-12-02T09:44:03.359518Z","shell.execute_reply":"2024-12-02T09:44:03.362907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for label_file in labels_dir.glob(\"*.txt\"):\n    with open(label_file, \"r\") as f:\n        for line in f:\n            class_id = int(line.split()[0])  # Extract class_id from each line\n            class_ids.add(class_id)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:45:54.092333Z","iopub.execute_input":"2024-12-02T09:45:54.093243Z","iopub.status.idle":"2024-12-02T09:46:53.725227Z","shell.execute_reply.started":"2024-12-02T09:45:54.093205Z","shell.execute_reply":"2024-12-02T09:46:53.724504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_ids = sorted(class_ids)\nnc = len(class_ids)\nnames = [f\"class_{i}\" for i in class_ids]  # Default names, replace with actual if known","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:47:05.23119Z","iopub.execute_input":"2024-12-02T09:47:05.231504Z","iopub.status.idle":"2024-12-02T09:47:05.236168Z","shell.execute_reply.started":"2024-12-02T09:47:05.231477Z","shell.execute_reply":"2024-12-02T09:47:05.235141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"Number of classes (nc): {nc}\")\nprint(f\"Class names (names): {names}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:57:33.719116Z","iopub.execute_input":"2024-12-02T09:57:33.720115Z","iopub.status.idle":"2024-12-02T09:57:33.725593Z","shell.execute_reply.started":"2024-12-02T09:57:33.720062Z","shell.execute_reply":"2024-12-02T09:57:33.7244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_yaml = {\n    \"train\": \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/images\",\n    \"val\": \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/images\",  \n    \"nc\": 6, \n    \"names\": ['class_0', 'class_1', 'class_2', 'class_3', 'class_4', 'class_5'], \n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T09:57:36.196478Z","iopub.execute_input":"2024-12-02T09:57:36.196868Z","iopub.status.idle":"2024-12-02T09:57:36.201625Z","shell.execute_reply.started":"2024-12-02T09:57:36.196832Z","shell.execute_reply":"2024-12-02T09:57:36.200606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"yaml_path = \"/kaggle/working/data.yaml\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T10:00:24.061228Z","iopub.execute_input":"2024-12-02T10:00:24.06198Z","iopub.status.idle":"2024-12-02T10:00:24.06593Z","shell.execute_reply.started":"2024-12-02T10:00:24.061943Z","shell.execute_reply":"2024-12-02T10:00:24.064873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import yaml","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T10:00:52.749228Z","iopub.execute_input":"2024-12-02T10:00:52.750239Z","iopub.status.idle":"2024-12-02T10:00:52.754923Z","shell.execute_reply.started":"2024-12-02T10:00:52.750187Z","shell.execute_reply":"2024-12-02T10:00:52.753898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(yaml_path, \"w\") as f:\n    yaml.dump(data_yaml, f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T10:01:05.338245Z","iopub.execute_input":"2024-12-02T10:01:05.338598Z","iopub.status.idle":"2024-12-02T10:01:05.344574Z","shell.execute_reply.started":"2024-12-02T10:01:05.338565Z","shell.execute_reply":"2024-12-02T10:01:05.343388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = YOLO(\"yolov5s.pt\") ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T10:01:28.921161Z","iopub.execute_input":"2024-12-02T10:01:28.921555Z","iopub.status.idle":"2024-12-02T10:01:29.655351Z","shell.execute_reply.started":"2024-12-02T10:01:28.921521Z","shell.execute_reply":"2024-12-02T10:01:29.654431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model.train(data=str(\"/kaggle/working/data.yaml\"), epochs=50, imgsz=640, batch=16)\nmodel.train(data=str(\"/kaggle/working/data.yaml\"), epochs=10, imgsz=416, batch=16, patience=3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T10:19:59.437906Z","iopub.execute_input":"2024-12-02T10:19:59.43858Z","iopub.status.idle":"2024-12-02T10:46:17.167509Z","shell.execute_reply.started":"2024-12-02T10:19:59.438541Z","shell.execute_reply":"2024-12-02T10:46:17.166341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(source=test_images, save=True, conf=0.25)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T10:47:02.401714Z","iopub.execute_input":"2024-12-02T10:47:02.402076Z","iopub.status.idle":"2024-12-02T10:48:02.821764Z","shell.execute_reply.started":"2024-12-02T10:47:02.402047Z","shell.execute_reply":"2024-12-02T10:48:02.820784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_mapping = {\n    0: \"aegypti\",\n    1: \"albopictus\",\n    2: \"anopheles\",\n    3: \"culex\",\n    4: \"culiseta\",\n    5: \"japonicus/koreicus\"\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:10:47.497237Z","iopub.execute_input":"2024-12-02T11:10:47.49797Z","iopub.status.idle":"2024-12-02T11:10:47.502624Z","shell.execute_reply.started":"2024-12-02T11:10:47.497928Z","shell.execute_reply":"2024-12-02T11:10:47.501632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_data = []\n\nfor pred in predictions:\n    image_id = os.path.basename(pred.path)\n\n    for det in pred.boxes:\n        det_xyxy = det.xyxy[0].cpu().numpy()  \n        conf = det.conf[0].cpu().numpy() \n        class_id = det.cls[0].cpu().numpy()  \n\n        x1, y1, x2, y2 = det_xyxy\n\n        x_center = (x1 + x2) / 2\n        y_center = (y1 + y2) / 2\n        width = x2 - x1\n        height = y2 - y1\n\n        conf = round(float(conf), 1)\n        x_center = round(float(x_center), 1)\n        y_center = round(float(y_center), 1)\n        width = round(float(width), 1)\n        height = round(float(height), 1)\n\n        submission_data.append({\n            \"id\": len(submission_data),\n            \"ImageID\": image_id,\n            \"LabelName\": class_mapping[int(class_id)], \n            \"Conf\": conf,\n            \"xcenter\": x_center,\n            \"ycenter\": y_center,\n            \"bbx_width\": width,\n            \"bbx_height\": height,\n        })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:10:50.750735Z","iopub.execute_input":"2024-12-02T11:10:50.75113Z","iopub.status.idle":"2024-12-02T11:10:50.882219Z","shell.execute_reply.started":"2024-12-02T11:10:50.751099Z","shell.execute_reply":"2024-12-02T11:10:50.881469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_data = []\n\nimage_width = 3024  \nimage_height = 4032  \n\nfor pred in predictions:\n    image_id = os.path.basename(pred.path)\n\n    for det in pred.boxes:\n        det_xyxy = det.xyxy[0].cpu().numpy()  \n        conf = det.conf[0].cpu().numpy()  \n        class_id = det.cls[0].cpu().numpy()  \n\n        x1, y1, x2, y2 = det_xyxy\n\n        x_center = (x1 + x2) / 2\n        y_center = (y1 + y2) / 2\n        width = x2 - x1\n        height = y2 - y1\n\n        x_center_norm = x_center / image_width\n        y_center_norm = y_center / image_height\n        width_norm = width / image_width\n        height_norm = height / image_height\n\n        conf = round(float(conf), 1)\n        x_center_norm = round(float(x_center_norm), 1)\n        y_center_norm = round(float(y_center_norm), 1)\n        width_norm = round(float(width_norm), 1)\n        height_norm = round(float(height_norm), 1)\n\n        submission_data.append({\n            \"id\": len(submission_data),\n            \"ImageID\": image_id,\n            \"LabelName\": class_mapping[int(class_id)], \n            \"Conf\": conf,\n            \"xcenter\": x_center_norm,\n            \"ycenter\": y_center_norm,\n            \"bbx_width\": width_norm,\n            \"bbx_height\": height_norm,\n        })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:23:34.122446Z","iopub.execute_input":"2024-12-02T11:23:34.123255Z","iopub.status.idle":"2024-12-02T11:23:34.268445Z","shell.execute_reply.started":"2024-12-02T11:23:34.123216Z","shell.execute_reply":"2024-12-02T11:23:34.26768Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame(submission_data)\nsubmission_df.to_csv(output_dir / \"submission.csv\", index=False)\n\nprint(f\"Submission file saved at: {output_dir / 'submission.csv'}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:23:38.052694Z","iopub.execute_input":"2024-12-02T11:23:38.053492Z","iopub.status.idle":"2024-12-02T11:23:38.067856Z","shell.execute_reply.started":"2024-12-02T11:23:38.053458Z","shell.execute_reply":"2024-12-02T11:23:38.066884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(output_dir / \"submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:23:41.02022Z","iopub.execute_input":"2024-12-02T11:23:41.02059Z","iopub.status.idle":"2024-12-02T11:23:41.028729Z","shell.execute_reply.started":"2024-12-02T11:23:41.020555Z","shell.execute_reply":"2024-12-02T11:23:41.027865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:23:44.001696Z","iopub.execute_input":"2024-12-02T11:23:44.002072Z","iopub.status.idle":"2024-12-02T11:23:44.016976Z","shell.execute_reply.started":"2024-12-02T11:23:44.002039Z","shell.execute_reply":"2024-12-02T11:23:44.015844Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install pillow","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:17:58.150006Z","iopub.execute_input":"2024-12-02T11:17:58.15067Z","iopub.status.idle":"2024-12-02T11:18:07.03496Z","shell.execute_reply.started":"2024-12-02T11:17:58.150636Z","shell.execute_reply":"2024-12-02T11:18:07.033864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nimport os\n\n# Path to the image (replace with the actual path)\nimage_path = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images/00fbfad7-9722-4581-831c-79faa576ea7f.jpeg\"  # Example image path\n\n# Open the image\nwith Image.open(image_path) as img:\n    # Get image size (width, height)\n    image_width, image_height = img.size\n\n# Print image size\nprint(f\"Image width: {image_width}, Image height: {image_height}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T11:19:03.318849Z","iopub.execute_input":"2024-12-02T11:19:03.319726Z","iopub.status.idle":"2024-12-02T11:19:03.330095Z","shell.execute_reply.started":"2024-12-02T11:19:03.319688Z","shell.execute_reply":"2024-12-02T11:19:03.329183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Image width: 3024, Image height: 4032\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(base_dir / \"data.yaml\", \"w\") as f:\n    import yaml\n    yaml.dump(data_yaml, f)\n\n# Step 2: Load YOLO Model\nmodel = YOLO(\"yolov5s.pt\")  # Load pre-trained YOLOv5 model\n\n# Step 3: Train the Model\nmodel.train(data=str(base_dir / \"data.yaml\"), epochs=50, imgsz=640, batch=16)\n\n# Step 4: Run Inference on Test Images\npredictions = model.predict(source=test_images, save=True, conf=0.25)\n\n# Step 5: Convert Predictions to Submission Format\nsubmission_data = []\nfor pred in predictions:\n    image_id = pred.path.name\n    for det in pred.boxes:\n        # YOLO outputs boxes as [x1, y1, x2, y2, confidence, class_id]\n        x1, y1, x2, y2, conf, class_id = det.xyxy[0].numpy()\n        xcenter = (x1 + x2) / 2\n        ycenter = (y1 + y2) / 2\n        bbx_width = x2 - x1\n        bbx_height = y2 - y1\n        label_name = model.names[int(class_id)]\n        submission_data.append({\n            \"id\": len(submission_data),\n            \"ImageID\": image_id,\n            \"LabelName\": label_name,\n            \"Conf\": round(conf, 2),\n            \"xcenter\": round(xcenter, 4),\n            \"ycenter\": round(ycenter, 4),\n            \"bbx_width\": round(bbx_width, 4),\n            \"bbx_height\": round(bbx_height, 4),\n        })\n\n# Step 6: Save Predictions to CSV\nsubmission_df = pd.DataFrame(submission_data)\nsubmission_df.to_csv(output_dir / \"submission.csv\", index=False)\n\nprint(f\"Submission file saved at: {output_dir / 'submission.csv'}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}