{"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":"gpu","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},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:37:15.552618Z","iopub.execute_input":"2024-12-04T06:37:15.552950Z","iopub.status.idle":"2024-12-04T06:37:25.915338Z","shell.execute_reply.started":"2024-12-04T06:37:15.552914Z","shell.execute_reply":"2024-12-04T06:37:25.914473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!rm -r /kaggle/working/datasets","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T06:32:57.724695Z","iopub.execute_input":"2024-11-26T06:32:57.725084Z","iopub.status.idle":"2024-11-26T06:32:58.813981Z","shell.execute_reply.started":"2024-11-26T06:32:57.725045Z","shell.execute_reply":"2024-11-26T06:32:58.813005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir /kaggle/working/datasets\n!cp -r /kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data /kaggle/working/datasets","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:55:26.218051Z","iopub.execute_input":"2024-12-04T06:55:26.218406Z","iopub.status.idle":"2024-12-04T06:57:08.993278Z","shell.execute_reply.started":"2024-12-04T06:55:26.218375Z","shell.execute_reply":"2024-12-04T06:57:08.991954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir /kaggle/working/datasets/final_dlp_data/val\n!mkdir /kaggle/working/datasets/final_dlp_data/val/images\n!mkdir /kaggle/working/datasets/final_dlp_data/val/labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:59:36.048737Z","iopub.execute_input":"2024-12-04T06:59:36.049113Z","iopub.status.idle":"2024-12-04T06:59:39.019137Z","shell.execute_reply.started":"2024-12-04T06:59:36.049082Z","shell.execute_reply":"2024-12-04T06:59:39.018152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\ntrain_dir = '/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train/labels'\nlabel_dict = {}\nfor file in os.listdir(train_dir):\n    f = open(os.path.join(train_dir, file), 'r')\n    label = f.readline().split(' ')[0]\n    if label in label_dict.keys():\n        label_dict[label] += 1\n    else:\n        label_dict[label] = 1\nprint(label_dict)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:40:26.844168Z","iopub.execute_input":"2024-12-04T06:40:26.845048Z","iopub.status.idle":"2024-12-04T06:40:48.599020Z","shell.execute_reply.started":"2024-12-04T06:40:26.845011Z","shell.execute_reply":"2024-12-04T06:40:48.598309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Create a validation folder and move 10% of the images from training to val\nimport os\nimport random\nimport shutil\ncount = 0\ntrain_images_path = '/kaggle/working/datasets/final_dlp_data/train/images'\ntrain_labels_path = '/kaggle/working/datasets/final_dlp_data/train/labels'\nval_images_path = '/kaggle/working/datasets/final_dlp_data/val/images'\nval_labels_path = '/kaggle/working/datasets/final_dlp_data/val/labels'\ntrain_images = os.listdir(train_images_path)\nfor file in train_images:\n    rand_num = random.randint(1,10)\n    source_image_file = train_images_path + '/' + file\n    source_label_file = train_labels_path + '/' + file.split('.')[0] + '.txt'\n    dest_image_folder = val_images_path\n    dest_label_folder = val_labels_path\n    f = open(source_label_file, 'r')\n    label = f.readline().split(' ')[0]\n    if label in ['0', '2']:\n        shutil.copy(source_image_file, dest_image_folder)\n        shutil.copy(source_label_file, dest_label_folder)\n        count += 1\n    elif label in ['4','5']:\n        if rand_num <= 2:\n            shutil.move(source_image_file, dest_image_folder)\n            shutil.move(source_label_file, dest_label_folder)\n            count += 1\n    else:\n        if rand_num <=1:\n            shutil.move(source_image_file, dest_image_folder)\n            shutil.move(source_label_file, dest_label_folder)\n            count += 1\n    f.close()\nprint(count)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:59:44.578349Z","iopub.execute_input":"2024-12-04T06:59:44.578680Z","iopub.status.idle":"2024-12-04T06:59:44.963262Z","shell.execute_reply.started":"2024-12-04T06:59:44.578653Z","shell.execute_reply":"2024-12-04T06:59:44.962390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Data.yaml file\nf = open('/kaggle/working/data.yaml', 'w')\nf.write(\"path: /kaggle/working/datasets/final_dlp_data # dataset root dir\\n\")\nf.write(\"train: train/images # train images (relative to 'path') \\n\")\nf.write(\"val: val/images # val images (relative to 'path')\\n\")\nf.write(\"test: test/images # test images (optional)\\n\")\nf.write(\"names:\\n\")\nf.write(\"    0: aegypti\\n\")\nf.write(\"    1: albopictus\\n\")\nf.write(\"    2: anopheles\\n\")\nf.write(\"    3: culex\\n\")\nf.write(\"    4: culiseta\\n\")\nf.write(\"    5: japonicus/koreicus\\n\")\nf.close()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:59:51.629579Z","iopub.execute_input":"2024-12-04T06:59:51.629895Z","iopub.status.idle":"2024-12-04T06:59:51.635870Z","shell.execute_reply.started":"2024-12-04T06:59:51.629870Z","shell.execute_reply":"2024-12-04T06:59:51.634892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!rm -r /kaggle/working/runs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-26T04:58:46.050145Z","iopub.execute_input":"2024-11-26T04:58:46.050643Z","iopub.status.idle":"2024-11-26T04:58:47.514176Z","shell.execute_reply.started":"2024-11-26T04:58:46.050598Z","shell.execute_reply":"2024-11-26T04:58:47.512460Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load a model\n#model = YOLO(\"yolo11n.yaml\")  # build a new model from YAML\nmodel = YOLO(\"yolo11s.pt\")  # load a pretrained model (recommended for training)\n#model = YOLO(\"yolo11n.yaml\").load(\"yolo11n.pt\")  # build from YAML and transfer weights\n\n# Train the model\nresults = model.train(data=\"/kaggle/working/data.yaml\", epochs=25, imgsz=640)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:00:08.704535Z","iopub.execute_input":"2024-12-04T07:00:08.705366Z","iopub.status.idle":"2024-12-04T08:11:21.516844Z","shell.execute_reply.started":"2024-12-04T07:00:08.705306Z","shell.execute_reply":"2024-12-04T08:11:21.515742Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load a model\nmodel = YOLO(\"yolo11s.pt\")  # load an official model\nmodel = YOLO(\"/kaggle/working/runs/detect/train/weights/best.pt\")  # load a custom model\n\n# Predict with the model\nresults = model('/kaggle/working/datasets/final_dlp_data/test/images',conf = 0.1, iou = 0.5, verbose=False)  # predict on an image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T08:12:38.559541Z","iopub.execute_input":"2024-12-04T08:12:38.559914Z","iopub.status.idle":"2024-12-04T08:14:10.648807Z","shell.execute_reply.started":"2024-12-04T08:12:38.559885Z","shell.execute_reply":"2024-12-04T08:14:10.647764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load a model\nmodel = YOLO(\"yolo11s.pt\")  # load an official model\nmodel = YOLO(\"/kaggle/working/runs/detect/train2/weights/best.pt\")  # load a custom model\n\n# Predict with the model\nresults = model('/kaggle/working/datasets/final_dlp_data/test/images',conf = 0.1, iou = 0.5, verbose=False)  # predict on an image","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\n!rm /kaggle/working/21f1001083.csv\nsub_file = open('/kaggle/working/21f1001083.csv', 'w')\nsub_file.write(\"id,ImageID,LabelName,Conf,xcenter,ycenter,bbx_width,bbx_height\\n\")\ncounter = 0\nfor i, result in enumerate(results):\n    if result.boxes.cls.numel() == 0:\n        cls = result.names[0]\n        conf = 0.0\n        x = 0.0\n        y = 0.0\n        w = 0.0\n        h = 0.0\n    else:\n        best_idx = torch.argmax(result.boxes.conf)\n        cls = result.names[int(result.boxes.cls[best_idx].item())]\n        conf = result.boxes.conf[best_idx].item()\n        x = result.boxes.xywhn[best_idx][0].item()\n        y = result.boxes.xywhn[best_idx][1].item()\n        w = result.boxes.xywhn[best_idx][2].item()\n        h = result.boxes.xywhn[best_idx][3].item()\n    sub_file.write(str(counter) + ',' + os.path.basename(result.path).split('.')[0] + ',' + str(cls) +  ',' + str(conf) + ',' + str(x) + ',' + str(y) + ',' + str(w) + ',' + str(h) +'\\n' )\n    counter += 1 \nsub_file.close()    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T08:14:18.031153Z","iopub.execute_input":"2024-12-04T08:14:18.032059Z","iopub.status.idle":"2024-12-04T08:14:19.796294Z","shell.execute_reply.started":"2024-12-04T08:14:18.032003Z","shell.execute_reply":"2024-12-04T08:14:19.795375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nimage = Image.open('/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test/images/03ef458e-8f40-4d42-811b-f3c2db667e12.jpeg')\nimage.size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T09:04:50.715714Z","iopub.execute_input":"2024-12-04T09:04:50.716075Z","iopub.status.idle":"2024-12-04T09:04:50.728889Z","shell.execute_reply.started":"2024-12-04T09:04:50.716044Z","shell.execute_reply":"2024-12-04T09:04:50.727993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"res_set = set()\nfor file in os.listdir('/kaggle/working/datasets/final_dlp_data/train/images'):\n    image = Image.open('/kaggle/working/datasets/final_dlp_data/train/images/'+file)\n    res_set.add(image.size)\n\nres_set","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T09:07:08.080831Z","iopub.execute_input":"2024-12-04T09:07:08.081726Z","iopub.status.idle":"2024-12-04T09:07:08.905824Z","shell.execute_reply.started":"2024-12-04T09:07:08.081689Z","shell.execute_reply":"2024-12-04T09:07:08.904955Z"}},"outputs":[],"execution_count":null}]}