{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.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":128792,"databundleVersionId":15494745,"isSourceIdPinned":false,"sourceType":"competition"}],"dockerImageVersionId":31260,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nos.listdir(\"/kaggle/input\")\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:32:41.198168Z","iopub.execute_input":"2026-01-29T08:32:41.198532Z","iopub.status.idle":"2026-01-29T08:32:41.205266Z","shell.execute_reply.started":"2026-01-29T08:32:41.198507Z","shell.execute_reply":"2026-01-29T08:32:41.204311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nos.listdir(\"/kaggle/input/vista26/Vistas Dataset Public\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:33:58.054071Z","iopub.execute_input":"2026-01-29T08:33:58.054411Z","iopub.status.idle":"2026-01-29T08:33:58.066904Z","shell.execute_reply.started":"2026-01-29T08:33:58.054386Z","shell.execute_reply":"2026-01-29T08:33:58.065966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/vista26/Vistas Dataset Public/Vistas Dataset Public\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:36:04.350664Z","iopub.execute_input":"2026-01-29T08:36:04.351596Z","iopub.status.idle":"2026-01-29T08:36:04.362346Z","shell.execute_reply.started":"2026-01-29T08:36:04.35156Z","shell.execute_reply":"2026-01-29T08:36:04.361253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ultralytics\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:41:24.273997Z","iopub.execute_input":"2026-01-29T08:41:24.274267Z","iopub.status.idle":"2026-01-29T08:41:29.938297Z","shell.execute_reply.started":"2026-01-29T08:41:24.274237Z","shell.execute_reply":"2026-01-29T08:41:29.937434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\n\njson_path = \"/kaggle/input/vista26/Vistas Dataset Public/Vistas Dataset Public/instances_train.json\"\n\nwith open(json_path, \"r\") as f:\n    data = json.load(f)\n\nprint(data.keys())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:46:12.818204Z","iopub.execute_input":"2026-01-29T08:46:12.818553Z","iopub.status.idle":"2026-01-29T08:46:13.158168Z","shell.execute_reply.started":"2026-01-29T08:46:12.818513Z","shell.execute_reply":"2026-01-29T08:46:13.157431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE = \"/kaggle/input/vista26/Vistas Dataset Public/Vistas Dataset Public\"\n\nTRAIN_JSON = f\"{BASE}/instances_train.json\"\nVAL_JSON   = f\"{BASE}/instances_val.json\"\nCAT_JSON   = f\"{BASE}/Categories.json\"\n\nTRAIN_IMG = f\"{BASE}/train\"\nVAL_IMG   = f\"{BASE}/validation\"\n\nOUT = \"/kaggle/working/yolo_data\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:49:12.686959Z","iopub.execute_input":"2026-01-29T08:49:12.687273Z","iopub.status.idle":"2026-01-29T08:49:12.691216Z","shell.execute_reply.started":"2026-01-29T08:49:12.687242Z","shell.execute_reply":"2026-01-29T08:49:12.690599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nfor split in [\"train\", \"val\"]:\n    os.makedirs(f\"{OUT}/images/{split}\", exist_ok=True)\n    os.makedirs(f\"{OUT}/labels/{split}\", exist_ok=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:49:46.373246Z","iopub.execute_input":"2026-01-29T08:49:46.373875Z","iopub.status.idle":"2026-01-29T08:49:46.377994Z","shell.execute_reply.started":"2026-01-29T08:49:46.373839Z","shell.execute_reply":"2026-01-29T08:49:46.377286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\n\nwith open(CAT_JSON) as f:\n    categories = json.load(f)\n\nprint(type(categories))\nprint(categories)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:52:36.568402Z","iopub.execute_input":"2026-01-29T08:52:36.568737Z","iopub.status.idle":"2026-01-29T08:52:36.576904Z","shell.execute_reply.started":"2026-01-29T08:52:36.568708Z","shell.execute_reply":"2026-01-29T08:52:36.576286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\n\nwith open(CAT_JSON) as f:\n    cat_data = json.load(f)\n\ncategories = cat_data[\"categories\"]\n\n# Map original category IDs to YOLO class IDs (0-based)\ncat_id_to_class = {\n    cat[\"id\"]: idx for idx, cat in enumerate(categories)\n}\n\n# Class names in YOLO order\nclass_names = [cat[\"name\"] for cat in categories]\n\nprint(\"Number of classes:\", len(class_names))\nprint(\"First 10 classes:\", class_names[:10])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:54:12.482975Z","iopub.execute_input":"2026-01-29T08:54:12.483707Z","iopub.status.idle":"2026-01-29T08:54:12.491529Z","shell.execute_reply.started":"2026-01-29T08:54:12.483674Z","shell.execute_reply":"2026-01-29T08:54:12.49084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import json\nimport os\nimport shutil\n\ndef convert(json_file, img_dir, split):\n    with open(json_file) as f:\n        data = json.load(f)\n\n    # Map image_id → image info\n    images = {img[\"id\"]: img for img in data[\"images\"]}\n\n    # Group annotations by image_id\n    anns_by_img = {}\n    for ann in data[\"annotations\"]:\n        anns_by_img.setdefault(ann[\"image_id\"], []).append(ann)\n\n    for img_id, img in images.items():\n        img_name = img[\"file_name\"]\n        w, h = img[\"width\"], img[\"height\"]\n\n        src_img = f\"{img_dir}/{img_name}\"\n        dst_img = f\"{OUT}/images/{split}/{img_name}\"\n\n        if not os.path.exists(src_img):\n            continue\n\n        shutil.copy(src_img, dst_img)\n\n        label_path = f\"{OUT}/labels/{split}/{img_name.rsplit('.',1)[0]}.txt\"\n\n        with open(label_path, \"w\") as lf:\n            for ann in anns_by_img.get(img_id, []):\n                cls = cat_id_to_class[ann[\"category_id\"]]\n                x, y, bw, bh = ann[\"bbox\"]\n\n                xc = (x + bw / 2) / w\n                yc = (y + bh / 2) / h\n                bw /= w\n                bh /= h\n\n                lf.write(f\"{cls} {xc:.6f} {yc:.6f} {bw:.6f} {bh:.6f}\\n\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T08:57:20.213979Z","iopub.execute_input":"2026-01-29T08:57:20.214277Z","iopub.status.idle":"2026-01-29T08:57:20.221247Z","shell.execute_reply.started":"2026-01-29T08:57:20.214219Z","shell.execute_reply":"2026-01-29T08:57:20.220714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import yaml\n\ndata_yaml = {\n    'train': f'{OUT}/images/train',\n    'val': f'{OUT}/images/val',\n    'nc': len(class_names),\n    'names': class_names\n}\n\nwith open(f'{OUT}/data.yaml', 'w') as f:\n    yaml.dump(data_yaml, f)\n\nprint(\"data.yaml created at:\", f\"{OUT}/data.yaml\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T09:18:25.651214Z","iopub.execute_input":"2026-01-29T09:18:25.651805Z","iopub.status.idle":"2026-01-29T09:18:25.661819Z","shell.execute_reply.started":"2026-01-29T09:18:25.651772Z","shell.execute_reply":"2026-01-29T09:18:25.661199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\nimport os\n\n# Remove previous working folder if exists\nif os.path.exists(\"/kaggle/working/yolo_vista26\"):\n    shutil.rmtree(\"/kaggle/working/yolo_vista26\")\n\n# Remove any old label caches\nlabel_cache_dirs = [f\"/kaggle/working/yolo_data/labels/train\",\n                    f\"/kaggle/working/yolo_data/labels/val\"]\n\nfor d in label_cache_dirs:\n    cache_files = [f for f in os.listdir(d) if f.endswith(\".cache\")]\n    for f in cache_files:\n        os.remove(os.path.join(d, f))\n\nprint(\"Cleaned old caches and previous runs.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T09:49:08.728102Z","iopub.execute_input":"2026-01-29T09:49:08.728905Z","iopub.status.idle":"2026-01-29T09:49:08.777324Z","shell.execute_reply.started":"2026-01-29T09:49:08.728873Z","shell.execute_reply":"2026-01-29T09:49:08.776778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"OUT = \"/kaggle/working/yolo_data\"\n\nos.makedirs(f\"{OUT}/images/train\", exist_ok=True)\nos.makedirs(f\"{OUT}/images/val\", exist_ok=True)\nos.makedirs(f\"{OUT}/labels/train\", exist_ok=True)\nos.makedirs(f\"{OUT}/labels/val\", exist_ok=True)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T09:49:56.208739Z","iopub.execute_input":"2026-01-29T09:49:56.209317Z","iopub.status.idle":"2026-01-29T09:49:56.21358Z","shell.execute_reply.started":"2026-01-29T09:49:56.20929Z","shell.execute_reply":"2026-01-29T09:49:56.21299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Train images:\", len(os.listdir(f\"{OUT}/images/train\")))\nprint(\"Train labels:\", len(os.listdir(f\"{OUT}/labels/train\")))\nprint(\"Val images:\", len(os.listdir(f\"{OUT}/images/val\")))\nprint(\"Val labels:\", len(os.listdir(f\"{OUT}/labels/val\")))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T09:50:14.393548Z","iopub.execute_input":"2026-01-29T09:50:14.394091Z","iopub.status.idle":"2026-01-29T09:50:14.471537Z","shell.execute_reply.started":"2026-01-29T09:50:14.394064Z","shell.execute_reply":"2026-01-29T09:50:14.470731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel = YOLO('yolov8n.pt')  # nano model for Kaggle\n\nmodel.train(\n    data=f'{OUT}/data.yaml',\n    epochs=50,\n    imgsz=640,\n    batch=8,\n    device=0,\n    project='yolo_vista26',\n    name='run1',\n    exist_ok=True,\n    cache=False   # important to avoid cache issues\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-29T09:50:44.678272Z","iopub.execute_input":"2026-01-29T09:50:44.67909Z","iopub.status.idle":"2026-01-29T09:50:45.660884Z","shell.execute_reply.started":"2026-01-29T09:50:44.679057Z","shell.execute_reply":"2026-01-29T09:50:45.659776Z"}},"outputs":[],"execution_count":null}]}