{"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":[{"sourceType":"competition","sourceId":24800,"datasetId":1042002,"databundleVersionId":1831594}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-03-15T05:59:46.521282Z","iopub.execute_input":"2026-03-15T05:59:46.52157Z","iopub.status.idle":"2026-03-15T06:00:10.021763Z","shell.execute_reply.started":"2026-03-15T05:59:46.521539Z","shell.execute_reply":"2026-03-15T06:00:10.020556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install pydicom\n!pip install opencv-python","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T06:01:43.071368Z","iopub.execute_input":"2026-03-15T06:01:43.071789Z","iopub.status.idle":"2026-03-15T06:01:50.410379Z","shell.execute_reply.started":"2026-03-15T06:01:43.07176Z","shell.execute_reply":"2026-03-15T06:01:50.409655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T06:02:11.216099Z","iopub.execute_input":"2026-03-15T06:02:11.216552Z","iopub.status.idle":"2026-03-15T06:02:12.844642Z","shell.execute_reply.started":"2026-03-15T06:02:11.21652Z","shell.execute_reply":"2026-03-15T06:02:12.844038Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"INPUT_DIR = \"/kaggle/input/competitions/vinbigdata-chest-xray-abnormalities-detection\"\n\nDICOM_DIR = os.path.join(INPUT_DIR, \"train\")\n\nANNOTATION_FILE = os.path.join(INPUT_DIR, \"annotations_train.csv\")\n\nprint(\"Total DICOM images:\", len(os.listdir(DICOM_DIR)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T06:02:50.663155Z","iopub.execute_input":"2026-03-15T06:02:50.663933Z","iopub.status.idle":"2026-03-15T06:02:50.674306Z","shell.execute_reply.started":"2026-03-15T06:02:50.663902Z","shell.execute_reply":"2026-03-15T06:02:50.673579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE_DIR = \"/kaggle/working/dataset\"\n\nIMG_DIR = os.path.join(BASE_DIR, \"images\")\nLBL_DIR = os.path.join(BASE_DIR, \"labels\")\n\nos.makedirs(IMG_DIR, exist_ok=True)\nos.makedirs(LBL_DIR, exist_ok=True)\n\nprint(\"Folders created\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T06:03:06.982688Z","iopub.execute_input":"2026-03-15T06:03:06.983564Z","iopub.status.idle":"2026-03-15T06:03:06.988632Z","shell.execute_reply.started":"2026-03-15T06:03:06.983533Z","shell.execute_reply":"2026-03-15T06:03:06.988006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def process_dicom(path):\n\n    dicom = pydicom.dcmread(path)\n\n    image = dicom.pixel_array\n\n    # Fix inverted X-ray\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        image = np.max(image) - image\n\n    # Normalize\n    image = image - np.min(image)\n    image = image / np.max(image)\n\n    image = (image * 255).astype(np.uint8)\n\n    return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T06:03:16.248072Z","iopub.execute_input":"2026-03-15T06:03:16.248539Z","iopub.status.idle":"2026-03-15T06:03:16.253641Z","shell.execute_reply.started":"2026-03-15T06:03:16.248512Z","shell.execute_reply":"2026-03-15T06:03:16.252673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_SIZE = 1024\n\ndef resize_image(img):\n    \n    return cv2.resize(img,(IMG_SIZE,IMG_SIZE))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T06:03:31.248815Z","iopub.execute_input":"2026-03-15T06:03:31.249991Z","iopub.status.idle":"2026-03-15T06:03:31.253662Z","shell.execute_reply.started":"2026-03-15T06:03:31.249959Z","shell.execute_reply":"2026-03-15T06:03:31.252992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"files = os.listdir(DICOM_DIR)\n\nfor file in tqdm(files):   \n    \n    path = os.path.join(DICOM_DIR,file)\n\n    image = process_dicom(path)\n\n    image = resize_image(image)\n\n    save_path = os.path.join(IMG_DIR,file.replace(\".dicom\",\".png\"))\n\n    cv2.imwrite(save_path,image)\n\nprint(\"Conversion finished\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T06:04:24.464716Z","iopub.execute_input":"2026-03-15T06:04:24.465524Z","iopub.status.idle":"2026-03-15T10:02:40.10127Z","shell.execute_reply.started":"2026-03-15T06:04:24.465491Z","shell.execute_reply":"2026-03-15T10:02:40.100555Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/competitions/vinbigdata-chest-xray-abnormalities-detection/train.csv\")\n\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:06:11.470286Z","iopub.execute_input":"2026-03-15T10:06:11.470694Z","iopub.status.idle":"2026-03-15T10:06:11.654937Z","shell.execute_reply.started":"2026-03-15T10:06:11.470668Z","shell.execute_reply":"2026-03-15T10:06:11.654313Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"classes = df[\"class_name\"].unique()\n\nclass_map = {name:i for i,name in enumerate(classes)}\n\nprint(class_map)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:06:35.558471Z","iopub.execute_input":"2026-03-15T10:06:35.559095Z","iopub.status.idle":"2026-03-15T10:06:35.569869Z","shell.execute_reply.started":"2026-03-15T10:06:35.559065Z","shell.execute_reply":"2026-03-15T10:06:35.569183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def convert_to_yolo(row):\n\n    x_center = (row.x_min + row.x_max) / 2 / IMG_SIZE\n    y_center = (row.y_min + row.y_max) / 2 / IMG_SIZE\n\n    width = (row.x_max - row.x_min) / IMG_SIZE\n    height = (row.y_max - row.y_min) / IMG_SIZE\n\n    return x_center,y_center,width,height","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:06:56.286004Z","iopub.execute_input":"2026-03-15T10:06:56.286578Z","iopub.status.idle":"2026-03-15T10:06:56.29093Z","shell.execute_reply.started":"2026-03-15T10:06:56.286549Z","shell.execute_reply":"2026-03-15T10:06:56.290181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for img_id,group in df.groupby(\"image_id\"):\n\n    label_path = os.path.join(LBL_DIR,img_id+\".txt\")\n\n    with open(label_path,\"w\") as f:\n\n        for _,row in group.iterrows():\n\n            xc,yc,w,h = convert_to_yolo(row)\n\n            class_id = class_map[row[\"class_name\"]]\n\n            f.write(f\"{class_id} {xc} {yc} {w} {h}\\n\")\n\nprint(\"Labels created\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:07:23.975672Z","iopub.execute_input":"2026-03-15T10:07:23.976632Z","iopub.status.idle":"2026-03-15T10:07:31.855254Z","shell.execute_reply.started":"2026-03-15T10:07:23.976601Z","shell.execute_reply":"2026-03-15T10:07:31.854557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = os.listdir(IMG_DIR)\n\ntrain_imgs, val_imgs = train_test_split(images,test_size=0.2)\n\nprint(len(train_imgs),\"train images\")\nprint(len(val_imgs),\"val images\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:08:10.389371Z","iopub.execute_input":"2026-03-15T10:08:10.389783Z","iopub.status.idle":"2026-03-15T10:08:10.409918Z","shell.execute_reply.started":"2026-03-15T10:08:10.389756Z","shell.execute_reply":"2026-03-15T10:08:10.409071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\n\nfor folder in [\"images/train\",\"images/val\",\"labels/train\",\"labels/val\"]:\n    \n    os.makedirs(os.path.join(BASE_DIR,folder),exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:08:31.257841Z","iopub.execute_input":"2026-03-15T10:08:31.258264Z","iopub.status.idle":"2026-03-15T10:08:31.263036Z","shell.execute_reply.started":"2026-03-15T10:08:31.258237Z","shell.execute_reply":"2026-03-15T10:08:31.262183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for img in train_imgs:\n\n    shutil.move(os.path.join(IMG_DIR,img),\n                os.path.join(BASE_DIR,\"images/train\",img))\n    \n    shutil.move(os.path.join(LBL_DIR,img.replace(\".png\",\".txt\")),\n                os.path.join(BASE_DIR,\"labels/train\",img.replace(\".png\",\".txt\")))\n\nfor img in val_imgs:\n\n    shutil.move(os.path.join(IMG_DIR,img),\n                os.path.join(BASE_DIR,\"images/val\",img))\n    \n    shutil.move(os.path.join(LBL_DIR,img.replace(\".png\",\".txt\")),\n                os.path.join(BASE_DIR,\"labels/val\",img.replace(\".png\",\".txt\")))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:08:50.222381Z","iopub.execute_input":"2026-03-15T10:08:50.223046Z","iopub.status.idle":"2026-03-15T10:08:50.922764Z","shell.execute_reply.started":"2026-03-15T10:08:50.223017Z","shell.execute_reply":"2026-03-15T10:08:50.922014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r processed_dataset.zip /kaggle/working/dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-15T10:09:13.105918Z","iopub.execute_input":"2026-03-15T10:09:13.106678Z"}},"outputs":[],"execution_count":null}]}