Spaces:
Runtime error
Runtime error
File size: 23,146 Bytes
fd601de |
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# create dataset\n",
"\n",
"as this study is based on various datasets, the step of creating dataset is kind of complicated, the steps will be explained with following examples"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\zy7\\.conda\\envs\\torch2\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import sys\n",
"sys.path.append('./dataprocesser')\n",
"sys.path.append('./synthrad_conversion')\n",
"from dataprocesser import step2_create_segmentation as createseg\n",
"from dataprocesser import step1_init_data_list as init_data_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## at first we should have a function to load our customised dataset\n",
"for better organizing, we should load our data in form of imagepath + pID\n",
"here is an example for synthRAD dataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"here we can try another dataset, namely LITS"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 1 files in E:\\Projects\\yang_proj\\data\\LITS \n",
"\n",
"Found 1 files in E:\\Projects\\yang_proj\\data\\LITS \n",
"\n",
"E:\\Projects\\yang_proj\\data\\LITS\\volume-0.nii volume-0 \n",
"\n"
]
}
],
"source": [
"import os\n",
"from dataprocesser.dataset_LITS import list_img_pID_from_LITS_folder\n",
"\n",
"dir = r'E:\\Projects\\yang_proj\\data\\LITS'\n",
"LITS_images, LITS_patient_IDs = list_img_pID_from_LITS_folder(dir, isseg=False)\n",
"LITS_segs, _ = list_img_pID_from_LITS_folder(dir, isseg=True)\n",
"for image, pID in zip(LITS_images, LITS_patient_IDs):\n",
" print(image, pID, '\\n')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# step 2: create segmentations with the data list using totalsegmentator\n",
"\n",
"here we use the new lits dataset as example"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"use GPU: NVIDIA RTX A6000\n",
"create segmentation mask for E:\\Projects\\yang_proj\\data\\LITS\\volume-0.nii\n",
"\n",
"If you use this tool please cite: https://pubs.rsna.org/doi/10.1148/ryai.230024\n",
"\n",
"Resampling...\n",
" Resampled in 4.00s\n",
"Predicting part 1 of 5 ...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\zy7\\.conda\\envs\\torch2\\Lib\\site-packages\\nnunetv2\\inference\\predict_from_raw_data.py:84: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" checkpoint = torch.load(join(model_training_output_dir, f'fold_{f}', checkpoint_name),\n",
"c:\\Users\\zy7\\.conda\\envs\\torch2\\Lib\\site-packages\\threadpoolctl.py:1214: RuntimeWarning: \n",
"Found Intel OpenMP ('libiomp') and LLVM OpenMP ('libomp') loaded at\n",
"the same time. Both libraries are known to be incompatible and this\n",
"can cause random crashes or deadlocks on Linux when loaded in the\n",
"same Python program.\n",
"Using threadpoolctl may cause crashes or deadlocks. For more\n",
"information and possible workarounds, please see\n",
" https://github.com/joblib/threadpoolctl/blob/master/multiple_openmp.md\n",
"\n",
" warnings.warn(msg, RuntimeWarning)\n",
"100%|ββββββββββ| 27/27 [00:01<00:00, 17.86it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicting part 2 of 5 ...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\zy7\\.conda\\envs\\torch2\\Lib\\site-packages\\nnunetv2\\inference\\predict_from_raw_data.py:84: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
" checkpoint = torch.load(join(model_training_output_dir, f'fold_{f}', checkpoint_name),\n",
"100%|ββββββββββ| 27/27 [00:00<00:00, 40.81it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicting part 3 of 5 ...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 27/27 [00:00<00:00, 45.97it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicting part 4 of 5 ...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 27/27 [00:00<00:00, 38.12it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicting part 5 of 5 ...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 27/27 [00:00<00:00, 46.31it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Predicted in 234.94s\n",
"Resampling...\n",
"Saving segmentations...\n",
" Saved in 0.38s\n",
"segmentation mask is saved as E:\\Projects\\yang_proj\\data\\LITS\\volume-0_seg.nii\n"
]
}
],
"source": [
"from dataprocesser import step2_create_segmentation as createseg\n",
"import torch\n",
"import os\n",
"gpu_index = 1\n",
"device = 'cuda'\n",
"task = 'total' # 'total'\n",
"GPU_ID=[gpu_index]\n",
"\n",
"# cuda:{GPU_ID[0]}\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"]=f'{gpu_index}'\n",
"device = 'cuda' #torch.device(f'cuda' if torch.cuda.is_available() else 'cpu') # 0=TitanXP, 1=P5000\n",
"\n",
"print('use GPU: ', torch.cuda.get_device_name(f'cuda'))\n",
"createseg.run(target_file_list = LITS_images, dataset=None, device=device, task=task) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"in this way we can get the segmentation as filename_seg.nii.gz that we want\n",
"\n",
"## step 3: create csv file\n",
"\n",
"we import the created datalist to a csv file, so that we can modify and reuse it later\n",
"\n",
"we should get an example.csv with following content:\n",
"\n",
"id,Aorta_diss,seg,img\n",
"\n",
"1PA001,0,E:\\Projects\\yang_proj\\data\\synthrad\\Task1\\pelvis\\1PA001\\ct_seg.nii.gz,E:\\Projects\\yang_proj\\data\\synthrad\\Task1\\pelvis\\1PA001\\ct.nii.gz\n",
"\n",
"1PA004,0,E:\\Projects\\yang_proj\\data\\synthrad\\Task1\\pelvis\\1PA004\\ct_seg.nii.gz,E:\\Projects\\yang_proj\\data\\synthrad\\Task1\\pelvis\\1PA004\\ct.nii.gz\n",
"\n",
"1PA005,0,E:\\Projects\\yang_proj\\data\\synthrad\\Task1\\pelvis\\1PA005\\ct_seg.nii.gz,E:\\Projects\\yang_proj\\data\\synthrad\\Task1\\pelvis\\1PA005\\ct.nii.gz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# create and save the new LITS dataset"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['E:\\\\Projects\\\\yang_proj\\\\data\\\\LITS\\\\volume-0.nii'] \n",
" ['E:\\\\Projects\\\\yang_proj\\\\data\\\\LITS\\\\volume-0_seg.nii'] \n",
" ['volume-0']\n"
]
}
],
"source": [
"from dataprocesser import create_csv \n",
"output_csv_file = r'tutorial_lits.csv'\n",
"print(LITS_images,'\\n',LITS_segs, '\\n',LITS_patient_IDs)\n",
"LITS_Aorta_diss = [0] * len(LITS_images)\n",
"datalist_LITS = [[id,Aorta_diss,seg,image] for id,Aorta_diss,seg,image in zip(LITS_patient_IDs, LITS_Aorta_diss, LITS_segs, LITS_images)]\n",
"create_csv.create_csv_info_file(datalist_LITS, output_csv_file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# start preparation for training or inference"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## create a monai/torch dataloader\n",
"\n",
"this dataloader uses add_CreateContour_MergeMask_MaskHUAssign_transforms, which assigns values to segmentation classes"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Infer_ddpm2d_seg2med\n",
"create combined segmentation dataset with assigned value\n",
"create base dataset\n",
"combined segmentation assigned dataset use keys: ['source', 'target', 'mask']\n",
"scale2000 normalization\n",
"scale2000 normalization\n",
"use train csv: tutorial.csv\n",
"use test csv: tutorial.csv\n",
"use keys for creating volume dataset: ['source', 'target', 'mask']\n",
"model name: ddpm2d_seg2med\n",
"val_use_patch: False\n",
"original image shape: torch.Size([1, 1, 512, 512, 75])\n"
]
}
],
"source": [
"from synthrad_conversion.utils.my_configs_yacs import init_cfg\n",
"from dataprocesser.step1_init_data_list import init_dataset\n",
"config_path = 'tutorial_config.yaml'\n",
"opt=init_cfg(config_path)\n",
"model_name_path='Infer_'+opt.model_name + opt.name_prefix\n",
"print(model_name_path)\n",
"dataset_name = 'combined_simplified_csv_seg_assigned'\n",
"loader, opt, my_paths = init_dataset(opt, model_name_path, dataset_name)\n",
"train_loader = loader.train_loader\n",
"val_loader = loader.val_loader\n",
"first_batch = next(iter(val_loader))\n",
"first_input = first_batch[opt.dataset.indicator_A]\n",
"first_target = first_batch[opt.dataset.indicator_B]\n",
"print(\"original image shape:\", first_target.shape)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# visualize a slice:\n",
"import matplotlib.pyplot as plt\n",
"def plottorchdata(data, i, slice_id):\n",
" data = data.cpu().numpy().squeeze().squeeze()\n",
" dataimg = f'tutorial_{i}.png'\n",
" plt.figure(i)\n",
" plt.imshow(data[..., slice_id], cmap='gray')\n",
" plt.savefig(dataimg)\n",
"slice_id = 50\n",
"plottorchdata(first_input, 0, slice_id)\n",
"plottorchdata(first_target, 1, slice_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# run inference!"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"use GPU: NVIDIA RTX A6000\n"
]
}
],
"source": [
"import torch\n",
"device = torch.device(f'cuda:1' if torch.cuda.is_available() else 'cpu') # 0=TitanXP, 1=P5000\n",
"print('use GPU: ', torch.cuda.get_device_name(device))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"all available models:\n",
"ddpm2d_seg2med\n",
"spade_ddpm2d_seg2med\n",
"pix2pix\n",
"cycle_gan\n",
"AttentionUnet\n",
"resUnet\n",
"use model: ddpm2d_seg2med\n",
"use pretrained model: logs\\241118ddpm_512.pt\n",
"continue from epoch 171\n",
"continue from step 1783714\n",
"aorta dissection not manually set, use the value in csv file\n",
"original image shape: torch.Size([1, 1, 512, 512, 75])\n",
"\n",
" inference val set from 0 to 0 batch\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/1 [00:00<?, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"patient ID: 1BA001\n",
"input volume shape: torch.Size([1, 512, 512, 75])\n",
"target volume shape: torch.Size([1, 512, 512, 75])\n",
"ad: tensor(0)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 500/500 [04:30<00:00, 1.85it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Epoch 171] [pID 1BA001] [totalstep 0] [loss: 0.004563] [ssim: 0.940804] [psnr: 23.956906] [mae: 50.321005] \n",
"\n",
"2025-04-25 21:31:18,373 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_0.nii.gz\n",
"2025-04-25 21:31:18,422 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_0.nii.gz\n",
"2025-04-25 21:31:18,600 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_0.nii.gz\n",
"[Epoch 171] [pID 1BA001] [totalstep 1] [loss: 0.004563] [ssim: 0.931547] [psnr: 24.208621] [mae: 53.802768] \n",
"\n",
"2025-04-25 21:31:18,645 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_1.nii.gz\n",
"2025-04-25 21:31:18,682 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_1.nii.gz\n",
"2025-04-25 21:31:18,690 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_1.nii.gz\n",
"[Epoch 171] [pID 1BA001] [totalstep 2] [loss: 0.004563] [ssim: 0.925131] [psnr: 23.789538] [mae: 55.851980] \n",
"\n",
"2025-04-25 21:31:18,736 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_2.nii.gz\n",
"2025-04-25 21:31:18,772 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_2.nii.gz\n",
"2025-04-25 21:31:18,782 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_2.nii.gz\n",
"[Epoch 171] [pID 1BA001] [totalstep 3] [loss: 0.004563] [ssim: 0.922738] [psnr: 23.491716] [mae: 55.262691] \n",
"\n",
"2025-04-25 21:31:18,828 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_3.nii.gz\n",
"2025-04-25 21:31:18,865 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_3.nii.gz\n",
"2025-04-25 21:31:18,874 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_3.nii.gz\n",
"[Epoch 171] [pID 1BA001] [totalstep 4] [loss: 0.004563] [ssim: 0.924360] [psnr: 23.233296] [mae: 55.587496] \n",
"\n",
"2025-04-25 21:31:19,286 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_4.nii.gz\n",
"2025-04-25 21:31:19,322 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_4.nii.gz\n",
"2025-04-25 21:31:19,332 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_4.nii.gz\n",
"[Epoch 171] [pID 1BA001] [totalstep 5] [loss: 0.004563] [ssim: 0.919393] [psnr: 23.410263] [mae: 55.692474] \n",
"\n",
"2025-04-25 21:31:19,379 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_5.nii.gz\n",
"2025-04-25 21:31:19,416 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_5.nii.gz\n",
"2025-04-25 21:31:19,425 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_5.nii.gz\n",
"[Epoch 171] [pID 1BA001] [totalstep 6] [loss: 0.004563] [ssim: 0.920130] [psnr: 23.044145] [mae: 55.196265] \n",
"\n",
"2025-04-25 21:31:19,471 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_6.nii.gz\n",
"2025-04-25 21:31:19,508 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_6.nii.gz\n",
"2025-04-25 21:31:19,518 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_6.nii.gz\n",
"[Epoch 171] [pID 1BA001] [totalstep 7] [loss: 0.004563] [ssim: 0.911006] [psnr: 22.585894] [mae: 60.327661] \n",
"\n",
"2025-04-25 21:31:19,563 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_synthesized_7.nii.gz\n",
"2025-04-25 21:31:19,600 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_seg_1BA001_seg_7.nii.gz\n",
"2025-04-25 21:31:19,609 INFO image_writer.py:197 - writing: logs\\20250425_2125_Infer_ddpm2d_seg2med\\saved_outputs\\volume-0_1BA001_target_7.nii.gz\n",
"[Epoch 171] [Step 7] [conclusion] [mean ssim: 0.924389] [mean psnr: 23.465048] [mean mae: 55.255292] \n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 1/1 [04:52<00:00, 292.99s/it]\n"
]
}
],
"source": [
"from synthrad_conversion.networks.ddpm.ddpm_mri2ct import DiffusionModel\n",
"from synthrad_conversion.networks.launch_model import launch_model\n",
"import synthrad_conversion.networks as networks\n",
"from synthrad_conversion.networks.model_registry import MODEL_REGISTRY\n",
"print(\"all available models:\")\n",
"for model in MODEL_REGISTRY:\n",
" print(model)\n",
"print(\"use model:\", opt.model_name)\n",
"mode = \"test\"\n",
"model_name = opt.model_name\n",
"train_patient_IDs=loader.train_patient_IDs\n",
"test_patient_IDs=loader.test_patient_IDs\n",
"launch_model(model_name, opt, my_paths, train_loader, val_loader, mode,\n",
" train_patient_IDs, test_patient_IDs)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# convert all generated slices to volume"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def rename(folder_path):\n",
" ## rename file as the format pID_type_sliceID\n",
" # 1 delete the prefix, prefix: ct, ct_seg, ct_volume\n",
" for filename in os.listdir(folder_path):\n",
" old_file_path = os.path.join(folder_path, filename)\n",
" parts = filename.split('_')\n",
" if len(parts)>=3:\n",
" patient_ID = parts[0]\n",
" sign_name = parts[-2]\n",
" slice_ID = parts[-1]\n",
"\n",
" if sign_name == 'seg':\n",
" new_filename = f'{patient_ID}_seg_{slice_ID}'\n",
" elif sign_name == 'input':\n",
" new_filename = f'{patient_ID}_seg_{slice_ID}'\n",
" elif sign_name == 'target':\n",
" new_filename = f'{patient_ID}_target_{slice_ID}'\n",
" else: # for synthesized and mask\n",
" new_filename = f'{patient_ID}_{sign_name}_{slice_ID}'\n",
" if not new_filename.endswith('.nii.gz'):\n",
" new_filename += '.nii.gz'\n",
" new_file_path = os.path.join(folder_path, new_filename)\n",
" os.rename(old_file_path, new_file_path)\n",
" print(f'Renamed {old_file_path} to {new_file_path}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from seg2med_evaluation import stack_volume_synthrad_anish \n",
"import importlib\n",
"importlib.reload(stack_volume_synthrad_anish) # εΌΊεΆιζ°ε 载樑ε\n",
"root_path = r'logs'\n",
"folder_list = [\n",
" '20250403_0359_Infer_ddpm2d_seg2med',\n",
" ]\n",
"for folder in folder_list:\n",
" folder_path = os.path.join(root_path, folder, 'saved_outputs')\n",
" # rename(folder_path)\n",
" stack_volume_synthrad_anish.stack_volume(folder_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "torch",
"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.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|