{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31089,"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":"2025-08-24T02:10:09.992715Z","iopub.execute_input":"2025-08-24T02:10:09.992895Z","iopub.status.idle":"2025-08-24T02:10:39.621052Z","shell.execute_reply.started":"2025-08-24T02:10:09.992879Z","shell.execute_reply":"2025-08-24T02:10:39.619833Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Opening dicom images through pydicom","metadata":{}},{"cell_type":"code","source":"import pydicom\nds = pydicom.dcmread(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/0005e8e3701dfb1dd93d53e2ff537b6e.dicom\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:03:53.747428Z","iopub.execute_input":"2025-08-24T03:03:53.748241Z","iopub.status.idle":"2025-08-24T03:03:54.46695Z","shell.execute_reply.started":"2025-08-24T03:03:53.748211Z","shell.execute_reply":"2025-08-24T03:03:54.466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom PIL import Image\nplt.imshow(ds.pixel_array, cmap = plt.cm.gray)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:47:58.997705Z","iopub.execute_input":"2025-08-24T03:47:58.998308Z","iopub.status.idle":"2025-08-24T03:47:59.708579Z","shell.execute_reply.started":"2025-08-24T03:47:58.998284Z","shell.execute_reply":"2025-08-24T03:47:59.707755Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### YOLO cannot be trained on dicom images so converting Dicom to jpg and comparing the normalization methods","metadata":{}},{"cell_type":"code","source":"def dicom_conversion(dicom_path):\n    # Load DICOM\n    ds = pydicom.dcmread(dicom_path)\n    img = ds.pixel_array.astype(float)\n    \n    # Check original image properties\n    print(f\"Original range: {img.min()} to {img.max()}\")\n    print(f\"Original dtype: {img.dtype}\")\n    print(f\"Image shape: {img.shape}\")\n    \n    # Method 1: Simple normalization (often fails)\n    simple_norm = ((img - img.min()) / (img.max() - img.min()) * 255).astype(np.uint8)\n    \n    # Method 2: Percentile-based normalization (usually better)\n    p1, p99 = np.percentile(img, (1, 99))  # Remove outliers\n    percentile_norm = np.clip((img - p1) / (p99 - p1) * 255, 0, 255).astype(np.uint8)\n    \n    # Method 3: Histogram equalization\n    from skimage import exposure\n    hist_eq = exposure.equalize_hist(img) * 255\n    hist_eq = hist_eq.astype(np.uint8)\n    \n    # Compare all methods\n    fig, axes = plt.subplots(2, 2, figsize=(18, 12))\n    \n    # Original DICOM\n    axes[0,0].imshow(img, cmap='gray')\n    axes[0,0].set_title('Original DICOM (Raw)')\n    \n    # Simple normalization\n    axes[0,1].imshow(simple_norm, cmap='gray')\n    axes[0,1].set_title('Simple Normalization')\n    \n    # Percentile normalization  \n    axes[1,0].imshow(percentile_norm, cmap='gray')\n    axes[1,0].set_title('Percentile Normalization (1-99%)')\n    \n    # Histogram equalized\n    axes[1,1].imshow(hist_eq, cmap='gray')\n    axes[1,1].set_title('Histogram Equalization')\n    \n    for ax in axes.flat:\n        ax.axis('off')\n    \n    plt.tight_layout()\n    plt.show()\n    \n    # Return the best looking one\n    return percentile_norm  # Usually this works best\n\n# Test with your DICOM file\nbetter_img = dicom_conversion(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/0005e8e3701dfb1dd93d53e2ff537b6e.dicom\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T18:46:02.484494Z","iopub.execute_input":"2025-08-23T18:46:02.48479Z","iopub.status.idle":"2025-08-23T18:46:06.173484Z","shell.execute_reply.started":"2025-08-23T18:46:02.484772Z","shell.execute_reply":"2025-08-23T18:46:06.172529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install ultralytics","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T02:13:45.142404Z","iopub.execute_input":"2025-08-24T02:13:45.143233Z","iopub.status.idle":"2025-08-24T02:15:06.945849Z","shell.execute_reply.started":"2025-08-24T02:13:45.143205Z","shell.execute_reply":"2025-08-24T02:15:06.94501Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel = YOLO(\"yolo11m.pt\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T02:15:14.493562Z","iopub.execute_input":"2025-08-24T02:15:14.493861Z","iopub.status.idle":"2025-08-24T02:15:20.306586Z","shell.execute_reply.started":"2025-08-24T02:15:14.493831Z","shell.execute_reply":"2025-08-24T02:15:20.305921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(os.getcwd())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T10:43:05.844761Z","iopub.execute_input":"2025-08-23T10:43:05.845082Z","iopub.status.idle":"2025-08-23T10:43:05.85047Z","shell.execute_reply.started":"2025-08-23T10:43:05.845064Z","shell.execute_reply":"2025-08-23T10:43:05.849865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.makedirs(\"val/images\")\nos.makedirs(\"val/labels\")\nos.makedirs(\"train/images\")\nos.makedirs(\"train/labels\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T10:43:05.851083Z","iopub.execute_input":"2025-08-23T10:43:05.851441Z","iopub.status.idle":"2025-08-23T10:43:05.868618Z","shell.execute_reply.started":"2025-08-23T10:43:05.851409Z","shell.execute_reply":"2025-08-23T10:43:05.868065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def dicom_to_jpeg(path,output):\n    ds = pydicom.dcmread(path)\n    img = ds.pixel_array.astype(float)\n    p1, p99 = np.percentile(img, (1, 99))  # Remove outliers\n    percentile_norm = np.clip((img - p1) / (p99 - p1) * 255, 0, 255).astype(np.uint8)\n    Image.fromarray(percentile_norm).save(output,quality=95)\n\ndicom_to_jpeg(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/test/002a34c58c5b758217ed1f584ccbcfe9.dicom\",\"test1.jpg\")\nimg = Image.open(\"/kaggle/working/test1.jpg\")\nplt.imshow(img, cmap='gray')  # gray colormap for X-rays\nplt.title(\"Your Converted X-ray Image\")\nplt.axis('off')  # Remove axes\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:12:53.904693Z","iopub.execute_input":"2025-08-24T03:12:53.905024Z","iopub.status.idle":"2025-08-24T03:12:57.147957Z","shell.execute_reply.started":"2025-08-24T03:12:53.905Z","shell.execute_reply":"2025-08-24T03:12:57.147122Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## DataSet Preparation\n\n* Training Set : 3000 images\n* Validation Set : 700 images","metadata":{}},{"cell_type":"code","source":"n= 1\nfor dirpath, dirnames, filenames in os.walk(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train\"):\n    for filename in filenames:\n        file_path = os.path.join(dirpath, filename)\n        # os.remove(file_path)\n        if(n<=3000):\n            jpeg_filename = os.path.splitext(filename)[0] + '.jpg'\n            jpeg_path = os.path.join(\"/kaggle/working/train/images\", jpeg_filename)\n            dicom_to_jpeg(file_path,jpeg_path)\n        else:\n            break\n        n+=1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T18:50:06.588596Z","iopub.execute_input":"2025-08-23T18:50:06.589259Z","iopub.status.idle":"2025-08-23T19:43:25.654877Z","shell.execute_reply.started":"2025-08-23T18:50:06.589237Z","shell.execute_reply":"2025-08-23T19:43:25.653974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n =1\nfor dirpath, dirnames, filenames in os.walk(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train\"):\n    for filename in filenames:\n        file_path = os.path.join(dirpath, filename)\n        if(n>4000 and n<=4700):\n            jpeg_filename = os.path.splitext(filename)[0] + '.jpg'\n            jpeg_path = os.path.join(\"/kaggle/working/val/images\", jpeg_filename)\n            dicom_to_jpeg(file_path,jpeg_path)\n        elif(n>4700):\n            break\n        n+=1\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T19:57:37.452887Z","iopub.execute_input":"2025-08-23T19:57:37.453198Z","iopub.status.idle":"2025-08-23T20:10:01.905804Z","shell.execute_reply.started":"2025-08-23T19:57:37.453176Z","shell.execute_reply":"2025-08-23T20:10:01.905138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:28:53.002319Z","iopub.execute_input":"2025-08-24T03:28:53.002655Z","iopub.status.idle":"2025-08-24T03:28:53.143354Z","shell.execute_reply.started":"2025-08-24T03:28:53.002632Z","shell.execute_reply":"2025-08-24T03:28:53.142636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for dirpath, dirnames, filenames in os.walk(\"/kaggle/working/train/images\"):\n    for filename in filenames:\n        img = Image.open(f\"/kaggle/working/train/images/{filename}\")\n        width, height = img.size\n        jpeg_filename = os.path.splitext(filename)[0]\n        rows = df[df[\"image_id\"]==jpeg_filename]\n        with open(f\"/kaggle/working/train/labels/{jpeg_filename}.txt\", \"w\") as f:\n            f.write(\"\")\n        if rows[\"class_id\"].iloc[0] != 14:\n            rows = rows.sort_values(by = 'class_id',ascending = True)\n            count = (df[\"image_id\"]==jpeg_filename).sum()\n            for i in range(0,count):\n                text = str(rows[\"class_id\"].iloc[i]) + \" \"\n                x_cen = (rows[\"x_min\"].iloc[i] + rows[\"x_max\"].iloc[i])/2\n                y_cen = (rows[\"y_min\"].iloc[i] + rows[\"y_max\"].iloc[i])/2\n                wid = (rows[\"x_max\"].iloc[i] - rows[\"x_min\"].iloc[i])\n                hei = (rows[\"y_max\"].iloc[i] - rows[\"y_min\"].iloc[i])\n                norm_x = x_cen/width\n                norm_y = y_cen/height\n                norm_w = wid/width\n                norm_h = hei/height\n                text=text+str(norm_x)+\" \"+str(norm_y)+\" \"+str(norm_w)+\" \"+str(norm_h)+\"\\n\"\n                # print(text)\n                with open(f\"/kaggle/working/train/labels/{jpeg_filename}.txt\", \"a\") as f:\n                    f.write(text)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T20:15:02.592264Z","iopub.execute_input":"2025-08-23T20:15:02.593004Z","iopub.status.idle":"2025-08-23T20:15:24.811768Z","shell.execute_reply.started":"2025-08-23T20:15:02.592973Z","shell.execute_reply":"2025-08-23T20:15:24.811138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f = open(\"/kaggle/working/train/labels/00aca42a24e4ea6066cca2546150c36e.txt\")\nprint(f.read())\nf.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T19:56:49.63403Z","iopub.execute_input":"2025-08-23T19:56:49.634403Z","iopub.status.idle":"2025-08-23T19:56:49.639544Z","shell.execute_reply.started":"2025-08-23T19:56:49.634378Z","shell.execute_reply":"2025-08-23T19:56:49.638602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for dirpath, dirnames, filenames in os.walk(\"/kaggle/working/val/images\"):\n    for filename in filenames:\n        img = Image.open(os.path.join(dirpath,filename))\n        width, height = img.size\n        jpeg_filename = os.path.splitext(filename)[0]\n        rows = df[df[\"image_id\"]==jpeg_filename]\n        with open(f\"/kaggle/working/val/labels/{jpeg_filename}.txt\", \"w\") as f:\n            f.write(\"\")\n        if rows[\"class_id\"].iloc[0] != 14:\n            rows = rows.sort_values(by = 'class_id',ascending = True)\n            count = (df[\"image_id\"]==jpeg_filename).sum()\n            for i in range(0,count):\n                text = str(rows[\"class_id\"].iloc[i]) + \" \"\n                x_cen = (rows[\"x_min\"].iloc[i] + rows[\"x_max\"].iloc[i])/2\n                y_cen = (rows[\"y_min\"].iloc[i] + rows[\"y_max\"].iloc[i])/2\n                wid = (rows[\"x_max\"].iloc[i] - rows[\"x_min\"].iloc[i])\n                hei = (rows[\"y_max\"].iloc[i] - rows[\"y_min\"].iloc[i])\n                norm_x = x_cen/width\n                norm_y = y_cen/height\n                norm_w = wid/width\n                norm_h = hei/height\n                text=text+str(norm_x)+\" \"+str(norm_y)+\" \"+str(norm_w)+\" \"+str(norm_h)+\"\\n\"\n                # print(text)\n                with open(f\"/kaggle/working/val/labels/{jpeg_filename}.txt\", \"a\") as f:\n                    f.write(text)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T20:15:57.885655Z","iopub.execute_input":"2025-08-23T20:15:57.885941Z","iopub.status.idle":"2025-08-23T20:16:02.996633Z","shell.execute_reply.started":"2025-08-23T20:15:57.885921Z","shell.execute_reply":"2025-08-23T20:16:02.995858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f = open(\"/kaggle/working/val/labels/02cd1d17763c869ff3d4af5e28539456.txt\")\nprint(f.read())\nf.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T20:16:15.963342Z","iopub.execute_input":"2025-08-23T20:16:15.963933Z","iopub.status.idle":"2025-08-23T20:16:15.968889Z","shell.execute_reply.started":"2025-08-23T20:16:15.963909Z","shell.execute_reply":"2025-08-23T20:16:15.968012Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"YAML file for YOLO","metadata":{}},{"cell_type":"code","source":"import yaml\n\n# Your dataset configuration\nconfig = {\n    'train': '../train/images',\n    'val': '../val/images',\n    'nc': 14,\n    'names': {0: 'Aortic enlargement', 1: 'Atelectasis', 2:'Calcification', 3:'Cardiomegaly', 4:'Consolidation',5:'ILD',6:'Infiltration',7:'Lung Opacity',8:'Nodule/Mass',9:'Other lesion',10:'Pleural effusion',11:'Pleural thickening', 12:'Pneumothorax', 13:'Pulmonary fibrosis'}\n}\n\n# Write to file\nwith open('data.yaml', 'w') as f:\n    yaml.dump(config, f, default_flow_style=False,sort_keys=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T20:16:25.142886Z","iopub.execute_input":"2025-08-23T20:16:25.14383Z","iopub.status.idle":"2025-08-23T20:16:25.149959Z","shell.execute_reply.started":"2025-08-23T20:16:25.143804Z","shell.execute_reply":"2025-08-23T20:16:25.149264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"f = open(\"/kaggle/working/data.yaml\",\"r\")\nprint(f.read())\nf.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T20:16:35.891053Z","iopub.execute_input":"2025-08-23T20:16:35.891389Z","iopub.status.idle":"2025-08-23T20:16:35.896255Z","shell.execute_reply.started":"2025-08-23T20:16:35.891364Z","shell.execute_reply":"2025-08-23T20:16:35.895476Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training the YOLO v11 model\n\n* epochs : 100\n* image size : 640\n* batch size : 16\n* Multiple GPU used for training","metadata":{}},{"cell_type":"code","source":"results = model.train(data=\"data.yaml\", epochs=100, imgsz=640,batch=16,device=\"0,1\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-23T20:29:03.512211Z","iopub.execute_input":"2025-08-23T20:29:03.512534Z","execution_failed":"2025-08-23T23:18:50.072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr_model = YOLO(\"/kaggle/working/runs/detect/train22/weights/last.pt\")\ntr_results = tr_model.train(resume = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T02:16:15.093296Z","iopub.execute_input":"2025-08-24T02:16:15.09409Z","iopub.status.idle":"2025-08-24T02:58:21.95731Z","shell.execute_reply.started":"2025-08-24T02:16:15.094051Z","shell.execute_reply":"2025-08-24T02:58:21.956269Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = Image.open(\"/kaggle/working/runs/detect/train22/results.png\")\nplt.figure(figsize = (10,10))\nplt.imshow(img)\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:08:25.070236Z","iopub.execute_input":"2025-08-24T03:08:25.071198Z","iopub.status.idle":"2025-08-24T03:08:25.469992Z","shell.execute_reply.started":"2025-08-24T03:08:25.071164Z","shell.execute_reply":"2025-08-24T03:08:25.469207Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Inference Running on Unseen Data","metadata":{}},{"cell_type":"code","source":"x_ray_model = YOLO(\"/kaggle/working/runs/detect/train22/weights/best.pt\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:11:56.198627Z","iopub.execute_input":"2025-08-24T03:11:56.198933Z","iopub.status.idle":"2025-08-24T03:11:56.316017Z","shell.execute_reply.started":"2025-08-24T03:11:56.198914Z","shell.execute_reply":"2025-08-24T03:11:56.315319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_bbimg(path,filename,ax):\n    img = Image.open(path)\n    ax.imshow(img,cmap='gray')\n    rows = df[df[\"image_id\"]==filename]\n    if rows[\"class_id\"].iloc[0] != 14:\n        dic={}\n        rows = rows.sort_values(by = 'class_id',ascending = True)\n        count = (df[\"image_id\"]==filename).sum()\n        for i in range(0,count):\n            wid = (rows[\"x_max\"].iloc[i] - rows[\"x_min\"].iloc[i])\n            hei = (rows[\"y_max\"].iloc[i] - rows[\"y_min\"].iloc[i])\n            label = rows[\"class_name\"].iloc[i]\n            rect = patches.Rectangle((rows[\"x_min\"].iloc[i], rows[\"y_min\"].iloc[i]), wid, hei, edgecolor=\"red\", facecolor=\"none\")\n            ax.add_patch(rect)\n            ax.text(rows[\"x_min\"].iloc[i], rows[\"y_min\"].iloc[i] - 5, str(label), color=\"white\", fontsize=10,\n                    bbox=dict(facecolor=\"red\", alpha=0.5, edgecolor=\"none\", boxstyle=\"round,pad=0.2\"))\n            if label in dic:\n                dic[label]+=1\n            else:\n                dic[label] = 1\n    print(\"Ground Truth\",dic)\n    ax.axis(\"off\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T04:30:26.926059Z","iopub.execute_input":"2025-08-24T04:30:26.926693Z","iopub.status.idle":"2025-08-24T04:30:26.934147Z","shell.execute_reply.started":"2025-08-24T04:30:26.926667Z","shell.execute_reply":"2025-08-24T04:30:26.933332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dicom_to_jpeg(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/009d837e29ba400e03856cf8d6a5b545.dicom\",\"/kaggle/working/test2.jpg\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:13:10.442544Z","iopub.execute_input":"2025-08-24T03:13:10.442901Z","iopub.status.idle":"2025-08-24T03:13:12.934278Z","shell.execute_reply.started":"2025-08-24T03:13:10.442876Z","shell.execute_reply":"2025-08-24T03:13:12.933618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results=x_ray_model(\"test2.jpg\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T03:27:21.216682Z","iopub.execute_input":"2025-08-24T03:27:21.217567Z","iopub.status.idle":"2025-08-24T03:27:21.309263Z","shell.execute_reply.started":"2025-08-24T03:27:21.217539Z","shell.execute_reply":"2025-08-24T03:27:21.308468Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(10,8))\nshow_bbimg(\"/kaggle/working/test2.jpg\",\"009d837e29ba400e03856cf8d6a5b545\",ax[0])\nax[0].set_title(\"Ground Truth\")\nax[1].set_title(\"Predicted X-ray Image\")\nax[1].imshow(results[0].plot())\nax[1].axis('off') \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T04:29:36.88759Z","iopub.execute_input":"2025-08-24T04:29:36.888241Z","iopub.status.idle":"2025-08-24T04:29:38.245985Z","shell.execute_reply.started":"2025-08-24T04:29:36.888216Z","shell.execute_reply":"2025-08-24T04:29:38.244986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_id=\"ff924bcbd38f123aec723aa7040d7e43\"\nfilepath=f\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/{img_id}.dicom\"\njpg_path = \"/kaggle/working/test3.jpg\"\n\ndicom_to_jpeg(filepath,jpg_path)\nres2=x_ray_model(jpg_path)\nfig, ax = plt.subplots(1,2,figsize=(10,8))\nshow_bbimg(jpg_path,img_id,ax[0])\nax[0].set_title(\"Ground Truth\")\nax[1].set_title(\"Predicted X-ray Image\")\nax[1].imshow(res2[0].plot())\nax[1].axis('off') \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T04:30:32.874819Z","iopub.execute_input":"2025-08-24T04:30:32.87519Z","iopub.status.idle":"2025-08-24T04:30:36.406679Z","shell.execute_reply.started":"2025-08-24T04:30:32.875145Z","shell.execute_reply":"2025-08-24T04:30:36.405897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_id=\"f9dda1a40ac162af4e9fbc6027ed5375\"\nfilepath=f\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/{img_id}.dicom\"\njpg_path = \"/kaggle/working/test4.jpg\"\n\ndicom_to_jpeg(filepath,jpg_path)\nres=x_ray_model(jpg_path)\nfig, ax = plt.subplots(1,2,figsize=(10,8))\nshow_bbimg(jpg_path,img_id,ax[0])\nax[0].set_title(\"Ground Truth\")\nax[1].set_title(\"Predicted X-ray Image\")\nax[1].imshow(res[0].plot())\nax[1].axis('off') \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T04:33:05.416496Z","iopub.execute_input":"2025-08-24T04:33:05.41727Z","iopub.status.idle":"2025-08-24T04:33:07.227557Z","shell.execute_reply.started":"2025-08-24T04:33:05.417227Z","shell.execute_reply":"2025-08-24T04:33:07.226688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"metrics = x_ray_model.val(data=\"data.yaml\", split=\"val\")\nprint(metrics) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T04:50:53.467582Z","iopub.execute_input":"2025-08-24T04:50:53.468342Z","iopub.status.idle":"2025-08-24T04:51:18.070294Z","shell.execute_reply.started":"2025-08-24T04:50:53.468318Z","shell.execute_reply":"2025-08-24T04:51:18.069425Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"mAP@0.5:\", metrics.box.map50) \nprint(\"mAP@0.5:0.95:\", metrics.box.map) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T05:18:13.823916Z","iopub.execute_input":"2025-08-24T05:18:13.824555Z","iopub.status.idle":"2025-08-24T05:18:13.829336Z","shell.execute_reply.started":"2025-08-24T05:18:13.824531Z","shell.execute_reply":"2025-08-24T05:18:13.828546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = Image.open(\"runs/detect/val/confusion_matrix.png\")\nplt.figure(figsize=(14,14))\nplt.imshow(img)\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-24T05:11:07.621125Z","iopub.execute_input":"2025-08-24T05:11:07.621414Z","iopub.status.idle":"2025-08-24T05:11:08.388819Z","shell.execute_reply.started":"2025-08-24T05:11:07.621395Z","shell.execute_reply":"2025-08-24T05:11:08.388022Z"}},"outputs":[],"execution_count":null}]}