{"metadata":{"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"},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"},{"sourceId":7615531,"sourceType":"datasetVersion","datasetId":4433474}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics -q\nfrom ultralytics import YOLO\nimport tqdm\nimport os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nimport wandb\nwandb.init(mode=\"disabled\")","metadata":{"execution":{"iopub.status.busy":"2024-02-12T18:09:24.822409Z","iopub.execute_input":"2024-02-12T18:09:24.822819Z","iopub.status.idle":"2024-02-12T18:09:42.936082Z","shell.execute_reply.started":"2024-02-12T18:09:24.822784Z","shell.execute_reply":"2024-02-12T18:09:42.935177Z"},"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/input/journey-by-neuron-modelckpt/best_second_step.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.45, 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    return predictions_df\n# call the inference function ...\na = predict_all_files(test_directory)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T18:09:42.941976Z","iopub.execute_input":"2024-02-12T18:09:42.942321Z","iopub.status.idle":"2024-02-12T18:11:35.360985Z","shell.execute_reply.started":"2024-02-12T18:09:42.942288Z","shell.execute_reply":"2024-02-12T18:11:35.360023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = pd.read_csv('/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-12T18:11:35.362256Z","iopub.execute_input":"2024-02-12T18:11:35.362532Z","iopub.status.idle":"2024-02-12T18:11:35.459473Z","shell.execute_reply.started":"2024-02-12T18:11:35.362508Z","shell.execute_reply":"2024-02-12T18:11:35.458651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(b)):\n    x = b.iloc[i]['ImageID']\n    b.iloc[i, 2] = a.iloc[np.where(a == x)[0][0]]['PredictionString_pred']","metadata":{"execution":{"iopub.status.busy":"2024-02-12T18:11:35.461856Z","iopub.execute_input":"2024-02-12T18:11:35.46215Z","iopub.status.idle":"2024-02-12T18:11:37.824784Z","shell.execute_reply.started":"2024-02-12T18:11:35.462124Z","shell.execute_reply":"2024-02-12T18:11:37.823962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T18:13:14.26543Z","iopub.execute_input":"2024-02-12T18:13:14.26637Z","iopub.status.idle":"2024-02-12T18:13:14.278531Z","shell.execute_reply.started":"2024-02-12T18:13:14.266332Z","shell.execute_reply":"2024-02-12T18:13:14.277442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b.to_csv('submission_sust_17.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T18:11:37.842971Z","iopub.execute_input":"2024-02-12T18:11:37.843285Z","iopub.status.idle":"2024-02-12T18:11:37.911888Z","shell.execute_reply.started":"2024-02-12T18:11:37.843258Z","shell.execute_reply":"2024-02-12T18:11:37.910848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The output csv file corresponds to the score of mAP score of 0.6263","metadata":{}}]}