{"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":"none","dataSources":[{"sourceType":"competition","sourceId":24800,"datasetId":1042002,"databundleVersionId":1831594},{"sourceType":"datasetVersion","sourceId":15161526,"datasetId":9708629,"databundleVersionId":16052758}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-27T09:20:39.410759Z","iopub.execute_input":"2026-02-27T09:20:39.410934Z","iopub.status.idle":"2026-02-27T09:20:59.285416Z","shell.execute_reply.started":"2026-02-27T09:20:39.410915Z","shell.execute_reply":"2026-02-27T09:20:59.283349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**#MILESTONE 2 **\n\n\n","metadata":{}},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T03:16:52.066716Z","iopub.execute_input":"2026-03-13T03:16:52.06708Z","iopub.status.idle":"2026-03-13T03:16:59.575161Z","shell.execute_reply.started":"2026-03-13T03:16:52.067035Z","shell.execute_reply":"2026-03-13T03:16:59.573678Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"> ##MILESTONE 2   ","metadata":{}},{"cell_type":"code","source":"!kaggle datasets init -p /kaggle/working","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:23:38.696942Z","iopub.execute_input":"2026-03-13T05:23:38.697281Z","iopub.status.idle":"2026-03-13T05:23:40.738136Z","shell.execute_reply.started":"2026-03-13T05:23:38.69725Z","shell.execute_reply":"2026-03-13T05:23:40.736995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r cliniscan_dataset.zip vinbig_yolo","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:24:02.00174Z","iopub.execute_input":"2026-03-13T05:24:02.002104Z","iopub.status.idle":"2026-03-13T05:24:02.128564Z","shell.execute_reply.started":"2026-03-13T05:24:02.002068Z","shell.execute_reply":"2026-03-13T05:24:02.126452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.listdir(\"/kaggle/input\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:50:40.49493Z","iopub.execute_input":"2026-03-13T05:50:40.495343Z","iopub.status.idle":"2026-03-13T05:50:40.503504Z","shell.execute_reply.started":"2026-03-13T05:50:40.495311Z","shell.execute_reply":"2026-03-13T05:50:40.502559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.listdir(\"/kaggle/input\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:54:02.914955Z","iopub.execute_input":"2026-03-13T05:54:02.915303Z","iopub.status.idle":"2026-03-13T05:54:02.922002Z","shell.execute_reply.started":"2026-03-13T05:54:02.915262Z","shell.execute_reply":"2026-03-13T05:54:02.921041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.listdir(\"/kaggle/input/datasets\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:55:08.543509Z","iopub.execute_input":"2026-03-13T05:55:08.544324Z","iopub.status.idle":"2026-03-13T05:55:08.553763Z","shell.execute_reply.started":"2026-03-13T05:55:08.544268Z","shell.execute_reply":"2026-03-13T05:55:08.552514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/datasets/kalpana25\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:56:03.704357Z","iopub.execute_input":"2026-03-13T05:56:03.70487Z","iopub.status.idle":"2026-03-13T05:56:03.711728Z","shell.execute_reply.started":"2026-03-13T05:56:03.704835Z","shell.execute_reply":"2026-03-13T05:56:03.710628Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/datasets/kalpana25/milestone-2\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:56:48.895931Z","iopub.execute_input":"2026-03-13T05:56:48.896343Z","iopub.status.idle":"2026-03-13T05:56:48.906034Z","shell.execute_reply.started":"2026-03-13T05:56:48.896306Z","shell.execute_reply":"2026-03-13T05:56:48.904824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!cp -r /kaggle/input/datasets/kalpana25/milestone-2/vinbig_images /kaggle/working/\n!cp -r /kaggle/input/datasets/kalpana25/milestone-2/vinbig_labels /kaggle/working/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T05:57:53.524265Z","iopub.execute_input":"2026-03-13T05:57:53.524667Z","iopub.status.idle":"2026-03-13T05:59:36.617569Z","shell.execute_reply.started":"2026-03-13T05:57:53.524618Z","shell.execute_reply":"2026-03-13T05:59:36.616458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nprint(\"Images:\", len(os.listdir(\"/kaggle/working/vinbig_images\")))\nprint(\"Labels:\", len(os.listdir(\"/kaggle/working/vinbig_labels\")))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:00:37.075623Z","iopub.execute_input":"2026-03-13T06:00:37.076034Z","iopub.status.idle":"2026-03-13T06:00:37.085018Z","shell.execute_reply.started":"2026-03-13T06:00:37.075995Z","shell.execute_reply":"2026-03-13T06:00:37.084016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/working/vinbig_images\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:02:25.242582Z","iopub.execute_input":"2026-03-13T06:02:25.242955Z","iopub.status.idle":"2026-03-13T06:02:25.249641Z","shell.execute_reply.started":"2026-03-13T06:02:25.242925Z","shell.execute_reply":"2026-03-13T06:02:25.248535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/working/vinbig_labels\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:02:39.705091Z","iopub.execute_input":"2026-03-13T06:02:39.705416Z","iopub.status.idle":"2026-03-13T06:02:39.713045Z","shell.execute_reply.started":"2026-03-13T06:02:39.705388Z","shell.execute_reply":"2026-03-13T06:02:39.711775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir(\"/kaggle/working/vinbig_images/kaggle/working/vinbig_yolo/images/train\"))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:07:09.375582Z","iopub.execute_input":"2026-03-13T06:07:09.375917Z","iopub.status.idle":"2026-03-13T06:07:09.393784Z","shell.execute_reply.started":"2026-03-13T06:07:09.375891Z","shell.execute_reply":"2026-03-13T06:07:09.392429Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir(\"/kaggle/working/vinbig_labels/kaggle/working/vinbig_yolo/labels/train\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:07:16.525011Z","iopub.execute_input":"2026-03-13T06:07:16.525341Z","iopub.status.idle":"2026-03-13T06:07:16.536912Z","shell.execute_reply.started":"2026-03-13T06:07:16.525312Z","shell.execute_reply":"2026-03-13T06:07:16.53577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nimage_dir = \"/kaggle/input/datasets/kalpana25/milestone-2/vinbig_images/kaggle/working/vinbig_yolo/images/train\"\nlabel_dir = \"/kaggle/input/datasets/kalpana25/milestone-2/vinbig_labels/kaggle/working/vinbig_yolo/labels/train\"\n\nprint(\"Images:\", len(os.listdir(image_dir)))\nprint(\"Labels:\", len(os.listdir(label_dir)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:08:32.468527Z","iopub.execute_input":"2026-03-13T06:08:32.468852Z","iopub.status.idle":"2026-03-13T06:08:32.490513Z","shell.execute_reply.started":"2026-03-13T06:08:32.468828Z","shell.execute_reply":"2026-03-13T06:08:32.488923Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!cp -r /kaggle/input/datasets/kalpana25/milestone-2/vinbig_images/kaggle/working/vinbig_yolo/images /kaggle/working/\n!cp -r /kaggle/input/datasets/kalpana25/milestone-2/vinbig_labels/kaggle/working/vinbig_yolo/labels /kaggle/working/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:08:51.954544Z","iopub.execute_input":"2026-03-13T06:08:51.955418Z","iopub.status.idle":"2026-03-13T06:09:14.497939Z","shell.execute_reply.started":"2026-03-13T06:08:51.955237Z","shell.execute_reply":"2026-03-13T06:09:14.49648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(os.listdir(\"/kaggle/working/images/train\")))\nprint(len(os.listdir(\"/kaggle/working/labels/train\")))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:12:31.245937Z","iopub.execute_input":"2026-03-13T06:12:31.24635Z","iopub.status.idle":"2026-03-13T06:12:31.276134Z","shell.execute_reply.started":"2026-03-13T06:12:31.246309Z","shell.execute_reply":"2026-03-13T06:12:31.273919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:13:22.064909Z","iopub.execute_input":"2026-03-13T06:13:22.065251Z","iopub.status.idle":"2026-03-13T06:13:30.55855Z","shell.execute_reply.started":"2026-03-13T06:13:22.065225Z","shell.execute_reply":"2026-03-13T06:13:30.557158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T06:13:45.354504Z","iopub.execute_input":"2026-03-13T06:13:45.354846Z","iopub.status.idle":"2026-03-13T06:13:53.667796Z","shell.execute_reply.started":"2026-03-13T06:13:45.354814Z","shell.execute_reply":"2026-03-13T06:13:53.666736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport shutil\n\n# SOURCE PATHS\nsrc_img = \"/kaggle/input/datasets/kalpana25/milestone-2/vinbig_images/kaggle/working/vinbig_yolo/images/train\"\nsrc_lbl = \"/kaggle/input/datasets/kalpana25/milestone-2/vinbig_labels/kaggle/working/vinbig_yolo/labels/train\"\n\n# DESTINATION PATHS\ndst_img = \"/kaggle/working/images_subset/train\"\ndst_lbl = \"/kaggle/working/labels_subset/train\"\n\nos.makedirs(dst_img, exist_ok=True)\nos.makedirs(dst_lbl, exist_ok=True)\n\nimages = os.listdir(src_img)\n\nsubset = random.sample(images, 5000)\n\nfor img in subset:\n    \n    shutil.copy(f\"{src_img}/{img}\", f\"{dst_img}/{img}\")\n\n    label = img.replace(\".jpg\",\".txt\")\n\n    if os.path.exists(f\"{src_lbl}/{label}\"):\n        shutil.copy(f\"{src_lbl}/{label}\", f\"{dst_lbl}/{label}\")\n\nprint(\"Subset created:\", len(os.listdir(dst_img)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T09:38:05.934763Z","iopub.execute_input":"2026-03-13T09:38:05.935499Z","iopub.status.idle":"2026-03-13T09:38:50.324912Z","shell.execute_reply.started":"2026-03-13T09:38:05.935465Z","shell.execute_reply":"2026-03-13T09:38:50.32398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_img = \"/kaggle/working/images_subset/val\"\nval_lbl = \"/kaggle/working/labels_subset/val\"\n\nos.makedirs(val_img, exist_ok=True)\nos.makedirs(val_lbl, exist_ok=True)\n\nsubset_images = os.listdir(dst_img)\n\nval_images = random.sample(subset_images, 1000)\n\nfor img in val_images:\n\n    shutil.move(f\"{dst_img}/{img}\", f\"{val_img}/{img}\")\n\n    label = img.replace(\".jpg\",\".txt\")\n\n    if os.path.exists(f\"{dst_lbl}/{label}\"):\n        shutil.move(f\"{dst_lbl}/{label}\", f\"{val_lbl}/{label}\")\n\nprint(\"Validation set created\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T09:39:08.35547Z","iopub.execute_input":"2026-03-13T09:39:08.355804Z","iopub.status.idle":"2026-03-13T09:39:08.406536Z","shell.execute_reply.started":"2026-03-13T09:39:08.35578Z","shell.execute_reply":"2026-03-13T09:39:08.405542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Train images:\", len(os.listdir(\"/kaggle/working/images_subset/train\")))\nprint(\"Val images:\", len(os.listdir(\"/kaggle/working/images_subset/val\")))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T09:39:27.514383Z","iopub.execute_input":"2026-03-13T09:39:27.515456Z","iopub.status.idle":"2026-03-13T09:39:27.52429Z","shell.execute_reply.started":"2026-03-13T09:39:27.515425Z","shell.execute_reply":"2026-03-13T09:39:27.523501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_yaml = \"\"\"\npath: /kaggle/working\ntrain: images_subset/train\nval: images_subset/val\n\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/data.yaml\",\"w\") as f:\n    f.write(data_yaml)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T09:39:55.534739Z","iopub.execute_input":"2026-03-13T09:39:55.535961Z","iopub.status.idle":"2026-03-13T09:39:55.541605Z","shell.execute_reply.started":"2026-03-13T09:39:55.535915Z","shell.execute_reply":"2026-03-13T09:39:55.540641Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T09:40:34.349998Z","iopub.execute_input":"2026-03-13T09:40:34.350306Z","iopub.status.idle":"2026-03-13T09:40:40.840946Z","shell.execute_reply.started":"2026-03-13T09:40:34.350274Z","shell.execute_reply":"2026-03-13T09:40:40.840021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel = YOLO(\"yolov8n.pt\")\n\nmodel.train(\n    data=\"/kaggle/working/data.yaml\",\n    epochs=8,\n    imgsz=640,\n    batch=8\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T09:40:44.779098Z","iopub.execute_input":"2026-03-13T09:40:44.77993Z","iopub.status.idle":"2026-03-13T13:51:42.301489Z","shell.execute_reply.started":"2026-03-13T09:40:44.779889Z","shell.execute_reply":"2026-03-13T13:51:42.300499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import Image\nImage(\"/kaggle/working/runs/detect/train/results.png\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-13T14:00:54.538525Z","iopub.execute_input":"2026-03-13T14:00:54.539634Z","iopub.status.idle":"2026-03-13T14:00:54.548744Z","shell.execute_reply.started":"2026-03-13T14:00:54.539594Z","shell.execute_reply":"2026-03-13T14:00:54.547968Z"}},"outputs":[],"execution_count":null}]}