{"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":30804,"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\n# for 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-06T08:24:56.200029Z","iopub.execute_input":"2024-12-06T08:24:56.200257Z","iopub.status.idle":"2024-12-06T08:24:57.254115Z","shell.execute_reply.started":"2024-12-06T08:24:56.200232Z","shell.execute_reply":"2024-12-06T08:24:57.253229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Install YOLOv8\n!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T08:26:17.325705Z","iopub.execute_input":"2024-12-06T08:26:17.326391Z","iopub.status.idle":"2024-12-06T08:26:27.365705Z","shell.execute_reply.started":"2024-12-06T08:26:17.326355Z","shell.execute_reply":"2024-12-06T08:26:27.364549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\n\n# Set device to GPU if available\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(f\"Using device: {device}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T08:26:34.878306Z","iopub.execute_input":"2024-12-06T08:26:34.879180Z","iopub.status.idle":"2024-12-06T08:26:37.708980Z","shell.execute_reply.started":"2024-12-06T08:26:34.879141Z","shell.execute_reply":"2024-12-06T08:26:37.708063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\n\n# Paths to image and label directories\nimage_dir = '/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/images'\nlabel_dir = '/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/labels'\n\n# Get list of image files (make sure they're .jpg or .png)\nimage_files = [f for f in os.listdir(image_dir) if f.endswith(('.jpeg', '.png'))]\n\n\n# Shuffle and split dataset into train (90%) and validation (10%)\nrandom.seed(42)\nrandom.shuffle(image_files)\ntrain_size = int(0.9 * len(image_files))\ntrain_dataset = image_files[:train_size]\nval_dataset = image_files[train_size:]\n\n# Print dataset sizes\nprint(f\"Total dataset size: {len(image_files)}\")\nprint(f\"Training set size: {len(train_dataset)}\")\nprint(f\"Validation set size: {len(val_dataset)}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T08:26:40.809536Z","iopub.execute_input":"2024-12-06T08:26:40.810013Z","iopub.status.idle":"2024-12-06T08:26:40.942516Z","shell.execute_reply.started":"2024-12-06T08:26:40.809984Z","shell.execute_reply":"2024-12-06T08:26:40.941743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images_path = \"dataset/train/images\"\ntrain_labels_path = \"dataset/train/labels\"\nval_images_path = \"dataset/val/images\"\nval_labels_path = \"dataset/val/labels\"\n\nos.makedirs(train_images_path,exist_ok=True)\nos.makedirs(train_labels_path,exist_ok=True)\nos.makedirs(val_images_path,exist_ok=True)\nos.makedirs(val_labels_path,exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T08:26:45.797037Z","iopub.execute_input":"2024-12-06T08:26:45.797375Z","iopub.status.idle":"2024-12-06T08:26:45.803010Z","shell.execute_reply.started":"2024-12-06T08:26:45.797341Z","shell.execute_reply":"2024-12-06T08:26:45.802098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\ndef copy_files(image_list, src_image_dir, src_label_dir, dest_image_dir, dest_label_dir):\n    for image_name in image_list:\n        # Copy image\n        src_image_path = os.path.join(src_image_dir, image_name)\n        dest_image_path = os.path.join(dest_image_dir, image_name)\n        shutil.copy2(src_image_path, dest_image_path)\n\n        # Copy corresponding label\n        label_name = image_name.replace('.jpeg', '.txt')  # Assuming label files match image names\n        src_label_path = os.path.join(src_label_dir, label_name)\n        dest_label_path = os.path.join(dest_label_dir, label_name)\n        \n        shutil.copy2(src_label_path, dest_label_path)\n\n# Copy train files\ncopy_files(train_dataset, image_dir, label_dir, train_images_path, train_labels_path)\n\n# Copy validation files\ncopy_files(val_dataset, image_dir, label_dir, val_images_path, val_labels_path)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T08:26:48.694302Z","iopub.execute_input":"2024-12-06T08:26:48.695008Z","iopub.status.idle":"2024-12-06T08:29:18.149607Z","shell.execute_reply.started":"2024-12-06T08:26:48.694973Z","shell.execute_reply":"2024-12-06T08:29:18.148821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import yaml\n\n# Define the dataset configuration\ndata = {\n    'train': '/kaggle/working/dataset/train/images',  # Path to training images (relative to 'path')\n    'val': '/kaggle/working/dataset/val/images',      # Path to validation images (relative to 'path')\n    'nc': 6,                  # Number of classes\n    'names': [\"aegypti\",\"albopictus\",\"anopheles\",\"culex\",\"culiseta\",\"japonicus/koreicus\"]   # Class names\n}\n\n# Save to a YAML file\nwith open('/kaggle/working/dataset.yaml', 'w') as file:\n    yaml.dump(data, file, default_flow_style=False)\n\nprint(\"YAML file created at /kaggle/working/dataset.yaml\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T08:30:18.835952Z","iopub.execute_input":"2024-12-06T08:30:18.836665Z","iopub.status.idle":"2024-12-06T08:30:18.859857Z","shell.execute_reply.started":"2024-12-06T08:30:18.836628Z","shell.execute_reply":"2024-12-06T08:30:18.859131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel = YOLO(\"yolov8n.pt\")\nresults = model.train(data=\"dataset.yaml\",\n                     epochs=15,\n                     imgsz=800,\n                     device=0,\n                     batch=32,\n                     verbose=True,\n                    \n                     )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T08:30:32.410901Z","iopub.execute_input":"2024-12-06T08:30:32.411615Z","iopub.status.idle":"2024-12-06T09:04:31.906186Z","shell.execute_reply.started":"2024-12-06T08:30:32.411580Z","shell.execute_reply":"2024-12-06T09:04:31.905122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_results = model.val()\nprint(val_results.box.map)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:07:49.171926Z","iopub.execute_input":"2024-12-06T09:07:49.172337Z","iopub.status.idle":"2024-12-06T09:08:12.574506Z","shell.execute_reply.started":"2024-12-06T09:07:49.172284Z","shell.execute_reply":"2024-12-06T09:08:12.573576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(val_results.box.map50)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:08:58.995753Z","iopub.execute_input":"2024-12-06T09:08:58.996158Z","iopub.status.idle":"2024-12-06T09:08:59.002125Z","shell.execute_reply.started":"2024-12-06T09:08:58.996112Z","shell.execute_reply":"2024-12-06T09:08:59.001213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_results = model.predict(\"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images\",\n                            save_txt=True,\n                            save_conf=True,\n                            conf=0.30,\n                            iou=0.50)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:09:36.106398Z","iopub.execute_input":"2024-12-06T09:09:36.106791Z","iopub.status.idle":"2024-12-06T09:13:07.581641Z","shell.execute_reply.started":"2024-12-06T09:09:36.106762Z","shell.execute_reply":"2024-12-06T09:13:07.580826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_dir = \"/kaggle/working/runs/detect/train3/labels\"\n\nfiles_list = os.listdir(predictions_dir)\nprint(len(files_list))\nwith open(os.path.join(predictions_dir,files_list[49]), \"r\") as f:\n    for line_id,line in enumerate(f):\n        print(line)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:14:04.703752Z","iopub.execute_input":"2024-12-06T09:14:04.704517Z","iopub.status.idle":"2024-12-06T09:14:04.710462Z","shell.execute_reply.started":"2024-12-06T09:14:04.704457Z","shell.execute_reply":"2024-12-06T09:14:04.709593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = [\"aegypti\",\"albopictus\",\"anopheles\",\n               \"culex\",\"culiseta\",\"japonicus/koreicus\"]   # Class names\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:14:08.966603Z","iopub.execute_input":"2024-12-06T09:14:08.966927Z","iopub.status.idle":"2024-12-06T09:14:08.971074Z","shell.execute_reply.started":"2024-12-06T09:14:08.966901Z","shell.execute_reply":"2024-12-06T09:14:08.970064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import csv\n\n# Define the output CSV file path\noutput_csv = \"/kaggle/working/21F1000641.csv\"\ntest_path=\"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images\"\n\n# Initialize the CSV file with the header\nwith open(output_csv, mode=\"w\", newline=\"\") as file:\n    writer = csv.writer(file)\n    sno=0\n    # Write header\n    writer.writerow([\"id\", \"ImageID\", \"LabelName\", \"Conf\", \"xcenter\", \"ycenter\", \"bbx_width\", \"bbx_height\"])\n\n    # Loop through all prediction files in the labels directory\n    for img_file in os.listdir(test_path):\n        txt_file = img_file.replace(\".jpeg\",\".txt\")\n        \n        image_id = img_file\n        label_test_file_path = os.path.join(predictions_dir,txt_file)\n        if txt_file in os.listdir(predictions_dir):\n            \n            # Read the predictions from the .txt file\n            with open(label_test_file_path, \"r\") as f:\n                lines = f.readlines()\n                if lines:  # File is empty\n                    max_conf=0\n                    for line in lines:\n                        #YOLO format: class x_center y_center width height confidence\n                        line_parts = line.strip().split()\n                        c = float(line_parts[5])\n                        if c>=max_conf:\n                            label_name = int(line_parts[0])  # Class ID\n                            xcenter = float(line_parts[1])\n                            ycenter = float(line_parts[2])\n                            bbx_width = float(line_parts[3])\n                            bbx_height = float(line_parts[4])\n                            conf = float(line_parts[5])\n                            max_conf = conf\n        else:\n            label_name = 5  # Placeholder class ID for no predictions\n            xcenter = 0.5   # Dummy values for bounding box\n            ycenter = 0.5\n            bbx_width = 0.2\n            bbx_height = 0.2\n            conf = 0.5\n\n        # Write row to CSV\n        writer.writerow([\n            sno,\n            image_id,\n            class_names[label_name],\n            conf,\n            xcenter,\n            ycenter,\n            bbx_width,\n            bbx_height\n        ])\n        sno+=1\n\nprint(f\"Submission file saved to {output_csv}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:14:20.765857Z","iopub.execute_input":"2024-12-06T09:14:20.766219Z","iopub.status.idle":"2024-12-06T09:14:20.944564Z","shell.execute_reply.started":"2024-12-06T09:14:20.766185Z","shell.execute_reply":"2024-12-06T09:14:20.943726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(lines)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:14:30.492091Z","iopub.execute_input":"2024-12-06T09:14:30.492706Z","iopub.status.idle":"2024-12-06T09:14:30.497050Z","shell.execute_reply.started":"2024-12-06T09:14:30.492667Z","shell.execute_reply":"2024-12-06T09:14:30.496172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(pd.read_csv(\"/kaggle/working/21F1000641.csv\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T09:14:51.832856Z","iopub.execute_input":"2024-12-06T09:14:51.833214Z","iopub.status.idle":"2024-12-06T09:14:51.846342Z","shell.execute_reply.started":"2024-12-06T09:14:51.833181Z","shell.execute_reply":"2024-12-06T09:14:51.845557Z"}},"outputs":[],"execution_count":null}]}