visualize.ipynb 7.95 KB
Newer Older
Sugon_ldc's avatar
Sugon_ldc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Please install the itkwidgts before experiment\n",
    "details to install it can be found [here](https://github.com/InsightSoftwareConsortium/itkwidgets#installation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: scipy in /ssd2/tangshiyu/anaconda3/lib/python3.8/site-packages (1.6.2)\r\n",
      "Requirement already satisfied: numpy<1.23.0,>=1.16.5 in /ssd2/tangshiyu/anaconda3/lib/python3.8/site-packages (from scipy) (1.20.1)\r\n"
     ]
    }
   ],
   "source": [
    "# Install dependencies for this example\n",
    "# Note: This does not include itkwidgets, itself\n",
    "import sys\n",
    "!{sys.executable} -m pip install scipy\n",
    "!wget https://bj.bcebos.com/paddleseg/paddleseg3d/lung_coronavirus/vnet_lung_coronavirus_128_128_128_15k_1e-3/prediction.npz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from itkwidgets import view, compare\n",
    "\n",
    "# load the compressed data img=a, label=b, pred=c, infer=d\n",
    "data = np.load(\"prediction.npz\")\n",
    "\n",
    "# the infer output \n",
    "infer_result = data['infer'].squeeze()\n",
    "print(infer_result.shape)\n",
    "infer_result = infer_result*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "infer_result = infer_result.astype(np.uint8)\n",
    "\n",
    "b = data['pred'].squeeze()\n",
    "print(b.shape)\n",
    "b = b*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "b = b.astype(np.uint8)\n",
    "\n",
    "c = data['label'].squeeze()\n",
    "print(b.shape)\n",
    "c = c*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "c = c.astype(np.uint8)\n",
    "\n",
    "d = data['img'].squeeze()\n",
    "print(d.shape)\n",
    "d = d*100\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "d = d.astype(np.uint8)\n",
    "\n",
    "# b is pred, c is label, d is img\n",
    "predlabel = np.concatenate((b, c), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(128, 256, 128)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "762485dcc73340b0a2b23c6eee6eaf3f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(axes=True, geometries=[], gradient_opacity=1.0, point_sets=[], rendered_image=<itk.itkImagePython.itkIm…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(predlabel.shape)\n",
    "\n",
    "view(predlabel[:, 50:240, :],  axes=True, vmin=0, vmax=200, gradient_opacity=1.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2412dd34d0cf4e568b6ca1fe628485e6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(axes=True, cmap=['OrPu', 'BkGn'], geometries=[], gradient_opacity=1.0, interpolation=False, point_sets=…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(label_image=predlabel[:, 50:240, :], rotate=True, axes=True, vmin=0, vmax=200, gradient_opacity=10.0, cmap=[\"OrPu\", 'BkGn'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2168100f87274a93b1c1b2c331f06cb7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "AppLayout(children=(HBox(children=(Label(value='Link:'), Checkbox(value=True, description='cmap'), Checkbox(va…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "compare(b, c, link_cmap=True, rotate=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "all the input arrays must have same number of dimensions, but the array at index 0 has 3 dimension(s) and the array at index 1 has 4 dimension(s)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-39-c5383c193e35>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mpredlabel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconcatenate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmri_pred\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmri_label\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\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     16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m \u001b[0mpredlabel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mconcatenate\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: all the input arrays must have same number of dimensions, but the array at index 0 has 3 dimension(s) and the array at index 1 has 4 dimension(s)"
     ]
    }
   ],
   "source": [
    "#  visualize  MRI\n",
    "import numpy as np\n",
    "from itkwidgets import view, compare\n",
    "\n",
    "mri_img = np.load(\"saved_model/vnet_mri_spine_seg_128_128_24_15k_0309_rmmax/1_img.npy\")\n",
    "mri_label = np.load(\"saved_model/vnet_mri_spine_seg_128_128_24_15k_0309_rmmax/1_label.npy\") * 10\n",
    "mri_pred = np.load(\" saved_model/vnet_mri_spine_seg_128_128_24_15k_0309_rmmax/1_pred.npy\") * 10\n",
    "\n",
    "# Ascent has a range of values from 0 to 255, but it is stored with a int64 dtype\n",
    "mri_img = mri_img.astype(np.uint8)\n",
    "mri_label = mri_label.astype(np.uint8)\n",
    "mri_pred = mri_pred.astype(np.uint8)\n",
    "\n",
    "\n",
    "predlabel = np.concatenate((mri_pred, mri_label), axis=2)\n",
    "\n",
    "predlabel.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6cf8328d4cdd4f4293c10ea116d261be",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=1.0, point_sets=[], rendered_image=<itk.itkImagePython.itkImageUC3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(mri_pred, axes=False, rotate=True, vmin=0, vmax=200, gradient_opacity=190)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}