|
98 | 98 | "import numpy as np\n",
|
99 | 99 | "import torch\n",
|
100 | 100 | "from torch import jit\n",
|
| 101 | + "import tempfile\n", |
101 | 102 | "\n",
|
102 | 103 | "import monai\n",
|
103 | 104 | "from monai.config import print_config\n",
|
|
121 | 122 | " SqueezeDimd,\n",
|
122 | 123 | " ToNumpyd,\n",
|
123 | 124 | " ToTensord,\n",
|
| 125 | + " FromMetaTensord,\n", |
124 | 126 | ")\n",
|
125 | 127 | "\n",
|
126 | 128 | "print_config()"
|
|
226 | 228 | ],
|
227 | 229 | "source": [
|
228 | 230 | "# Download data and model\n",
|
| 231 | + "data_dir = tempfile.TemporaryDirectory().name\n", |
229 | 232 | "\n",
|
230 | 233 | "resource = \"https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/_image.nii.gz\"\n",
|
231 |
| - "dst = \"_image.nii.gz\"\n", |
232 |
| - "\n", |
233 |
| - "if not os.path.exists(dst):\n", |
234 |
| - " monai.apps.download_url(resource, dst)\n", |
| 234 | + "image_path = os.path.join(data_dir, \"_image.nii.gz\")\n", |
| 235 | + "monai.apps.download_url(resource, image_path)\n", |
235 | 236 | "\n",
|
236 | 237 | "resource = \"https://github.com/Project-MONAI/MONAI-extra-test-data/releases/\\\n",
|
237 | 238 | "download/0.8.1/pretrained_deepedit_dynunet-final.ts\"\n",
|
238 |
| - "dst = \"pretrained_deepedit_dynunet-final.ts\"\n", |
239 |
| - "if not os.path.exists(dst):\n", |
240 |
| - " monai.apps.download_url(resource, dst)" |
| 239 | + "model_path = os.path.join(data_dir, \"pretrained_deepedit_dynunet-final.ts\")\n", |
| 240 | + "monai.apps.download_url(resource, model_path)" |
241 | 241 | ]
|
242 | 242 | },
|
243 | 243 | {
|
|
277 | 277 | "output_type": "stream",
|
278 | 278 | "text": [
|
279 | 279 | "EnsureChannelFirstd => image shape: (1, 392, 392, 210)\n",
|
280 |
| - "Orientationd => image shape: (1, 392, 392, 210)\n", |
281 |
| - "ScaleIntensityRanged => image shape: (1, 392, 392, 210)\n", |
282 |
| - "AddGuidanceFromPointsDeepEditd => image shape: (1, 392, 392, 210)\n", |
283 |
| - "Resized => image shape: (1, 128, 128, 128)\n", |
284 |
| - "ResizeGuidanceMultipleLabelDeepEditd => image shape: (1, 128, 128, 128)\n", |
| 280 | + "FromMetaTensord => image shape: torch.Size([1, 392, 392, 210])\n", |
| 281 | + "Orientationd => image shape: torch.Size([1, 392, 392, 210])\n", |
| 282 | + "ScaleIntensityRanged => image shape: torch.Size([1, 392, 392, 210])\n", |
| 283 | + "AddGuidanceFromPointsDeepEditd => image shape: torch.Size([1, 392, 392, 210])\n", |
| 284 | + "Resized => image shape: torch.Size([1, 128, 128, 128])\n", |
| 285 | + "ResizeGuidanceMultipleLabelDeepEditd => image shape: torch.Size([1, 128, 128, 128])\n", |
285 | 286 | "AddGuidanceSignalDeepEditd => image shape: (3, 128, 128, 128)\n",
|
286 | 287 | "ToTensord => image shape: torch.Size([3, 128, 128, 128])\n"
|
287 | 288 | ]
|
|
297 | 298 | "spatial_size = [128, 128, 128]\n",
|
298 | 299 | "\n",
|
299 | 300 | "data = {\n",
|
300 |
| - " 'image': '_image.nii.gz',\n", |
| 301 | + " 'image': image_path,\n", |
301 | 302 | " 'guidance': {'spleen': [[66, 180, 105], [66, 180, 145]], 'background': []},\n",
|
302 | 303 | "}\n",
|
303 | 304 | "\n",
|
|
308 | 309 | " LoadImaged(keys=\"image\", reader=\"ITKReader\"),\n",
|
309 | 310 | " # Ensure channel first\n",
|
310 | 311 | " EnsureChannelFirstd(keys=\"image\"),\n",
|
| 312 | + " # Convert away from MetaTensor\n", |
| 313 | + " FromMetaTensord(keys=\"image\"),\n", |
311 | 314 | " # Change image orientation\n",
|
312 | 315 | " Orientationd(keys=\"image\", axcodes=\"RAS\"),\n",
|
313 | 316 | " # Scaling image intensity - works well for CT images\n",
|
|
475 | 478 | ],
|
476 | 479 | "source": [
|
477 | 480 | "# Evaluation\n",
|
478 |
| - "model_path = 'pretrained_deepedit_dynunet-final.ts'\n", |
479 | 481 | "model = jit.load(model_path)\n",
|
480 | 482 | "model.cuda()\n",
|
481 | 483 | "model.eval()\n",
|
|
512 | 514 | " i, image.shape, label.shape, np.min(label), np.max(label), np.sum(label)))\n",
|
513 | 515 | " show_image(image, label)"
|
514 | 516 | ]
|
515 |
| - }, |
516 |
| - { |
517 |
| - "cell_type": "code", |
518 |
| - "execution_count": 7, |
519 |
| - "metadata": {}, |
520 |
| - "outputs": [], |
521 |
| - "source": [ |
522 |
| - "# remove downloaded files\n", |
523 |
| - "os.remove('_image.nii.gz')\n", |
524 |
| - "os.remove('pretrained_deepedit_dynunet-final.ts')" |
525 |
| - ] |
526 | 517 | }
|
527 | 518 | ],
|
528 | 519 | "metadata": {
|
|
0 commit comments