{"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":6799,"databundleVersionId":4225553,"sourceType":"competition"},{"sourceId":13360371,"sourceType":"datasetVersion","datasetId":7258770}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!nvidia-smi","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Dependencies and Configurations","metadata":{}},{"cell_type":"code","source":"import shutil, os\nimport time\nfrom pathlib import Path\nimport pandas as pd\nfrom PIL import Image\n\n%pip install ultralytics albumentations\nimport ultralytics\nultralytics.checks()\nfrom ultralytics import YOLO","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T15:25:12.353301Z","iopub.execute_input":"2025-10-14T15:25:12.354005Z","iopub.status.idle":"2025-10-14T15:25:16.029703Z","shell.execute_reply.started":"2025-10-14T15:25:12.35398Z","shell.execute_reply":"2025-10-14T15:25:16.029042Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"INPUT_DIR = \"/kaggle/input\"\nWORKING_DIR = \"/kaggle/working\"\n\nDATASET_INPUT_DIR = f\"{INPUT_DIR}/test-tools/dataset\"\nDATASET_WORKING_DIR = f\"{WORKING_DIR}/dataset\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T02:37:28.107169Z","iopub.execute_input":"2025-10-13T02:37:28.107907Z","iopub.status.idle":"2025-10-13T02:37:28.111169Z","shell.execute_reply.started":"2025-10-13T02:37:28.107887Z","shell.execute_reply":"2025-10-13T02:37:28.110539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# enables caching for faster training\nif os.path.isdir(DATASET_WORKING_DIR):\n    shutil.rmtree(DATASET_WORKING_DIR)\n\nshutil.copytree(DATASET_INPUT_DIR, DATASET_WORKING_DIR) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T03:40:39.578027Z","iopub.execute_input":"2025-10-13T03:40:39.57879Z","iopub.status.idle":"2025-10-13T03:41:54.552211Z","shell.execute_reply.started":"2025-10-13T03:40:39.578763Z","shell.execute_reply":"2025-10-13T03:41:54.551507Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Experiment Configuration","metadata":{}},{"cell_type":"code","source":"SCENARIO = 0 # 0: tools, 1: hammers\nEXPERIMENT_CONFIG = '2background'  # 0baseline, 0real, 1distractors_T0, 1distractors_T1, 2background, 2background_mat, 3materials, 4lighting, 5mixed, 6wct\nseeds = [1, 10, 42]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T15:37:59.106672Z","iopub.execute_input":"2025-10-14T15:37:59.107407Z","iopub.status.idle":"2025-10-14T15:37:59.111093Z","shell.execute_reply.started":"2025-10-14T15:37:59.107371Z","shell.execute_reply":"2025-10-14T15:37:59.110321Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"os.chdir(WORKING_DIR)\n\nstart_time = time.time()\n\nfor seed in seeds:\n    print(f\"Start training for scenario {SCENARIO} {EXPERIMENT_CONFIG} with seed {seed}\")\n    intermediate_time = time.time()\n\n    model = YOLO(\"yolov8m.pt\")\n    \n    results = model.train(data=f\"{DATASET_WORKING_DIR}/scenario{SCENARIO}/{EXPERIMENT_CONFIG}.yaml\",\n                          epochs=100,\n                          imgsz=640,\n                          project=f\"train/{SCENARIO}\",\n                          name=f\"{EXPERIMENT_CONFIG}_{seed}\",\n                          seed=seed,\n                          freeze=9,\n                          exist_ok=True,\n                          cache=True)\n\n    \n    elapsed_time = time.strftime(\"%H:%M:%S\", time.gmtime(time.time() - intermediate_time))\n    print(f'Training took {elapsed_time}')\n\nelapsed_time = time.strftime(\"%H:%M:%S\", time.gmtime(time.time() - start_time))\nprint(f'Total took {elapsed_time}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T03:42:49.702084Z","iopub.execute_input":"2025-10-13T03:42:49.702419Z","iopub.status.idle":"2025-10-13T03:58:45.004758Z","shell.execute_reply.started":"2025-10-13T03:42:49.702397Z","shell.execute_reply":"2025-10-13T03:58:45.003761Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"os.chdir(WORKING_DIR)\n\nfor seed in seeds:\n    eval_model = YOLO(f\"train/{SCENARIO}/{EXPERIMENT_CONFIG}_{seed}/weights/last.pt\")\n    metrics = eval_model.val(\n        data=f\"{DATASET_WORKING_DIR}/scenario{SCENARIO}/{EXPERIMENT_CONFIG}.yaml\",\n        split=\"test\",\n        imgsz=640,\n        plots=True,\n        exist_ok=True,\n        save_json=True,\n        project=f\"eval/{SCENARIO}\",\n        name=f\"{EXPERIMENT_CONFIG}_{seed}_test\"\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-13T03:59:10.573866Z","iopub.execute_input":"2025-10-13T03:59:10.574362Z","iopub.status.idle":"2025-10-13T03:59:36.240662Z","shell.execute_reply.started":"2025-10-13T03:59:10.574335Z","shell.execute_reply":"2025-10-13T03:59:36.239581Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"seed = 1\ninfer_model = YOLO(f\"{SCENARIO}/{EXPERIMENT_CONFIG}_{seed}/weights/last.pt\")\n\nfile_path = f\"/kaggle/working/dataset/scenario{SCENARIO}/test/images\"\nimage_file = \"42792047161_9cf2029374_z.jpg\"\nimage_file = \"wikimgs_construction794.jpg\"\nimage_path = f\"{file_path}/{image_file}\"\n\nw, h = Image.open(image_path).size\n\nresults = infer_model.predict(image_path,\n                              save=True,\n                              imgsz=640,\n                              project=f\"infer/{SCENARIO}\",\n                              name=f\"{EXPERIMENT_CONFIG}\")\n\nfor result in results:\n    boxes = result.boxes\n    result.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-14T15:44:03.262173Z","iopub.execute_input":"2025-10-14T15:44:03.262476Z","iopub.status.idle":"2025-10-14T15:44:03.69366Z","shell.execute_reply.started":"2025-10-14T15:44:03.262456Z","shell.execute_reply":"2025-10-14T15:44:03.69274Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Clean Up","metadata":{}},{"cell_type":"code","source":"for name in os.listdir(WORKING_DIR):\n    path = os.path.join(WORKING_DIR, name)\n    if os.path.isfile(path) or os.path.islink(path):\n        os.unlink(path)\n    else:\n        shutil.rmtree(path)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}