{"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}],"dockerImageVersionId":31287,"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-11T10:39:55.921478Z","iopub.execute_input":"2026-03-11T10:39:55.922376Z","iopub.status.idle":"2026-03-11T10:40:24.007878Z","shell.execute_reply.started":"2026-03-11T10:39:55.922319Z","shell.execute_reply":"2026-03-11T10:40:24.00675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATASET_PATH = \"/kaggle/input/competitions/vinbigdata-chest-xray-abnormalities-detection\"\n\nTRAIN_DIR = DATASET_PATH + \"/train\"\nANNOTATION_FILE = DATASET_PATH + \"/train.csv\"\n\nOUTPUT_DIR = \"/kaggle/working/dataset\"\n\nIMAGE_DIR = os.path.join(OUTPUT_DIR,\"images/train\")\nLABEL_DIR = os.path.join(OUTPUT_DIR,\"labels/train\")\n\nos.makedirs(IMAGE_DIR,exist_ok=True)\nos.makedirs(LABEL_DIR,exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T10:40:24.009692Z","iopub.execute_input":"2026-03-11T10:40:24.010159Z","iopub.status.idle":"2026-03-11T10:40:24.015278Z","shell.execute_reply.started":"2026-03-11T10:40:24.010134Z","shell.execute_reply":"2026-03-11T10:40:24.014446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport pydicom\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T10:40:24.016316Z","iopub.execute_input":"2026-03-11T10:40:24.016612Z","iopub.status.idle":"2026-03-11T10:40:24.89569Z","shell.execute_reply.started":"2026-03-11T10:40:24.016589Z","shell.execute_reply":"2026-03-11T10:40:24.895118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def process_dicom(path):\n\n    dicom = pydicom.dcmread(path)\n\n    image = apply_voi_lut(dicom.pixel_array, dicom)\n\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        image = np.max(image) - image\n\n    image = image - np.min(image)\n    image = image / np.max(image)\n    image = (image * 255).astype(np.uint8)\n\n    image = cv2.resize(image,(1024,1024))\n\n    return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T10:40:24.897164Z","iopub.execute_input":"2026-03-11T10:40:24.897651Z","iopub.status.idle":"2026-03-11T10:40:24.902063Z","shell.execute_reply.started":"2026-03-11T10:40:24.897629Z","shell.execute_reply":"2026-03-11T10:40:24.901251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"files = os.listdir(TRAIN_DIR)\n\nprint(\"Total images:\",len(files))\n\nfor file in tqdm(files):\n\n    dicom_path = os.path.join(TRAIN_DIR,file)\n\n    img = process_dicom(dicom_path)\n\n    save_path = os.path.join(IMAGE_DIR,file.replace(\".dicom\",\".png\"))\n\n    cv2.imwrite(save_path,img)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T10:40:24.902929Z","iopub.execute_input":"2026-03-11T10:40:24.903153Z","iopub.status.idle":"2026-03-11T14:58:20.947535Z","shell.execute_reply.started":"2026-03-11T10:40:24.903134Z","shell.execute_reply":"2026-03-11T14:58:20.946739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(ANNOTATION_FILE)\n\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T14:58:20.948663Z","iopub.execute_input":"2026-03-11T14:58:20.949299Z","iopub.status.idle":"2026-03-11T14:58:21.092752Z","shell.execute_reply.started":"2026-03-11T14:58:20.949265Z","shell.execute_reply":"2026-03-11T14:58:21.091935Z"}},"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-11T14:58:21.093797Z","iopub.execute_input":"2026-03-11T14:58:21.094223Z","iopub.status.idle":"2026-03-11T14:58:21.105117Z","shell.execute_reply.started":"2026-03-11T14:58:21.094198Z","shell.execute_reply":"2026-03-11T14:58:21.104162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def convert_to_yolo(size, box):\n\n    dw = 1./size[0]\n    dh = 1./size[1]\n\n    x = (box[0] + box[2]) / 2\n    y = (box[1] + box[3]) / 2\n    w = box[2] - box[0]\n    h = box[3] - box[1]\n\n    x *= dw\n    w *= dw\n    y *= dh\n    h *= dh\n\n    return (x,y,w,h)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T14:58:21.106216Z","iopub.execute_input":"2026-03-11T14:58:21.106551Z","iopub.status.idle":"2026-03-11T14:58:21.116669Z","shell.execute_reply.started":"2026-03-11T14:58:21.106515Z","shell.execute_reply":"2026-03-11T14:58:21.115996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for image_id, group in tqdm(df.groupby(\"image_id\")):\n\n    label_path = os.path.join(LABEL_DIR,image_id+\".txt\")\n\n    with open(label_path,\"w\") as f:\n\n        for _,row in group.iterrows():\n\n            cls = class_map[row[\"class_name\"]]\n\n            box=[row.x_min,row.y_min,row.x_max,row.y_max]\n\n            x,y,w,h = convert_to_yolo((1024,1024),box)\n\n            f.write(f\"{cls} {x} {y} {w} {h}\\n\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T14:58:21.117746Z","iopub.execute_input":"2026-03-11T14:58:21.118046Z","iopub.status.idle":"2026-03-11T14:58:28.295225Z","shell.execute_reply.started":"2026-03-11T14:58:21.118017Z","shell.execute_reply":"2026-03-11T14:58:28.294426Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Images:\",len(os.listdir(IMAGE_DIR)))\nprint(\"Labels:\",len(os.listdir(LABEL_DIR)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-11T14:58:28.297169Z","iopub.execute_input":"2026-03-11T14:58:28.297482Z","iopub.status.idle":"2026-03-11T14:58:28.319478Z","shell.execute_reply.started":"2026-03-11T14:58:28.297458Z","shell.execute_reply":"2026-03-11T14:58:28.318877Z"}},"outputs":[],"execution_count":null}]}