├── .gitignore ├── LICENSE ├── README.md ├── images ├── annotator.png ├── kaggle-project-clone-small.gif ├── kaggle-project-clone.gif └── kaggle-project.png ├── lesson1-xray-images-classification.ipynb ├── lesson2-lung-xrays-segmentation.ipynb ├── lesson3-rsna-pneumonia-detection-kaggle.ipynb ├── lesson3-rsna-pneumonia-detection-mdai-client-lib.ipynb └── pneumonia-detection-clone-project-data.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2020 MD.ai, Inc. 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introductory lessons to deep learning for medical imaging by [MD.ai](https://www.md.ai) 2 | 3 | The following are several Jupyter notebooks covering the basics of training deep learning models for medical imaging using data from MD.ai. They demonstrate how to download and parse annotation data from MD.ai, as well as train and evaluate different deep learning models for classification, segmentation, and object detection problems. The notebooks can be run on Google Colab with GPU (see instruction below). 4 | 5 | | # | Name | GitHub | MD.ai | Colab | 6 | |---|---|---|---|---| 7 | | 1 | Classification of chest vs. adominal X-rays using TensorFlow/Keras | [Link](https://github.com/mdai/ml-lessons/blob/master/lesson1-xray-images-classification.ipynb) | [Link](https://public.md.ai/annotator/project/PVq9raBJ) | [](https://colab.research.google.com/github/mdai/ml-lessons/blob/master/lesson1-xray-images-classification.ipynb) | 8 | | 2 | Lung X-Rays Semantic Segmentation using U-Nets | [Link](https://github.com/mdai/ml-lessons/blob/master/lesson2-lung-xrays-segmentation.ipynb) | [Link](https://public.md.ai/annotator/project/aGq4k6NW) | [](https://colab.research.google.com/github/mdai/ml-lessons/blob/master/lesson2-lung-xrays-segmentation.ipynb) | 9 | | 3a | RSNA Pneumonia detection using Kaggle data format | [Link](https://github.com/mdai/ml-lessons/blob/master/lesson3-rsna-pneumonia-detection-kaggle.ipynb) | [Link](https://public.md.ai/annotator/project/LxR6zdR2) | [](https://colab.research.google.com/github/mdai/ml-lessons/blob/master/lesson3-rsna-pneumonia-detection-kaggle.ipynb) | 10 | | 3b | RSNA Pneumonia detection using the MD.ai python client library | [Link](https://github.com/mdai/ml-lessons/blob/master/lesson3-rsna-pneumonia-detection-mdai-client-lib.ipynb) | [Link](https://public.md.ai/annotator/project/LxR6zdR2) | [](https://colab.research.google.com/github/mdai/ml-lessons/blob/master/lesson3-rsna-pneumonia-detection-mdai-client-lib.ipynb) | 11 | 12 | *Note that the mdai client requires an access token, which authenticates you as the user. To create a new token or select an existing token, to go a specific MD.ai domain (e.g., public.md.ai), register, then navigate to the "Personal Access Tokens" tab on your user settings page to create and obtain your access token.* 13 | 14 | ## MD.ai Annotator 15 | 16 | MD.ai annotator is a web-based application to store, view, and collaboratively annotate medical images (e.g, DICOM) in the cloud. The MD.ai python client library can be used to download images and annotations, prepare the datasets, and then be used to train and evaluate deep learning models. Further documentation and videos are available at https://docs.md.ai/. 17 | 18 | - MD.ai Annotator example project URL: https://public.md.ai/annotator/project/aGq4k6NW/workspace 19 | - MD.ai python client libray URL: https://github.com/mdai/mdai-client-py 20 | 21 |  22 | 23 | ## Running Jupyter notebooks on Google Colab 24 | 25 | It’s easy to run a Jupyter notebook on Google Colab with free GPU use within time-limited sessions. For example, add the Github Jupyter notebook path to https://colab.research.google.com/notebook: 26 | 27 | Select the "GITHUB" tab, and add the Lesson 1 URL: https://github.com/mdai/ml-lessons/blob/master/lesson1-xray-images-classification.ipynb 28 | 29 | To use the GPU, in the notebook menu, go to Runtime -> Change runtime type -> switch to Python 3, and turn on GPU. See more Colab tips and tricks [here](https://www.kdnuggets.com/2018/02/essential-google-colaboratory-tips-tricks.html). 30 | 31 | --- 32 | 33 | © 2020 MD.ai, Inc. 34 | Licensed under the Apache License, Version 2.0 35 | -------------------------------------------------------------------------------- /images/annotator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mdai/ml-lessons/e5a0c560c38cf5f1c3a1ef5632d87c5c3a4a78b8/images/annotator.png -------------------------------------------------------------------------------- /images/kaggle-project-clone-small.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mdai/ml-lessons/e5a0c560c38cf5f1c3a1ef5632d87c5c3a4a78b8/images/kaggle-project-clone-small.gif -------------------------------------------------------------------------------- /images/kaggle-project-clone.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mdai/ml-lessons/e5a0c560c38cf5f1c3a1ef5632d87c5c3a4a78b8/images/kaggle-project-clone.gif -------------------------------------------------------------------------------- /images/kaggle-project.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mdai/ml-lessons/e5a0c560c38cf5f1c3a1ef5632d87c5c3a4a78b8/images/kaggle-project.png -------------------------------------------------------------------------------- /pneumonia-detection-clone-project-data.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "*Copyright 2018 MD.ai, Inc. \n", 8 | "Licensed under the Apache License, Version 2.0*\n", 9 | "\n", 10 | "# Create additional annotations using the MD.ai Annotator " 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "#### Pneumonia Detection Challenge: https://www.kaggle.com/c/rsna-pneumonia-detection-challenge \n", 18 | "\n", 19 | "For further data exploration and to create your own additional annotations, clone the MD.ai project at: \n", 20 | "https://public.md.ai/annotator/project/LxR6zdR2. \n", 21 | "\n", 22 | "You can then create your own team, add new labels and additional annotations. The “Users“ tab will allow you to create teams and assign exams to team members. You can track progress and export your new annotations in JSON format.\n", 23 | "\n", 24 | "Further instructions and videos are available at https://docs.md.ai." 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": { 30 | "colab_type": "text", 31 | "id": "z0i1JLTF8_Pu" 32 | }, 33 | "source": [ 34 | "### Clone the Annotator Project\n", 35 | "\n", 36 | "RSNA Pneumonia Detection Challenge Annotator project URL: \n", 37 | "https://public.md.ai/annotator/project/LxR6zdR2/workspace. \n", 38 | "\n", 39 | "To add annotations to the cloned project, you need to clone the project first. \n", 40 | "\n", 41 | "First, navigate to the original project URL (above), click on \"Clone Project\" button. \n", 42 | "\n", 43 | "\n" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": { 49 | "colab_type": "text", 50 | "id": "FdivV_kX8_PN" 51 | }, 52 | "source": [ 53 | "**Intro to deep learning for medical imaging lessons**\n", 54 | "\n", 55 | "- Lesson 1. Classification of chest vs. adominal X-rays using TensorFlow/Keras [Github](https://github.com/mdai/ml-lessons/blob/master/lesson1-xray-images-classification.ipynb) [Annotator](https://public.md.ai/annotator/project/PVq9raBJ)\n", 56 | "\n", 57 | "- Lesson 2. Lung X-Rays Semantic Segmentation using UNets. [Github](https://github.com/mdai/ml-lessons/blob/master/lesson2-lung-xrays-segmentation.ipynb)\n", 58 | "[Annotator](https://public.md.ai/annotator/project/aGq4k6NW/workspace) \n", 59 | "\n", 60 | "- Lesson 3. RSNA Pneumonia detection using Kaggle data format [Github](https://github.com/mdai/ml-lessons/blob/master/lesson3-rsna-pneumonia-detection-kaggle.ipynb) [Annotator](https://public.md.ai/annotator/project/LxR6zdR2/workspace) \n", 61 | " \n", 62 | "- Lesson 3. RSNA Pneumonia detection using MD.ai python client library [Github](https://github.com/mdai/ml-lessons/blob/master/lesson3-rsna-pneumonia-detection-mdai-client-lib.ipynb) [Annotator](https://public.md.ai/annotator/project/LxR6zdR2/workspace)\n", 63 | "\n", 64 | "- MD.ai python client libray URL: https://github.com/mdai/mdai-client-py\n", 65 | "- MD.ai documentation URL: https://docs.md.ai/" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 1, 71 | "metadata": { 72 | "colab": {}, 73 | "colab_type": "code", 74 | "id": "lVBBH_EF8_PU" 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "import os\n", 79 | "import sys\n", 80 | "import json\n", 81 | "import pydicom\n", 82 | "import pandas as pd" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": { 88 | "colab_type": "text", 89 | "id": "0a1bLOhX8_PW" 90 | }, 91 | "source": [ 92 | "### Import the `mdai` library\n", 93 | "\n", 94 | "Run the block below to install the `mdai` client library into your python environment." 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 2, 100 | "metadata": { 101 | "colab": { 102 | "base_uri": "https://localhost:8080/", 103 | "height": 35 104 | }, 105 | "colab_type": "code", 106 | "id": "cYvDgipk8_PX", 107 | "outputId": "28c7fa59-b481-4b40-dfe1-3e2f49cbb9a5" 108 | }, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "'0.0.5'" 114 | ] 115 | }, 116 | "execution_count": 2, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "!pip install --upgrade --quiet mdai\n", 123 | "import mdai\n", 124 | "mdai.__version__" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 3, 130 | "metadata": { 131 | "colab": {}, 132 | "colab_type": "code", 133 | "id": "WZQjgjnK8_Pb" 134 | }, 135 | "outputs": [], 136 | "source": [ 137 | "# Root directory of the project \n", 138 | "ROOT_DIR = os.path.abspath('./lesson3-data')" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": { 144 | "colab_type": "text", 145 | "id": "RrVjmqtJ8_Po" 146 | }, 147 | "source": [ 148 | "### Create an `mdai` client\n", 149 | "\n", 150 | "The mdai client requires an access token, which authenticates you as the user. To create a new token or select an existing token, navigate to the \"Personal Access Tokens\" tab on your user settings page at the specified MD.ai domain (e.g., public.md.ai).\n", 151 | "\n", 152 | "**Important: keep your access tokens safe. Do not ever share your tokens.**" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 4, 158 | "metadata": { 159 | "colab": { 160 | "base_uri": "https://localhost:8080/", 161 | "height": 35 162 | }, 163 | "colab_type": "code", 164 | "id": "XB28YDKw8_Pq", 165 | "outputId": "68eed3fe-121f-4966-f53d-bd734025e262" 166 | }, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "Successfully authenticated to public.md.ai.\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "mdai_client = mdai.Client(domain='public.md.ai', access_token=\"MY_PERSONAL_ACCESS_TOKEN\")" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": { 183 | "colab_type": "text", 184 | "id": "Zl509Hs38_Pu" 185 | }, 186 | "source": [ 187 | "### Define project\n", 188 | "\n", 189 | "Define a project you have access to by passing in the project id. The project id can be found in the URL in the following format: `https://public.md.ai/annotator/project/{project_id}`.\n", 190 | "\n", 191 | "For example, `project_id` would be `XXXX` for `https://public.md.ai/annotator/project/XXXX`.\n", 192 | "\n", 193 | "Specify optional `path` as the data directory (if left blank, will default to current working directory)." 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 5, 199 | "metadata": { 200 | "colab": {}, 201 | "colab_type": "code", 202 | "id": "uXUv3t_98_Pv" 203 | }, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "Using path '/home/txia/mdai-git/ml-lessons/lesson3-data' for data.\n", 210 | "Preparing annotations export for project EoBKoMBG... \n", 211 | "Preparing images export for project EoBKoMBG... \n", 212 | "Using cached images data for project EoBKoMBG.\n", 213 | "Using cached annotations data for project EoBKoMBG.\n" 214 | ] 215 | } 216 | ], 217 | "source": [ 218 | "# use cloned project_id! \n", 219 | "CLONED_PROJECT_ID = 'EoBKoMBG' \n", 220 | "p = mdai_client.project(project_id=CLONED_PROJECT_ID, path=ROOT_DIR)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": { 226 | "colab_type": "text", 227 | "id": "PXiRWnxX8_Pz" 228 | }, 229 | "source": [ 230 | "## Prepare data\n", 231 | "\n", 232 | "### Grab the label ids. You'll need these to create a label dictionary." 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 6, 238 | "metadata": { 239 | "colab": { 240 | "base_uri": "https://localhost:8080/", 241 | "height": 107 242 | }, 243 | "colab_type": "code", 244 | "id": "EDecTsz28_P0", 245 | "outputId": "aeec82e1-7a0b-477f-fd37-6ec1a5a7b4df" 246 | }, 247 | "outputs": [ 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "Label Group, Id: G_q563m2, Name: Default group\n", 253 | "\tLabels:\n", 254 | "\tId: L_NBy1aB, Name: Lung Opacity\n", 255 | "\tId: L_Wdjx2B, Name: No Lung Opacity\n", 256 | "\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "p.show_label_groups()" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": { 267 | "colab_type": "text", 268 | "id": "gB22GyWU8_P4" 269 | }, 270 | "source": [ 271 | "### Set label ids\n", 272 | "\n", 273 | "Selected label ids must be explicitly set by `Project#set_label_ids` method in order to prepare datasets." 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "## Note: Your label ids and dataset ids will be different. Use show_label_groups() and show_datasets() to find your specific ids." 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 7, 286 | "metadata": { 287 | "colab": { 288 | "base_uri": "https://localhost:8080/", 289 | "height": 71 290 | }, 291 | "colab_type": "code", 292 | "id": "ChuEtYTz8_P4", 293 | "outputId": "67326c39-cc58-414b-9189-f9337e24a9f2" 294 | }, 295 | "outputs": [ 296 | { 297 | "name": "stdout", 298 | "output_type": "stream", 299 | "text": [ 300 | "{'L_Wdjx2B': 0, 'L_NBy1aB': 1}\n", 301 | "None\n", 302 | "bbox\n" 303 | ] 304 | } 305 | ], 306 | "source": [ 307 | "# this maps label ids to class ids\n", 308 | "# make sure this matches the Kaggle dataset\n", 309 | "# target = 0: No Lung Opacity \n", 310 | "# target = 1: Lung Opacity \n", 311 | "labels_dict = {\n", 312 | " 'L_Wdjx2B':0, # target = 0, background \n", 313 | " 'L_NBy1aB':1, # target = 1, lung opacity \n", 314 | " }\n", 315 | "\n", 316 | "print(labels_dict)\n", 317 | "p.set_labels_dict(labels_dict)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "### Use this formula to find your specific dataset id and use it to load dataset." 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 8, 330 | "metadata": { 331 | "colab": { 332 | "base_uri": "https://localhost:8080/", 333 | "height": 71 334 | }, 335 | "colab_type": "code", 336 | "id": "dFJRPTL18_P8", 337 | "outputId": "a198f18a-ab68-4eda-d28e-af888206d35e" 338 | }, 339 | "outputs": [ 340 | { 341 | "name": "stdout", 342 | "output_type": "stream", 343 | "text": [ 344 | "Datasets:\n", 345 | "Id: D_gEX5do, Name: stage 1 train\n", 346 | "\n" 347 | ] 348 | } 349 | ], 350 | "source": [ 351 | "p.show_datasets()" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 9, 357 | "metadata": { 358 | "colab": {}, 359 | "colab_type": "code", 360 | "id": "1K-f6JdP8_QB" 361 | }, 362 | "outputs": [], 363 | "source": [ 364 | "dataset = p.get_dataset_by_id('D_gEX5do')\n", 365 | "dataset.prepare()" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 10, 371 | "metadata": { 372 | "colab": { 373 | "base_uri": "https://localhost:8080/", 374 | "height": 53 375 | }, 376 | "colab_type": "code", 377 | "id": "xBIVY6IL8_QE", 378 | "outputId": "833066ff-317a-4c9d-f38b-1e7c9269b16d" 379 | }, 380 | "outputs": [ 381 | { 382 | "name": "stdout", 383 | "output_type": "stream", 384 | "text": [ 385 | "Label id: L_Wdjx2B, Class id: 0, Class text: No Lung Opacity\n", 386 | "Label id: L_NBy1aB, Class id: 1, Class text: Lung Opacity\n" 387 | ] 388 | } 389 | ], 390 | "source": [ 391 | "dataset.show_classes()" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 11, 397 | "metadata": {}, 398 | "outputs": [], 399 | "source": [ 400 | "# generate kaggle labels format (see, stage_1_train_labels.csv)\n", 401 | "\n", 402 | "# use dataset object from above \n", 403 | "image_ids = dataset.get_image_ids() \n", 404 | "\n", 405 | "kaggle_data = []\n", 406 | "for image_id in image_ids: \n", 407 | " ds = pydicom.dcmread(image_id) \n", 408 | " anns = dataset.get_annotations_by_image_id(image_id)\n", 409 | " for ann in anns: \n", 410 | " labelId = ann['labelId']\n", 411 | " target = int(dataset.label_id_to_class_id(labelId))\n", 412 | "\n", 413 | " if target == 0: \n", 414 | " kaggle_data.append((ds.PatientID, None, None, None, None, target))\n", 415 | "\n", 416 | " elif target == 1: \n", 417 | "\n", 418 | " x = ann['data']['x']\n", 419 | " y = ann['data']['y']\n", 420 | " height = ann['data']['height']\n", 421 | " width = ann['data']['width']\n", 422 | " kaggle_data.append((ds.PatientID, x, y, height, width, target))\n", 423 | " else: \n", 424 | " raise ValueError('Target {} is invalid.'.format(target))\n", 425 | " \n", 426 | "kaggle_df = pd.DataFrame(kaggle_data, columns=['patientId', 'x', 'y', 'width', 'height', 'Target'])" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 12, 432 | "metadata": {}, 433 | "outputs": [], 434 | "source": [ 435 | "kaggle_df.to_csv('stage_1_train_cloned.csv')" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 13, 441 | "metadata": {}, 442 | "outputs": [ 443 | { 444 | "data": { 445 | "text/html": [ 446 | "
\n", 464 | " | patientId | \n", 465 | "x | \n", 466 | "y | \n", 467 | "width | \n", 468 | "height | \n", 469 | "Target | \n", 470 | "
---|---|---|---|---|---|---|
0 | \n", 475 | "00322d4d-1c29-4943-afc9-b6754be640eb | \n", 476 | "111.59540 | \n", 477 | "92.98391 | \n", 478 | "634.40922 | \n", 479 | "497.87585 | \n", 480 | "1 | \n", 481 | "
1 | \n", 484 | "003d8fa0-6bf1-40ed-b54c-ac657f8495c5 | \n", 485 | "NaN | \n", 486 | "NaN | \n", 487 | "NaN | \n", 488 | "NaN | \n", 489 | "0 | \n", 490 | "
2 | \n", 493 | "00313ee0-9eaa-42f4-b0ab-c148ed3241cd | \n", 494 | "NaN | \n", 495 | "NaN | \n", 496 | "NaN | \n", 497 | "NaN | \n", 498 | "0 | \n", 499 | "
3 | \n", 502 | "0004cfab-14fd-4e49-80ba-63a80b6bddd6 | \n", 503 | "175.58974 | \n", 504 | "281.97101 | \n", 505 | "429.23523 | \n", 506 | "286.53734 | \n", 507 | "1 | \n", 508 | "
4 | \n", 511 | "00436515-870c-4b36-a041-de91049b9ab4 | \n", 512 | "350.52874 | \n", 513 | "609.69195 | \n", 514 | "117.70117 | \n", 515 | "133.00229 | \n", 516 | "1 | \n", 517 | "