{"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}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install pydicom opencv-python tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-05T08:57:24.003583Z","iopub.execute_input":"2026-03-05T08:57:24.00387Z","iopub.status.idle":"2026-03-05T08:57:31.112499Z","shell.execute_reply.started":"2026-03-05T08:57:24.003841Z","shell.execute_reply":"2026-03-05T08:57:31.111114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ncsv_path = \"/kaggle/input/competitions/vinbigdata-chest-xray-abnormalities-detection/train.csv\"\ndf = pd.read_csv(csv_path)\n\nprint(\"CSV Loaded Successfully\")\nprint(\"Total rows:\", len(df))\nprint(df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T09:02:48.23806Z","iopub.execute_input":"2026-03-05T09:02:48.23844Z","iopub.status.idle":"2026-03-05T09:02:48.600286Z","shell.execute_reply.started":"2026-03-05T09:02:48.238411Z","shell.execute_reply":"2026-03-05T09:02:48.599112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# Separate abnormal and normal\nabnormal_df = df[df['class_id'] != 14]   # 14 = No finding in VinBigData\nnormal_df = df[df['class_id'] == 14]\n\nabnormal_ids = abnormal_df['image_id'].unique()\nnormal_ids = normal_df['image_id'].unique()\n\nprint(\"Total abnormal images:\", len(abnormal_ids))\nprint(\"Total normal images:\", len(normal_ids))\n\n# Select 3000 abnormal\nselected_abnormal = np.random.choice(abnormal_ids, 3000, replace=False)\n\n# Select 2000 normal\nselected_normal = np.random.choice(normal_ids, 2000, replace=False)\n\nselected_ids = np.concatenate([selected_abnormal, selected_normal])\n\nselected_df = df[df['image_id'].isin(selected_ids)]\n\nselected_df.to_csv(\"train_selected.csv\", index=False)\n\nprint(\"Total selected images:\", len(selected_ids))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T09:03:38.469769Z","iopub.execute_input":"2026-03-05T09:03:38.470154Z","iopub.status.idle":"2026-03-05T09:03:38.700959Z","shell.execute_reply.started":"2026-03-05T09:03:38.470124Z","shell.execute_reply":"2026-03-05T09:03:38.699905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(selected_ids)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T09:03:51.619293Z","iopub.execute_input":"2026-03-05T09:03:51.619668Z","iopub.status.idle":"2026-03-05T09:03:51.628256Z","shell.execute_reply.started":"2026-03-05T09:03:51.61964Z","shell.execute_reply":"2026-03-05T09:03:51.627104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\ntrain_folder = \"/kaggle/input/competitions/vinbigdata-chest-xray-abnormalities-detection/train\"\nprint(os.listdir(train_folder)[:5])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T09:05:18.186047Z","iopub.execute_input":"2026-03-05T09:05:18.186385Z","iopub.status.idle":"2026-03-05T09:05:18.712285Z","shell.execute_reply.started":"2026-03-05T09:05:18.186359Z","shell.execute_reply":"2026-03-05T09:05:18.710892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pydicom\nimport cv2\nfrom tqdm import tqdm\n\n# Create output folder\nos.makedirs(\"images\", exist_ok=True)\n\ndicom_folder = \"/kaggle/input/competitions/vinbigdata-chest-xray-abnormalities-detection/train\"\n\nfor img_id in tqdm(selected_ids):\n    \n    dicom_path = os.path.join(dicom_folder, img_id + \".dicom\")\n    \n    ds = pydicom.dcmread(dicom_path)\n    img = ds.pixel_array\n    \n    # Normalize (VERY IMPORTANT)\n    img = (img - img.min()) / (img.max() - img.min())\n    img = (img * 255).astype(\"uint8\")\n    \n    # Resize to 512x512\n    img = cv2.resize(img, (512, 512))\n    \n    cv2.imwrite(f\"images/{img_id}.png\", img)\n\nprint(\"✅ 5000 Images Converted Successfully\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T09:07:16.642709Z","iopub.execute_input":"2026-03-05T09:07:16.643105Z","iopub.status.idle":"2026-03-05T11:03:47.708687Z","shell.execute_reply.started":"2026-03-05T09:07:16.643075Z","shell.execute_reply":"2026-03-05T11:03:47.704753Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir(\"images\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T11:03:53.285205Z","iopub.execute_input":"2026-03-05T11:03:53.286231Z","iopub.status.idle":"2026-03-05T11:03:53.31473Z","shell.execute_reply.started":"2026-03-05T11:03:53.286148Z","shell.execute_reply":"2026-03-05T11:03:53.313337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport cv2\nimport pydicom\nfrom tqdm import tqdm\n\n# Load filtered CSV\ndf = pd.read_csv(\"train_selected.csv\")\n\nos.makedirs(\"labels\", exist_ok=True)\n\ndicom_folder = \"/kaggle/input/competitions/vinbigdata-chest-xray-abnormalities-detection/train\"\n\nfor img_id in tqdm(df['image_id'].unique()):\n    \n    img_df = df[df['image_id'] == img_id]\n    \n    # Read original DICOM to get original size\n    dicom_path = os.path.join(dicom_folder, img_id + \".dicom\")\n    ds = pydicom.dcmread(dicom_path)\n    orig_h, orig_w = ds.pixel_array.shape\n    \n    label_path = f\"labels/{img_id}.txt\"\n    \n    with open(label_path, \"w\") as f:\n        \n        for _, row in img_df.iterrows():\n            \n            # Skip normal class (class_id = 14)\n            if row['class_id'] == 14:\n                continue\n            \n            x_min = row['x_min']\n            y_min = row['y_min']\n            x_max = row['x_max']\n            y_max = row['y_max']\n            \n            # Scale to 512x512\n            x_min = x_min * (512 / orig_w)\n            x_max = x_max * (512 / orig_w)\n            y_min = y_min * (512 / orig_h)\n            y_max = y_max * (512 / orig_h)\n            \n            # Convert to YOLO format\n            x_center = ((x_min + x_max) / 2) / 512\n            y_center = ((y_min + y_max) / 2) / 512\n            width = (x_max - x_min) / 512\n            height = (y_max - y_min) / 512\n            \n            class_id = int(row['class_id'])\n            \n            f.write(f\"{class_id} {x_center} {y_center} {width} {height}\\n\")\n\nprint(\"✅ YOLO Labels Created Successfully\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T11:04:02.909744Z","iopub.execute_input":"2026-03-05T11:04:02.91025Z","iopub.status.idle":"2026-03-05T12:53:23.902429Z","shell.execute_reply.started":"2026-03-05T11:04:02.910216Z","shell.execute_reply":"2026-03-05T12:53:23.899695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir(\"labels\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T12:53:28.482159Z","iopub.execute_input":"2026-03-05T12:53:28.48332Z","iopub.status.idle":"2026-03-05T12:53:28.506925Z","shell.execute_reply.started":"2026-03-05T12:53:28.483221Z","shell.execute_reply":"2026-03-05T12:53:28.505886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"labels/\" + df['image_id'].iloc[0] + \".txt\") as f:\n    print(f.read())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T12:53:31.648376Z","iopub.execute_input":"2026-03-05T12:53:31.648949Z","iopub.status.idle":"2026-03-05T12:53:31.657825Z","shell.execute_reply.started":"2026-03-05T12:53:31.64883Z","shell.execute_reply":"2026-03-05T12:53:31.656851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = [\n    \"Aortic enlargement\",\n    \"Atelectasis\",\n    \"Calcification\",\n    \"Cardiomegaly\",\n    \"Consolidation\",\n    \"ILD\",\n    \"Infiltration\",\n    \"Lung Opacity\",\n    \"Nodule/Mass\",\n    \"Other lesion\",\n    \"Pleural effusion\",\n    \"Pleural thickening\",\n    \"Pneumothorax\",\n    \"Pulmonary fibrosis\"\n]\n\nwith open(\"dataset.yaml\", \"w\") as f:\n    f.write(\"train: images\\n\")\n    f.write(\"val: images\\n\\n\")\n    f.write(\"nc: 14\\n\")\n    f.write(\"names:\\n\")\n    for name in class_names:\n        f.write(f\"  - {name}\\n\")\n\nprint(\"✅ dataset.yaml created successfully\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T12:53:38.270104Z","iopub.execute_input":"2026-03-05T12:53:38.270459Z","iopub.status.idle":"2026-03-05T12:53:38.28068Z","shell.execute_reply.started":"2026-03-05T12:53:38.270431Z","shell.execute_reply":"2026-03-05T12:53:38.279331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"dataset.yaml\", \"r\") as f:\n    print(f.read())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T12:53:42.256163Z","iopub.execute_input":"2026-03-05T12:53:42.256501Z","iopub.status.idle":"2026-03-05T12:53:42.263603Z","shell.execute_reply.started":"2026-03-05T12:53:42.256473Z","shell.execute_reply":"2026-03-05T12:53:42.262371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport shutil\n\n# Create clean export folder\nos.makedirs(\"Final_Module1_Output\", exist_ok=True)\n\n# Copy only necessary folders/files\nshutil.copytree(\"images\", \"Final_Module1_Output/images\")\nshutil.copytree(\"labels\", \"Final_Module1_Output/labels\")\nshutil.copy(\"dataset.yaml\", \"Final_Module1_Output/dataset.yaml\")\n\n# Optional\nif os.path.exists(\"train_selected.csv\"):\n    shutil.copy(\"train_selected.csv\", \"Final_Module1_Output/train_selected.csv\")\n\n# Now zip only this folder\nshutil.make_archive(\"VinDr_Module1_Output\", 'zip', \"Final_Module1_Output\")\n\nprint(\"✅ Clean ZIP file created successfully\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T13:02:15.987387Z","iopub.execute_input":"2026-03-05T13:02:15.98777Z","iopub.status.idle":"2026-03-05T13:03:09.108931Z","shell.execute_reply.started":"2026-03-05T13:02:15.987738Z","shell.execute_reply":"2026-03-05T13:03:09.107436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}