{"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":"# Import Modules","metadata":{}},{"cell_type":"code","source":"# Import necessary libraries\n!pip install ultralytics\nimport os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import average_precision_score\nfrom ultralytics import YOLO\nimport wandb","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:14.165683Z","iopub.execute_input":"2024-01-28T10:32:14.166166Z","iopub.status.idle":"2024-01-28T10:32:33.291057Z","shell.execute_reply.started":"2024-01-28T10:32:14.166138Z","shell.execute_reply":"2024-01-28T10:32:33.290080Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function To create symbolink","metadata":{}},{"cell_type":"code","source":"def all_files_symlik(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-28T10:32:33.292956Z","iopub.execute_input":"2024-01-28T10:32:33.293713Z","iopub.status.idle":"2024-01-28T10:32:33.299061Z","shell.execute_reply.started":"2024-01-28T10:32:33.293679Z","shell.execute_reply":"2024-01-28T10:32:33.298143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Set Up Working Directory","metadata":{}},{"cell_type":"code","source":"# Set up the directory structure\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-28T10:32:33.300327Z","iopub.execute_input":"2024-01-28T10:32:33.300631Z","iopub.status.idle":"2024-01-28T10:32:39.933435Z","shell.execute_reply.started":"2024-01-28T10:32:33.300609Z","shell.execute_reply":"2024-01-28T10:32:39.932334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! tree -d","metadata":{"execution":{"iopub.status.busy":"2024-01-25T19:50:47.259556Z","iopub.execute_input":"2024-01-25T19:50:47.259898Z","iopub.status.idle":"2024-01-25T19:50:48.201357Z","shell.execute_reply.started":"2024-01-25T19:50:47.259870Z","shell.execute_reply":"2024-01-25T19:50:48.200200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create symlinks for the dataset\nall_files_symlik(\"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/labels/train\", \"/kaggle/working/datasets/badodd/labels/train\")\nall_files_symlik(\"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/train\", \"/kaggle/working/datasets/badodd/images/train\")\nall_files_symlik(\"/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-28T10:34:26.858536Z","iopub.execute_input":"2024-01-28T10:34:26.859184Z","iopub.status.idle":"2024-01-28T10:34:27.622680Z","shell.execute_reply.started":"2024-01-28T10:34:26.859146Z","shell.execute_reply":"2024-01-28T10:34:27.621448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config YAML file","metadata":{}},{"cell_type":"code","source":"file_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\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:34:51.095562Z","iopub.execute_input":"2024-01-28T10:34:51.096201Z","iopub.status.idle":"2024-01-28T10:34:51.100567Z","shell.execute_reply.started":"2024-01-28T10:34:51.096172Z","shell.execute_reply":"2024-01-28T10:34:51.099726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('yolov8.yaml',mode='w') as f:\n    f.write(file_content)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:34:53.988558Z","iopub.execute_input":"2024-01-28T10:34:53.989364Z","iopub.status.idle":"2024-01-28T10:34:53.993535Z","shell.execute_reply.started":"2024-01-28T10:34:53.989332Z","shell.execute_reply":"2024-01-28T10:34:53.992711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize wandb\nimport wandb\nwandb.init(mode='disabled')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:34:55.696267Z","iopub.execute_input":"2024-01-28T10:34:55.697032Z","iopub.status.idle":"2024-01-28T10:34:56.841619Z","shell.execute_reply.started":"2024-01-28T10:34:55.696997Z","shell.execute_reply":"2024-01-28T10:34:56.840691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing Function","metadata":{}},{"cell_type":"code","source":"def resize_image(image_path, output_path, target_size=(416, 416)):\n    image = cv2.imread(image_path)\n    resized_image = cv2.resize(image, target_size)\n    cv2.imwrite(output_path, resized_image)\n\n    \n    \ndef normalize_labels(yolo_labels, image_shape, target_size=(416, 416)):\n    image_width, image_height = image_shape\n    target_width, target_height = target_size\n    \n    # Normalize the bounding box cordinates\n    yolo_labels[:,1] = yolo_labels[:, 1]  *  target_width / image_width\n    yolo_labels[:,2] = yolo_labels[:, 2]  *  target_height / image_height\n    yolo_labels[:,3] = yolo_labels[:, 3]  *  target_width / image_width\n    yolo_labels[:,4] = yolo_labels[:, 4]  *  target_height / image_height\n    \n    return yolo_labels\n\n\ndef preprocess_dataset(input_dir, output_dir, target_size=(416, 416)):\n    for filename in tqdm(os.listdir(os.path.join(input_dir, 'images', 'train'))):\n        if filename.endswith('.jpg'):\n            image_path = os.path.join(input_dir, 'images', 'train', filename)\n            label_path = os.path.join(input_dir, 'labels', 'train', filename.replace('.jpg', '.txt'))\n            \n            output_image_path = os.path.join(output_dir, 'images', 'train', filename)\n            output_label_path = os.path.join(output_dir, 'labels', 'train', filename.replace('.jpg', '.txt'))\n\n            # Resize the image\n            resize_image(image_path, output_image_path, target_size=target_size)\n            \n            # Load and normalize the labels\n            labels = np.loadtxt(label_path).reshape(-1, 5)\n            labels = normalize_labels(labels, image_shape=(target_size[0], target_size[1]))\n            \n            # Save the normalized image\n            np.savetxt(output_label_path, labels, fmt=['%d', '%f', '%f', '%f', '%f'])\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:34:59.135290Z","iopub.execute_input":"2024-01-28T10:34:59.135958Z","iopub.status.idle":"2024-01-28T10:34:59.147379Z","shell.execute_reply.started":"2024-01-28T10:34:59.135929Z","shell.execute_reply":"2024-01-28T10:34:59.146397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_dir = \"/kaggle/working/datasets/badodd\"\noutput_dir = \"/kaggle/working/preprocessed_data\"","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:35:04.658424Z","iopub.execute_input":"2024-01-28T10:35:04.658796Z","iopub.status.idle":"2024-01-28T10:35:04.663207Z","shell.execute_reply.started":"2024-01-28T10:35:04.658766Z","shell.execute_reply":"2024-01-28T10:35:04.662325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create output directories","metadata":{}},{"cell_type":"code","source":"os.makedirs(os.path.join(output_dir, 'images', 'train'), exist_ok =True)\nos.makedirs(os.path.join(output_dir, 'labels', 'train'), exist_ok =True)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:35:07.855542Z","iopub.execute_input":"2024-01-28T10:35:07.856424Z","iopub.status.idle":"2024-01-28T10:35:07.861583Z","shell.execute_reply.started":"2024-01-28T10:35:07.856376Z","shell.execute_reply":"2024-01-28T10:35:07.860721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocess The Dataset","metadata":{}},{"cell_type":"code","source":"import cv2\npreprocess_dataset(input_dir, output_dir, target_size=(416,416))","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:35:11.133197Z","iopub.execute_input":"2024-01-28T10:35:11.133922Z","iopub.status.idle":"2024-01-28T10:39:39.123269Z","shell.execute_reply.started":"2024-01-28T10:35:11.133892Z","shell.execute_reply":"2024-01-28T10:39:39.122347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train YOLOv8 Model","metadata":{}},{"cell_type":"code","source":"model = YOLO('yolov8s.pt')\nmodel.train(data='/kaggle/working/yolov8.yaml', epochs=100)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:39:39.125147Z","iopub.execute_input":"2024-01-28T10:39:39.125831Z","iopub.status.idle":"2024-01-28T16:19:51.097686Z","shell.execute_reply.started":"2024-01-28T10:39:39.125794Z","shell.execute_reply":"2024-01-28T16:19:51.096581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to convert predictions to the required format","metadata":{}},{"cell_type":"code","source":"def get_prediction_string(boxes, scores, classes):\n    pred_strs = []\n    for i, scores in enumerate(scores):\n        single_pred_str = \"\"\n        single_pred_str += str(float(classes[i])) + ' ' + str(float(scores)) + ' '\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        \n        single_pred_str += f\"{x1} {y1} {width} {height}\"\n        pred_strs.append(single_pred_str)\n        \n    ans = ','.join(map(str, pred_strs))\n    if len(ans):\n        return ans\n    return '0 0 0 0 0 0'\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T16:20:14.156364Z","iopub.execute_input":"2024-01-28T16:20:14.157367Z","iopub.status.idle":"2024-01-28T16:20:14.165131Z","shell.execute_reply.started":"2024-01-28T16:20:14.157316Z","shell.execute_reply":"2024-01-28T16:20:14.164079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to get preidictin entry","metadata":{}},{"cell_type":"code","source":"def get_prediction_entry(i, filename, boxes, scores, classes):\n    return{\n        'id':i,\n        'ImageID':filename.split('.')[0],\n        'PredictionString_pred':get_prediction_string(boxes, scores, classes)}","metadata":{"execution":{"iopub.status.busy":"2024-01-28T16:20:15.978112Z","iopub.execute_input":"2024-01-28T16:20:15.979046Z","iopub.status.idle":"2024-01-28T16:20:15.984089Z","shell.execute_reply.started":"2024-01-28T16:20:15.979010Z","shell.execute_reply":"2024-01-28T16:20:15.982984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_directory = \"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test\"","metadata":{"execution":{"iopub.status.busy":"2024-01-28T16:20:17.270392Z","iopub.execute_input":"2024-01-28T16:20:17.271250Z","iopub.status.idle":"2024-01-28T16:20:17.275450Z","shell.execute_reply.started":"2024-01-28T16:20:17.271217Z","shell.execute_reply":"2024-01-28T16:20:17.274514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO('/kaggle/working/runs/detect/train/weights/best.pt')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T16:20:18.970516Z","iopub.execute_input":"2024-01-28T16:20:18.971687Z","iopub.status.idle":"2024-01-28T16:20:19.051128Z","shell.execute_reply.started":"2024-01-28T16:20:18.971636Z","shell.execute_reply":"2024-01-28T16:20:19.049961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inferance Function","metadata":{}},{"cell_type":"code","source":"def 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    prediction_df = pd.DataFrame(predictions)\n    prediction_df.to_csv('submission1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T16:20:24.530029Z","iopub.execute_input":"2024-01-28T16:20:24.530399Z","iopub.status.idle":"2024-01-28T16:20:24.537873Z","shell.execute_reply.started":"2024-01-28T16:20:24.530370Z","shell.execute_reply":"2024-01-28T16:20:24.536820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run Interface\npredict_all_files(test_directory)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T16:20:25.430339Z","iopub.execute_input":"2024-01-28T16:20:25.430743Z","iopub.status.idle":"2024-01-28T16:22:08.175147Z","shell.execute_reply.started":"2024-01-28T16:20:25.430712Z","shell.execute_reply":"2024-01-28T16:22:08.174136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p","metadata":{},"execution_count":null,"outputs":[]}]}