{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":6799,"databundleVersionId":4225553,"sourceType":"competition"},{"sourceId":13120281,"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\nimport random","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"INPUT_DIR = \"/kaggle/input\"\nWORKING_DIR = \"/kaggle/working\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T16:08:42.495116Z","iopub.execute_input":"2025-09-20T16:08:42.495488Z","iopub.status.idle":"2025-09-20T16:08:42.499315Z","shell.execute_reply.started":"2025-09-20T16:08:42.495466Z","shell.execute_reply":"2025-09-20T16:08:42.498336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATASET_INPUT_DIR = f\"{INPUT_DIR}/test-tools/dataset\"\nDATASET_WORKING_DIR = f\"{WORKING_DIR}/dataset\"\n\n# 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-09-20T19:03:46.923936Z","iopub.execute_input":"2025-09-20T19:03:46.924381Z","iopub.status.idle":"2025-09-20T19:04:25.847148Z","shell.execute_reply.started":"2025-09-20T19:03:46.924348Z","shell.execute_reply":"2025-09-20T19:04:25.846237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"REPO_DIR = f\"{WORKING_DIR}/repos\"\nif not os.path.isdir(REPO_DIR):\n    os.mkdir(REPO_DIR)\n\nCUT_DIR = f\"{REPO_DIR}/CUT\"\nWCT2_DIR = f\"{REPO_DIR}/WCT2\"\n\nos.chdir(REPO_DIR)\n\nif not os.path.isdir(CUT_DIR):\n    !git clone https://github.com/taesungp/contrastive-unpaired-translation.git CUT\n\nos.chdir(CUT_DIR)\n!pip install -r requirements.txt\n\nos.chdir(REPO_DIR)\n\nif not os.path.isdir(WCT2_DIR):\n    !git clone https://github.com/clovaai/WCT2.git\n\n!pip install dominate","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T16:09:33.535932Z","iopub.execute_input":"2025-09-20T16:09:33.536226Z","iopub.status.idle":"2025-09-20T16:09:54.283781Z","shell.execute_reply.started":"2025-09-20T16:09:33.536206Z","shell.execute_reply":"2025-09-20T16:09:54.282896Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Experiment Configuration","metadata":{}},{"cell_type":"code","source":"SCENARIO = 1\nEXPERIMENT_CONFIG = '1distractors_T0'  # 0baseline, 1distractors_T0, 1distractors_T1, 2background, 3materials, 4lighting, 6wct\nseeds = [1, 10, 42]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T19:13:13.577324Z","iopub.execute_input":"2025-09-20T19:13:13.577672Z","iopub.status.idle":"2025-09-20T19:13:13.581689Z","shell.execute_reply.started":"2025-09-20T19:13:13.577643Z","shell.execute_reply":"2025-09-20T19:13:13.580693Z"}},"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+1} {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+1}/{EXPERIMENT_CONFIG}.yaml\",\n                          epochs=100,\n                          imgsz=640,\n                          project=f\"{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-09-20T19:13:19.791379Z","iopub.execute_input":"2025-09-20T19:13:19.791703Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom ultralytics import YOLO\nimport numpy as np\n\nos.chdir(WORKING_DIR)\n\nall_results = []\n\n# Schleife über alle Seeds\nfor seed in seeds:\n    eval_model = YOLO(f\"{SCENARIO}/{EXPERIMENT_CONFIG}_{seed}/weights/last.pt\")\n    metrics = eval_model.val(\n        data=f\"{DATASET_WORKING_DIR}/scenario{SCENARIO+1}/{EXPERIMENT_CONFIG}.yaml\",\n        split=\"test\",\n        conf=0.001,\n        plots=True,\n        exist_ok=True,\n        project=f\"{SCENARIO}\",\n        name=f\"{EXPERIMENT_CONFIG}_{seed}_test\"\n    )\n\n    names = eval_model.names\n\n    df = pd.DataFrame({\n        \"class\": [names[i] for i in range(len(metrics.box.maps))],\n        \"AP50\": metrics.box.ap50,\n        \"AP\": metrics.box.ap,\n        \"mAP50\": [metrics.box.map50]*len(metrics.box.maps),\n        \"mAP\": [metrics.box.map]*len(metrics.box.maps)\n    })\n\n    all_results.append(df)\n\n# Alle Ergebnisse kombinieren\ncombined = pd.concat(all_results)\n\n# Mittelwert und Std pro Klasse\nsummary = combined.groupby(\"class\").agg(\n    AP50_mean=(\"AP50\", \"mean\"),\n    AP50_std=(\"AP50\", \"std\"),\n    AP_mean=(\"AP\", \"mean\"),\n    AP_std=(\"AP\", \"std\")\n).reset_index()\n\n# Formatieren als \"mean ± std\"\nsummary[\"AP50\"] = summary.apply(lambda row: f\"{row['AP50_mean']:.4f} ± {row['AP50_std']:.4f}\", axis=1)\nsummary[\"AP\"] = summary.apply(lambda row: f\"{row['AP_mean']:.4f} ± {row['AP_std']:.4f}\", axis=1)\n\n# Nur Klasse, AP50, AP\nsummary_latex = summary[[\"class\", \"AP50\", \"AP\"]]\n\n# Gesamt-mAP berechnen über alle Seeds\nmap50_mean = combined[\"mAP50\"].mean()\nmap50_std = combined[\"mAP50\"].std()\nmap_mean = combined[\"mAP\"].mean()\nmap_std = combined[\"mAP\"].std()\n\n# mAP-Zeile erstellen\nmap_row = pd.DataFrame({\n    \"class\": [\"mAP\"],\n    \"AP50\": [f\"{map50_mean:.4f} ± {map50_std:.4f}\"],\n    \"AP\": [f\"{map_mean:.4f} ± {map_std:.4f}\"]\n})\n\n# Tabelle erweitern\nsummary_latex = pd.concat([summary_latex, map_row], ignore_index=True)\n\n# LaTeX-Tabelle schreiben (UTF-8 für ±)\nwith open(f\"{SCENARIO}_{EXPERIMENT_CONFIG}_results_summary.tex\", \"w\", encoding=\"utf-8\") as f:\n    f.write(summary_latex.to_latex(index=False, escape=False))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T18:51:28.529172Z","iopub.execute_input":"2025-09-20T18:51:28.529471Z","iopub.status.idle":"2025-09-20T18:52:00.679032Z","shell.execute_reply.started":"2025-09-20T18:51:28.529451Z","shell.execute_reply":"2025-09-20T18:52:00.677995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model = YOLO(\"/kaggle/working/runs/detect/train36/weights/best.pt\")\n\nimage_dir = Path(f\"{WORKING_DIR}/images/test\")\n\nimage_files = list(image_dir.glob(\"*.jpg\"))\n\nindex = 8\nimage_path = image_files[index]\n\nresults = model(str(image_path))\n\nfor result in results:\n    boxes = result.boxes\n    result.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Domain Adaptation","metadata":{}},{"cell_type":"markdown","source":"## Photorealistic Style Transfer (WCT2)","metadata":{}},{"cell_type":"code","source":"os.chdir(WCT2_DIR)\nif not os.path.exists(\"results\"):\n    os.mkdir(\"results\")\nelse:\n    shutil.rmtree(\"results\")\n    os.mkdir(\"results\")\n    \nif not os.path.exists(\"data\"):\n    os.mkdir(\"data\")\nelse:\n    shutil.rmtree(\"data\")\n    os.mkdir(\"data\")\n\n\nos.chdir(\"data\")\nif not os.path.exists(\"style\"):\n    os.mkdir(\"style\")\nif not os.path.exists(\"content\"):\n    os.mkdir(\"content\")\n\nCONTENT_PATH = f\"{DATASET_WORKING_DIR}/scenario{SCENARIO}/images/0baseline/train\"\nSTYLE_PATH = f\"{DATASET_WORKING_DIR}/da/style/{SCENARIO}\"\n\nrng = random.Random(42)\nstyle_files = [p for p in Path(STYLE_PATH).iterdir() if p.is_file()]\n\nfor path in os.listdir(CONTENT_PATH):\n    if os.path.isdir(path):\n        continue\n\n    content_file_name = Path(path).name\n\n    style_file_name = rng.choice(style_files).name\n\n    shutil.copyfile(f\"{CONTENT_PATH}/{content_file_name}\", f\"{WCT2_DIR}/data/content/{content_file_name}\")\n    shutil.copyfile(f\"{STYLE_PATH}/{style_file_name}\", f\"{WCT2_DIR}/data/style/{content_file_name}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir(WCT2_DIR)\nshutil.rmtree(\"results\")\n!python transfer.py --option_unpool cat5 -s --content ./data/content --style ./data/style --output ./results/ --image_size 640 ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir(WCT2_DIR)\nshutil.make_archive(\"/kaggle/working/wct_images\",\"zip\",\"./results\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir(WCT2_DIR)\nshutil.make_archive(\"/kaggle/working/test\",\"zip\",\"./data/content\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Image-To-Image (CUT)","metadata":{}},{"cell_type":"code","source":"# DATA PREPARATION\n# training and test data must be in one folder divived by domains, subfolders for division must be names trainA & trainB (trainA -> trainB) and testA & testB resspectetvily\n\nos.chdir(CUT_DIR)\nos.chdir(\"datasets\")\n\nif not os.path.isdir(\"images\"):\n    os.mkdir(\"images\")\nelse:\n    shutil.rmtree(\"images\")\n    os.mkdir(\"images\")\n\nos.chdir(\"images\")\nos.mkdir(\"trainB\")\n\nDA_DATASET_DIR = f\"{DATASET_WORKING_DIR}/da\"\n\nos.symlink(f\"{DA_DATASET_DIR}/content\", f\"{CUT_DIR}/datasets/images/trainA\", target_is_directory=True)\nshutil.copytree(f\"{DA_DATASET_DIR}/style/0\", f\"{CUT_DIR}/datasets/images/trainB\", dirs_exist_ok=True)\nshutil.copytree(f\"{DA_DATASET_DIR}/style/1\", f\"{CUT_DIR}/datasets/images/trainB\", dirs_exist_ok=True)\n\n\nos.chdir(f\"{CUT_DIR}/datasets/images\")\n\nos.symlink(f\"{DATASET_WORKING_DIR}/scenario{SCENARIO}/images/0baseline\", f\"{CUT_DIR}/datasets/images/testA\", target_is_directory=True)\nos.symlink(f\"{CUT_DIR}/datasets/images/trainB\", f\"{CUT_DIR}/datasets/images/testB\", target_is_directory=True)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TRAINING\n# to learn how to tranlaste synthetic images to real\n\nos.chdir(CUT_DIR)\n!python train.py \\\n  --dataroot /kaggle/working/repos/CUT/datasets/images \\\n  --name syn2real_fastcut \\\n  --CUT_mode FastCUT \\\n  --n_epochs 200 \\\n  --n_epochs_decay 200 \\\n  --batch_size 16 \\\n  --nce_idt \\\n  --lambda_identity 1.0 \\\n  --lambda_NCE 2.0 \\\n  --nce_layers 0,2,4,6 \\\n  --preprocess scale_shortside_and_crop --load_size 640 --crop_size 160","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TRAINING\n# to translate synthetic images to real\n\nos.chdir(CUT_DIR)\n!python test.py --dataroot ./datasets/images \\\n    --CUT_mode FastCUT \\\n    --phase test \\\n    --name syn2real_fastcut \\\n    --preprocess none\n# --num_test ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.chdir(CUT_DIR)\nshutil.make_archive(\"/kaggle/working/images\",\"zip\",\"./results/syn2real_fastcut/test_latest/images/fake_B/\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Stable Diffusion","metadata":{}},{"cell_type":"code","source":"!pip install -U \"transformers>=4.51\" \"diffusers>=0.35\"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from diffusers import StableDiffusionInpaintPipeline\nfrom diffusers.utils import load_image\n\npipe = StableDiffusionInpaintPipeline.from_pretrained(\n    \"stabilityai/stable-diffusion-2-inpainting\",\n    torch_dtype=torch.float16,\n)\npipe.to(\"cuda\")\nprompt = \"concept art digital painting of an elven castle, inspired by lord of the rings, highly detailed, 8k\"\n#image and mask_image should be PIL images.\n#The mask structure is white for inpainting and black for keeping as is\ninit_image = load_image(\"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint.png\")\nmask_image = load_image(\"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png\")\n\nimage = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]\nimage.save(\"./yellow_cat_on_park_bench.png\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from diffusers import AutoPipelineForInpainting\nfrom diffusers.utils import load_image\nimport torch\n\npipe = AutoPipelineForInpainting.from_pretrained(\"diffusers/stable-diffusion-xl-1.0-inpainting-0.1\", torch_dtype=torch.float16, variant=\"fp16\").to(\"cuda\")\n\nimg_url = \"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint.png\"\nmask_url = \"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png\"\n\nimage = load_image(img_url).resize((1024, 1024))\nmask_image = load_image(mask_url).resize((1024, 1024))\n\nprompt = \"concept art digital painting of an elven castle, inspired by lord of the rings, highly detailed, 8k\"\ngenerator = torch.Generator(device=\"cuda\").manual_seed(0)\n\nimage = pipe(\n  prompt=prompt,\n  image=image,\n  mask_image=mask_image,\n  guidance_scale=8.0,\n  num_inference_steps=20,  # steps between 15 and 30 work well for us\n  strength=0.99,  # make sure to use `strength` below 1.0\n  generator=generator,\n).images[0]\nimage.save(\"./yellow_cat_on_park_bench.png\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from diffusers import StableDiffusionInpaintPipeline\n\npipe = StableDiffusionInpaintPipeline.from_pretrained(\n    \"stable-diffusion-v1-5/stable-diffusion-inpainting\",\n    torch_dtype=torch.float16, variant=\"fp16\"\n).to('cuda')\nprompt = \"concept art digital painting of an elven castle, inspired by lord of the rings, highly detailed, 8k\"\n#image and mask_image should be PIL images.\n#The mask structure is white for inpainting and black for keeping as is\nimage = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]\nimage.save(\"./yellow_cat_on_park_bench.png\")","metadata":{"trusted":true},"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},{"cell_type":"code","source":"import torch, gc\n\ngc.collect()            \ntorch.cuda.empty_cache()\ntorch.cuda.ipc_collect()\nprint(torch.cuda.mem_get_info())","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}