{"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":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30840,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h3>This Notebook Is For Submission</h3>\n<p>The solution is developed in the <a href=\"https://www.kaggle.com/code/sayedgamal99/in-cabin-state-farm-distracted-driver-detection\" target=\"_blank\">Developing Notebook (In-Cabin | State Farm Distracted Driver Detection)</a>.</p>\n","metadata":{}},{"cell_type":"code","source":"%%capture cell\n!pip install ultralytics tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:58:36.309275Z","iopub.execute_input":"2025-02-05T05:58:36.309550Z","iopub.status.idle":"2025-02-05T05:58:39.714744Z","shell.execute_reply.started":"2025-02-05T05:58:36.309529Z","shell.execute_reply":"2025-02-05T05:58:39.713795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nfrom kaggle.api.kaggle_api_extended import KaggleApi\nimport os\n\nsecrets = UserSecretsClient()\nkaggle_key = secrets.get_secret(\"KAGGLE_KEY\")\nkaggle_username = 'sayedgamal99'\n\n\nos.environ[\"KAGGLE_USERNAME\"] = kaggle_username\nos.environ[\"KAGGLE_KEY\"] = kaggle_key\n\napi = KaggleApi()\napi.authenticate()\n\nprint(\"✅ Kaggle API authenticated successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:58:43.952951Z","iopub.execute_input":"2025-02-05T05:58:43.953251Z","iopub.status.idle":"2025-02-05T05:58:44.033665Z","shell.execute_reply.started":"2025-02-05T05:58:43.953224Z","shell.execute_reply":"2025-02-05T05:58:44.033004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" !wget -O best.pt \"https://storage.googleapis.com/kaggle-script-versions/220359309/output/runs/classify/train/weights/best.pt?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com%2F20250205%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20250205T053930Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=95c57893c3a8a25003e1b4d677fedfd1b9f516241e3d9797f2d56d2e6696c70440004fb770520a4fc1383c14f9796d597d111f652128785b0bbe75bb8ddf64b3e64a55ed29f971f0068d8c41dba5536c20e36f21bd53157832dad35847513e360f4a7cf0a3e4685094e4c703b70d051d8197456eb6bae1d88dd71d83e64f5aae811d9dba09bcdadb6203efa77f3510787b67ba84282a4f6edd7f3e57e27476ff48c34ad9e9ae842146b2d9e9e444944a13df64bb95b8fc1c05fc824b933c27b37eff04d78386d33cc50da8ef05d49299447ed01d9ad5cad1f1369de9a02d72326dd0b2b3dc3ab8eb265076193dabc91105c5a7e144cc3ebbe07408c6050d9ab1\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T05:58:45.450690Z","iopub.execute_input":"2025-02-05T05:58:45.450989Z","iopub.status.idle":"2025-02-05T05:58:45.903187Z","shell.execute_reply.started":"2025-02-05T05:58:45.450964Z","shell.execute_reply":"2025-02-05T05:58:45.902332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nfrom ultralytics import YOLO\nfrom tqdm import tqdm\nimport time\n\ndef create_submission(batch_size=32):\n    # Load the trained model\n    model = YOLO('/kaggle/working/best.pt')\n    \n    # Path to test images\n    test_dir = '/kaggle/input/state-farm-distracted-driver-detection/imgs/test'\n    \n    # Get sorted list of test images for consistent order\n    test_images = sorted([f for f in os.listdir(test_dir) if f.endswith('.jpg')])\n    \n    # Initialize results dictionary\n    results_dict = {'img': test_images}\n    for i in range(10):\n        results_dict[f'c{i}'] = []\n    \n    # Get predictions using YOLO's built-in batch processing\n    results = model.predict(\n        source=test_dir,\n        batch=batch_size,\n        conf=0.1,\n        save=False,\n        stream=True,\n        verbose=False  # This will show progress bar\n    )\n    \n    # Process results\n    for idx, result in enumerate(results):\n        probs = result.probs.data.cpu().numpy()\n        \n        # If no probabilities, use uniform distribution\n        if probs is None or len(probs) == 0:\n            probs = np.ones(10) / 10\n            \n        # Add probabilities for each class\n        for i in range(10):\n            results_dict[f'c{i}'].append(float(probs[i]))\n    \n    # Create DataFrame\n    submission_df = pd.DataFrame(results_dict)\n    \n    # Save submission file\n    submission_path = 'submission.csv'\n    submission_df.to_csv(submission_path, index=False)\n    print(f\"\\nSubmission saved to {submission_path}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T06:08:14.738878Z","iopub.execute_input":"2025-02-05T06:08:14.739214Z","iopub.status.idle":"2025-02-05T06:08:14.745809Z","shell.execute_reply.started":"2025-02-05T06:08:14.739189Z","shell.execute_reply":"2025-02-05T06:08:14.745108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"create_submission(batch_size=64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T06:08:23.445955Z","iopub.execute_input":"2025-02-05T06:08:23.446292Z","iopub.status.idle":"2025-02-05T06:41:10.086436Z","shell.execute_reply.started":"2025-02-05T06:08:23.446264Z","shell.execute_reply":"2025-02-05T06:41:10.085448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"message = \"YOLO model submission1 with batch processing\"\ncompetition = \"state-farm-distracted-driver-detection\"\napi.competition_submit(\"/kaggle/working/submission.csv\", message, competition)\nprint(f\"Successfully submitted to {competition}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-05T06:53:29.180638Z","iopub.execute_input":"2025-02-05T06:53:29.180994Z","iopub.status.idle":"2025-02-05T06:53:30.648456Z","shell.execute_reply.started":"2025-02-05T06:53:29.180962Z","shell.execute_reply":"2025-02-05T06:53:30.647593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}