{"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":24800,"databundleVersionId":1831594,"sourceType":"competition"},{"sourceId":254955799,"sourceType":"kernelVersion"},{"sourceId":255339881,"sourceType":"kernelVersion"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os, random\n\nimages_input_dir = \"/kaggle/input/xray-chest-jpg-conversion/train_jpg\"\nlabels_input_dir = \"/kaggle/input/chest-xray-yolo-dataset/labels\"\n\njpg_files = sorted([f for f in os.listdir(images_input_dir) if f.lower().endswith(\".jpg\")])\nids = [os.path.splitext(f)[0] for f in jpg_files if os.path.exists(os.path.join(labels_input_dir, f\"{os.path.splitext(f)[0]}.txt\"))]\n\nrandom.seed(42)\nrandom.shuffle(ids)\n\nsplit = int(len(ids) * 0.85)\ntrain_ids, val_ids = ids[:split], ids[split:]\n\nos.makedirs(\"/kaggle/working/splits\", exist_ok=True)\n\nwith open(\"/kaggle/working/splits/train.txt\", \"w\") as f:\n    for i in train_ids:\n        f.write(f\"{os.path.join(images_input_dir, i+'.jpg')}\\n\")\n\nwith open(\"/kaggle/working/splits/val.txt\", \"w\") as f:\n    for i in val_ids:\n        f.write(f\"{os.path.join(images_input_dir, i+'.jpg')}\\n\")\n\nprint(\"Train:\", len(train_ids), \"Val:\", len(val_ids))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"yaml_content = \"\"\"path: /kaggle/working/yolo_symlink\ntrain: images/train\nval: images/val\nnames:\n  0: Aortic enlargement\n  1: Atelectasis\n  2: Calcification\n  3: Cardiomegaly\n  4: Consolidation\n  5: ILD\n  6: Infiltration\n  7: Lung Opacity\n  8: Nodule/Mass\n  9: Other lesion\n  10: Pleural effusion\n  11: Pleural thickening\n  12: Pneumothorax\n  13: Pulmonary fibrosis\n\"\"\"\n\nwith open(\"/kaggle/working/vinbig_yolo.yaml\", \"w\") as f:\n    f.write(yaml_content)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os, errno\nfrom tqdm import tqdm\n\nimages_input_dir = \"/kaggle/input/xray-chest-jpg-conversion/train_jpg\"\nlabels_input_dir = \"/kaggle/input/chest-xray-yolo-dataset/labels\"\n\n# önceki split’leri kullanıyoruz\ntrain_list = [l.strip() for l in open(\"/kaggle/working/splits/train.txt\").read().splitlines()]\nval_list   = [l.strip() for l in open(\"/kaggle/working/splits/val.txt\").read().splitlines()]\n\nroot = \"/kaggle/working/yolo_symlink\"\npaths = {\n    \"images/train\": os.path.join(root, \"images/train\"),\n    \"images/val\":   os.path.join(root, \"images/val\"),\n    \"labels/train\": os.path.join(root, \"labels/train\"),\n    \"labels/val\":   os.path.join(root, \"labels/val\"),\n}\nfor p in paths.values():\n    os.makedirs(p, exist_ok=True)\n\ndef safe_symlink(src, dst):\n    try:\n        if os.path.islink(dst) or os.path.exists(dst):\n            os.remove(dst)\n        os.symlink(src, dst)\n    except OSError as e:\n        if e.errno != errno.EEXIST:\n            raise e\n\ndef link_split(img_paths, split):\n    for img_path in tqdm(img_paths, desc=f\"link {split}\"):\n        base = os.path.splitext(os.path.basename(img_path))[0]\n        lbl_path = os.path.join(labels_input_dir, base + \".txt\")\n        if not os.path.exists(lbl_path):\n            continue\n        safe_symlink(img_path, os.path.join(paths[f\"images/{split}\"], base + \".jpg\"))\n        safe_symlink(lbl_path, os.path.join(paths[f\"labels/{split}\"], base + \".txt\"))\n\nlink_split(train_list, \"train\")\nlink_split(val_list, \"val\")\n\nprint(\"Symlink dataset root:\", root)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ultralytics --quiet","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel = YOLO(\"yolov8s.pt\")\n\nmodel.train(\n    data=\"/kaggle/working/vinbig_yolo.yaml\",\n    epochs=200,          # üst sınır\n    patience=15,         # early stopping\n    imgsz=640,\n    batch=16,\n    workers=2,\n    device=0,\n    seed=42,\n    cache=True,          # hızlı okuma (Kaggle için iyi)\n    project=\"runs\", name=\"vinbig_yolov8s\",\n    save=True,\n    plots=True,\n    cos_lr=True,\n    close_mosaic=10,\n    hsv_h=0.0, hsv_s=0.0, hsv_v=0.0  # X-ray: renk augment kapalı\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}