{"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":"!pip install ultralytics -q","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:45.118508Z","iopub.execute_input":"2024-12-05T18:56:45.118843Z","iopub.status.idle":"2024-12-05T18:56:55.465564Z","shell.execute_reply.started":"2024-12-05T18:56:45.118799Z","shell.execute_reply":"2024-12-05T18:56:55.464612Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"# dataset\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\ntest_images_dir = \"/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images\"\n\nsubmission = \"/kaggle/input/dlp-object-detection/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-12-05T18:56:55.467329Z","iopub.execute_input":"2024-12-05T18:56:55.467636Z","iopub.status.idle":"2024-12-05T18:56:55.472232Z","shell.execute_reply.started":"2024-12-05T18:56:55.467610Z","shell.execute_reply":"2024-12-05T18:56:55.471427Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Map class labels to class names\nclass_names = {\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-05T18:56:55.473224Z","iopub.execute_input":"2024-12-05T18:56:55.473547Z","iopub.status.idle":"2024-12-05T18:56:55.482277Z","shell.execute_reply.started":"2024-12-05T18:56:55.473522Z","shell.execute_reply":"2024-12-05T18:56:55.481403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport matplotlib.pyplot as plt\nfrom collections import Counter\nimport pandas as pd\n\nimport shutil\nfrom sklearn.model_selection import train_test_split\n\n\nseed =42","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:55.484577Z","iopub.execute_input":"2024-12-05T18:56:55.484889Z","iopub.status.idle":"2024-12-05T18:56:56.490543Z","shell.execute_reply.started":"2024-12-05T18:56:55.484854Z","shell.execute_reply":"2024-12-05T18:56:56.489889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Function to load label data\ndef load_labels(label_path):\n    with open(label_path, \"r\") as file:\n        lines = file.readlines()\n    annotations = []\n    for line in lines:\n        class_label, x_center, y_center, width, height = map(float, line.strip().split())\n        annotations.append({\n            \"class_label\": int(class_label),\n            \"x_center\": x_center,\n            \"y_center\": y_center,\n            \"width\": width,\n            \"height\": height\n        })\n    return annotations\n\n# Visualize an image with bounding boxes\ndef visualize_image_with_boxes(image_path, label_path):\n    # Load image\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    h, w, _ = image.shape\n    \n    # Load labels\n    annotations = load_labels(label_path)\n    \n    # Draw bounding boxes\n    for annotation in annotations:\n        x_center, y_center, box_width, box_height = annotation[\"x_center\"], annotation[\"y_center\"], annotation[\"width\"], annotation[\"height\"]\n        class_label = annotation[\"class_label\"]\n        print(class_label)\n        \n        # Convert normalized bbox to absolute values\n        x_min = int((x_center - box_width / 2) * w)\n        x_max = int((x_center + box_width / 2) * w)\n        y_min = int((y_center - box_height / 2) * h)\n        y_max = int((y_center + box_height / 2) * h)\n        \n        # Draw rectangle and label\n        color = (255, 0, 0)\n        cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 2)\n        cv2.putText(image, str(class_label), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)\n    \n    # Plot the image\n    plt.figure(figsize=(10, 10))\n    plt.imshow(image)\n    plt.axis(\"off\")\n    plt.show()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:56.491545Z","iopub.execute_input":"2024-12-05T18:56:56.491975Z","iopub.status.idle":"2024-12-05T18:56:56.500339Z","shell.execute_reply.started":"2024-12-05T18:56:56.491938Z","shell.execute_reply":"2024-12-05T18:56:56.499588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# List files\ntrain_image_files = sorted(os.listdir(train_images_dir))\ntrain_label_files = sorted(os.listdir(train_labels_dir))  \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:56.501614Z","iopub.execute_input":"2024-12-05T18:56:56.501967Z","iopub.status.idle":"2024-12-05T18:56:56.971275Z","shell.execute_reply.started":"2024-12-05T18:56:56.501931Z","shell.execute_reply":"2024-12-05T18:56:56.970621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Test with one sample image and its label\nsample_image_path = os.path.join(train_images_dir, train_image_files[0])\nsample_label_path = os.path.join(train_labels_dir, train_label_files[0])\n\n# print(f\"Visualizing: {sample_image_path} with {sample_label_path}\")\nvisualize_image_with_boxes(sample_image_path, sample_label_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:56.972448Z","iopub.execute_input":"2024-12-05T18:56:56.972780Z","iopub.status.idle":"2024-12-05T18:56:57.509748Z","shell.execute_reply.started":"2024-12-05T18:56:56.972742Z","shell.execute_reply":"2024-12-05T18:56:57.508907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Test with one sample image and its label\n# sample_image_path = os.path.join(train_images_dir, train_image_files[1000])\n# sample_label_path = os.path.join(train_labels_dir, train_label_files[1000])\n\n# # print(f\"Visualizing: {sample_image_path} with {sample_label_path}\")\n# visualize_image_with_boxes(sample_image_path, sample_label_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:57.510988Z","iopub.execute_input":"2024-12-05T18:56:57.511321Z","iopub.status.idle":"2024-12-05T18:56:57.515797Z","shell.execute_reply.started":"2024-12-05T18:56:57.511285Z","shell.execute_reply":"2024-12-05T18:56:57.515013Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Analysis","metadata":{}},{"cell_type":"code","source":"# Function to count class occurrences\ndef analyze_class_distribution(label_dir):\n    class_counts = Counter()\n    label_files = os.listdir(label_dir)\n    \n    for label_file in label_files:\n        label_path = os.path.join(label_dir, label_file)\n        annotations = load_labels(label_path)\n        for annotation in annotations:\n            class_counts[annotation[\"class_label\"]] += 1\n    \n    return class_counts\n\n\n# Check for missing labels\ndef check_missing_labels(image_dir, label_dir):\n    image_files = set(f.split('.')[0] for f in os.listdir(image_dir))\n    label_files = set(f.split('.')[0] for f in os.listdir(label_dir))\n    \n    missing_labels = image_files - label_files\n    missing_images = label_files - image_files\n    \n    return missing_labels, missing_images","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:57.516721Z","iopub.execute_input":"2024-12-05T18:56:57.516963Z","iopub.status.idle":"2024-12-05T18:56:57.525871Z","shell.execute_reply.started":"2024-12-05T18:56:57.516940Z","shell.execute_reply":"2024-12-05T18:56:57.524931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Analyze class distribution in training labels\nclass_counts = analyze_class_distribution(train_labels_dir)\n\n# Display class distribution\nclass_distribution = pd.DataFrame({\n    \"Class Label\": [class_names[i] for i in class_counts.keys()],\n    \"Count\": list(class_counts.values())\n}).sort_values(by=\"Count\", ascending=False)\n\nprint(\"Class Distribution:\")\nprint(class_distribution)\n\n# Plot class distribution\nplt.figure(figsize=(10, 6))\nplt.bar(class_distribution[\"Class Label\"], class_distribution[\"Count\"], color='skyblue')\nplt.xlabel(\"Class\")\nplt.ylabel(\"Count\")\nplt.title(\"Class Distribution in Training Data\")\nplt.xticks(rotation=45)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:56:57.528697Z","iopub.execute_input":"2024-12-05T18:56:57.528950Z","iopub.status.idle":"2024-12-05T18:57:29.364032Z","shell.execute_reply.started":"2024-12-05T18:56:57.528926Z","shell.execute_reply":"2024-12-05T18:57:29.363258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_labels, missing_images = check_missing_labels(train_images_dir, train_labels_dir)\n\nif missing_labels:\n    print(f\"Images with missing labels: {missing_labels}\")\nelse:\n    print(\"No missing labels found.\")\n\nif missing_images:\n    print(f\"Labels with missing images: {missing_images}\")\nelse:\n    print(\"No missing images found.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:57:29.365177Z","iopub.execute_input":"2024-12-05T18:57:29.365527Z","iopub.status.idle":"2024-12-05T18:57:29.382838Z","shell.execute_reply.started":"2024-12-05T18:57:29.365500Z","shell.execute_reply":"2024-12-05T18:57:29.382146Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training data set for YOLO","metadata":{}},{"cell_type":"code","source":"train_files = os.listdir(train_images_dir)\ntrain_labels = [f.replace('.jpeg', '.txt') for f in train_files]\ntrain_files, val_files = train_test_split(train_files, test_size=0.2, random_state=seed)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:57:29.383756Z","iopub.execute_input":"2024-12-05T18:57:29.384070Z","iopub.status.idle":"2024-12-05T18:57:29.393973Z","shell.execute_reply.started":"2024-12-05T18:57:29.384034Z","shell.execute_reply":"2024-12-05T18:57:29.393329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.makedirs(\"yolo_dataset/train/images\", exist_ok=True)\nos.makedirs(\"yolo_dataset/train/labels\", exist_ok=True)\nos.makedirs(\"yolo_dataset/val/images\", exist_ok=True)\nos.makedirs(\"yolo_dataset/val/labels\", exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:57:29.394965Z","iopub.execute_input":"2024-12-05T18:57:29.395279Z","iopub.status.idle":"2024-12-05T18:57:29.400158Z","shell.execute_reply.started":"2024-12-05T18:57:29.395245Z","shell.execute_reply":"2024-12-05T18:57:29.399444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for f in train_files:\n    shutil.copy(os.path.join(train_images_dir, f), f\"yolo_dataset/train/images/{f}\")\n    shutil.copy(os.path.join(train_labels_dir, f.replace('.jpeg', '.txt')), f\"yolo_dataset/train/labels/{f.replace('.jpeg', '.txt')}\")\n\nfor f in val_files:\n    shutil.copy(os.path.join(train_images_dir, f), f\"yolo_dataset/val/images/{f}\")\n    shutil.copy(os.path.join(train_labels_dir, f.replace('.jpeg', '.txt')), f\"yolo_dataset/val/labels/{f.replace('.jpeg', '.txt')}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T18:58:51.249295Z","iopub.execute_input":"2024-12-05T18:58:51.249675Z","iopub.status.idle":"2024-12-05T19:01:55.159355Z","shell.execute_reply.started":"2024-12-05T18:58:51.249645Z","shell.execute_reply":"2024-12-05T19:01:55.158259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"yaml_content = f\"\"\"\npath: yolo_dataset\ntrain: /kaggle/working/yolo_dataset/train/images\nval: /kaggle/working/yolo_dataset/val/images\ntest: {test_images_dir}\nnames:\n  0: aegypti\n  1: albopictus\n  2: anopheles\n  3: culex\n  4: culiseta\n  5: japonicus/koreicus\n\"\"\"\n\nwith open(\"dataset.yaml\", \"w\") as f:\n    f.write(yaml_content)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:02:10.959257Z","iopub.execute_input":"2024-12-05T19:02:10.959613Z","iopub.status.idle":"2024-12-05T19:02:10.964371Z","shell.execute_reply.started":"2024-12-05T19:02:10.959582Z","shell.execute_reply":"2024-12-05T19:02:10.963573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n# Load a COCO-pretrained YOLO11n model\nmodel = YOLO(\"yolo11n.pt\")\n# Step 4: Train the Model\nmodel.train(data=\"dataset.yaml\", epochs=2, batch=30, imgsz=640, device='cuda')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:08:23.958111Z","iopub.execute_input":"2024-12-05T19:08:23.958918Z","iopub.status.idle":"2024-12-05T19:14:23.092883Z","shell.execute_reply.started":"2024-12-05T19:08:23.958881Z","shell.execute_reply":"2024-12-05T19:14:23.091891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(test_images_dir, save_txt = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:14:34.278250Z","iopub.execute_input":"2024-12-05T19:14:34.279206Z","iopub.status.idle":"2024-12-05T19:18:38.713418Z","shell.execute_reply.started":"2024-12-05T19:14:34.279169Z","shell.execute_reply":"2024-12-05T19:18:38.712674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nsubmission_data = []\n\n# Loop through each prediction\nfor i, result in enumerate(predictions):\n    # Iterate over each detected object in the image\n    for box in result.boxes:\n        image_id = result.path.split(\"/\")[-1]  # Get the image file name\n        label_name = result.names[int(box.cls)]  # Class name\n        conf = float(box.conf)  # Confidence score\n        x_center, y_center, width, height = box.xywh[0].tolist()  # Bounding box details\n        \n        # Append to the submission data list\n        submission_data.append({\n            \"id\": i,\n            \"ImageID\": image_id,\n            \"LabelName\": label_name,\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-05T19:18:38.714747Z","iopub.execute_input":"2024-12-05T19:18:38.715011Z","iopub.status.idle":"2024-12-05T19:18:38.896737Z","shell.execute_reply.started":"2024-12-05T19:18:38.714984Z","shell.execute_reply":"2024-12-05T19:18:38.896119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame(submission_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:18:38.897732Z","iopub.execute_input":"2024-12-05T19:18:38.898045Z","iopub.status.idle":"2024-12-05T19:18:38.904426Z","shell.execute_reply.started":"2024-12-05T19:18:38.898019Z","shell.execute_reply":"2024-12-05T19:18:38.903591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# submission_df = submission_df.drop_duplicates()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:21:48.798086Z","iopub.execute_input":"2024-12-05T19:21:48.798982Z","iopub.status.idle":"2024-12-05T19:21:48.805140Z","shell.execute_reply.started":"2024-12-05T19:21:48.798945Z","shell.execute_reply":"2024-12-05T19:21:48.804435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = submission_df[[\n    \"id\", \"ImageID\", \"LabelName\", \"Conf\", \"xcenter\", \"ycenter\", \"bbx_width\", \"bbx_height\"\n]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:18:38.906302Z","iopub.execute_input":"2024-12-05T19:18:38.907160Z","iopub.status.idle":"2024-12-05T19:18:38.914085Z","shell.execute_reply.started":"2024-12-05T19:18:38.907132Z","shell.execute_reply":"2024-12-05T19:18:38.913144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:23:03.599274Z","iopub.execute_input":"2024-12-05T19:23:03.600115Z","iopub.status.idle":"2024-12-05T19:23:03.607075Z","shell.execute_reply.started":"2024-12-05T19:23:03.600067Z","shell.execute_reply":"2024-12-05T19:23:03.606035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# submission_df = submission_df.drop_duplicates(subset=['id'], keep='first')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:23:50.919291Z","iopub.execute_input":"2024-12-05T19:23:50.920128Z","iopub.status.idle":"2024-12-05T19:23:50.925397Z","shell.execute_reply.started":"2024-12-05T19:23:50.920094Z","shell.execute_reply":"2024-12-05T19:23:50.924510Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:24:04.678184Z","iopub.execute_input":"2024-12-05T19:24:04.678592Z","iopub.status.idle":"2024-12-05T19:24:04.684311Z","shell.execute_reply.started":"2024-12-05T19:24:04.678557Z","shell.execute_reply":"2024-12-05T19:24:04.683459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-05T19:21:53.238121Z","iopub.execute_input":"2024-12-05T19:21:53.238850Z","iopub.status.idle":"2024-12-05T19:21:53.249664Z","shell.execute_reply.started":"2024-12-05T19:21:53.238818Z","shell.execute_reply":"2024-12-05T19:21:53.248810Z"}},"outputs":[],"execution_count":null}]}