{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"}],"dockerImageVersionId":30636,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Minimum Working Solution DL Engima 1.0","metadata":{}},{"cell_type":"markdown","source":"#### This notebook was inspired from [here](https://www.kaggle.com/code/sameen53/yolov8-minimum-working-sample) ##and [here](https://www.kaggle.com/code/abrarjahinniloy/yolo-model-e-2-training)##and [here](https://www.kaggle.com/code/ataullhasaim/mws-dl-enigma-1-0-starter-notebook)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\n!pip install ultralytics -q\nfrom ultralytics import YOLO\n# delete all the files and directories recursively in the current working directory ...\n\n!rm -rf *\n\n# make directory ...\n\n!mkdir /kaggle/working/datasets\n!mkdir /kaggle/working/datasets/badodd\n!mkdir /kaggle/working/datasets/badodd/labels\n!mkdir /kaggle/working/datasets/badodd/labels/train\n!mkdir /kaggle/working/datasets/badodd/images\n!mkdir /kaggle/working/datasets/badodd/images/train\n!mkdir /kaggle/working/datasets/badodd/images/test\n#  reference original files without duplicating their content ...\n\ndef all_files_in_folder_symlink(source_dir, target_dir):\n    files = os.listdir(source_dir)\n    \n    for file in tqdm(files):\n        source_file = os.path.join(source_dir, file)\n        target_file = os.path.join(target_dir, file)\n        os.symlink(source_file, target_file)\n# symbolic link function as above ...\n\nall_files_in_folder_symlink(\"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/labels/train\",\"/kaggle/working/datasets/badodd/labels/train\")\nall_files_in_folder_symlink(\"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/train\",\"/kaggle/working/datasets/badodd/images/train\")\nall_files_in_folder_symlink(\"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test\",\"/kaggle/working/datasets/badodd/images/test\")        \n# configuration file that tells YOLO where to find the dataset and what objects to detect ...\n!tree -d\nfile_content = \"\"\"\npath: /kaggle/working/datasets/badodd  # dataset root dir\ntrain: images/train  # train images (relative to 'path')\nval: images/train  # val images (relative to 'path')\ntest:  images/test\n\n# Classes\nnames:\n    0: auto_rickshaw\n    1: bicycle\n    2: bus\n    3: car\n    4: cart_vehicle\n    5: construction_vehicle\n    6: motorbike\n    7: person\n    8: priority_vehicle\n    9: three_wheeler\n    10: train\n    11: truck\n    12: wheelchair\n\"\"\"\n\nwith open(\"yolov8.yaml\", mode=\"w\") as f:\n    f.write(file_content)\n# Weights & Biases ...\n\nimport wandb\nwandb.init(mode=\"disabled\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T01:38:27.570581Z","iopub.execute_input":"2024-01-28T01:38:27.570943Z","iopub.status.idle":"2024-01-28T01:39:13.202153Z","shell.execute_reply.started":"2024-01-28T01:38:27.570905Z","shell.execute_reply":"2024-01-28T01:39:13.201035Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# yolov8 small model, configuration file and run the model in 3 epochs ... \nfrom torch.optim import AdamW\nmodel = YOLO('yolov8m.pt') \n\n# ,optimizer='AdamW',warmup_epochs=3.0,lr0=0.0005,weight_decay=0.0005\nmodel.train(data=\"/kaggle/working/yolov8.yaml\", epochs=47,optimizer='AdamW',warmup_epochs=3.0,lr0=0.0001,weight_decay=0.0005,scale=0.6)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T01:51:13.797575Z","iopub.execute_input":"2024-01-28T01:51:13.797971Z","iopub.status.idle":"2024-01-28T01:51:14.013283Z","shell.execute_reply.started":"2024-01-28T01:51:13.797942Z","shell.execute_reply":"2024-01-28T01:51:14.011925Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# inference part ...","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:25:59.977483Z","iopub.status.idle":"2024-01-21T16:25:59.977823Z","shell.execute_reply.started":"2024-01-21T16:25:59.977661Z","shell.execute_reply":"2024-01-21T16:25:59.977678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# get prediction boxes string according to the submission format ...\n\ndef get_prediction_string(boxes, scores, classes):\n    pred_strs = []\n    for i, score in enumerate(scores):\n        single_pred_str = \"\"\n        single_pred_str += str(float(classes[i])) + \" \" + str(float(score)) + \" \"\n        \n        x_center , y_center, width,height = boxes[i]\n        x1 = float(x_center) - (float(width) / 2)\n        y1 = float(y_center) - (float(height) / 2)\n        width = float(width)\n        height = float(height)\n        #single_pred_str += \" \".join(str(float(x)) for x in boxes[i])\n        single_pred_str +=  f\"{x1} {y1} {width} {height}\"\n        \n        pred_strs.append(single_pred_str)\n    ans = ','.join(map(str, pred_strs))\n    if len(ans):\n        return ans\n#     the solution metrics faield in case of a NaN, '' (empty). So, return \"0 0 0 0 0 0\" for NaN, '' (empty) string\n    return \"0 0 0 0 0 0\"\n\n# get the predcition in id, ImageID, PredictionString_pred foramt ...\n\ndef get_prediction_entry(i, filename, boxes, scores, classes):\n    return {\n        \"id\": i, # strating from 0 ...\n        \"ImageID\": filename.split('.')[0], # before the extension ...\n        \"PredictionString_pred\": get_prediction_string(boxes, scores, classes)\n    }\n\n# Directory path ...\ntest_directory = \"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test\"\n\n# Load the model ...\nmodel = YOLO('/kaggle/working/runs/detect/train/weights/best.pt')\n\n# do the inference ...\n\ndef predict_all_files(test_directory):\n    predictions = []\n    for i,filename in tqdm(enumerate(os.listdir(test_directory))):\n        if filename.endswith(\".jpg\"):\n            filepath = os.path.join(test_directory, filename)\n            results = model.predict(source=filepath, conf=0.50, verbose=False)\n            boxes = results[0].boxes.xywhn\n            scores = results[0].boxes.conf\n            classes = results[0].boxes.cls\n            prediction = get_prediction_entry(i, filename, boxes, scores, classes)\n            predictions.append(prediction)\n           \n#             to csv format ...\n    predictions_df = pd.DataFrame(predictions)\n    predictions_df.to_csv(\"submission.csv\", index=False)\n\n# call the inference function ...\npredict_all_files(test_directory)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T01:52:51.519480Z","iopub.execute_input":"2024-02-08T01:52:51.520230Z","iopub.status.idle":"2024-02-08T01:52:51.866611Z","shell.execute_reply.started":"2024-02-08T01:52:51.520194Z","shell.execute_reply":"2024-02-08T01:52:51.865497Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load the submission dataframe ....\n\nsubmission_df = pd.read_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:25:59.980483Z","iopub.status.idle":"2024-01-21T16:25:59.980782Z","shell.execute_reply.started":"2024-01-21T16:25:59.980631Z","shell.execute_reply":"2024-01-21T16:25:59.980645Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# preview ...\n\nsubmission_df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:25:59.982518Z","iopub.status.idle":"2024-01-21T16:25:59.982844Z","shell.execute_reply.started":"2024-01-21T16:25:59.982669Z","shell.execute_reply":"2024-01-21T16:25:59.982683Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# dataframe shape ...\n\nsubmission_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:25:59.983778Z","iopub.status.idle":"2024-01-21T16:25:59.984148Z","shell.execute_reply.started":"2024-01-21T16:25:59.983925Z","shell.execute_reply":"2024-01-21T16:25:59.983939Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# any null value? ...\n\nsubmission_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:25:59.986057Z","iopub.status.idle":"2024-01-21T16:25:59.986506Z","shell.execute_reply.started":"2024-01-21T16:25:59.986274Z","shell.execute_reply":"2024-01-21T16:25:59.986296Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# acceptable submission format ...\n\nsubmission_df.to_csv('acceptable_submission_format.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:25:59.987936Z","iopub.status.idle":"2024-01-21T16:25:59.988386Z","shell.execute_reply.started":"2024-01-21T16:25:59.988165Z","shell.execute_reply":"2024-01-21T16:25:59.988186Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"1","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:25:59.989770Z","iopub.status.idle":"2024-01-21T16:25:59.990221Z","shell.execute_reply.started":"2024-01-21T16:25:59.989993Z","shell.execute_reply":"2024-01-21T16:25:59.990015Z"},"trusted":true},"outputs":[],"execution_count":null}]}