{"metadata":{"colab":{"name":"lesson-3-rsna-pneumonia-detection-challenge-kaggle","version":"0.3.2","provenance":[],"collapsed_sections":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"accelerator":"GPU","language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os \nimport sys\nimport random\nimport math\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport json\nimport pydicom\nfrom imgaug import augmenters as iaa\nfrom tqdm import tqdm\nimport pandas as pd \nimport glob \nimport pandas as pd\n!pip install pillow","metadata":{"id":"4kjcC6QqywWl","colab_type":"code","colab":{},"_uuid":"40c67b3ff0fa04587dec508363308adaa3ceaf34","execution":{"iopub.status.busy":"2023-11-07T11:44:36.331056Z","iopub.execute_input":"2023-11-07T11:44:36.331466Z","iopub.status.idle":"2023-11-07T11:44:41.126856Z","shell.execute_reply.started":"2023-11-07T11:44:36.331399Z","shell.execute_reply":"2023-11-07T11:44:41.12552Z"},"trusted":true},"execution_count":49,"outputs":[{"name":"stdout","text":"Requirement already satisfied: pillow in /opt/conda/lib/python3.6/site-packages\nRequirement already satisfied: olefile in /opt/conda/lib/python3.6/site-packages (from pillow)\n\u001b[33mYou are using pip version 9.0.1, however version 23.3.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n","output_type":"stream"}]},{"cell_type":"code","source":"print( tf. __version__)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:05:23.396Z","iopub.execute_input":"2023-11-07T14:05:23.396423Z","iopub.status.idle":"2023-11-07T14:05:23.621206Z","shell.execute_reply.started":"2023-11-07T14:05:23.396359Z","shell.execute_reply":"2023-11-07T14:05:23.61939Z"},"trusted":true},"execution_count":1,"outputs":[{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)","\u001b[0;32m<ipython-input-1-275546240a40>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m \u001b[0m__version__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;31mNameError\u001b[0m: name 'tf' is not defined"],"ename":"NameError","evalue":"name 'tf' is not defined","output_type":"error"}]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-07T11:38:21.553918Z","iopub.execute_input":"2023-11-07T11:38:21.55438Z","iopub.status.idle":"2023-11-07T11:38:26.571054Z","shell.execute_reply.started":"2023-11-07T11:38:21.55433Z","shell.execute_reply":"2023-11-07T11:38:26.570032Z"},"trusted":true},"execution_count":43,"outputs":[{"name":"stdout","text":"Requirement already satisfied: pydicom in /opt/conda/lib/python3.6/site-packages\n\u001b[33mYou are using pip version 9.0.1, however version 23.3.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n","output_type":"stream"}]},{"cell_type":"code","source":"DATA_DIR = '/kaggle/input'\nROOT_DIR = '/kaggle/working'","metadata":{"id":"yP0XLJx_x_6o","colab_type":"code","colab":{},"_uuid":"6e5764759e6a0a9b698b44645658f66873edd807","execution":{"iopub.status.busy":"2023-11-07T11:28:54.223676Z","iopub.execute_input":"2023-11-07T11:28:54.224054Z","iopub.status.idle":"2023-11-07T11:28:54.22957Z","shell.execute_reply.started":"2023-11-07T11:28:54.22396Z","shell.execute_reply":"2023-11-07T11:28:54.2285Z"},"trusted":true},"execution_count":25,"outputs":[]},{"cell_type":"code","source":"!git clone https://www.github.com/matterport/Mask_RCNN.git","metadata":{"id":"KgllzLnDr7kF","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":143},"outputId":"6c978df7-2013-437e-acd1-5011048dfb53","_uuid":"b37d22551d332f0f7b722cc7204eb614524b6c21","execution":{"iopub.status.busy":"2023-11-07T11:30:02.308585Z","iopub.execute_input":"2023-11-07T11:30:02.309659Z","iopub.status.idle":"2023-11-07T11:30:12.897137Z","shell.execute_reply.started":"2023-11-07T11:30:02.309584Z","shell.execute_reply":"2023-11-07T11:30:12.896069Z"},"trusted":true},"execution_count":33,"outputs":[{"name":"stdout","text":"Cloning into 'Mask_RCNN'...\nremote: Enumerating objects: 956, done.\u001b[K\nremote: Total 956 (delta 0), reused 0 (delta 0), pack-reused 956\u001b[K\nReceiving objects: 100% (956/956), 137.67 MiB | 21.29 MiB/s, done.\nResolving deltas: 100% (558/558), done.\nChecking connectivity... done.\n","output_type":"stream"}]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-07T11:30:37.648236Z","iopub.execute_input":"2023-11-07T11:30:37.648743Z","iopub.status.idle":"2023-11-07T11:30:47.684702Z","shell.execute_reply.started":"2023-11-07T11:30:37.64865Z","shell.execute_reply":"2023-11-07T11:30:47.683658Z"},"trusted":true},"execution_count":37,"outputs":[{"name":"stdout","text":"Collecting mrcnn\n  Downloading https://files.pythonhosted.org/packages/80/3d/56e05c297a1f464a042b2c47bcd9e5f2d452ce0e5eca3894f7cbdcaee758/mrcnn-0.2.tar.gz (51kB)\n\u001b[K    100% |████████████████████████████████| 61kB 5.2MB/s ta 0:00:011\n\u001b[?25hBuilding wheels for collected packages: mrcnn\n  Running setup.py bdist_wheel for mrcnn ... \u001b[?25ldone\n\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/11/ed/28/e550ddc897c04c336b923eae4eb35c9aae993d20ce39d9cc40\nSuccessfully built mrcnn\nInstalling collected packages: mrcnn\nSuccessfully installed mrcnn-0.2\n\u001b[33mYou are using pip version 9.0.1, however version 23.3.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n","output_type":"stream"}]},{"cell_type":"code","source":"sys.path.append(os.path.join(ROOT_DIR, 'Mask_RCNN')) \nfrom mrcnn.config import Config\nfrom mrcnn import utils\nimport mrcnn.model as modellib\nfrom mrcnn import visualize\nfrom mrcnn.model import log","metadata":{"id":"-KZXyWwhzOVU","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"outputId":"2576cc17-7484-4311-ad72-3c5643dcb5bb","_uuid":"3acbbbe055b6a409d3c50ae0f893acf51b5ae7ba","execution":{"iopub.status.busy":"2023-11-07T11:30:50.536668Z","iopub.execute_input":"2023-11-07T11:30:50.537102Z","iopub.status.idle":"2023-11-07T11:30:53.352469Z","shell.execute_reply.started":"2023-11-07T11:30:50.537015Z","shell.execute_reply":"2023-11-07T11:30:53.351428Z"},"trusted":true},"execution_count":38,"outputs":[{"name":"stderr","text":"/opt/conda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n  from ._conv import register_converters as _register_converters\nUsing TensorFlow backend.\n","output_type":"stream"}]},{"cell_type":"code","source":"train_dicom_dir = os.path.join(DATA_DIR, 'stage_2_train_images')\ntest_dicom_dir = os.path.join(DATA_DIR, 'stage_2_test_images')","metadata":{"id":"FghMmiMjzOX2","colab_type":"code","colab":{},"_uuid":"50089cc61791871cdf6a5c0037dc4f28b7b7d7cc","execution":{"iopub.status.busy":"2023-11-07T11:31:08.315873Z","iopub.execute_input":"2023-11-07T11:31:08.316249Z","iopub.status.idle":"2023-11-07T11:31:08.321689Z","shell.execute_reply.started":"2023-11-07T11:31:08.316173Z","shell.execute_reply":"2023-11-07T11:31:08.320849Z"},"trusted":true},"execution_count":39,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-07T11:39:32.179827Z","iopub.execute_input":"2023-11-07T11:39:32.180267Z","iopub.status.idle":"2023-11-07T11:39:32.185272Z","shell.execute_reply.started":"2023-11-07T11:39:32.1802Z","shell.execute_reply":"2023-11-07T11:39:32.184151Z"},"trusted":true},"execution_count":46,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-07T11:46:09.512166Z","iopub.execute_input":"2023-11-07T11:46:09.512576Z","iopub.status.idle":"2023-11-07T11:46:10.042474Z","shell.execute_reply.started":"2023-11-07T11:46:09.512526Z","shell.execute_reply":"2023-11-07T11:46:10.040837Z"},"trusted":true},"execution_count":52,"outputs":[{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pydicom/tag.py\u001b[0m in \u001b[0;36mtag_in_exception\u001b[0;34m(tag)\u001b[0m\n\u001b[1;32m     29\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 30\u001b[0;31m         \u001b[0;32myield\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     31\u001b[0m     \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mex\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\u001b[0m in \u001b[0;36mwrite_dataset\u001b[0;34m(fp, dataset, parent_encoding)\u001b[0m\n\u001b[1;32m    460\u001b[0m             \u001b[0;31m# XXX for writing raw tags without converting to DataElement\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 461\u001b[0;31m             \u001b[0mwrite_data_element\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtag\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataset_encoding\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    462\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\u001b[0m in \u001b[0;36mwrite_data_element\u001b[0;34m(fp, data_element, encoding)\u001b[0m\n\u001b[1;32m    424\u001b[0m                     not val.startswith(b'\\xff\\xfe\\xe0\\x00')):\n\u001b[0;32m--> 425\u001b[0;31m                 raise ValueError('Pixel Data with undefined length must '\n\u001b[0m\u001b[1;32m    426\u001b[0m                                  'start with an item tag')\n","\u001b[0;31mValueError\u001b[0m: Pixel Data with undefined length must start with an item tag","\nDuring handling of the above exception, another exception occurred:\n","\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)","\u001b[0;32m<ipython-input-52-75719695bd21>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     13\u001b[0m     \u001b[0mds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPixelRepresentation\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m     \u001b[0mds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPixelData\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp_image\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtobytes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m     \u001b[0mds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave_as\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m'.dcm'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pydicom/dataset.py\u001b[0m in \u001b[0;36msave_as\u001b[0;34m(self, filename, write_like_original)\u001b[0m\n\u001b[1;32m    956\u001b[0m                                  \"saving.\".format(self.__class__.__name__))\n\u001b[1;32m    957\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 958\u001b[0;31m         \u001b[0mpydicom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdcmwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwrite_like_original\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    959\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    960\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__setattr__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\u001b[0m in \u001b[0;36mdcmwrite\u001b[0;34m(filename, dataset, write_like_original)\u001b[0m\n\u001b[1;32m    848\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    849\u001b[0m         \u001b[0;31m# Write non-Command Set elements now\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 850\u001b[0;31m         \u001b[0mwrite_dataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0x00010000\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    851\u001b[0m     \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    852\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mcaller_owns_file\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\u001b[0m in \u001b[0;36mwrite_dataset\u001b[0;34m(fp, dataset, parent_encoding)\u001b[0m\n\u001b[1;32m    459\u001b[0m             \u001b[0;31m# write_data_element(fp, dataset.get_item(tag), dataset_encoding)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    460\u001b[0m             \u001b[0;31m# XXX for writing raw tags without converting to DataElement\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 461\u001b[0;31m             \u001b[0mwrite_data_element\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtag\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdataset_encoding\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    462\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    463\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mfp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtell\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mfpStart\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/contextlib.py\u001b[0m in \u001b[0;36m__exit__\u001b[0;34m(self, type, value, traceback)\u001b[0m\n\u001b[1;32m     97\u001b[0m                 \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     98\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 99\u001b[0;31m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgen\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mthrow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtraceback\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    100\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    101\u001b[0m                 \u001b[0;31m# Suppress StopIteration *unless* it's the same exception that\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pydicom/tag.py\u001b[0m in \u001b[0;36mtag_in_exception\u001b[0;34m(tag)\u001b[0m\n\u001b[1;32m     35\u001b[0m             \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     36\u001b[0m             stack_trace)\n\u001b[0;32m---> 37\u001b[0;31m         \u001b[0;32mraise\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     38\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     39\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mValueError\u001b[0m: With tag (7fe0, 0010) got exception: Pixel Data with undefined length must start with an item tag\nTraceback (most recent call last):\n  File \"/opt/conda/lib/python3.6/site-packages/pydicom/tag.py\", line 30, in tag_in_exception\n    yield\n  File \"/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\", line 461, in write_dataset\n    write_data_element(fp, dataset[tag], dataset_encoding)\n  File \"/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\", line 425, in write_data_element\n    raise ValueError('Pixel Data with undefined length must '\nValueError: Pixel Data with undefined length must start with an item tag\n"],"ename":"ValueError","evalue":"With tag (7fe0, 0010) got exception: Pixel Data with undefined length must start with an item tag\nTraceback (most recent call last):\n  File \"/opt/conda/lib/python3.6/site-packages/pydicom/tag.py\", line 30, in tag_in_exception\n    yield\n  File \"/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\", line 461, in write_dataset\n    write_data_element(fp, dataset[tag], dataset_encoding)\n  File \"/opt/conda/lib/python3.6/site-packages/pydicom/filewriter.py\", line 425, in write_data_element\n    raise ValueError('Pixel Data with undefined length must '\nValueError: Pixel Data with undefined length must start with an item tag\n","output_type":"error"}]},{"cell_type":"code","source":"def get_dicom_fps(dicom_dir):\n    dicom_fps = glob.glob(dicom_dir+'/'+'*.dcm')\n    return list(set(dicom_fps))\n\ndef parse_dataset(dicom_dir, anns): \n    image_fps = get_dicom_fps(dicom_dir)\n    image_annotations = {fp: [] for fp in image_fps}\n    for index, row in anns.iterrows(): \n        fp = os.path.join(dicom_dir, row['patientId']+'.dcm')\n        image_annotations[fp].append(row)\n    return image_fps, image_annotations ","metadata":{"id":"ivqC4cnszOaM","colab_type":"code","colab":{},"_uuid":"778cb19865d7cc63440491aef9202b71c61e8bb2","execution":{"iopub.status.busy":"2023-11-07T11:32:32.500892Z","iopub.execute_input":"2023-11-07T11:32:32.50134Z","iopub.status.idle":"2023-11-07T11:32:32.51739Z","shell.execute_reply.started":"2023-11-07T11:32:32.501267Z","shell.execute_reply":"2023-11-07T11:32:32.51641Z"},"trusted":true},"execution_count":40,"outputs":[]},{"cell_type":"code","source":"class DetectorConfig(Config):\n    NAME = 'builing'\n    GPU_COUNT = 1\n    IMAGES_PER_GPU = 8 \n    BACKBONE = 'resnet50'\n    NUM_CLASSES = 2  \n    IMAGE_MIN_DIM = 256\n    IMAGE_MAX_DIM = 256\n    RPN_ANCHOR_SCALES = (32, 64, 128, 256)\n    TRAIN_ROIS_PER_IMAGE = 32\n    MAX_GT_INSTANCES = 3\n    DETECTION_MAX_INSTANCES = 3\n    DETECTION_MIN_CONFIDENCE = 0.9\n    DETECTION_NMS_THRESHOLD = 0.1\n    STEPS_PER_EPOCH = 100\nconfig = DetectorConfig()\nconfig.display()","metadata":{"id":"_SfzTa-1zOck","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":917},"outputId":"91ae8935-bccb-4b8e-9a7e-aa690f95fd9b","_uuid":"dfcffc4eaa94a41497717851dee9f702d8a2a73b","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DetectorDataset(utils.Dataset):\n    def __init__(self, image_fps, image_annotations, orig_height, orig_width):\n        super().__init__(self)\n        self.add_class('pneumonia', 1, 'Lung Opacity')\n        for i, fp in enumerate(image_fps):\n            annotations = image_annotations[fp]\n            self.add_image('pneumonia', image_id=i, path=fp, \n                           annotations=annotations, orig_height=orig_height, orig_width=orig_width)\n    def image_reference(self, image_id):\n        info = self.image_info[image_id]\n        return info['path']\n    def load_image(self, image_id):\n        info = self.image_info[image_id]\n        fp = info['path']\n        ds = pydicom.read_file(fp)\n        image = ds.pixel_array\n        if len(image.shape) != 3 or image.shape[2] != 3:\n            image = np.stack((image,) * 3, -1)\n        return image\n    def load_mask(self, image_id):\n        info = self.image_info[image_id]\n        annotations = info['annotations']\n        count = len(annotations)\n        if count == 0:\n            mask = np.zeros((info['orig_height'], info['orig_width'], 1), dtype=np.uint8)\n            class_ids = np.zeros((1,), dtype=np.int32)\n        else:\n            mask = np.zeros((info['orig_height'], info['orig_width'], count), dtype=np.uint8)\n            class_ids = np.zeros((count,), dtype=np.int32)\n            for i, a in enumerate(annotations):\n                if a['Target'] == 1:\n                    x = int(a['x'])\n                    y = int(a['y'])\n                    w = int(a['width'])\n                    h = int(a['height'])\n                    mask_instance = mask[:, :, i].copy()\n                    cv2.rectangle(mask_instance, (x, y), (x+w, y+h), 255, -1)\n                    mask[:, :, i] = mask_instance\n                    class_ids[i] = 1\n        return mask.astype(np.bool), class_ids.astype(np.int32)","metadata":{"id":"8EBVA1M60yAj","colab_type":"code","colab":{},"_uuid":"52bd3ffbdde0173a363055482d675da51c2aba99","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nanns = pd.read_csv(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\")\nanns.tail()","metadata":{"id":"EdhUEFDr0yDA","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":235},"outputId":"1715a5df-a577-41fd-bf20-f1a27aadb28c","_uuid":"793b1c6c6ba4e5f0d51e130080aa799f230b5ef6","execution":{"iopub.status.busy":"2023-11-07T10:45:35.950849Z","iopub.execute_input":"2023-11-07T10:45:35.951274Z","iopub.status.idle":"2023-11-07T10:45:36.063945Z","shell.execute_reply.started":"2023-11-07T10:45:35.951221Z","shell.execute_reply":"2023-11-07T10:45:36.063161Z"},"trusted":true},"execution_count":5,"outputs":[{"execution_count":5,"output_type":"execute_result","data":{"text/plain":"                                  patientId      x      y  width  height  \\\n30222  c1ec14ff-f6d7-4b38-b0cb-fe07041cbdc8  185.0  298.0  228.0   379.0   \n30223  c1edf42b-5958-47ff-a1e7-4f23d99583ba    NaN    NaN    NaN     NaN   \n30224  c1f6b555-2eb1-4231-98f6-50a963976431    NaN    NaN    NaN     NaN   \n30225  c1f7889a-9ea9-4acb-b64c-b737c929599a  570.0  393.0  261.0   345.0   \n30226  c1f7889a-9ea9-4acb-b64c-b737c929599a  233.0  424.0  201.0   356.0   \n\n       Target  \n30222       1  \n30223       0  \n30224       0  \n30225       1  \n30226       1  ","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>patientId</th>\n      <th>x</th>\n      <th>y</th>\n      <th>width</th>\n      <th>height</th>\n      <th>Target</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>30222</th>\n      <td>c1ec14ff-f6d7-4b38-b0cb-fe07041cbdc8</td>\n      <td>185.0</td>\n      <td>298.0</td>\n      <td>228.0</td>\n      <td>379.0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>30223</th>\n      <td>c1edf42b-5958-47ff-a1e7-4f23d99583ba</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>30224</th>\n      <td>c1f6b555-2eb1-4231-98f6-50a963976431</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>30225</th>\n      <td>c1f7889a-9ea9-4acb-b64c-b737c929599a</td>\n      <td>570.0</td>\n      <td>393.0</td>\n      <td>261.0</td>\n      <td>345.0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>30226</th>\n      <td>c1f7889a-9ea9-4acb-b64c-b737c929599a</td>\n      <td>233.0</td>\n      <td>424.0</td>\n      <td>201.0</td>\n      <td>356.0</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"import json\nimport os\njson_path = os.path.join('/kaggle/input/synthetic-word-ocr/annotation.json')\nwith open(json_path, 'r') as f:\n    annot_data = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T10:44:02.015372Z","iopub.execute_input":"2023-11-07T10:44:02.015805Z","iopub.status.idle":"2023-11-07T10:44:37.757126Z","shell.execute_reply.started":"2023-11-07T10:44:02.015712Z","shell.execute_reply":"2023-11-07T10:44:37.75622Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"code","source":"image_fps, image_annotations = parse_dataset(train_dicom_dir, anns=anns)","metadata":{"id":"Mxz-pNbt5txY","colab_type":"code","colab":{},"_uuid":"7aebc88f910b232e3b8759421914a007c6ffed94","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pydicom.read_file(image_fps[0]) \nimage = ds.pixel_array","metadata":{"id":"YPqjEIXWRhSf","colab_type":"code","colab":{},"_uuid":"6c386dcef041b972f6209dd19e247d547c3c349f","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds","metadata":{"id":"81lovwF2Ro5R","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":647},"outputId":"e2263fe2-1a32-432a-ec75-b9220a24e697","_uuid":"0ef68a41cf1a5e842e86a219b6392e3695004720","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ORIG_SIZE = 300","metadata":{"id":"gYNSd1AhRqOV","colab_type":"code","colab":{},"_uuid":"74277ae9af4a3b044e62b664d10d76b23848bb43","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_fps_list = list(image_fps[:1000]) \nsorted(image_fps_list)\nrandom.seed(42)\nrandom.shuffle(image_fps_list)\nvalidation_split = 0.1\nsplit_index = int((1 - validation_split) * len(image_fps_list))\nimage_fps_train = image_fps_list[:split_index]\nimage_fps_val = image_fps_list[split_index:]\nprint(len(image_fps_train), len(image_fps_val))","metadata":{"id":"7jByVCZt-ZOC","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"outputId":"f1aa267d-7530-4620-ffc5-2f7aa39083bb","_uuid":"6175c72e73639e3190e127f67783988eadced9ba","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_train = DetectorDataset(image_fps_train, image_annotations, ORIG_SIZE, ORIG_SIZE)\ndataset_train.prepare()","metadata":{"id":"jwMkhotP0yFf","colab_type":"code","colab":{},"_uuid":"86c3333d4dfb8b7d00ce1f401693d0df4e6254e1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_fp = random.choice(image_fps_train)\nimage_annotations[test_fp]","metadata":{"id":"0xEc47Jz59x5","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":251},"outputId":"129edfbc-cf9d-46c7-b569-d804a50cd12d","_uuid":"93da5a58731ad483a4bd2b20543f2b1df4b8ad74","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_val = DetectorDataset(image_fps_val, image_annotations, ORIG_SIZE, ORIG_SIZE)\ndataset_val.prepare()","metadata":{"id":"K1TkWuGP0yHl","colab_type":"code","colab":{},"_uuid":"313347d838fa8321a714858c8073f98c50c5be26","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = dataset_train.image_ids[290]\nimage_fp = dataset_train.image_reference(image_id)\nimage = dataset_train.load_image(image_id)\nmask, class_ids = dataset_train.load_mask(image_id)\nprint(image.shape)\nplt.figure(figsize=(10, 10))\nplt.subplot(1, 2, 1)\nplt.imshow(image[:, :, 0], cmap='gray')\nplt.axis('off')\nplt.subplot(1, 2, 2)\nmasked = np.zeros(image.shape[:2])\nfor i in range(mask.shape[2]):\n    masked += image[:, :, 0] * mask[:, :, i]\nplt.imshow(masked, cmap='gray')\nplt.axis('off')\nprint(image_fp)\nprint(class_ids)","metadata":{"id":"4xwsrf9G1lHR","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":359},"outputId":"a13386d3-a918-41fe-8824-13625c9d7b08","_uuid":"491b78ec96d28fcdbbf8e2d7f9320a05d64c9249","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = modellib.MaskRCNN(mode='training', config=config, model_dir=ROOT_DIR)","metadata":{"id":"geTvh0sU1lJo","colab_type":"code","colab":{},"_uuid":"ac5e11c89daeaa4e73af6a4967a4a375cddf284c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image augmentation \naugmentation = iaa.SomeOf((0, 1), [\n    iaa.Fliplr(0.5),\n    iaa.Affine(\n        scale={\"x\": (0.8, 1.2), \"y\": (0.8, 1.2)},\n        translate_percent={\"x\": (-0.2, 0.2), \"y\": (-0.2, 0.2)},\n        rotate=(-25, 25),\n        shear=(-8, 8)\n    ),\n    iaa.Multiply((0.9, 1.1))\n])","metadata":{"id":"STZnQTE61lME","colab_type":"code","colab":{},"_uuid":"4ab9d6086ce611a46f189c047956c43b29783e6d","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_EPOCHS = 1\nimport warnings \nwarnings.filterwarnings(\"ignore\")\nmodel.train(dataset_train, dataset_val, \n            learning_rate=config.LEARNING_RATE, \n            epochs=NUM_EPOCHS, \n            layers='all',\n            augmentation=augmentation)","metadata":{"id":"RVgNhHjl1lOS","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":2575},"outputId":"2cba9efc-eeea-472d-d155-3c3d856585bf","_uuid":"64cce2581ffdb8c2b1cb07948ada4a93f64874b0","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_names = next(os.walk(model.model_dir))[1]\nkey = config.NAME.lower()\ndir_names = filter(lambda f: f.startswith(key), dir_names)\ndir_names = sorted(dir_names)\n\nif not dir_names:\n    import errno\n    raise FileNotFoundError(\n        errno.ENOENT,\n        \"Could not find model directory under {}\".format(self.model_dir))\n    \nfps = []\nfor d in dir_names: \n    dir_name = os.path.join(model.model_dir, d)\n    checkpoints = next(os.walk(dir_name))[2]\n    checkpoints = filter(lambda f: f.startswith(\"mask_rcnn\"), checkpoints)\n    checkpoints = sorted(checkpoints)\n    if not checkpoints:\n        print('No weight files in {}'.format(dir_name))\n    else: \n      \n      checkpoint = os.path.join(dir_name, checkpoints[-1])\n      fps.append(checkpoint)\n\nmodel_path = sorted(fps)[-1]\nprint('Found model {}'.format(model_path))","metadata":{"id":"eraRlzgPmmIZ","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"outputId":"de9e688c-ba4f-4b62-f842-dbcf00ce397c","_uuid":"db5c10d3f7da099e5751a04a6e6d49819882ecd4","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class InferenceConfig(DetectorConfig):\n    GPU_COUNT = 1\n    IMAGES_PER_GPU = 1\ninference_config = InferenceConfig()\nmodel = modellib.MaskRCNN(mode='inference', \n                          config=inference_config,\n                          model_dir=ROOT_DIR)\nassert model_path != \"\", \"Provide path to trained weights\"\nprint(\"Loading weights from \", model_path)\nmodel.load_weights(model_path, by_name=True)","metadata":{"id":"TgpT9AzC2Bgz","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":53},"outputId":"60f5a175-4666-497d-b4e8-0bdab39a92d0","_uuid":"52138636b2ae5bf444bba808518cd8313bde65cd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_colors_for_class_ids(class_ids):\n    colors = []\n    for class_id in class_ids:\n        if class_id == 1:\n            colors.append((.941, .204, .204))\n    return colors","metadata":{"id":"9mTBig7D2BjU","colab_type":"code","colab":{},"_uuid":"e13c61bee23b791c61ecf1256f7512295cd4d9ab","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = dataset_val\nfig = plt.figure(figsize=(10, 30))\nfor i in range(4):\n\n    image_id = random.choice(dataset.image_ids)\n    \n    original_image, image_meta, gt_class_id, gt_bbox, gt_mask =\\\n        modellib.load_image_gt(dataset_val, inference_config, \n                               image_id, use_mini_mask=False)\n    print(original_image.shape)\n    plt.subplot(6, 2, 2*i + 1)\n    visualize.display_instances(original_image, gt_bbox, gt_mask, gt_class_id, \n                                dataset.class_names,\n                                colors=get_colors_for_class_ids(gt_class_id), ax=fig.axes[-1])\n    plt.subplot(6, 2, 2*i + 2)\n    results = model.detect([original_image]) \n    r = results[0]\n    visualize.display_instances(original_image, r['rois'], r['masks'], r['class_ids'], \n                                dataset.class_names, r['scores'], \n                                colors=get_colors_for_class_ids(r['class_ids']), ax=fig.axes[-1])","metadata":{"id":"irheTbrW2Bl0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":1394},"outputId":"56041ad4-173d-45ab-af67-f54e8333511e","_uuid":"186412199e25b98719f71cfe5e8869abcce516c4","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_fps = get_dicom_fps(test_dicom_dir)","metadata":{"id":"qRWBVJKYNdWM","colab_type":"code","colab":{},"_uuid":"fd9f53fa319a425693e07fe4898ddeeaa5d07f99","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(image_fps, filepath='submission.csv', min_conf=0.95): \n    resize_factor = ORIG_SIZE / config.IMAGE_SHAPE[0]\n    #resize_factor = ORIG_SIZE \n    with open(filepath, 'w') as file:\n      for image_id in tqdm(image_fps): \n        ds = pydicom.read_file(image_id)\n        image = ds.pixel_array\n        if len(image.shape) != 3 or image.shape[2] != 3:\n            image = np.stack((image,) * 3, -1) \n        image, window, scale, padding, crop = utils.resize_image(\n            image,\n            min_dim=config.IMAGE_MIN_DIM,\n            min_scale=config.IMAGE_MIN_SCALE,\n            max_dim=config.IMAGE_MAX_DIM,\n            mode=config.IMAGE_RESIZE_MODE)\n            \n        patient_id = os.path.splitext(os.path.basename(image_id))[0]\n\n        results = model.detect([image])\n        r = results[0]\n\n        out_str = \"\"\n        out_str += patient_id \n        out_str += \",\"\n        assert( len(r['rois']) == len(r['class_ids']) == len(r['scores']) )\n        if len(r['rois']) == 0: \n            pass\n        else: \n            num_instances = len(r['rois'])\n            for i in range(num_instances): \n                if r['scores'][i] > min_conf: \n                    out_str += ' '\n                    out_str += str(round(r['scores'][i], 2))\n                    out_str += ' '\n                    x1 = r['rois'][i][1]\n                    y1 = r['rois'][i][0]\n                    width = r['rois'][i][3] - x1 \n                    height = r['rois'][i][2] - y1 \n                    bboxes_str = \"{} {} {} {}\".format(x1*resize_factor, y1*resize_factor, \\\n                                                       width*resize_factor, height*resize_factor)   \n        file.write(out_str+\"\\n\")","metadata":{"id":"C6UWVrbM2Bob","colab_type":"code","colab":{},"_uuid":"4a5c0c6134408ddbf5a34496d7e9d7be5692e9a1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize(): \n    image_id = random.choice(test_image_fps)\n    ds = pydicom.read_file(image_id)\n    image = ds.pixel_array\n    resize_factor = ORIG_SIZE / config.IMAGE_SHAPE[0]\n    if len(image.shape) != 3 or image.shape[2] != 3:\n        image = np.stack((image,) * 3, -1) \n    resized_image, window, scale, padding, crop = utils.resize_image(\n        image,\n        min_dim=config.IMAGE_MIN_DIM,\n        min_scale=config.IMAGE_MIN_SCALE,\n        max_dim=config.IMAGE_MAX_DIM,\n        mode=config.IMAGE_RESIZE_MODE)\n    patient_id = os.path.splitext(os.path.basename(image_id))[0]\n    print(patient_id)\n    results = model.detect([resized_image])\n    r = results[0]\n    for bbox in r['rois']: \n        print(bbox)\n        x1 = int(bbox[1] * resize_factor)\n        y1 = int(bbox[0] * resize_factor)\n        x2 = int(bbox[3] * resize_factor)\n        y2 = int(bbox[2]  * resize_factor)\n        cv2.rectangle(image, (x1,y1), (x2,y2), (77, 255, 9), 3, 1)\n        width = x2 - x1 \n        height = y2 - y1 \n        print(\"x {} y {} h {} w {}\".format(x1, y1, width, height))\n    plt.figure() \n    plt.imshow(image, cmap=plt.cm.gist_gray)\n\nvisualize()","metadata":{"_uuid":"ea110f197abc2acb1c3435383f7259079dc0eb0e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}