├── .gitattributes ├── .gitignore ├── Download_LITS_dataset_from_torrent.ipynb ├── LITS17-27772adef6f563a1ecc0ae19a528b956e6c803ce.torrent ├── LITS_prediction.ipynb ├── Model_Training.ipynb ├── README.md └── aap-lits.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.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 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # celery beat schedule file 95 | celerybeat-schedule 96 | 97 | # SageMath parsed files 98 | *.sage.py 99 | 100 | # Environments 101 | .env 102 | .venv 103 | env/ 104 | venv/ 105 | ENV/ 106 | env.bak/ 107 | venv.bak/ 108 | 109 | # Spyder project settings 110 | .spyderproject 111 | .spyproject 112 | 113 | # Rope project settings 114 | .ropeproject 115 | 116 | # mkdocs documentation 117 | /site 118 | 119 | # mypy 120 | .mypy_cache/ 121 | .dmypy.json 122 | dmypy.json 123 | 124 | # Pyre type checker 125 | .pyre/ 126 | -------------------------------------------------------------------------------- /Download_LITS_dataset_from_torrent.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Download LITS dataset from torrent", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "display_name": "Python 3", 12 | "language": "python", 13 | "name": "python3" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "aQuWDmfm9YOi", 21 | "colab_type": "text" 22 | }, 23 | "source": [ 24 | "# Torrent To Google Drive Downloader " 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": { 30 | "id": "qYk44mBwJf6E", 31 | "colab_type": "text" 32 | }, 33 | "source": [ 34 | "**Important Note:** To get more disk space:\n", 35 | "> Go to Runtime -> Change Runtime and give GPU as the Hardware Accelerator. You will get around 384GB to download any torrent you want." 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": { 41 | "id": "NaFa7M-e9YOr", 42 | "colab_type": "text" 43 | }, 44 | "source": [ 45 | "### Install libtorrent and Initialize Session" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "metadata": { 51 | "colab_type": "code", 52 | "id": "m6hF0emftx4h", 53 | "colab": {} 54 | }, 55 | "source": [ 56 | "!apt install python3-libtorrent\n", 57 | "\n", 58 | "import libtorrent as lt\n", 59 | "\n", 60 | "ses = lt.session()\n", 61 | "ses.listen_on(6881, 6891)\n", 62 | "downloads = []" 63 | ], 64 | "execution_count": 0, 65 | "outputs": [] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": { 70 | "id": "Uf90U73y9YOj", 71 | "colab_type": "text" 72 | }, 73 | "source": [ 74 | "### Mount Google Drive\n", 75 | "To stream files we need to mount Google Drive." 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "metadata": { 81 | "colab_type": "code", 82 | "id": "oWM9l2fvtuvO", 83 | "colab": {} 84 | }, 85 | "source": [ 86 | "from google.colab import drive\n", 87 | "\n", 88 | "drive.mount(\"/content/drive\")" 89 | ], 90 | "execution_count": 0, 91 | "outputs": [] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": { 96 | "id": "R_1XuuIf9YOn", 97 | "colab_type": "text" 98 | }, 99 | "source": [ 100 | "### Add From Torrent File\n", 101 | "You can run this cell to add more files as many times as you want" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "metadata": { 107 | "colab_type": "code", 108 | "id": "0et2A6N3udA0", 109 | "colab": {} 110 | }, 111 | "source": [ 112 | "from google.colab import files\n", 113 | "\n", 114 | "source = files.upload()\n", 115 | "params = {\n", 116 | " \"save_path\": \"/content/drive/My Drive/Torrent\",\n", 117 | " \"ti\": lt.torrent_info(list(source.keys())[0]),\n", 118 | "}\n", 119 | "downloads.append(ses.add_torrent(params))" 120 | ], 121 | "execution_count": 0, 122 | "outputs": [] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": { 127 | "id": "WD-6M6eZyzmj", 128 | "colab_type": "text" 129 | }, 130 | "source": [ 131 | "### Add From Magnet Link\n", 132 | "You can run this cell to add more files as many times as you want" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "metadata": { 138 | "id": "Cwi1GMlxy3te", 139 | "colab_type": "code", 140 | "colab": {} 141 | }, 142 | "source": [ 143 | "params = {\"save_path\": \"/content/drive/My Drive/Torrent\"}\n", 144 | "\n", 145 | "while True:\n", 146 | " magnet_link = input(\"Enter Magnet Link Or Type Exit: \")\n", 147 | " if magnet_link.lower() == \"exit\":\n", 148 | " break\n", 149 | " downloads.append(\n", 150 | " lt.add_magnet_uri(ses, magnet_link, params)\n", 151 | " )\n" 152 | ], 153 | "execution_count": 0, 154 | "outputs": [] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": { 159 | "id": "m-a1CUP39YOu", 160 | "colab_type": "text" 161 | }, 162 | "source": [ 163 | "### Start Download\n" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "metadata": { 169 | "colab_type": "code", 170 | "id": "DBNoYYoSuDBT", 171 | "colab": {} 172 | }, 173 | "source": [ 174 | "import time\n", 175 | "from IPython.display import display\n", 176 | "import ipywidgets as widgets\n", 177 | "\n", 178 | "state_str = [\n", 179 | " \"queued\",\n", 180 | " \"checking\",\n", 181 | " \"downloading metadata\",\n", 182 | " \"downloading\",\n", 183 | " \"finished\",\n", 184 | " \"seeding\",\n", 185 | " \"allocating\",\n", 186 | " \"checking fastresume\",\n", 187 | "]\n", 188 | "\n", 189 | "layout = widgets.Layout(width=\"auto\")\n", 190 | "style = {\"description_width\": \"initial\"}\n", 191 | "download_bars = [\n", 192 | " widgets.FloatSlider(\n", 193 | " step=0.01, disabled=True, layout=layout, style=style\n", 194 | " )\n", 195 | " for _ in downloads\n", 196 | "]\n", 197 | "display(*download_bars)\n", 198 | "\n", 199 | "while downloads:\n", 200 | " next_shift = 0\n", 201 | " for index, download in enumerate(downloads[:]):\n", 202 | " bar = download_bars[index + next_shift]\n", 203 | " if not download.is_seed():\n", 204 | " s = download.status()\n", 205 | "\n", 206 | " bar.description = \" \".join(\n", 207 | " [\n", 208 | " download.name(),\n", 209 | " str(s.download_rate / 1000),\n", 210 | " \"kB/s\",\n", 211 | " state_str[s.state],\n", 212 | " ]\n", 213 | " )\n", 214 | " bar.value = s.progress * 100\n", 215 | " else:\n", 216 | " next_shift -= 1\n", 217 | " ses.remove_torrent(download)\n", 218 | " downloads.remove(download)\n", 219 | " bar.close() # Seems to be not working in Colab (see https://github.com/googlecolab/colabtools/issues/726#issue-486731758)\n", 220 | " download_bars.remove(bar)\n", 221 | " print(download.name(), \"complete\")\n", 222 | " time.sleep(1)\n" 223 | ], 224 | "execution_count": 0, 225 | "outputs": [] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": { 230 | "id": "vNej2L-s5SZ_", 231 | "colab_type": "text" 232 | }, 233 | "source": [ 234 | "Unzip" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "metadata": { 240 | "id": "iJfV2GGy5Q4V", 241 | "colab_type": "code", 242 | "colab": {} 243 | }, 244 | "source": [ 245 | "import os, zipfile\n", 246 | "\n", 247 | "dir_name = '/gdrive/My Drive/Torrent/'\n", 248 | "extension = \".zip\"\n", 249 | "\n", 250 | "os.chdir(dir_name) # change directory from working dir to dir with files\n", 251 | "\n", 252 | "for item in os.listdir(dir_name): # loop through items in dir\n", 253 | " if item.endswith(extension): # check for \".zip\" extension\n", 254 | " file_name = os.path.abspath(item) # get full path of files\n", 255 | " zip_ref = zipfile.ZipFile(file_name) # create zipfile object\n", 256 | " zip_ref.extractall(dir_name) # extract file to dir\n", 257 | " zip_ref.close() # close file\n", 258 | " os.remove(file_name) # delete zipped file" 259 | ], 260 | "execution_count": 0, 261 | "outputs": [] 262 | } 263 | ] 264 | } -------------------------------------------------------------------------------- /LITS17-27772adef6f563a1ecc0ae19a528b956e6c803ce.torrent: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Auggen21/LITS-Challenge/017615c04d8b14ba97b223861e174900c362660b/LITS17-27772adef6f563a1ecc0ae19a528b956e6c803ce.torrent -------------------------------------------------------------------------------- /LITS_prediction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "LITS_prediction.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | } 13 | }, 14 | "cells": [ 15 | { 16 | "cell_type": "code", 17 | "metadata": { 18 | "id": "nZ84PAyvMPrm", 19 | "colab_type": "code", 20 | "outputId": "5b1ffdbf-510f-4422-9e73-cc5a6b403a93", 21 | "colab": { 22 | "base_uri": "https://localhost:8080/", 23 | "height": 125 24 | } 25 | }, 26 | "source": [ 27 | "from google.colab import drive\n", 28 | "drive.mount('/content/drive')" 29 | ], 30 | "execution_count": 0, 31 | "outputs": [ 32 | { 33 | "output_type": "stream", 34 | "text": [ 35 | "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n", 36 | "\n", 37 | "Enter your authorization code:\n", 38 | "··········\n", 39 | "Mounted at /content/drive\n" 40 | ], 41 | "name": "stdout" 42 | } 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "metadata": { 48 | "id": "dQbRQxu3L2OX", 49 | "colab_type": "code", 50 | "outputId": "ca87b26e-81be-426e-b0dd-9f9a352c8958", 51 | "colab": { 52 | "base_uri": "https://localhost:8080/", 53 | "height": 52 54 | } 55 | }, 56 | "source": [ 57 | "from keras.models import model_from_json\n", 58 | "json_file = open('/content/drive/My Drive/LITS/model_json.json', 'r')\n", 59 | "loaded_model_json = json_file.read()\n", 60 | "json_file.close()\n", 61 | "loaded_model = model_from_json(loaded_model_json)\n", 62 | "# load weights into new model\n", 63 | "loaded_model.load_weights(\"/content/drive/My Drive/LITS/model_weights.h5\")\n", 64 | "print(\"Loaded model from disk\")" 65 | ], 66 | "execution_count": 0, 67 | "outputs": [ 68 | { 69 | "output_type": "stream", 70 | "text": [ 71 | "Using TensorFlow backend.\n" 72 | ], 73 | "name": "stderr" 74 | }, 75 | { 76 | "output_type": "stream", 77 | "text": [ 78 | "Loaded model from disk\n" 79 | ], 80 | "name": "stdout" 81 | } 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "metadata": { 87 | "id": "HfEfnH_0MH9E", 88 | "colab_type": "code", 89 | "outputId": "4ac348da-1ec0-41b8-c64d-d09990b623b3", 90 | "colab": { 91 | "base_uri": "https://localhost:8080/", 92 | "height": 125 93 | } 94 | }, 95 | "source": [ 96 | "import nibabel as nib\n", 97 | "img_path='/content/drive/My Drive/LITS/Dataset/volume-23.nii'\n", 98 | "img_ex = nib.load(img_path).get_data()\n", 99 | "#mask_ex = nib.load(mask_path[25]).get_data()" 100 | ], 101 | "execution_count": 0, 102 | "outputs": [ 103 | { 104 | "output_type": "stream", 105 | "text": [ 106 | "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: get_data() is deprecated in favor of get_fdata(), which has a more predictable return type. To obtain get_data() behavior going forward, use numpy.asanyarray(img.dataobj).\n", 107 | "\n", 108 | "* deprecated from version: 3.0\n", 109 | "* Will raise as of version: 5.0\n", 110 | " This is separate from the ipykernel package so we can avoid doing imports until\n" 111 | ], 112 | "name": "stderr" 113 | } 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "metadata": { 119 | "id": "MQ3OOLYLOs6Y", 120 | "colab_type": "code", 121 | "colab": {} 122 | }, 123 | "source": [ 124 | "patch_ratio = []\n", 125 | "\n", 126 | "for i in range(16 + 1):\n", 127 | " patch_ratio.append(32 * i)" 128 | ], 129 | "execution_count": 0, 130 | "outputs": [] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "metadata": { 135 | "id": "clyy-_wdOWQS", 136 | "colab_type": "code", 137 | "colab": {} 138 | }, 139 | "source": [ 140 | "def slice_to_patch(slice, patch_ratio):\n", 141 | " \n", 142 | " slice[slice == 1] = 0\n", 143 | " slice[slice == 2] = 1\n", 144 | " \n", 145 | " patch_list = []\n", 146 | " \n", 147 | " for x_bin in range(2, len(patch_ratio)):\n", 148 | " for y_bin in range(2, len(patch_ratio)):\n", 149 | " patch = slice[patch_ratio[x_bin-2] : patch_ratio[x_bin], patch_ratio[y_bin - 2] : patch_ratio[y_bin]]\n", 150 | " patch = patch.reshape(patch.shape + (1,))\n", 151 | " patch_list.append(patch)\n", 152 | " \n", 153 | " return np.array(patch_list)" 154 | ], 155 | "execution_count": 0, 156 | "outputs": [] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "metadata": { 161 | "id": "TBpXvgG8O1_5", 162 | "colab_type": "code", 163 | "colab": {} 164 | }, 165 | "source": [ 166 | "def patch_to_slice(patch, patch_ratio, input_shape, conf_threshold):\n", 167 | " \n", 168 | " slice = np.zeros((512, 512, 1))\n", 169 | " row_idx = 0\n", 170 | " col_idx = 0\n", 171 | " \n", 172 | " for i in range(len(patch)):\n", 173 | " \n", 174 | " slice[patch_ratio[row_idx]:patch_ratio[row_idx + 2], patch_ratio[col_idx]:patch_ratio[col_idx + 2]][patch[i] > conf_threshold] = 1\n", 175 | " \n", 176 | " col_idx += 1\n", 177 | " \n", 178 | " if i != 0 and (i+1) % 15 == 0:\n", 179 | " row_idx += 1\n", 180 | " col_idx = 0\n", 181 | " \n", 182 | " return slice" 183 | ], 184 | "execution_count": 0, 185 | "outputs": [] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "metadata": { 190 | "id": "YYNgx7FoMJ1Q", 191 | "colab_type": "code", 192 | "colab": {} 193 | }, 194 | "source": [ 195 | "import numpy as np\n", 196 | "import matplotlib.pyplot as plt\n", 197 | "input_shape = [64, 64, 1]\n", 198 | "\n", 199 | "for i in range(img_ex.shape[2]): \n", 200 | " \n", 201 | " patch_ex = slice_to_patch(img_ex[:, :, i], patch_ratio)\n", 202 | " prediction = loaded_model.predict(patch_ex)\n", 203 | " prediction_mask = patch_to_slice(prediction, patch_ratio, input_shape, conf_threshold = 0.97)\n", 204 | " \n", 205 | " fig, (ax1,ax3) = plt.subplots(1, 2, figsize = ((15, 15)))\n", 206 | " \n", 207 | " ax1.imshow(np.rot90(img_ex[:, :, i], 3), cmap = 'bone')\n", 208 | " ax1.set_title(\"Image\", fontsize = \"x-large\")\n", 209 | " ax1.grid(False)\n", 210 | " \n", 211 | " ax3.imshow(np.rot90(prediction_mask.reshape((512, 512)), 3), cmap = 'bone')\n", 212 | " ax3.set_title(\"Mask (Pred)\", fontsize = \"x-large\")\n", 213 | " ax3.grid(False)\n", 214 | " plt.show()" 215 | ], 216 | "execution_count": 0, 217 | "outputs": [] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "metadata": { 222 | "id": "p9FBaNTjaOI7", 223 | "colab_type": "code", 224 | "colab": {} 225 | }, 226 | "source": [ 227 | "" 228 | ], 229 | "execution_count": 0, 230 | "outputs": [] 231 | } 232 | ] 233 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LITS Challenge 2 | Liver Tumor Segmentation Challenge 3 | 4 | 5 | 1. Download the notebook and upload it in google colab. 6 | 2. Download the LITS data using the Download the LITS data.ipynb file, The dataset size is around 50 GB, so you may require paid storage for google drive as it supports only 15GB. Upload the torrent file when asked in colab. 7 | 3. It has around 100+ files, but you cannot train these much in colab 8 | 4. You can download a smaller dataset from this link which can be used in colab training 9 | https://drive.google.com/drive/folders/1_pzWz60wAvsMx35kd_x8w5A5Xyyf6ulh?usp=sharing 10 | 5. Train the model using model training.ipynb 11 | 6. Predict the new data using the model LITS_pred.ipynb 12 | 13 | You can download the remaining dataset from here 14 | https://drive.google.com/drive/folders/1-1rskjkiG46RkeoRpxzI4JHa1sBKxOQM?usp=sharing 15 | 16 | aap-lits.py will create a desktop application using kivy which can be used for the prediction of liver tumor segmentation 17 | -------------------------------------------------------------------------------- /aap-lits.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """lits_out.ipynb 3 | 4 | """ 5 | 6 | from kivy.app import App 7 | from kivy.uix.boxlayout import BoxLayout 8 | from kivy.lang import Builder 9 | from tensorflow.keras.models import model_from_json 10 | import os 11 | import nibabel as nib 12 | import numpy as np 13 | import matplotlib.pyplot as plt 14 | import time 15 | 16 | 17 | json_file = open('model_json.json', 'r') 18 | loaded_model_json = json_file.read() 19 | json_file.close() 20 | loaded_model = model_from_json(loaded_model_json) 21 | # load weights into new model 22 | loaded_model.load_weights("model_weights.h5") 23 | print("Loaded model from disk") 24 | 25 | input_shape = [64, 64, 1] 26 | 27 | def slice_to_patch(slice, patch_ratio): 28 | 29 | slice[slice == 1] = 0 30 | slice[slice == 2] = 1 31 | 32 | patch_list = [] 33 | 34 | for x_bin in range(2, len(patch_ratio)): 35 | for y_bin in range(2, len(patch_ratio)): 36 | patch = slice[patch_ratio[x_bin-2] : patch_ratio[x_bin], patch_ratio[y_bin - 2] : patch_ratio[y_bin]] 37 | patch = patch.reshape(patch.shape + (1,)) 38 | patch_list.append(patch) 39 | 40 | return np.array(patch_list) 41 | 42 | def patch_to_slice(patch, patch_ratio, input_shape, conf_threshold): 43 | 44 | slice = np.zeros((512, 512, 1)) 45 | row_idx = 0 46 | col_idx = 0 47 | 48 | for i in range(len(patch)): 49 | 50 | slice[patch_ratio[row_idx]:patch_ratio[row_idx + 2], patch_ratio[col_idx]:patch_ratio[col_idx + 2]][patch[i] > conf_threshold] = 1 51 | 52 | col_idx += 1 53 | 54 | if i != 0 and (i+1) % 15 == 0: 55 | row_idx += 1 56 | col_idx = 0 57 | 58 | return slice 59 | 60 | Builder.load_string(""" 61 | : 62 | id: my_widget 63 | FileChooserListView: 64 | id: filechooser 65 | on_selection: my_widget.selected(filechooser.selection) 66 | Button 67 | text: "open" 68 | on_release: my_widget.open(filechooser.path, filechooser.selection) 69 | """) 70 | 71 | 72 | 73 | class MyWidget(BoxLayout): 74 | 75 | 76 | def open(self, path, filename): 77 | img_path=os.path.join(path, filename[0]) 78 | img_ex = nib.load(img_path).get_data() 79 | r,c,ch=img_ex.shape 80 | # img_ex=img_ex1[:,:,100] 81 | #mask_ex = nib.load(mask_path[25]).get_data() 82 | 83 | output_img=np.zeros(img_ex.shape) 84 | patch_ratio = [] 85 | for i in range(16 + 1): 86 | patch_ratio.append(32 * i) 87 | for i in range(0,img_ex.shape[2]): 88 | 89 | patch_ex = slice_to_patch(img_ex[:, :, i], patch_ratio) 90 | prediction = loaded_model.predict(patch_ex) 91 | prediction_mask = patch_to_slice(prediction, patch_ratio, input_shape, conf_threshold = 0.97) 92 | prediction_mask1=img_ex[:,:,0] 93 | # cv2.imwrite('output.jpg',prediction_mask1) 94 | 95 | # self.ids.image.source='output.jpg' 96 | 97 | fig, (ax1,ax3) = plt.subplots(1, 2, figsize = ((15, 15))) 98 | 99 | ax1.imshow(np.rot90(img_ex[:, :, i], 3), cmap = 'bone') 100 | ax1.set_title("Image", fontsize = "x-large") 101 | ax1.grid(False) 102 | 103 | ax3.imshow(np.rot90(prediction_mask.reshape((512, 512)), 3), cmap = 'bone') 104 | ax3.set_title("Mask (Pred)", fontsize = "x-large") 105 | ax3.grid(False) 106 | plt.show() 107 | # plt.close() 108 | print('Finished') 109 | 110 | 111 | 112 | def selected(self, filename): 113 | print ("selected: %s" % filename[0]) 114 | 115 | 116 | class MyApp(App): 117 | def build(self): 118 | return MyWidget() 119 | 120 | if __name__ == '__main__': 121 | MyApp().run() --------------------------------------------------------------------------------