{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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"},{"sourceId":7617924,"sourceType":"datasetVersion","datasetId":4436872}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Inference Notebook for DL Engima 1.0 by **DL_noobies**","metadata":{}},{"cell_type":"markdown","source":"## You can access my Training notebook follow this link [Training Notebook By DL_nooobie]()\n\n## Competition Link [DL Enigma 1.0](https://www.kaggle.com/competitions/dl-enigma-10-sust-cse-carnival-2024/overview)\n\n### This notebook was inspired from [here](https://www.kaggle.com/code/ataullhasaim/mws-dl-enigma-1-0-starter-notebook)","metadata":{}},{"cell_type":"markdown","source":"# **Instructions for Running the Code**\n\nTo run the provided code, follow these steps:\n\n1. **Running Cells**: Click on the \"Run\" button located to the left of each cell in the Jupyter notebook. Alternatively, you can press `Ctrl+Enter` to run a single cell or `Shift+Enter` to run a cell and move to the next one.\n\n2. **Checking GPU Accelerator**: Ensure that your GPU Accelerator is enabled. It's recommended to use a GPU like the P100, but any GPU should work.\n\n3. **No External Libraries Needed**: You don't need to install any external libraries manually. The code utilizes libraries and functionalities already available in the Kaggle environment.\n\n4. **Step-by-Step Execution**: Run each cell one by one in sequential order. The code is organized in a way that each cell builds upon the previous one, so executing them sequentially ensures proper functionality.\n\n5. **Let Kaggle Do the Work**: Kaggle will handle the execution of the code and utilize the available GPU resources for accelerated processing.\n","metadata":{}},{"cell_type":"markdown","source":"# **Libraries Importation and Installation**","metadata":{}},{"cell_type":"code","source":"# basic libraries ...\n\nimport os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:55:40.579525Z","iopub.execute_input":"2024-02-13T12:55:40.580388Z","iopub.status.idle":"2024-02-13T12:55:40.930353Z","shell.execute_reply.started":"2024-02-13T12:55:40.580353Z","shell.execute_reply":"2024-02-13T12:55:40.929516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Installs Ultralytics for computer vision tasks.\n\n!pip install ultralytics","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:55:49.463729Z","iopub.execute_input":"2024-02-13T12:55:49.464050Z","iopub.status.idle":"2024-02-13T12:56:03.247293Z","shell.execute_reply.started":"2024-02-13T12:55:49.464026Z","shell.execute_reply":"2024-02-13T12:56:03.246361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing YOLO model from Ultralytics library.\n\nfrom ultralytics import YOLO","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:56:03.249270Z","iopub.execute_input":"2024-02-13T12:56:03.249565Z","iopub.status.idle":"2024-02-13T12:56:09.599040Z","shell.execute_reply.started":"2024-02-13T12:56:03.249537Z","shell.execute_reply":"2024-02-13T12:56:09.598023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Creating Working Directories for Dataset Organization**","metadata":{}},{"cell_type":"code","source":"# Deletes all files and directories in the current working directory recursively.\n\n!rm -rf *\n\n# Creates necessary directories for dataset organization.\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-02-13T12:56:09.600251Z","iopub.execute_input":"2024-02-13T12:56:09.600705Z","iopub.status.idle":"2024-02-13T12:56:17.228106Z","shell.execute_reply.started":"2024-02-13T12:56:09.600670Z","shell.execute_reply":"2024-02-13T12:56:17.226888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creates symbolic links to reference original files in another directory 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-02-13T12:56:17.231161Z","iopub.execute_input":"2024-02-13T12:56:17.231578Z","iopub.status.idle":"2024-02-13T12:56:17.237340Z","shell.execute_reply.started":"2024-02-13T12:56:17.231541Z","shell.execute_reply":"2024-02-13T12:56:17.236369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating symbolic links for files in specified directories.\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-02-13T12:56:17.238560Z","iopub.execute_input":"2024-02-13T12:56:17.238940Z","iopub.status.idle":"2024-02-13T12:56:18.950959Z","shell.execute_reply.started":"2024-02-13T12:56:17.238908Z","shell.execute_reply":"2024-02-13T12:56:18.949993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Displaying the working directory structure ...\n\n!tree -d","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:57:22.865996Z","iopub.execute_input":"2024-02-13T12:57:22.866363Z","iopub.status.idle":"2024-02-13T12:57:29.396087Z","shell.execute_reply.started":"2024-02-13T12:57:22.866336Z","shell.execute_reply":"2024-02-13T12:57:29.395085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Inference part** \n","metadata":{"execution":{"iopub.status.busy":"2024-02-10T17:36:40.399870Z","iopub.execute_input":"2024-02-10T17:36:40.400197Z","iopub.status.idle":"2024-02-10T17:36:40.404387Z","shell.execute_reply.started":"2024-02-10T17:36:40.400171Z","shell.execute_reply":"2024-02-10T17:36:40.403428Z"}}},{"cell_type":"code","source":"# Function to convert prediction boxes to the submission format string.\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 +=  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 failed 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\"","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:57:38.118088Z","iopub.execute_input":"2024-02-13T12:57:38.118504Z","iopub.status.idle":"2024-02-13T12:57:38.126846Z","shell.execute_reply.started":"2024-02-13T12:57:38.118470Z","shell.execute_reply":"2024-02-13T12:57:38.125996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to generate prediction entry in the required format.\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    }","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:57:40.348396Z","iopub.execute_input":"2024-02-13T12:57:40.348737Z","iopub.status.idle":"2024-02-13T12:57:40.353988Z","shell.execute_reply.started":"2024-02-13T12:57:40.348712Z","shell.execute_reply":"2024-02-13T12:57:40.352852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to perform inference on all test images and generate predictions.\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            \n            results = model.predict(\n                source=filepath,\n                conf=0.40, # changed the confidence from default 0.25 to 0.4 for better prediction\n                verbose=False,\n            )\n            \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)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:57:41.730537Z","iopub.execute_input":"2024-02-13T12:57:41.731379Z","iopub.status.idle":"2024-02-13T12:57:41.738457Z","shell.execute_reply.started":"2024-02-13T12:57:41.731345Z","shell.execute_reply":"2024-02-13T12:57:41.737460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Perform Inference**","metadata":{}},{"cell_type":"code","source":"# Directory path for test images.\ntest_directory = \"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test\"\n\n# Loading the Trained best YOLO model.\nmodel = YOLO('/kaggle/input/dl-noobies-best-model-in-dl-enigma/best_model.pt')\n\n# Perform inference.\npredict_all_files(test_directory)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:57:46.918823Z","iopub.execute_input":"2024-02-13T12:57:46.919714Z","iopub.status.idle":"2024-02-13T12:59:50.784853Z","shell.execute_reply.started":"2024-02-13T12:57:46.919676Z","shell.execute_reply":"2024-02-13T12:59:50.783950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading the submission dataframe ....\n\nsubmission_df = pd.read_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:59:54.632381Z","iopub.execute_input":"2024-02-13T12:59:54.633304Z","iopub.status.idle":"2024-02-13T12:59:54.655753Z","shell.execute_reply.started":"2024-02-13T12:59:54.633260Z","shell.execute_reply":"2024-02-13T12:59:54.654995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataframe shape ...\n\nsubmission_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:59:56.668199Z","iopub.execute_input":"2024-02-13T12:59:56.668562Z","iopub.status.idle":"2024-02-13T12:59:56.675546Z","shell.execute_reply.started":"2024-02-13T12:59:56.668532Z","shell.execute_reply":"2024-02-13T12:59:56.674661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# last few row of the dataframe\n\nsubmission_df.tail()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T12:59:59.880510Z","iopub.execute_input":"2024-02-13T12:59:59.881715Z","iopub.status.idle":"2024-02-13T12:59:59.899393Z","shell.execute_reply.started":"2024-02-13T12:59:59.881665Z","shell.execute_reply":"2024-02-13T12:59:59.898382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking for any null values \n\nsubmission_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:00:01.966252Z","iopub.execute_input":"2024-02-13T13:00:01.966674Z","iopub.status.idle":"2024-02-13T13:00:01.975799Z","shell.execute_reply.started":"2024-02-13T13:00:01.966635Z","shell.execute_reply":"2024-02-13T13:00:01.974894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## You can submit the following CSV file or the previously created \"submission.csv\" file to check the result.\n","metadata":{}},{"cell_type":"code","source":"# acceptable submission format ...\n\nsubmission_df.to_csv('Submission_final.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:00:05.187393Z","iopub.execute_input":"2024-02-13T13:00:05.187790Z","iopub.status.idle":"2024-02-13T13:00:05.257465Z","shell.execute_reply.started":"2024-02-13T13:00:05.187758Z","shell.execute_reply":"2024-02-13T13:00:05.256645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Happy Ending ","metadata":{}}]}