{"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":"2023-01-01T05:43:00.561777Z","iopub.execute_input":"2023-01-01T05:43:00.562309Z","iopub.status.idle":"2023-01-01T05:43:18.053928Z","shell.execute_reply.started":"2023-01-01T05:43:00.562195Z","shell.execute_reply":"2023-01-01T05:43:18.052379Z"},"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":"2023-01-01T05:43:18.056561Z","iopub.execute_input":"2023-01-01T05:43:18.056978Z","iopub.status.idle":"2023-01-01T05:43:19.124909Z","shell.execute_reply.started":"2023-01-01T05:43:18.056936Z","shell.execute_reply":"2023-01-01T05:43:19.123698Z"},"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":"2023-01-01T05:43:19.126441Z","iopub.execute_input":"2023-01-01T05:43:19.126815Z","iopub.status.idle":"2023-01-01T05:43:53.884566Z","shell.execute_reply.started":"2023-01-01T05:43:19.126782Z","shell.execute_reply":"2023-01-01T05:43:53.883352Z"},"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":"2023-01-01T05:43:53.887798Z","iopub.execute_input":"2023-01-01T05:43:53.888697Z","iopub.status.idle":"2023-01-01T05:43:58.231408Z","shell.execute_reply.started":"2023-01-01T05:43:53.888644Z","shell.execute_reply":"2023-01-01T05:43:58.230044Z"},"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":"2023-01-01T05:43:58.233011Z","iopub.execute_input":"2023-01-01T05:43:58.233503Z","iopub.status.idle":"2023-01-01T05:43:58.24434Z","shell.execute_reply.started":"2023-01-01T05:43:58.233456Z","shell.execute_reply":"2023-01-01T05:43:58.242799Z"},"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:30000])\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-01T05:43:58.246197Z","iopub.execute_input":"2023-01-01T05:43:58.247108Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Done !","metadata":{}}]}