{"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":"### Installing and importing dependencies","metadata":{}},{"cell_type":"code","source":"!unzip -q ../input/timm-with-dependencies/timm_all -d timm-with-dependencies\n!pip install --no-index --find-links timm-with-dependencies timm\n\n\n# This is a dependency that is needed for reading DICOM images\n\ntry:\n    import pylibjpeg\nexcept:\n    !rm -rf /root/.cache/torch/hub/checkpoints/\n    !mkdir -p /root/.cache/torch/hub/checkpoints/\n    !pip install /kaggle/input/rsna-2022-whl/{pydicom-2.3.0-py3-none-any.whl,pylibjpeg-1.4.0-py3-none-any.whl,python_gdcm-3.0.15-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl}\n    !pip install /kaggle/input/rsna-2022-whl/{torch-1.12.1-cp37-cp37m-manylinux1_x86_64.whl,torchvision-0.13.1-cp37-cp37m-manylinux1_x86_64.whl}","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-14T00:00:32.767126Z","iopub.execute_input":"2023-01-14T00:00:32.767528Z","iopub.status.idle":"2023-01-14T00:03:00.437258Z","shell.execute_reply.started":"2023-01-14T00:00:32.767447Z","shell.execute_reply":"2023-01-14T00:03:00.43593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# copying the pretrained weights\nif not os.path.exists('/root/.cache/torch/hub/checkpoints/'):\n        os.makedirs('/root/.cache/torch/hub/checkpoints/')\n!cp '/kaggle/input/timm-pretrained-model-weights/resnet26d-69e92c46.pth' '/root/.cache/torch/hub/checkpoints/resnet26d-69e92c46.pth'","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-14T00:04:22.422811Z","iopub.execute_input":"2023-01-14T00:04:22.423416Z","iopub.status.idle":"2023-01-14T00:04:24.667737Z","shell.execute_reply.started":"2023-01-14T00:04:22.42337Z","shell.execute_reply":"2023-01-14T00:04:24.666308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.learner import *\nfrom fastai.data.all import *\nfrom fastai.vision.all import *\nimport timm\n\nfrom collections import defaultdict\nimport pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-14T00:05:02.81247Z","iopub.execute_input":"2023-01-14T00:05:02.812931Z","iopub.status.idle":"2023-01-14T00:05:02.820721Z","shell.execute_reply.started":"2023-01-14T00:05:02.812892Z","shell.execute_reply":"2023-01-14T00:05:02.819673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Loading [PNG images](https://www.kaggle.com/datasets/radek1/rsna-mammography-images-as-pngs) of this competition dataset converted by Radek Osmulski","metadata":{}},{"cell_type":"code","source":"train_image_path = '/kaggle/input/rsna-mammography-images-as-pngs/images_as_pngs/train_images_processed'\n\ntrain_csv = pd.read_csv('../input/rsna-breast-cancer-detection/train.csv')\nfn2label = {fn: cancer_or_not for fn, cancer_or_not in zip(train_csv['image_id'].astype('str'), train_csv['cancer'])}\n\ndef label_func(path):\n    return fn2label[path.stem]\n\ndblock = DataBlock(\n    blocks    = (ImageBlock, CategoryBlock),\n    get_items = get_image_files,\n    get_y = label_func,\n    splitter  = RandomSplitter(),\n    item_tfms=[Resize(256, method='squish')],\n    batch_tfms=[IntToFloatTensor(div=2**16-1), *aug_transforms()]\n)\ndsets = dblock.datasets(train_image_path)\ndls = dblock.dataloaders(train_image_path)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:05:09.659092Z","iopub.execute_input":"2023-01-14T00:05:09.659533Z","iopub.status.idle":"2023-01-14T00:06:04.544865Z","shell.execute_reply.started":"2023-01-14T00:05:09.659494Z","shell.execute_reply":"2023-01-14T00:06:04.543678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:06:04.550611Z","iopub.execute_input":"2023-01-14T00:06:04.553033Z","iopub.status.idle":"2023-01-14T00:06:06.796193Z","shell.execute_reply.started":"2023-01-14T00:06:04.552991Z","shell.execute_reply":"2023-01-14T00:06:06.792543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training","metadata":{"execution":{"iopub.status.busy":"2023-01-13T22:16:02.644739Z","iopub.execute_input":"2023-01-13T22:16:02.64524Z","iopub.status.idle":"2023-01-13T22:16:02.653811Z","shell.execute_reply.started":"2023-01-13T22:16:02.645191Z","shell.execute_reply":"2023-01-13T22:16:02.652843Z"}}},{"cell_type":"code","source":"learn = vision_learner(dls, 'resnet26d', metrics=error_rate, pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:06:45.826912Z","iopub.execute_input":"2023-01-14T00:06:45.827388Z","iopub.status.idle":"2023-01-14T00:06:46.311475Z","shell.execute_reply.started":"2023-01-14T00:06:45.827348Z","shell.execute_reply":"2023-01-14T00:06:46.310456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find(suggest_funcs=(valley, slide))","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:06:49.338995Z","iopub.execute_input":"2023-01-14T00:06:49.339443Z","iopub.status.idle":"2023-01-14T00:07:28.117123Z","shell.execute_reply.started":"2023-01-14T00:06:49.339406Z","shell.execute_reply":"2023-01-14T00:07:28.116104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(1, 0.1)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:07:28.12212Z","iopub.execute_input":"2023-01-14T00:07:28.124502Z","iopub.status.idle":"2023-01-14T00:18:20.261995Z","shell.execute_reply.started":"2023-01-14T00:07:28.124461Z","shell.execute_reply":"2023-01-14T00:18:20.26087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Prediction","metadata":{}},{"cell_type":"code","source":"import pydicom\n    \nfrom pathlib import Path\nfrom PIL import Image\nimport multiprocessing as mp\n\nRESIZE_TO = (256, 256)\n!rm -rf test_resized_{RESIZE_TO[0]}\n\n# https://www.kaggle.com/code/tanlikesmath/brain-tumor-radiogenomic-classification-eda/notebook\ndef dicom_file_to_ary(path):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data\n\ndirectories = list(Path('/kaggle/input/rsna-breast-cancer-detection/test_images').iterdir())\n\ndef process_directory(directory_path):\n    parent_directory = str(directory_path).split('/')[-1]\n    !mkdir -p test_resized_{RESIZE_TO[0]}/{parent_directory}\n    for image_path in directory_path.iterdir():\n        processed_ary = dicom_file_to_ary(image_path)\n        im = Image.fromarray(processed_ary).resize(RESIZE_TO)\n        im.save(f'test_resized_{RESIZE_TO[0]}/{parent_directory}/{image_path.stem}.png')\n\nwith mp.Pool(mp.cpu_count()) as p:\n    p.map(process_directory, directories)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-14T00:18:20.266727Z","iopub.execute_input":"2023-01-14T00:18:20.269464Z","iopub.status.idle":"2023-01-14T00:18:27.448267Z","shell.execute_reply.started":"2023-01-14T00:18:20.269422Z","shell.execute_reply":"2023-01-14T00:18:27.446684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = learn.dls.test_dl(get_image_files(f'test_resized_{RESIZE_TO[0]}'))\npreds, _ = learn.get_preds(dl=test_dl)\n\nimage_ids = [path.stem for path in test_dl.items]\npreds = np.array(preds)[:, 1]","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:18:27.455304Z","iopub.execute_input":"2023-01-14T00:18:27.457756Z","iopub.status.idle":"2023-01-14T00:18:27.749841Z","shell.execute_reply.started":"2023-01-14T00:18:27.457696Z","shell.execute_reply":"2023-01-14T00:18:27.748518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id2pred = defaultdict(lambda: 0)\nfor image_id, pred in zip(image_ids, preds):\n    image_id2pred[int(image_id)] = pred","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:18:27.751436Z","iopub.execute_input":"2023-01-14T00:18:27.751837Z","iopub.status.idle":"2023-01-14T00:18:27.762308Z","shell.execute_reply.started":"2023-01-14T00:18:27.751796Z","shell.execute_reply":"2023-01-14T00:18:27.761114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Submission","metadata":{}},{"cell_type":"code","source":"test_csv = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\n\nprediction_ids = []\npreds = []\n\nfor _, row in test_csv.iterrows():\n    prediction_ids.append(row.prediction_id)\n    preds.append(image_id2pred[row.image_id])\n\nsubmission = pd.DataFrame(data={'prediction_id': prediction_ids, 'cancer': preds}).groupby('prediction_id').mean().reset_index()\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:18:27.767044Z","iopub.execute_input":"2023-01-14T00:18:27.769341Z","iopub.status.idle":"2023-01-14T00:18:27.818214Z","shell.execute_reply.started":"2023-01-14T00:18:27.769303Z","shell.execute_reply":"2023-01-14T00:18:27.817293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:18:27.822658Z","iopub.execute_input":"2023-01-14T00:18:27.825052Z","iopub.status.idle":"2023-01-14T00:18:27.844641Z","shell.execute_reply.started":"2023-01-14T00:18:27.825015Z","shell.execute_reply":"2023-01-14T00:18:27.843769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T00:18:27.848874Z","iopub.execute_input":"2023-01-14T00:18:27.851266Z","iopub.status.idle":"2023-01-14T00:18:27.861607Z","shell.execute_reply.started":"2023-01-14T00:18:27.85122Z","shell.execute_reply":"2023-01-14T00:18:27.860574Z"},"trusted":true},"execution_count":null,"outputs":[]}]}