{"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":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:01:03.802718Z","iopub.execute_input":"2024-12-06T12:01:03.803802Z","iopub.status.idle":"2024-12-06T12:01:14.23262Z","shell.execute_reply.started":"2024-12-06T12:01:03.803747Z","shell.execute_reply":"2024-12-06T12:01:14.231727Z"}},"outputs":[],"execution_count":null},{"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    pass\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-06T11:09:21.310739Z","iopub.execute_input":"2024-12-06T11:09:21.311964Z","iopub.status.idle":"2024-12-06T11:09:25.201124Z","shell.execute_reply.started":"2024-12-06T11:09:21.311875Z","shell.execute_reply":"2024-12-06T11:09:25.199984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport pandas as pd\nimport os\nfrom ultralytics import YOLO","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:01:14.234812Z","iopub.execute_input":"2024-12-06T12:01:14.235505Z","iopub.status.idle":"2024-12-06T12:01:17.686005Z","shell.execute_reply.started":"2024-12-06T12:01:14.23546Z","shell.execute_reply":"2024-12-06T12:01:17.684999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls '/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images' | head -5","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:01:20.638832Z","iopub.execute_input":"2024-12-06T12:01:20.63936Z","iopub.status.idle":"2024-12-06T12:01:21.714988Z","shell.execute_reply.started":"2024-12-06T12:01:20.639327Z","shell.execute_reply":"2024-12-06T12:01:21.713811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images_dir = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/images/\"\ntrain_labels_dir = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/labels/\"\ntest_images_dir = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images/\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:01:24.615646Z","iopub.execute_input":"2024-12-06T12:01:24.616041Z","iopub.status.idle":"2024-12-06T12:01:24.621025Z","shell.execute_reply.started":"2024-12-06T12:01:24.616006Z","shell.execute_reply":"2024-12-06T12:01:24.619917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport matplotlib.patches as patches\n\n# Local paths\ntrain_images_dir = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/images/\"\ntrain_labels_dir = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/labels/\"\n\n# Function to visualize a random image with bounding boxes\ndef visualize_random_image_with_boxes(images_dir, labels_dir):\n    # Get all image files\n    image_files = os.listdir(images_dir)\n    if not image_files:\n        print(\"No images found in the directory!\")\n        return\n\n    # Select a random image\n    random_image_file = random.choice(image_files)\n    image_path = os.path.join(images_dir, random_image_file)\n    label_path = os.path.join(labels_dir, random_image_file.replace('.jpeg', '.txt'))\n\n    # Load the image\n    try:\n        image = Image.open(image_path)\n        width, height = image.size\n    except Exception as e:\n        print(f\"Error loading image {random_image_file}: {e}\")\n        return\n\n    # Create a plot\n    fig, ax = plt.subplots(1, figsize=(10, 10))\n    ax.imshow(image)\n\n    # Load and parse the label file\n    if os.path.exists(label_path):\n        try:\n            with open(label_path, 'r') as f:\n                for line in f:\n                    class_id, x_center, y_center, box_width, box_height = map(float, line.strip().split())\n                    \n                    # Convert normalized coordinates to absolute\n                    x_center *= width\n                    y_center *= height\n                    box_width *= width\n                    box_height *= height\n                    \n                    # Calculate top-left corner of the box\n                    x_min = x_center - (box_width / 2)\n                    y_min = y_center - (box_height / 2)\n                    \n                    # Draw bounding box\n                    rect = patches.Rectangle((x_min, y_min), box_width, box_height,\n                                              linewidth=2, edgecolor='red', facecolor='none')\n                    ax.add_patch(rect)\n                    ax.text(x_min, y_min - 10, f\"Class: {int(class_id)}\", color='red', fontsize=12,\n                            bbox=dict(facecolor='white', alpha=0.5))\n        except Exception as e:\n            print(f\"Error reading label file {label_path}: {e}\")\n    else:\n        print(f\"Label file not found for image: {random_image_file}\")\n\n    plt.axis('off')\n    plt.show()\n\n# Run the function\nvisualize_random_image_with_boxes(train_images_dir, train_labels_dir)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:01:32.858397Z","iopub.execute_input":"2024-12-06T12:01:32.858741Z","iopub.status.idle":"2024-12-06T12:01:34.553943Z","shell.execute_reply.started":"2024-12-06T12:01:32.858709Z","shell.execute_reply":"2024-12-06T12:01:34.553048Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## create yml files","metadata":{}},{"cell_type":"code","source":"import yaml\n\n\n# Define the dataset configuration\ndata = {\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/\",  # Using train images as validation\n    \"nc\": 6,\n    \"names\": ['aegypti', 'albopictus', 'anopheles', 'culex', 'culiseta', 'japonicus/koreicus']\n}\n\n# Save to data.yaml\nwith open(\"data.yaml\", \"w\") as file:\n    yaml.dump(data, file, default_flow_style=False)\n\nprint(\"YAML file created as data.yaml\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:01:43.365017Z","iopub.execute_input":"2024-12-06T12:01:43.365829Z","iopub.status.idle":"2024-12-06T12:01:43.372856Z","shell.execute_reply.started":"2024-12-06T12:01:43.365795Z","shell.execute_reply":"2024-12-06T12:01:43.371857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load YOLOv5 model pre-trained on COCO\nmodel = YOLO(\"yolov5su.pt\")  # Use yolov5n.pt or yolov5m.pt for smaller or larger models\n\n# Train the model\nmodel.train(\n    data=\"data.yaml\",  # Path to the data configuration file\n    epochs=15,                  # Number of epochs\n    imgsz=640,                  # Image size\n    batch=16,                   # Batch size\n    name=\"mosquito_detection_no_val\"  # Experiment name\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T12:01:48.485296Z","iopub.execute_input":"2024-12-06T12:01:48.486096Z","iopub.status.idle":"2024-12-06T13:01:35.659906Z","shell.execute_reply.started":"2024-12-06T12:01:48.486065Z","shell.execute_reply":"2024-12-06T13:01:35.658627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!yolo train resume=runs/detect/mosquito_detection_no_val3/weights/last.pt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T14:21:36.350668Z","iopub.execute_input":"2024-12-06T14:21:36.351029Z","iopub.status.idle":"2024-12-06T14:21:41.584456Z","shell.execute_reply.started":"2024-12-06T14:21:36.350995Z","shell.execute_reply":"2024-12-06T14:21:41.583276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load the model and resume training\nmodel = YOLO(\"runs/detect/train/weights/last.pt\")\nmodel.train(\n    data=\"data.yaml\",  # Path to your dataset config\n    epochs=5,                 # Specify total epochs if needed\n    imgsz=640,                 # Image size\n    batch=16                   # Batch size\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T15:46:57.095141Z","iopub.execute_input":"2024-12-06T15:46:57.095829Z","iopub.status.idle":"2024-12-06T16:22:18.938171Z","shell.execute_reply.started":"2024-12-06T15:46:57.095795Z","shell.execute_reply":"2024-12-06T16:22:18.937155Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import csv\n\n# Load the test dataset and model\ntest_images_dir = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images/\"\n# model = YOLO(\"runs/detect/mosquito_detection_no_val3/weights/best.pt\")  # Path to trained weights\n# model = YOLO(\"runs/detect/train/weights/best.pt\")  # Path to trained weights\n# model = YOLO(\"runs/detect/train2/weights/best.pt\")  # Path to trained weights\nmodel = YOLO(\"runs/detect/train3/weights/best.pt\")  # Path to trained weights\n\n# # Prepare submission file\n# submission_file = \"submission.csv\"\n# submission_file = \"train_submission.csv\"\n# submission_file = \"train2_submission.csv\"\nsubmission_file = \"train3_submission.csv\"\nheader = [\"id\", \"ImageID\", \"LabelName\", \"Conf\", \"xcenter\", \"ycenter\", \"bbx_width\", \"bbx_height\"]\n\nwith open(submission_file, \"w\", newline=\"\") as f:\n    writer = csv.writer(f)\n    writer.writerow(header)\n\n    image_files = os.listdir(test_images_dir)\n    for i, image_file in enumerate(image_files):\n        image_path = os.path.join(test_images_dir, image_file)\n        results = model(image_path)\n\n        # Select the object with the highest confidence for each image\n        highest_conf_result = None\n        highest_conf = 0.0\n\n        for result in results:\n            boxes = result.boxes.xywhn  # Normalized x_center, y_center, width, height\n            confs = result.boxes.conf  # Confidence scores\n            labels = result.boxes.cls  # Class IDs\n\n            for box, conf, label in zip(boxes, confs, labels):\n                if conf > highest_conf:\n                    highest_conf = conf\n                    highest_conf_result = {\n                        \"ImageID\": image_file,\n                        \"LabelName\": model.names[int(label)],  # Class name\n                        \"Conf\": float(conf),\n                        \"xcenter\": float(box[0]),  # x_center\n                        \"ycenter\": float(box[1]),  # y_center\n                        \"bbx_width\": float(box[2]),  # width\n                        \"bbx_height\": float(box[3])  # height\n                    }\n\n        # Write the result for the image if a valid detection is found\n        if highest_conf_result:\n            writer.writerow([\n                i,\n                highest_conf_result[\"ImageID\"],\n                highest_conf_result[\"LabelName\"],\n                highest_conf_result[\"Conf\"],\n                highest_conf_result[\"xcenter\"],\n                highest_conf_result[\"ycenter\"],\n                highest_conf_result[\"bbx_width\"],\n                highest_conf_result[\"bbx_height\"]\n            ])\n        else:\n            # If no object is detected, write a dummy row with 0 confidence\n            writer.writerow([\n                i,\n                image_file,\n                \"albopictus\",  # No label\n                0.5,  # Confidence\n                0.1,  # x_center\n                0.2,  # y_center\n                0.3,  # width\n                0.4   # height\n            ])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T16:22:55.930529Z","iopub.execute_input":"2024-12-06T16:22:55.931478Z","iopub.status.idle":"2024-12-06T16:23:25.97244Z","shell.execute_reply.started":"2024-12-06T16:22:55.93144Z","shell.execute_reply":"2024-12-06T16:23:25.971793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir(test_images_dir))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T13:41:39.038273Z","iopub.execute_input":"2024-12-06T13:41:39.039077Z","iopub.status.idle":"2024-12-06T13:41:39.04925Z","shell.execute_reply.started":"2024-12-06T13:41:39.039047Z","shell.execute_reply":"2024-12-06T13:41:39.048155Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ====================================","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T11:20:49.836638Z","iopub.execute_input":"2024-12-06T11:20:49.837123Z","iopub.status.idle":"2024-12-06T11:20:49.841975Z","shell.execute_reply.started":"2024-12-06T11:20:49.837079Z","shell.execute_reply":"2024-12-06T11:20:49.840885Z"}},"outputs":[],"execution_count":null}]}