{"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":"# 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-03T04:55:06.965550Z","iopub.execute_input":"2024-12-03T04:55:06.966170Z","iopub.status.idle":"2024-12-03T04:55:06.971116Z","shell.execute_reply.started":"2024-12-03T04:55:06.966135Z","shell.execute_reply":"2024-12-03T04:55:06.970225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install ultralytics pandas opencv-python torch torchvision matplotlib","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\nimport numpy as np\nimport pandas as pd\nimport torch\nfrom torchvision import transforms\nfrom ultralytics import YOLO\nfrom sklearn.model_selection import train_test_split\nimport cv2\nimport shutil","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T05:02:21.706280Z","iopub.execute_input":"2024-12-03T05:02:21.707349Z","iopub.status.idle":"2024-12-03T05:02:21.713333Z","shell.execute_reply.started":"2024-12-03T05:02:21.707285Z","shell.execute_reply":"2024-12-03T05:02:21.712072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"SEED = 42\nrandom.seed(SEED)\nnp.random.seed(SEED)\ntorch.manual_seed(SEED)\nif torch.cuda.is_available():\n    torch.cuda.manual_seed(SEED)\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {DEVICE}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T04:55:15.390091Z","iopub.execute_input":"2024-12-03T04:55:15.390349Z","iopub.status.idle":"2024-12-03T04:55:15.401824Z","shell.execute_reply.started":"2024-12-03T04:55:15.390323Z","shell.execute_reply":"2024-12-03T04:55:15.400876Z"}},"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\"\nSUBMISSION_TEMPLATE = \"/kaggle/input/dlp-object-detection/sample_submission.csv\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T04:57:35.268910Z","iopub.execute_input":"2024-12-03T04:57:35.269237Z","iopub.status.idle":"2024-12-03T04:57:35.273434Z","shell.execute_reply.started":"2024-12-03T04:57:35.269211Z","shell.execute_reply":"2024-12-03T04:57:35.272578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_files = os.listdir(TRAIN_IMAGES_DIR)\ntrain_labels = [f.replace('.jpg', '.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-03T04:57:37.442932Z","iopub.execute_input":"2024-12-03T04:57:37.443379Z","iopub.status.idle":"2024-12-03T04:57:37.677524Z","shell.execute_reply.started":"2024-12-03T04:57:37.443340Z","shell.execute_reply":"2024-12-03T04:57:37.676658Z"}},"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-03T04:58:57.584961Z","iopub.execute_input":"2024-12-03T04:58:57.585739Z","iopub.status.idle":"2024-12-03T04:58:57.591227Z","shell.execute_reply.started":"2024-12-03T04:58:57.585701Z","shell.execute_reply":"2024-12-03T04:58:57.590251Z"}},"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-03T05:03:17.470586Z","iopub.execute_input":"2024-12-03T05:03:17.470955Z","iopub.status.idle":"2024-12-03T05:06:14.095105Z","shell.execute_reply.started":"2024-12-03T05:03:17.470927Z","shell.execute_reply":"2024-12-03T05:06:14.094303Z"}},"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-03T05:11:59.859285Z","iopub.execute_input":"2024-12-03T05:11:59.860106Z","iopub.status.idle":"2024-12-03T05:11:59.865073Z","shell.execute_reply.started":"2024-12-03T05:11:59.860070Z","shell.execute_reply":"2024-12-03T05:11:59.864414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = YOLO(\"yolov5s.pt\")\nmodel.train(data=\"dataset.yaml\", epochs=50, batch=16, imgsz=640, device=DEVICE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T05:12:06.712434Z","iopub.execute_input":"2024-12-03T05:12:06.713093Z","iopub.status.idle":"2024-12-03T07:59:27.118029Z","shell.execute_reply.started":"2024-12-03T05:12:06.713058Z","shell.execute_reply":"2024-12-03T07:59:27.116705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls runs/detect/train2/weights","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T08:04:59.836365Z","iopub.execute_input":"2024-12-03T08:04:59.837246Z","iopub.status.idle":"2024-12-03T08:05:01.114932Z","shell.execute_reply.started":"2024-12-03T08:04:59.837207Z","shell.execute_reply":"2024-12-03T08:05:01.113949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model = YOLO(\"runs/detect/train2/weights/best.pt\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T08:02:02.219222Z","iopub.execute_input":"2024-12-03T08:02:02.219590Z","iopub.status.idle":"2024-12-03T08:02:02.308811Z","shell.execute_reply.started":"2024-12-03T08:02:02.219559Z","shell.execute_reply":"2024-12-03T08:02:02.307851Z"}},"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-03T08:08:05.707055Z","iopub.execute_input":"2024-12-03T08:08:05.707887Z","iopub.status.idle":"2024-12-03T08:08:39.812858Z","shell.execute_reply.started":"2024-12-03T08:08:05.707846Z","shell.execute_reply":"2024-12-03T08:08:39.811907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\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-03T08:42:25.179551Z","iopub.execute_input":"2024-12-03T08:42:25.179944Z","iopub.status.idle":"2024-12-03T08:42:25.384553Z","shell.execute_reply.started":"2024-12-03T08:42:25.179911Z","shell.execute_reply":"2024-12-03T08:42:25.383737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame(submission_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T08:45:16.565105Z","iopub.execute_input":"2024-12-03T08:45:16.565479Z","iopub.status.idle":"2024-12-03T08:45:16.571756Z","shell.execute_reply.started":"2024-12-03T08:45:16.565445Z","shell.execute_reply":"2024-12-03T08:45:16.570724Z"}},"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-03T08:45:33.353830Z","iopub.execute_input":"2024-12-03T08:45:33.354227Z","iopub.status.idle":"2024-12-03T08:45:33.361002Z","shell.execute_reply.started":"2024-12-03T08:45:33.354186Z","shell.execute_reply":"2024-12-03T08:45:33.359918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T08:45:37.929320Z","iopub.execute_input":"2024-12-03T08:45:37.929715Z","iopub.status.idle":"2024-12-03T08:45:37.938930Z","shell.execute_reply.started":"2024-12-03T08:45:37.929680Z","shell.execute_reply":"2024-12-03T08:45:37.937959Z"}},"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-03T08:45:50.804320Z","iopub.execute_input":"2024-12-03T08:45:50.804745Z","iopub.status.idle":"2024-12-03T08:45:50.819244Z","shell.execute_reply.started":"2024-12-03T08:45:50.804708Z","shell.execute_reply":"2024-12-03T08:45:50.818490Z"}},"outputs":[],"execution_count":null}]}