{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":19989,"databundleVersionId":1160143,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":282702272,"sourceType":"kernelVersion"},{"sourceId":282702666,"sourceType":"kernelVersion"},{"sourceId":666598,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":504582,"modelId":519547}],"dockerImageVersionId":31193,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install /kaggle/input/sam3-project-offline-packages/wheels/ultralytics-8.3.233-py3-none-any.whl --no-deps ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-30T07:45:36.146448Z","iopub.execute_input":"2025-11-30T07:45:36.146696Z","iopub.status.idle":"2025-11-30T07:45:39.808556Z","shell.execute_reply.started":"2025-11-30T07:45:36.146676Z","shell.execute_reply":"2025-11-30T07:45:39.807708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n### build pretrained YOLO v11 model\nWEIGHT_FILE = \"/kaggle/input/global-wheat-head-detection/pytorch/yolo-v11-large/1/best.pt\"\nmodel = YOLO(WEIGHT_FILE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T07:45:39.810233Z","iopub.execute_input":"2025-11-30T07:45:39.810620Z","iopub.status.idle":"2025-11-30T07:46:48.634496Z","shell.execute_reply.started":"2025-11-30T07:45:39.810588Z","shell.execute_reply":"2025-11-30T07:46:48.633671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# visualize a random sample\nimport matplotlib.pyplot as plt\nimport random\nimport os\n\n# randomely select one image\nimage_base_path = \"/kaggle/input/global-wheat-detection/test\"\nimage_names = os.listdir(image_base_path)\nimage_name = random.choice(image_names)\nimage_path = os.path.join(image_base_path, image_name)\n\n# run the model on one image\nyolo_predictions = model.predict(image_path, verbose=False)[0]\n\n# draw the bounding boxes on image and showcase\nimage_drawn = yolo_predictions.plot(labels=False)\nplt.figure(figsize=(16, 16))\nplt.imshow(image_drawn)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T07:46:48.635351Z","iopub.execute_input":"2025-11-30T07:46:48.635992Z","iopub.status.idle":"2025-11-30T07:46:51.647368Z","shell.execute_reply.started":"2025-11-30T07:46:48.635962Z","shell.execute_reply":"2025-11-30T07:46:51.646667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom PIL import Image\n\n\nsubmission_data = []\nfor image_name in image_names:\n    # collect the image\n    image_path = os.path.join(image_base_path, image_name)\n    image_pil = Image.open(image_path)\n    image_width, image_height = image_pil.size\n    \n    # run the model on one image\n    yolo_predictions = model.predict(image_path, verbose=False)[0]\n\n    # collect the predictions\n    predictions = []\n    for bbox in yolo_predictions.boxes.data.tolist():\n        x1, y1, x2, y2, score, _ = bbox\n        x1, y1 = int(max(0, x1)), int(max(0, y1))\n        x2, y2 = int(min(x2, 1024)), int(min(y2, 1024))\n        width, height = x2-x1, y2- y1\n        predictions += [round(score, 4), x1, y1, width, height]\n\n    # append the predictions to the submission data\n    submission_data.append({\n        \"image_id\": image_name.split('.')[0],\n        \"PredictionString\": \" \".join(list(map(str, predictions)))\n    })\n\n\nsubmission_df = pd.DataFrame(submission_data)\nsubmission_df.to_csv(\"submission.csv\", index=False)\nsubmission_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T07:46:51.648544Z","iopub.execute_input":"2025-11-30T07:46:51.648793Z","iopub.status.idle":"2025-11-30T07:46:52.574496Z","shell.execute_reply.started":"2025-11-30T07:46:51.648748Z","shell.execute_reply":"2025-11-30T07:46:52.573845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}