{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":87536,"databundleVersionId":9979270,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Requirements","metadata":{}},{"cell_type":"code","source":"!pip install -q ultralytics","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-28T07:16:26.211966Z","iopub.execute_input":"2024-10-28T07:16:26.212809Z","iopub.status.idle":"2024-10-28T07:16:39.666298Z","shell.execute_reply.started":"2024-10-28T07:16:26.212767Z","shell.execute_reply":"2024-10-28T07:16:39.665125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\nimport pandas as pd\n\nfrom tqdm import tqdm\nfrom ultralytics import YOLO","metadata":{"execution":{"iopub.status.busy":"2024-10-28T07:16:39.668028Z","iopub.execute_input":"2024-10-28T07:16:39.668316Z","iopub.status.idle":"2024-10-28T07:16:43.879295Z","shell.execute_reply.started":"2024-10-28T07:16:39.668285Z","shell.execute_reply":"2024-10-28T07:16:43.878510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLOv5 Model","metadata":{}},{"cell_type":"markdown","source":"## Load & Training","metadata":{}},{"cell_type":"code","source":"# Load pretrained YOLOv5 model\nmodel = YOLO(\"yolov5n.pt\")\n\n# Train the model\nresults = model.train(data=\"/kaggle/input/data-centric-noisy-pascal-challenge/voc2007.yaml\",\n                      epochs=50, batch=16, imgsz=640, patience=10,    # DO NOT CHANGE THIS LINE\n                      plots=True,                                     # DO NOT CHANGE THIS LINE\n                      project='DIP_Project',\n                      name='voc2007',\n                      exist_ok=True,\n                      hsv_h=0.0,                                      # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      hsv_s=0.0,                                      # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      hsv_v=0.0,                                      # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      degrees=0.0,                                    # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      translate=0.0,                                  # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      scale=0.0,                                      # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      shear=0.0,                                      # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      perspective=0.0,                                # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      flipud=0.0,                                     # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      fliplr=0.0,                                     # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      bgr=0.0,                                        # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      mosaic=0.0,                                     # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      copy_paste=0.0,                                 # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      erasing=0.0,                                    # CHANGE IT ONLY FOR PHASE 3 (Augmentation)\n                      crop_fraction=0.0)                              # CHANGE IT ONLY FOR PHASE 3 (Augmentation)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T07:16:43.880881Z","iopub.execute_input":"2024-10-28T07:16:43.881450Z","iopub.status.idle":"2024-10-28T07:32:39.812199Z","shell.execute_reply.started":"2024-10-28T07:16:43.881403Z","shell.execute_reply":"2024-10-28T07:32:39.811099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"# Change following path to your best model path\nBEST_MODEL_PATH = \"/kaggle/working/DIP_Project/voc2007/weights/best.pt\"","metadata":{"execution":{"iopub.status.busy":"2024-10-28T07:41:54.824894Z","iopub.execute_input":"2024-10-28T07:41:54.825830Z","iopub.status.idle":"2024-10-28T07:41:54.830041Z","shell.execute_reply.started":"2024-10-28T07:41:54.825786Z","shell.execute_reply":"2024-10-28T07:41:54.829157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#########################################\n####### DO NOT CHANGE THIS CELL #########\n#########################################\n###### CREATE SUBMISSION.CSV FILE #######\n#########################################\n\n# Load the best model\nmodel = YOLO(BEST_MODEL_PATH)\n\n# Define the path to the test images\ntest_images_path = \"/kaggle/input/data-centric-noisy-pascal-challenge/voc2007/images/test\"\n\n# Get all image file paths\ntest_images = glob.glob(os.path.join(test_images_path, \"*.jpg\"))\n\n# List to store predictions\npredictions = []\n\n# Loop through all test images and predict using YOLO\nfor img_path in tqdm(test_images):\n    results = model(img_path, verbose=False, imgsz=640)  # Run inference on each image\n    img_id = os.path.basename(img_path)  # Get the image ID (assumed to be the file name with extension)\n    image_height, image_width = results[0].orig_shape\n\n    prediction_strings = []\n\n    # Iterate over the results in `results` object\n    for box in results[0].boxes:\n        class_id = int(box.cls)\n        confidence = float(box.conf)\n        x_center, y_center, width, height = box.xywhn.tolist()[0]\n        prediction_strings.append(f\"{class_id} {confidence} {x_center} {y_center} {width} {height}\")\n    \n    if not prediction_strings:\n        prediction_strings.append(\" \")\n    predictions.append([img_id, \" \".join(prediction_strings), image_width, image_height, \"Public\"])\n\n# Create a DataFrame for the submission\nsubmission_df = pd.DataFrame(predictions, columns=[\"image_id\", \"prediction_string\", \"image_width\", \"image_height\", \"Usage\"])\n\n# Save to CSV (submission.csv)\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-28T07:42:00.185190Z","iopub.execute_input":"2024-10-28T07:42:00.185574Z","iopub.status.idle":"2024-10-28T07:43:42.639230Z","shell.execute_reply.started":"2024-10-28T07:42:00.185537Z","shell.execute_reply":"2024-10-28T07:43:42.638300Z"},"trusted":true},"execution_count":null,"outputs":[]}]}