|
19 | 19 | "id": "doj9EzIVIMFv"
|
20 | 20 | },
|
21 | 21 | "source": [
|
22 |
| - "# PyTorch" |
| 22 | + "# Playing with SimpleITK and nnU-Net" |
23 | 23 | ]
|
24 | 24 | },
|
25 | 25 | {
|
|
31 | 31 | "## Installation"
|
32 | 32 | ]
|
33 | 33 | },
|
34 |
| - { |
35 |
| - "cell_type": "code", |
36 |
| - "metadata": { |
37 |
| - "id": "oWi4McPs_NuY" |
38 |
| - }, |
39 |
| - "source": [ |
40 |
| - "!pip install -qq SimpleITK nnunetv2" |
41 |
| - ], |
42 |
| - "execution_count": null, |
43 |
| - "outputs": [] |
44 |
| - }, |
45 | 34 | {
|
46 | 35 | "cell_type": "code",
|
47 | 36 | "source": [
|
48 | 37 | "%cd /content\n",
|
49 |
| - "!git clone https://github.com/woctezuma/playing-with-simpleitk.git" |
| 38 | + "!git clone https://github.com/woctezuma/playing-with-simpleitk.git\n", |
| 39 | + "\n", |
| 40 | + "%pip install -qq SimpleITK nnunetv2" |
50 | 41 | ],
|
51 | 42 | "metadata": {
|
52 |
| - "id": "vL5WJI3NVxIe" |
53 |
| - }, |
54 |
| - "execution_count": null, |
55 |
| - "outputs": [] |
| 43 | + "id": "vL5WJI3NVxIe", |
| 44 | + "outputId": "cedbef25-33a3-45f1-e036-913a318ba177", |
| 45 | + "colab": { |
| 46 | + "base_uri": "https://localhost:8080/" |
| 47 | + } |
| 48 | + }, |
| 49 | + "execution_count": 2, |
| 50 | + "outputs": [ |
| 51 | + { |
| 52 | + "output_type": "stream", |
| 53 | + "name": "stdout", |
| 54 | + "text": [ |
| 55 | + "/content\n", |
| 56 | + "Cloning into 'playing-with-simpleitk'...\n", |
| 57 | + "remote: Enumerating objects: 295, done.\u001b[K\n", |
| 58 | + "remote: Counting objects: 100% (42/42), done.\u001b[K\n", |
| 59 | + "remote: Compressing objects: 100% (35/35), done.\u001b[K\n", |
| 60 | + "remote: Total 295 (delta 22), reused 18 (delta 7), pack-reused 253\u001b[K\n", |
| 61 | + "Receiving objects: 100% (295/295), 30.21 MiB | 23.78 MiB/s, done.\n", |
| 62 | + "Resolving deltas: 100% (42/42), done.\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ] |
56 | 66 | },
|
57 | 67 | {
|
58 | 68 | "cell_type": "markdown",
|
|
63 | 73 | "## Data nomenclature"
|
64 | 74 | ]
|
65 | 75 | },
|
66 |
| - { |
67 |
| - "cell_type": "markdown", |
68 |
| - "metadata": { |
69 |
| - "id": "5oFoBRkhIVVX" |
70 |
| - }, |
71 |
| - "source": [ |
72 |
| - "Patients" |
73 |
| - ] |
74 |
| - }, |
75 | 76 | {
|
76 | 77 | "cell_type": "code",
|
77 | 78 | "metadata": {
|
78 | 79 | "id": "8AJwRH-8FmaD"
|
79 | 80 | },
|
80 | 81 | "source": [
|
81 |
| - "def get_patient_indices():\n", |
82 |
| - " patient_indices = [1,2,5,6,8,10,14,16,18,19,21,22,23,24,25,26,27,28,29,30]\n", |
83 |
| - "\n", |
84 |
| - " return patient_indices" |
85 |
| - ], |
86 |
| - "execution_count": null, |
87 |
| - "outputs": [] |
88 |
| - }, |
89 |
| - { |
90 |
| - "cell_type": "markdown", |
91 |
| - "metadata": { |
92 |
| - "id": "P65kljpNIRK4" |
93 |
| - }, |
94 |
| - "source": [ |
95 |
| - "Folders" |
96 |
| - ] |
97 |
| - }, |
98 |
| - { |
99 |
| - "cell_type": "code", |
100 |
| - "metadata": { |
101 |
| - "id": "uiLgnz7zFY13" |
102 |
| - }, |
103 |
| - "source": [ |
104 |
| - "def get_data_folder():\n", |
105 |
| - " data_folder = 'data/'\n", |
106 |
| - "\n", |
107 |
| - " return data_folder\n", |
108 |
| - "\n", |
109 |
| - "def get_image_folder():\n", |
110 |
| - " image_folder = get_data_folder() + 'imagesTr/'\n", |
111 |
| - "\n", |
112 |
| - " return image_folder\n", |
113 |
| - "\n", |
114 |
| - "def get_ground_truth_folder():\n", |
115 |
| - " ground_truth_folder = get_data_folder() + 'labelsTr/'\n", |
116 |
| - "\n", |
117 |
| - " return ground_truth_folder" |
118 |
| - ], |
119 |
| - "execution_count": null, |
120 |
| - "outputs": [] |
121 |
| - }, |
122 |
| - { |
123 |
| - "cell_type": "markdown", |
124 |
| - "metadata": { |
125 |
| - "id": "NfQXankUIS88" |
126 |
| - }, |
127 |
| - "source": [ |
128 |
| - "Files" |
129 |
| - ] |
130 |
| - }, |
131 |
| - { |
132 |
| - "cell_type": "code", |
133 |
| - "metadata": { |
134 |
| - "id": "jbGlq7B8biXg" |
135 |
| - }, |
136 |
| - "source": [ |
137 |
| - "def get_file_extension():\n", |
138 |
| - " file_extension = '.nii.gz'\n", |
| 82 | + "ROOT_FOLDER = '/content/'\n", |
| 83 | + "DATA_FOLDER = 'data/'\n", |
| 84 | + "IMAGE_FOLDER = f'{DATA_FOLDER}imagesTr/'\n", |
| 85 | + "GROUND_TRUTH_FOLDER = f'{DATA_FOLDER}labelsTr/'\n", |
139 | 86 | "\n",
|
140 |
| - " return file_extension\n", |
| 87 | + "PATIENT_INDICES = [1,2,5,6,8,10,14,16,18,19,21,22,23,24,25,26,27,28,29,30]\n", |
| 88 | + "IMAGE_TYPE = '.nii.gz'\n", |
141 | 89 | "\n",
|
142 | 90 | "def get_image_file_name(patient_no, modality_no=0):\n",
|
143 |
| - " image_modality_convention = '_{:04.0f}'.format(modality_no)\n", |
144 |
| - " image_file_name = get_image_folder() + 'patientID' + str(patient_no) + image_modality_convention + get_file_extension()\n", |
145 |
| - "\n", |
146 |
| - " return image_file_name\n", |
| 91 | + " return f'{IMAGE_FOLDER}patientID{patient_no}_{modality_no:04}{IMAGE_TYPE}'\n", |
147 | 92 | "\n",
|
148 | 93 | "def get_ground_truth_file_name(patient_no):\n",
|
149 |
| - " ground_truth_file_name = get_ground_truth_folder() +'patientID' + str(patient_no) + get_file_extension()\n", |
150 |
| - "\n", |
151 |
| - " return ground_truth_file_name" |
| 94 | + " return f'{GROUND_TRUTH_FOLDER}patientID{patient_no}{IMAGE_TYPE}'" |
152 | 95 | ],
|
153 |
| - "execution_count": null, |
| 96 | + "execution_count": 3, |
154 | 97 | "outputs": []
|
155 | 98 | },
|
156 | 99 | {
|
|
170 | 113 | "!unzip -qq CHAOS_Train_Sets.zip"
|
171 | 114 | ],
|
172 | 115 | "metadata": {
|
173 |
| - "id": "bSD1Q_6Quslt" |
174 |
| - }, |
175 |
| - "execution_count": null, |
176 |
| - "outputs": [] |
177 |
| - }, |
178 |
| - { |
179 |
| - "cell_type": "code", |
180 |
| - "source": [ |
181 |
| - "%mv /content/Train_Sets /content/playing-with-simpleitk/data/" |
182 |
| - ], |
183 |
| - "metadata": { |
184 |
| - "id": "TQ8-7kLU7bs5" |
185 |
| - }, |
186 |
| - "execution_count": null, |
187 |
| - "outputs": [] |
| 116 | + "id": "bSD1Q_6Quslt", |
| 117 | + "outputId": "3341ee8a-eaf7-4dda-b5b8-d12c670f2a04", |
| 118 | + "colab": { |
| 119 | + "base_uri": "https://localhost:8080/" |
| 120 | + } |
| 121 | + }, |
| 122 | + "execution_count": 7, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "output_type": "stream", |
| 126 | + "name": "stdout", |
| 127 | + "text": [ |
| 128 | + "/content\n", |
| 129 | + " % Total % Received % Xferd Average Speed Time Time Time Current\n", |
| 130 | + " Dload Upload Total Spent Left Speed\n", |
| 131 | + "100 150 100 150 0 0 182 0 --:--:-- --:--:-- --:--:-- 182\n", |
| 132 | + "[CHAOS_Train_Sets.zip]\n", |
| 133 | + " End-of-central-directory signature not found. Either this file is not\n", |
| 134 | + " a zipfile, or it constitutes one disk of a multi-part archive. In the\n", |
| 135 | + " latter case the central directory and zipfile comment will be found on\n", |
| 136 | + " the last disk(s) of this archive.\n", |
| 137 | + "unzip: cannot find zipfile directory in one of CHAOS_Train_Sets.zip or\n", |
| 138 | + " CHAOS_Train_Sets.zip.zip, and cannot find CHAOS_Train_Sets.zip.ZIP, period.\n" |
| 139 | + ] |
| 140 | + } |
| 141 | + ] |
188 | 142 | },
|
189 | 143 | {
|
190 | 144 | "cell_type": "code",
|
191 | 145 | "source": [
|
| 146 | + "DATASET_FNAME = \"Dataset000_MY_DATASET\"\n", |
| 147 | + "NNUNET_FOLDER_NAME = f\"{ROOT_FOLDER}nnUNet_base/{DATASET_FNAME}/\"\n", |
| 148 | + "\n", |
192 | 149 | "%cd /content/playing-with-simpleitk\n",
|
193 |
| - "!python convert_to_nii.py" |
| 150 | + "%mv /content/Train_Sets data/\n", |
| 151 | + "!python convert_to_nii.py\n", |
| 152 | + "%mkdir -p {NNUNET_FOLDER_NAME}\n", |
| 153 | + "%mv data/output/* {NNUNET_FOLDER_NAME}" |
194 | 154 | ],
|
195 | 155 | "metadata": {
|
196 | 156 | "id": "qugHxbsFvCtM"
|
197 | 157 | },
|
198 | 158 | "execution_count": null,
|
199 | 159 | "outputs": []
|
200 | 160 | },
|
201 |
| - { |
202 |
| - "cell_type": "code", |
203 |
| - "metadata": { |
204 |
| - "id": "HbPo9F8kB31W" |
205 |
| - }, |
206 |
| - "source": [ |
207 |
| - "%mkdir -p /content/nnUNet_base/Dataset000_MY_DATASET\n", |
208 |
| - "%mv /content/playing-with-simpleitk/data/output/* /content/nnUNet_base/Dataset000_MY_DATASET/" |
209 |
| - ], |
210 |
| - "execution_count": null, |
211 |
| - "outputs": [] |
212 |
| - }, |
213 | 161 | {
|
214 | 162 | "cell_type": "markdown",
|
215 | 163 | "metadata": {
|
|
219 | 167 | "## Tests"
|
220 | 168 | ]
|
221 | 169 | },
|
222 |
| - { |
223 |
| - "cell_type": "code", |
224 |
| - "metadata": { |
225 |
| - "id": "7sGD_c2Iq5Da" |
226 |
| - }, |
227 |
| - "source": [ |
228 |
| - "root_folder = '/content/'" |
229 |
| - ], |
230 |
| - "execution_count": null, |
231 |
| - "outputs": [] |
232 |
| - }, |
233 |
| - { |
234 |
| - "cell_type": "code", |
235 |
| - "metadata": { |
236 |
| - "id": "QgbS9WFvrJQb" |
237 |
| - }, |
238 |
| - "source": [ |
239 |
| - "patient_indices = get_patient_indices()\n", |
240 |
| - "patient_no = patient_indices[0]" |
241 |
| - ], |
242 |
| - "execution_count": null, |
243 |
| - "outputs": [] |
244 |
| - }, |
245 | 170 | {
|
246 | 171 | "cell_type": "code",
|
247 | 172 | "metadata": {
|
|
251 | 176 | "import numpy as np\n",
|
252 | 177 | "import SimpleITK as sitk\n",
|
253 | 178 | "\n",
|
254 |
| - "image_name = root_folder + get_image_file_name(patient_no)\n", |
255 |
| - "\n", |
256 |
| - "image = sitk.ReadImage(image_name)\n", |
257 |
| - "print(image.GetSize())\n", |
258 |
| - "\n", |
259 |
| - "v = sitk.GetArrayViewFromImage(image)\n", |
260 |
| - "\n", |
261 |
| - "np.unique(v)" |
262 |
| - ], |
263 |
| - "execution_count": null, |
264 |
| - "outputs": [] |
265 |
| - }, |
266 |
| - { |
267 |
| - "cell_type": "code", |
268 |
| - "metadata": { |
269 |
| - "id": "5_X6uel5LMmS" |
270 |
| - }, |
271 |
| - "source": [ |
272 |
| - "import numpy as np\n", |
273 |
| - "import SimpleITK as sitk\n", |
274 |
| - "\n", |
275 |
| - "image_name = root_folder + get_ground_truth_file_name(patient_no)\n", |
| 179 | + "patient_no = PATIENT_INDICES[0]\n", |
276 | 180 | "\n",
|
277 |
| - "image = sitk.ReadImage(image_name)\n", |
278 |
| - "print(image.GetSize())\n", |
| 181 | + "for image_name in [\n", |
| 182 | + " get_image_file_name(patient_no),\n", |
| 183 | + " get_ground_truth_file_name(patient_no),\n", |
| 184 | + " ]:\n", |
279 | 185 | "\n",
|
280 |
| - "v = sitk.GetArrayViewFromImage(image)\n", |
| 186 | + " image = sitk.ReadImage(ROOT_FOLDER + image_name)\n", |
| 187 | + " print(image.GetSize())\n", |
281 | 188 | "\n",
|
282 |
| - "np.unique(v)" |
| 189 | + " v = sitk.GetArrayViewFromImage(image)\n", |
| 190 | + " print(np.unique(v))" |
283 | 191 | ],
|
284 | 192 | "execution_count": null,
|
285 | 193 | "outputs": []
|
|
292 | 200 | "source": [
|
293 | 201 | "## Down-sample images\n",
|
294 | 202 | "\n",
|
295 |
| - "Reference: https://github.com/jonasteuwen/SimpleITK-examples/blob/master/examples/resample_isotropically.py" |
| 203 | + "References:\n", |
| 204 | + "- https://github.com/jonasteuwen/SimpleITK-examples/blob/master/examples/resample_isotropically.py\n", |
| 205 | + "- https://simpleitk.org/SPIE2019_COURSE/02_images_and_resampling.html" |
296 | 206 | ]
|
297 | 207 | },
|
298 | 208 | {
|
|
0 commit comments