{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Base code:\nhttps://www.kaggle.com/code/theoviel/dicom-resized-png-jpg\nI just changed the size of img from base code.\n\n## Dataset Links :\n\n - [256x256 pngs](https://www.kaggle.com/datasets/theoviel/rsna-breast-cancer-256-pngs)\n - [512x512 pngs](https://www.kaggle.com/datasets/theoviel/rsna-breast-cancer-512-pngs)\n - [768x768 pngs](https://www.kaggle.com/datasets/theoviel/rsna-breast-cancer-768-pngs)\n - [1024x1024 pngs](https://www.kaggle.com/datasets/theoviel/rsna-breast-cancer-1024-pngs)\n\n**Changes :**\n- Invert images with PhotometricInterpretation == \"MONOCHROME1\" ","metadata":{}},{"cell_type":"markdown","source":"## Initialization","metadata":{}},{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-29T12:43:29.413393Z","iopub.execute_input":"2022-11-29T12:43:29.41422Z","iopub.status.idle":"2022-11-29T12:43:40.907748Z","shell.execute_reply.started":"2022-11-29T12:43:29.414176Z","shell.execute_reply":"2022-11-29T12:43:40.906227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport gdcm\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2022-11-29T17:03:00.464675Z","iopub.execute_input":"2022-11-29T17:03:00.465225Z","iopub.status.idle":"2022-11-29T17:03:01.913946Z","shell.execute_reply.started":"2022-11-29T17:03:00.465177Z","shell.execute_reply":"2022-11-29T17:03:01.912536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob.glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\")\n\nlen(train_images)  # 54706","metadata":{"execution":{"iopub.status.busy":"2022-11-29T17:03:07.807974Z","iopub.execute_input":"2022-11-29T17:03:07.808442Z","iopub.status.idle":"2022-11-29T17:03:46.905905Z","shell.execute_reply.started":"2022-11-29T17:03:07.808401Z","shell.execute_reply":"2022-11-29T17:03:46.904464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Examples","metadata":{}},{"cell_type":"code","source":"for f in tqdm(train_images[:3]):\n    patient = f.split('/')[-2]\n    image = f.split('/')[-1][:-4]\n\n    dicom = pydicom.dcmread(f)\n    img = dicom.pixel_array\n\n    img = (img - img.min()) / (img.max() - img.min())\n\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n        \n    plt.figure(figsize=(15, 15))\n    plt.imshow(img, cmap=\"gray\")\n    plt.title(f\"{patient} {image}\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T17:05:08.416376Z","iopub.execute_input":"2022-11-29T17:05:08.416831Z","iopub.status.idle":"2022-11-29T17:05:13.760543Z","shell.execute_reply.started":"2022-11-29T17:05:08.416797Z","shell.execute_reply":"2022-11-29T17:05:13.759212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save the processed data\n**Images are quite big so resizing them is necessary.**\n  - Use 256 to train your first models, or if you don't have a lot of compute\n  - use 512 to have competitive models\n  - Check if 768/1024 is better, if you have the compute power\n\n**I advise using the `png` format because the jpg compression can be annoying during inference.**","metadata":{}},{"cell_type":"code","source":"def process(f, size=512, save_folder=\"\", extension=\"png\"):\n    patient = f.split('/')[-2]\n    image = f.split('/')[-1][:-4]\n\n    dicom = pydicom.dcmread(f)\n    img = dicom.pixel_array\n\n    img = (img - img.min()) / (img.max() - img.min())\n\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n\n    img = cv2.resize(img, (size, size))\n\n    cv2.imwrite(save_folder + f\"{patient}_{image}.{extension}\", (img * 255).astype(np.uint8))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T12:44:00.790915Z","iopub.execute_input":"2022-11-29T12:44:00.791403Z","iopub.status.idle":"2022-11-29T12:44:00.804306Z","shell.execute_reply.started":"2022-11-29T12:44:00.791356Z","shell.execute_reply":"2022-11-29T12:44:00.802533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob.glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\")\n\nSAVE_FOLDER = \"output/train_images/\"\nSIZE = 2048\nEXTENSION = \"png\"\n\nos.makedirs(SAVE_FOLDER, exist_ok=True)\n\n_ = Parallel(n_jobs=4)(\n    delayed(process)(uid, size=SIZE, save_folder=SAVE_FOLDER, extension=EXTENSION)\n    for uid in tqdm(train_images[:15000])\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T12:44:00.783273Z","iopub.execute_input":"2022-11-29T12:44:00.783617Z","iopub.status.idle":"2022-11-29T12:44:00.78914Z","shell.execute_reply.started":"2022-11-29T12:44:00.783577Z","shell.execute_reply":"2022-11-29T12:44:00.787637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = glob.glob(\"/kaggle/input/rsna-breast-cancer-detection/test_images/*/*.dcm\")\n\nSAVE_FOLDER = \"output/test_images/\"\n\nos.makedirs(SAVE_FOLDER, exist_ok=True)\n\n_ = Parallel(n_jobs=4)(\n    delayed(process)(uid, size=SIZE, save_folder=SAVE_FOLDER, extension=EXTENSION)\n    for uid in tqdm(test_images)\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T12:44:00.80574Z","iopub.execute_input":"2022-11-29T12:44:00.806226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Done !","metadata":{}}]}