{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":107469,"databundleVersionId":13058354,"sourceType":"competition"},{"sourceId":12537987,"sourceType":"datasetVersion","datasetId":7915408},{"sourceId":12578044,"sourceType":"datasetVersion","datasetId":7943689}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from IPython.display import clear_output\n!pip install git+https://github.com/3lc-ai/3lc-ultralytics@develop\nclear_output()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-27T22:36:31.261082Z","iopub.execute_input":"2025-07-27T22:36:31.261320Z","iopub.status.idle":"2025-07-27T22:38:31.706677Z","shell.execute_reply.started":"2025-07-27T22:36:31.261302Z","shell.execute_reply":"2025-07-27T22:38:31.705961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\ntlc_key = user_secrets.get_secret(\"API Key\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-27T22:38:31.708219Z","iopub.execute_input":"2025-07-27T22:38:31.708502Z","iopub.status.idle":"2025-07-27T22:38:31.855229Z","shell.execute_reply.started":"2025-07-27T22:38:31.708478Z","shell.execute_reply":"2025-07-27T22:38:31.854692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!3lc login {tlc_key}\nclear_output()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-27T22:38:31.855854Z","iopub.execute_input":"2025-07-27T22:38:31.856077Z","iopub.status.idle":"2025-07-27T22:38:34.904692Z","shell.execute_reply.started":"2025-07-27T22:38:31.856060Z","shell.execute_reply":"2025-07-27T22:38:34.904018Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tlc\nfrom tlc_ultralytics import Settings, YOLO\nfrom pathlib import Path\nimport csv\nimport os\nimport numpy as np\nimport pandas as pd\nimport random\nimport torch\n# Set random seeds for reproducibility\nnp.random.seed(42)\nrandom.seed(42)\ntorch.manual_seed(42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-27T22:38:34.906384Z","iopub.execute_input":"2025-07-27T22:38:34.906611Z","iopub.status.idle":"2025-07-27T22:38:43.636355Z","shell.execute_reply.started":"2025-07-27T22:38:34.906588Z","shell.execute_reply":"2025-07-27T22:38:43.635711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import yaml\nfrom pathlib import Path\n\n# 1. Mount points\nfalcon_mount = Path('/kaggle/input/falcon-multiclass-cheerios-soup/falcon-multiclass-cheerios-soup')\nv2_mount     = Path('/kaggle/input/falcon-multiclass-cheerios-soupv2/falcon-multiclass-cheerios-soupV2')\nchal_mount   = Path('/kaggle/input/multi-class-object-detection-challenge/Starter_Dataset')\n\n# 2. Auto-discover original Falcon root\ntrain_img_dirs = list(falcon_mount.glob('**/train/images'))\nif not train_img_dirs:\n    raise FileNotFoundError(f\"No nested train/images under {falcon_mount}\")\nfalcon_root = train_img_dirs[0].parents[1]  # go up two levels\n\n# 3. Auto-discover V2 root (assumes scenarios at v2_mount/ScenarioX/train/images)\nv2_img_dirs = list(v2_mount.glob('**/Scenario1/train/images'))\nif not v2_img_dirs:\n    raise FileNotFoundError(f\"No Scenario1/train/images under {v2_mount}\")\nv2_root = v2_img_dirs[0].parents[2]\n\n# 4. Build train list\ntrain_dirs = [\n    str(falcon_root/'train'/'images'),\n    str(falcon_root/'val'  /'images')  # if you also want to oversample the original val\n]\nfor i in range(1, 7):\n    train_dirs += [\n        str(v2_root/f\"Scenario{i}\" / 'train' / 'images'),\n        str(v2_root/f\"Scenario{i}\" / 'val'   / 'images')\n    ]\n\n# 5. Use **only** the competition val folder for validation\nval_dirs = [\n    str(chal_mount/'val'/'images')\n]\n\n# 6. Test set\ntest_dir = str(chal_mount/'testImages'/'images')\n\n# 7. Assemble and write YAML\ndata = {\n    'train': train_dirs,\n    'val':   val_dirs,\n    'test':  test_dir,\n    'nc':    2,\n    'names': ['cheerios', 'soup']\n}\n\nwith open('data.yaml', 'w') as f:\n    yaml.safe_dump(data, f, sort_keys=False, default_flow_style=False)\n\nprint(\"✅ data.yaml:\")\nprint(open('data.yaml').read())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-27T22:38:43.636919Z","iopub.execute_input":"2025-07-27T22:38:43.637320Z","iopub.status.idle":"2025-07-27T22:38:45.351905Z","shell.execute_reply.started":"2025-07-27T22:38:43.637303Z","shell.execute_reply":"2025-07-27T22:38:45.351168Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PROJECT_NAME = \"Duality-3LC-Kaggle\"  # Place all 3LC Tables and Runs in the same project\n\n# This for loop allows you to create multiple 3LC Tables (e.g., train and val sets) in one go\nfor split in [\"train\", \"val\"]:\n    table = tlc.Table.from_yolo(\n        dataset_yaml_file=\"data.yaml\",  # the yolo_params.yaml file in the data folder you generate from Falcon\n        split=split,\n        table_name=\"initial\",\n        dataset_name=split,\n        project_name=PROJECT_NAME,\n    )\n\n    print(f\"Created table with URL: {table.url}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-27T22:38:45.352623Z","iopub.execute_input":"2025-07-27T22:38:45.352980Z","iopub.status.idle":"2025-07-27T22:40:12.189075Z","shell.execute_reply.started":"2025-07-27T22:38:45.352943Z","shell.execute_reply":"2025-07-27T22:40:12.188350Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nPROJECT_NAME = \"Duality-3LC-Kaggle\"  # Place all 3LC Tables and Runs in the same project\n\nRUN_NAME = \"run-1\"  # Define the run name to organize all your runs in a nice way\n\n# Set 3LC specific settings\nsettings = Settings(\n    project_name=PROJECT_NAME,\n    run_name=RUN_NAME,\n    run_description=\"description of the run\",\n)\n\n# Update the URLs for the train and val tables when you make data revisions in 3LC Dashboard\ntrain_table = tlc.Table.from_url(\"/root/.local/share/3LC/projects/Duality-3LC-Kaggle/datasets/train/tables/initial\")  # Hint: Copy Table URLs from Dashboard\nval_table = tlc.Table.from_url(\"/root/.local/share/3LC/projects/Duality-3LC-Kaggle/datasets/val/tables/initial\")\n\nmodel = YOLO(\"yolo11x.pt\")\n\n# You may add any YOLO arguments here\nmodel.train(\n    tables={\"train\": train_table, \"val\": val_table},\n    settings=settings,\n    epochs=75,                \n    batch=16,                   \n    imgsz=512,\n    patience=50,               \n    optimizer='SGD',\n    momentum=0.937,          \n    lr0=0.001,                \n    weight_decay=0.0005,       \n    cos_lr=True,               \n    save_period=5,             \n    workers=4,\n    # Augmentations\n    close_mosaic=15,\n    hsv_h=0.015,\n    hsv_s=0.7,\n    hsv_v=0.4,\n    flipud=0.5,\n    fliplr=0.5,\n    translate=0.1,\n    scale=0.5,\n    shear=0.01,\n    agnostic_nms=True,\n    project=PROJECT_NAME,\n    name=RUN_NAME,\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-27T22:40:12.189822Z","iopub.execute_input":"2025-07-27T22:40:12.190097Z","iopub.status.idle":"2025-07-27T23:29:53.157832Z","shell.execute_reply.started":"2025-07-27T22:40:12.190077Z","shell.execute_reply":"2025-07-27T23:29:53.156932Z"}},"outputs":[],"execution_count":null}]}