{"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)","metadata":{}},{"cell_type":"code","source":"# basic libraries ...\n\nimport os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ultrlytics ...\n\n!pip install ultralytics -q","metadata":{"execution":{"iopub.status.busy":"2024-01-20T11:46:04.655481Z","iopub.execute_input":"2024-01-20T11:46:04.656368Z","iopub.status.idle":"2024-01-20T11:46:16.918555Z","shell.execute_reply.started":"2024-01-20T11:46:04.656333Z","shell.execute_reply":"2024-01-20T11:46:16.917327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# yolo ...\n\nfrom ultralytics import YOLO","metadata":{"execution":{"iopub.status.busy":"2024-01-20T11:46:21.125478Z","iopub.execute_input":"2024-01-20T11:46:21.125810Z","iopub.status.idle":"2024-01-20T11:46:24.858755Z","shell.execute_reply.started":"2024-01-20T11:46:21.125784Z","shell.execute_reply":"2024-01-20T11:46:24.857772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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","metadata":{"execution":{"iopub.status.busy":"2024-01-20T11:46:42.911819Z","iopub.execute_input":"2024-01-20T11:46:42.912675Z","iopub.status.idle":"2024-01-20T11:46:50.636399Z","shell.execute_reply.started":"2024-01-20T11:46:42.912642Z","shell.execute_reply":"2024-01-20T11:46:50.635150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  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)","metadata":{"execution":{"iopub.status.busy":"2024-01-20T11:46:50.638584Z","iopub.execute_input":"2024-01-20T11:46:50.638924Z","iopub.status.idle":"2024-01-20T11:46:50.645346Z","shell.execute_reply.started":"2024-01-20T11:46:50.638892Z","shell.execute_reply":"2024-01-20T11:46:50.644294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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\")","metadata":{"execution":{"iopub.status.busy":"2024-01-20T11:46:55.782373Z","iopub.execute_input":"2024-01-20T11:46:55.783060Z","iopub.status.idle":"2024-01-20T11:46:57.475999Z","shell.execute_reply.started":"2024-01-20T11:46:55.783029Z","shell.execute_reply":"2024-01-20T11:46:57.475098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# working directory structure ...\n\n!tree -d","metadata":{"execution":{"iopub.status.busy":"2024-01-19T20:49:31.324341Z","iopub.execute_input":"2024-01-19T20:49:31.324725Z","iopub.status.idle":"2024-01-19T20:49:38.401012Z","shell.execute_reply.started":"2024-01-19T20:49:31.324694Z","shell.execute_reply":"2024-01-19T20:49:38.399742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model part ...","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# configuration file that tells YOLO where to find the dataset and what objects to detect ...\n\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)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T20:52:53.073339Z","iopub.execute_input":"2024-01-19T20:52:53.074222Z","iopub.status.idle":"2024-01-19T20:52:53.080997Z","shell.execute_reply.started":"2024-01-19T20:52:53.074183Z","shell.execute_reply":"2024-01-19T20:52:53.079943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Weights & Biases ...\n\nimport wandb\nwandb.init(mode=\"disabled\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# yolov8 small model, configuration file and run the model in 3 epochs ... \n\nmodel = YOLO(\"yolov8s.pt\")\nmodel.train(data=\"/kaggle/working/yolov8.yaml\", epochs=3)","metadata":{"execution":{"iopub.status.busy":"2024-01-19T20:53:50.249992Z","iopub.execute_input":"2024-01-19T20:53:50.250621Z","iopub.status.idle":"2024-01-19T21:08:10.748824Z","shell.execute_reply.started":"2024-01-19T20:53:50.250587Z","shell.execute_reply":"2024-01-19T21:08:10.747638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# inference part ...","metadata":{"execution":{"iopub.status.busy":"2024-01-20T12:35:40.170733Z","iopub.execute_input":"2024-01-20T12:35:40.171538Z","iopub.status.idle":"2024-01-20T12:35:40.175805Z","shell.execute_reply.started":"2024-01-20T12:35:40.171504Z","shell.execute_reply":"2024-01-20T12:35:40.174723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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#             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-01-19T22:05:56.873859Z","iopub.execute_input":"2024-01-19T22:05:56.874780Z","iopub.status.idle":"2024-01-19T22:07:18.582022Z","shell.execute_reply.started":"2024-01-19T22:05:56.874729Z","shell.execute_reply":"2024-01-19T22:07:18.580982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-19T22:07:29.650834Z","iopub.execute_input":"2024-01-19T22:07:29.651801Z","iopub.status.idle":"2024-01-19T22:07:29.670485Z","shell.execute_reply.started":"2024-01-19T22:07:29.651759Z","shell.execute_reply":"2024-01-19T22:07:29.669454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preview ...\n\nsubmission_df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T22:07:33.288153Z","iopub.execute_input":"2024-01-19T22:07:33.288537Z","iopub.status.idle":"2024-01-19T22:07:33.299527Z","shell.execute_reply.started":"2024-01-19T22:07:33.288507Z","shell.execute_reply":"2024-01-19T22:07:33.298495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataframe shape ...\n\nsubmission_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-19T22:07:34.211013Z","iopub.execute_input":"2024-01-19T22:07:34.211932Z","iopub.status.idle":"2024-01-19T22:07:34.218402Z","shell.execute_reply.started":"2024-01-19T22:07:34.211894Z","shell.execute_reply":"2024-01-19T22:07:34.217261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# any null value? ...\n\nsubmission_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-01-19T22:07:38.943605Z","iopub.execute_input":"2024-01-19T22:07:38.944449Z","iopub.status.idle":"2024-01-19T22:07:38.952909Z","shell.execute_reply.started":"2024-01-19T22:07:38.944410Z","shell.execute_reply":"2024-01-19T22:07:38.951875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-19T22:07:41.790983Z","iopub.execute_input":"2024-01-19T22:07:41.791412Z","iopub.status.idle":"2024-01-19T22:07:41.848184Z","shell.execute_reply.started":"2024-01-19T22:07:41.791382Z","shell.execute_reply":"2024-01-19T22:07:41.847127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"1","metadata":{"execution":{"iopub.status.busy":"2024-01-19T22:07:44.495656Z","iopub.execute_input":"2024-01-19T22:07:44.496735Z","iopub.status.idle":"2024-01-19T22:07:44.502620Z","shell.execute_reply.started":"2024-01-19T22:07:44.496695Z","shell.execute_reply":"2024-01-19T22:07:44.501506Z"},"trusted":true},"execution_count":null,"outputs":[]}]}