{"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":"# 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},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install pydicom","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T15:27:27.627089Z","iopub.execute_input":"2026-02-24T15:27:27.627477Z","iopub.status.idle":"2026-02-24T15:27:33.169975Z","shell.execute_reply.started":"2026-02-24T15:27:27.627436Z","shell.execute_reply":"2026-02-24T15:27:33.168849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport pydicom\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T15:27:53.094539Z","iopub.execute_input":"2026-02-24T15:27:53.094898Z","iopub.status.idle":"2026-02-24T15:27:54.595211Z","shell.execute_reply.started":"2026-02-24T15:27:53.094844Z","shell.execute_reply":"2026-02-24T15:27:54.594288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torchvision\n\nprint(torch.__version__)\nprint(torchvision.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T15:28:38.264547Z","iopub.execute_input":"2026-02-24T15:28:38.265736Z","iopub.status.idle":"2026-02-24T15:28:45.297076Z","shell.execute_reply.started":"2026-02-24T15:28:38.265697Z","shell.execute_reply":"2026-02-24T15:28:45.295953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\ntrain_path = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train\"\n\nprint(os.listdir(train_path)[:5])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T15:30:35.464364Z","iopub.execute_input":"2026-02-24T15:30:35.464955Z","iopub.status.idle":"2026-02-24T15:30:35.838728Z","shell.execute_reply.started":"2026-02-24T15:30:35.464922Z","shell.execute_reply":"2026-02-24T15:30:35.837983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\ntrain_path = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train\"\n\nfiles = os.listdir(train_path)\n\nprint(\"Total files in train folder:\", len(files))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T15:32:11.344041Z","iopub.execute_input":"2026-02-24T15:32:11.344436Z","iopub.status.idle":"2026-02-24T15:32:11.363065Z","shell.execute_reply.started":"2026-02-24T15:32:11.344405Z","shell.execute_reply":"2026-02-24T15:32:11.361855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pydicom\n\ndicom_path = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/000434271f63a053c4128a0ba6352c7f.dicom\"\n\ndicom = pydicom.dcmread(dicom_path)\nimage = dicom.pixel_array\n\n# Fix MONOCHROME1 if needed\nif dicom.PhotometricInterpretation == \"MONOCHROME1\":\n    image = np.max(image) - image\n\n# Normalize\nimage = image - np.min(image)\nimage = image / np.max(image)\nimage = (image * 255).astype(np.uint8)\n\n# 🔥 Resize to 1024x1024\nimage_resized = cv2.resize(image, (640, 640))\n\n# Save as PNG\ncv2.imwrite(\"/kaggle/working/sample_1024.png\", image_resized)\n\nprint(\"Image resized and saved successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:31:45.082724Z","iopub.execute_input":"2026-02-24T17:31:45.083095Z","iopub.status.idle":"2026-02-24T17:31:45.445117Z","shell.execute_reply.started":"2026-02-24T17:31:45.083065Z","shell.execute_reply":"2026-02-24T17:31:45.443833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nprint(os.listdir(\"/kaggle/working\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:35:45.081716Z","iopub.execute_input":"2026-02-24T17:35:45.082018Z","iopub.status.idle":"2026-02-24T17:35:45.088455Z","shell.execute_reply.started":"2026-02-24T17:35:45.081992Z","shell.execute_reply":"2026-02-24T17:35:45.087213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\n\nimg = Image.open(\"/kaggle/working/sample_1024.png\")\nw, h = img.size\nprint(w, h)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:36:45.240075Z","iopub.execute_input":"2026-02-24T17:36:45.240313Z","iopub.status.idle":"2026-02-24T17:36:45.286955Z","shell.execute_reply.started":"2026-02-24T17:36:45.240295Z","shell.execute_reply":"2026-02-24T17:36:45.284614Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"yolo_annotations = []\n\nfor _, row in records.iterrows():\n    \n    # Convert to YOLO normalized format\n    x_center = ((row.x_min + row.x_max) / 2) / w\n    y_center = ((row.y_min + row.y_max) / 2) / h\n    width = (row.x_max - row.x_min) / w\n    height = (row.y_max - row.y_min) / h\n    \n    # Create YOLO line\n    yolo_line = f\"{int(row.class_id)} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\"\n    \n    yolo_annotations.append(yolo_line)\n\nyolo_annotations","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:37:29.709936Z","iopub.execute_input":"2026-02-24T17:37:29.710208Z","iopub.status.idle":"2026-02-24T17:37:29.723997Z","shell.execute_reply.started":"2026-02-24T17:37:29.71019Z","shell.execute_reply":"2026-02-24T17:37:29.721745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_path = \"/kaggle/working/sample_1024.txt\"\n\nwith open(label_path, \"w\") as f:\n    for line in yolo_annotations:\n        f.write(line + \"\\n\")\n\nprint(\"YOLO label file saved at:\", label_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:37:49.122967Z","iopub.execute_input":"2026-02-24T17:37:49.12323Z","iopub.status.idle":"2026-02-24T17:37:49.129744Z","shell.execute_reply.started":"2026-02-24T17:37:49.123211Z","shell.execute_reply":"2026-02-24T17:37:49.128973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"yolo_annotations = []\n\nfor _, row in records.iterrows():\n    \n    # Skip \"No Finding\"\n    if int(row.class_id) == 14:\n        continue\n    \n    x_center = ((row.x_min + row.x_max) / 2) / w\n    y_center = ((row.y_min + row.y_max) / 2) / h\n    width = (row.x_max - row.x_min) / w\n    height = (row.y_max - row.y_min) / h\n    \n    yolo_line = f\"{int(row.class_id)} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\"\n    yolo_annotations.append(yolo_line)\n\nyolo_annotations","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:40:13.193971Z","iopub.execute_input":"2026-02-24T17:40:13.194238Z","iopub.status.idle":"2026-02-24T17:40:13.203817Z","shell.execute_reply.started":"2026-02-24T17:40:13.19422Z","shell.execute_reply":"2026-02-24T17:40:13.201637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"/kaggle/working/sample_1024.txt\", \"w\") as f:\n    for line in yolo_annotations:\n        f.write(line + \"\\n\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-24T17:40:51.586488Z","iopub.execute_input":"2026-02-24T17:40:51.588154Z","iopub.status.idle":"2026-02-24T17:40:51.59614Z","shell.execute_reply.started":"2026-02-24T17:40:51.588086Z","shell.execute_reply":"2026-02-24T17:40:51.595359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# Kaggle dataset path\nBASE_PATH = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection\"\nTRAIN_DICOM_PATH = os.path.join(BASE_PATH, \"train\")\nCSV_PATH = os.path.join(BASE_PATH, \"train.csv\")\n\n# Working directory\nWORK_PATH = \"/kaggle/working/CliniScan\"\nIMAGE_SAVE_PATH = os.path.join(WORK_PATH, \"images\")\nLABEL_SAVE_PATH = os.path.join(WORK_PATH, \"labels\")\n\nos.makedirs(IMAGE_SAVE_PATH, exist_ok=True)\nos.makedirs(LABEL_SAVE_PATH, exist_ok=True)\n\nprint(\"Working directory created.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-02T16:38:16.566732Z","iopub.execute_input":"2026-03-02T16:38:16.567082Z","iopub.status.idle":"2026-03-02T16:38:16.575936Z","shell.execute_reply.started":"2026-03-02T16:38:16.567053Z","shell.execute_reply":"2026-03-02T16:38:16.574817Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**DICOM TO JPG CONVERSION**","metadata":{}},{"cell_type":"code","source":"import pydicom\nimport cv2\nimport numpy as np\nfrom tqdm import tqdm\n\nimage_ids = [f.replace(\".dicom\", \"\") for f in os.listdir(TRAIN_DICOM_PATH)]\n\nfor image_id in tqdm(image_ids):\n    \n    dicom_path = os.path.join(TRAIN_DICOM_PATH, image_id + \".dicom\")\n    save_path = os.path.join(IMAGE_SAVE_PATH, image_id + \".jpg\")\n    \n    try:\n        dicom = pydicom.dcmread(dicom_path)\n        image = dicom.pixel_array.astype(np.float32)\n         # Normalize to 0–255\n        image = (image - image.min()) / (image.max() - image.min())\n        image = (image * 255).astype(np.uint8)\n        \n        # Resize to 1024×1024\n        image = cv2.resize(image, (1024, 1024))\n        \n        # Save as JPG\n        cv2.imwrite(save_path, image, [cv2.IMWRITE_JPEG_QUALITY, 95])\n        \n    except Exception as e:\n        print(\"Error:\", image_id)\n\nprint(\"DICOM → JPG conversion completed.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-02T16:39:01.679744Z","iopub.execute_input":"2026-03-02T16:39:01.680592Z","iopub.status.idle":"2026-03-02T21:52:32.318948Z","shell.execute_reply.started":"2026-03-02T16:39:01.680559Z","shell.execute_reply":"2026-03-02T21:52:32.31609Z"}},"outputs":[],"execution_count":null}]}