-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTrainer.py
More file actions
688 lines (603 loc) · 25.9 KB
/
TestTrainer.py
File metadata and controls
688 lines (603 loc) · 25.9 KB
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import os
import sys
import torch
import random
import numpy as np
import torch.backends.cudnn as cudnn
from datetime import datetime
from tqdm import tqdm
from time import time, sleep
from torch.amp import autocast
from torch.utils.data import DataLoader
from torch.amp import GradScaler
from torch._dynamo import OptimizedModule
from typing import Tuple, Union, List
from wcode.training.data_augmentation.custom_transforms.scalar_type import RandomScalar
from wcode.training.data_augmentation.compute_initial_patch_size import get_patch_size
from wcode.preprocessing.resampling import ANISO_THRESHOLD
from wcode.net.build_network import build_network
from wcode.training.dataset.BaseDataset import BaseDataset
from wcode.training.loss.CompoundLoss import Tversky_and_CE_loss
from wcode.training.loss.EntropyLoss import MixCrossEntropyLoss
from wcode.training.loss.DiceLoss import TverskyLoss
from wcode.training.loss.deep_supervision import DeepSupervisionWeightedSummator
from wcode.training.logs_writer.logger_for_segmentation import logger
from wcode.training.dataloader.Collater import PatchBasedCollater
from wcode.training.learning_rate.PolyLRScheduler import PolyLRScheduler
from wcode.training.metrics import get_tp_fp_fn_tn
from wcode.utils.file_operations import open_yaml, open_json, copy_file_to_dstFolder
from wcode.utils.others import empty_cache, dummy_context
from wcode.utils.collate_outputs import collate_outputs
from wcode.utils.data_io import files_ending_for_2d_img, files_ending_for_sitk
from wcode.inferring.PatchBasedPredictor import PatchBasedPredictor
from wcode.inferring.NaturalImagePredictor import NaturalImagePredictor
from wcode.inferring.Evaluator import Evaluator
from wcode.inferring.utils.load_pretrain_weight import load_pretrained_weights
from wcode.training.Trainers.Fully.PatchBasedTrainer.PatchBasedTrainer import (
PatchBasedTrainer,
)
from wcode.training.Trainers.Weakly.Incomplete_Learning.ReCo_I2P_Plus.models import (
DIVNet_v4,
)
from wcode.training.loss.EntropyLoss import reliability_based_co_teaching_loss_J
from wcode.utils.ramps import ramps
class TestTrainer(PatchBasedTrainer):
def __init__(
self,
config_file_path: str,
fold: int,
tversky_alpha: float,
awce_beta: float,
consis_weight: float,
rampup_epoch: int,
update_way: str,
select_way: str,
num_prototype: int,
memory_rate: float,
verbose: bool = False,
):
# hyperparameter
self.tversky_alpha = tversky_alpha
self.awce_beta = awce_beta
self.consis_weight = consis_weight
self.rampup_epoch = rampup_epoch
self.update_way = update_way
self.select_way = select_way
self.num_prototype = num_prototype
self.memory_rate = memory_rate
hyperparams_name = "tversky_alpha_{}_awce_beta_{}_consis_weight_{}_rampup_epoch_{}_update_way_{}_select_way_{}_num_prototype_{}_memory_rate_{}".format(
self.tversky_alpha,
self.awce_beta,
self.consis_weight,
self.rampup_epoch,
self.update_way,
self.select_way,
self.num_prototype,
self.memory_rate,
)
self.verbose = verbose
self.config_dict = open_yaml(config_file_path)
if self.config_dict.__contains__("Inferring_settings"):
del self.config_dict["Inferring_settings"]
self.get_train_settings(self.config_dict["Training_settings"])
self.fold = fold
self.allow_mirroring_axes_during_inference = None
self.was_initialized = False
self._best_ema = None
timestamp = datetime.now()
time_ = "Train_Log_%d_%d_%d_%02.0d_%02.0d_%02.0d" % (
timestamp.year,
timestamp.month,
timestamp.day,
timestamp.hour,
timestamp.minute,
timestamp.second,
)
log_folder_name = self.method_name if self.method_name is not None else time_
self.logs_output_folder = os.path.join(
"./Logs",
self.dataset_name,
log_folder_name,
hyperparams_name,
"fold_" + self.fold,
)
config_and_code_save_path = os.path.join(
self.logs_output_folder, "Config_and_code"
)
if not os.path.exists(config_and_code_save_path):
os.makedirs(config_and_code_save_path)
print("Training logs will be saved in:", self.logs_output_folder)
# copy the config file to the logs folder
copy_file_to_dstFolder(config_file_path, config_and_code_save_path)
# copy the trainer file to the logs folder
script_path = os.path.abspath(__file__)
copy_file_to_dstFolder(script_path, config_and_code_save_path)
self.log_file = os.path.join(self.logs_output_folder, time_ + ".txt")
self.logger = logger()
self.current_epoch = 0
# checkpoint saving stuff
self.save_every = 1
self.disable_checkpointing = False
self.device = self.get_device()
self.grad_scaler = GradScaler() if self.device.type == "cuda" else None
if self.checkpoint_path is not None:
self.load_checkpoint(self.checkpoint_path)
def initialize(self):
if not self.was_initialized:
self.is_ddp = False
self.init_random()
self.setting_check()
if len(self.config_dict["Network"]["kernel_size"][0]) == 3:
self.preprocess_config = "3d"
elif len(self.config_dict["Network"]["kernel_size"][0]) == 2:
self.preprocess_config = "2d"
else:
raise Exception()
self.network = self.get_networks(self.config_dict["Network"])
if self.pretrained_weight is not None:
self.print_to_log_file(
"Loading pretrained weight from {}".format(self.pretrained_weight)
)
load_pretrained_weights(self.network, self.pretrained_weight)
self.network.to(self.device)
self.print_to_log_file("Compiling network...")
self.network = torch.compile(self.network)
self.do_deep_supervision = self.config_dict["Network"]["deep_supervision"]
self.optimizer, self.lr_scheduler = self.get_optimizers()
self.rampup = ramps(
start_iter=0,
end_iter=self.rampup_epoch - 1,
start_value=0.0,
end_value=self.consis_weight,
mode="sigmoid",
)
self.tversky_loss = TverskyLoss(
alpha=self.tversky_alpha,
beta=1 - self.tversky_alpha,
smooth=1e-5,
batch_dice=True,
do_bg=False,
ddp=self.is_ddp,
apply_nonlin=True,
)
self.mixce_loss = MixCrossEntropyLoss(beta=self.awce_beta)
self.pseudo_loss = reliability_based_co_teaching_loss_J()
self.val_loss = Tversky_and_CE_loss(
{
"batch_dice": True,
"smooth": 1e-5,
"do_bg": True,
"ddp": self.is_ddp,
"apply_nonlin": True,
},
{},
weight_ce=1,
weight_tversky=1,
ignore_label=self.ignore_label,
)
if self.do_deep_supervision:
self.tversky_loss = self._build_deep_supervision_loss_object(
self.tversky_loss
)
self.mixce_loss = self._build_deep_supervision_loss_object(
self.mixce_loss
)
self.pseudo_loss = self._build_deep_supervision_loss_object(
self.pseudo_loss
)
self.val_loss = self._build_deep_supervision_loss_object(self.val_loss)
self.was_initialized = True
else:
raise Exception("Initialization was done before initialize method???")
def get_networks(self, network_settings):
return DIVNet_v4(
network_settings,
num_prototype=self.num_prototype,
memory_rate=self.memory_rate,
update_way=self.update_way,
select_way=self.select_way,
num_head_lst=[1, 1, 2, 4, 8],
)
def run_training(self):
self.train_start()
for epoch in range(self.current_epoch, self.num_epochs):
self.epoch_start()
self.train_epoch_start()
train_outputs = []
for batch_id in tqdm(
range(self.tr_iterations_per_epoch), disable=self.verbose
):
try:
train_outputs.append(self.train_step(next(self.iter_train)))
except StopIteration:
self.iter_train = iter(self.dataloader_train)
train_outputs.append(self.train_step(next(self.iter_train)))
self.train_epoch_end(train_outputs)
with torch.no_grad():
self.validation_epoch_start()
val_outputs = []
for batch_id in tqdm(
range(self.val_iterations_per_epoch), disable=self.verbose
):
try:
val_outputs.append(self.validation_step(next(self.iter_val)))
except StopIteration:
self.iter_val = iter(self.dataloader_val)
val_outputs.append(self.validation_step(next(self.iter_val)))
self.validation_epoch_end(val_outputs)
self.epoch_end(epoch)
self.train_end()
def train_start(self):
if not self.was_initialized:
self.initialize()
empty_cache(self.device)
self.dataloader_train, self.dataloader_val = self.get_train_and_val_dataloader()
def train_end(self):
self.save_checkpoint(
os.path.join(self.logs_output_folder, "checkpoint_final.pth")
)
if os.path.isfile(
os.path.join(self.logs_output_folder, "checkpoint_latest.pth")
):
os.remove(os.path.join(self.logs_output_folder, "checkpoint_latest.pth"))
empty_cache(self.device)
self.print_to_log_file("Training done.")
self.perform_actual_validation(save_probabilities=False)
def epoch_start(self):
self.logger.log("epoch_start_timestamps", time(), self.current_epoch)
def epoch_end(self, epoch):
self.logger.log("epoch_end_timestamps", time(), self.current_epoch)
self.print_to_log_file(
"train_loss", np.round(self.logger.logging["train_losses"][-1], decimals=4)
)
self.print_to_log_file(
"val_loss", np.round(self.logger.logging["val_losses"][-1], decimals=4)
)
self.print_to_log_file(
"Pseudo dice",
[
np.round(i, decimals=4)
for i in self.logger.logging["dice_per_class"][-1]
],
)
self.print_to_log_file(
f"Epoch time: {np.round(self.logger.logging['epoch_end_timestamps'][-1] - self.logger.logging['epoch_start_timestamps'][-1], decimals=2)} s"
)
# handling periodic checkpointing
current_epoch = self.current_epoch
if (current_epoch + 1) % self.save_every == 0 and current_epoch != (
self.num_epochs - 1
):
self.save_checkpoint(
os.path.join(self.logs_output_folder, "checkpoint_latest.pth")
)
# handle 'best' checkpointing. ema_fg_dice is computed by the logger and can be accessed like this
if (
self._best_ema is None
or self.logger.logging["ema_fg_dice"][-1] > self._best_ema
):
self._best_ema = self.logger.logging["ema_fg_dice"][-1]
self.print_to_log_file(
f"Yayy! New best EMA pseudo Dice: {np.round(self._best_ema, decimals=4)}"
)
self.save_checkpoint(
os.path.join(self.logs_output_folder, "checkpoint_best.pth")
)
self.logger.plot_progress_png(self.logs_output_folder)
self.current_epoch += 1
def train_epoch_start(self):
self.iter_train = iter(self.dataloader_train)
self.network.train()
# self.lr_scheduler.step(self.current_epoch)
# self.lr_scheduler.step()
self.print_to_log_file("")
self.print_to_log_file(f"Epoch {self.current_epoch}")
self.print_to_log_file(
f"learning rate: {np.round(self.optimizer.param_groups[0]['lr'], decimals=5)}"
)
self.logger.log(
"learning_rates", self.optimizer.param_groups[0]["lr"], self.current_epoch
)
def train_step(self, batch):
# images in (b, c, (z,) y, x) and labels in (b, 1, (z,) y, x) or list object if do deep supervision
images = batch["image"]
labels = batch["label"]
# to device
images = images.to(self.device, non_blocking=True)
if isinstance(labels, list):
labels = [i.to(self.device, non_blocking=True) for i in labels]
else:
labels = labels.to(self.device, non_blocking=True)
self.optimizer.zero_grad(set_to_none=True)
with (
autocast(self.device.type, enabled=True)
if self.device.type == "cuda"
else dummy_context()
):
outputs = self.network(images, labels)
net1_logits, net2_logits = (
outputs["pred_for_train"][0],
outputs["pred_for_train"][1],
)
# Compute Loss
loss_by_label = (
self.tversky_loss(net1_logits, labels)
+ self.mixce_loss(net1_logits, labels)
+ self.tversky_loss(net2_logits, labels)
+ self.mixce_loss(net2_logits, labels)
)
weight_pseudo = self.rampup.get_value(self.current_epoch)
# # CPS
# loss_by_pseudo = weight_pseudo * (
# self.pseudo_loss(
# net1_logits, net2_logits.detach().argmax(dim=1, keepdim=True)
# )
# + self.pseudo_loss(
# net2_logits, net1_logits.detach().argmax(dim=1, keepdim=True)
# )
# )
loss_by_pseudo = self.pseudo_loss(
net1_logits,
net2_logits,
outputs["feature"][0][-1],
outputs["feature"][1][-1],
)
l = loss_by_label + weight_pseudo * loss_by_pseudo
if self.grad_scaler is not None:
self.grad_scaler.scale(l).backward()
self.grad_scaler.unscale_(self.optimizer)
torch.nn.utils.clip_grad_norm_(self.network.parameters(), 12)
self.grad_scaler.step(self.optimizer)
self.grad_scaler.update()
else:
l.backward()
torch.nn.utils.clip_grad_norm_(self.network.parameters(), 12)
self.optimizer.step()
return {"loss": l.detach().cpu().numpy()}
def train_epoch_end(self, train_outputs):
outputs = collate_outputs(train_outputs)
self.lr_scheduler.step()
loss_here = np.mean(outputs["loss"])
self.logger.log("train_losses", loss_here, self.current_epoch)
def validation_epoch_start(self):
self.iter_val = iter(self.dataloader_val)
self.network.eval()
def validation_step(self, batch):
# images in (b, c, (z,) y, x) and labels in (b, 1, (z,) y, x) or list object if do deep supervision
images = batch["image"]
labels = batch["label"]
images = images.to(self.device, non_blocking=True)
if isinstance(labels, list):
labels = [i.to(self.device, non_blocking=True) for i in labels]
else:
labels = labels.to(self.device, non_blocking=True)
with (
autocast(self.device.type, enabled=True)
if self.device.type == "cuda"
else dummy_context()
):
outputs = self.network(images)
if isinstance(outputs, dict):
outputs = outputs["pred"]
del images
l = self.val_loss(outputs, labels)
# use the new name of outputs and labels, so that you only need to change the network inference process
# during validation and the variable name assignment code below, without changing any evaluation code.
if isinstance(outputs, (list, tuple)):
output = outputs[0]
target = labels[0]
else:
output = outputs
target = labels
# the following is needed for online evaluation. Fake dice (green line)
axes = [0] + list(range(2, len(output.shape)))
# no need for softmax
output_seg = output.argmax(1)[:, None]
predicted_segmentation_onehot = torch.zeros(
output.shape, device=output.device, dtype=torch.float32
)
predicted_segmentation_onehot.scatter_(1, output_seg, 1)
del output_seg
tp, fp, fn, _ = get_tp_fp_fn_tn(
predicted_segmentation_onehot, target, axes=axes, mask=None
)
tp_hard = tp.detach().cpu().numpy()
fp_hard = fp.detach().cpu().numpy()
fn_hard = fn.detach().cpu().numpy()
tp_hard = tp_hard[1:]
fp_hard = fp_hard[1:]
fn_hard = fn_hard[1:]
return {
"loss": l.detach().cpu().numpy(),
"tp_hard": tp_hard,
"fp_hard": fp_hard,
"fn_hard": fn_hard,
}
def validation_epoch_end(self, val_outputs):
outputs_collated = collate_outputs(val_outputs)
tp = np.sum(outputs_collated["tp_hard"], 0)
fp = np.sum(outputs_collated["fp_hard"], 0)
fn = np.sum(outputs_collated["fn_hard"], 0)
loss_here = np.mean(outputs_collated["loss"])
global_dc_per_class = [
i for i in [2 * i / (2 * i + j + k) for i, j, k in zip(tp, fp, fn)]
]
mean_fg_dice = np.nanmean(global_dc_per_class)
self.logger.log("mean_fg_dice", mean_fg_dice, self.current_epoch)
self.logger.log("dice_per_class", global_dc_per_class, self.current_epoch)
self.logger.log("val_losses", loss_here, self.current_epoch)
def save_checkpoint(self, filename: str) -> None:
if not self.disable_checkpointing:
mod = self.network
if isinstance(mod, OptimizedModule):
mod = mod._orig_mod
checkpoint = {
"network_weights": mod.state_dict(),
"optimizer_state": self.optimizer.state_dict(),
"grad_scaler_state": (
self.grad_scaler.state_dict()
if self.grad_scaler is not None
else None
),
"logging": self.logger.get_checkpoint(),
"_best_ema": self._best_ema,
"current_epoch": self.current_epoch + 1,
"LRScheduler_step": self.lr_scheduler.ctr,
}
torch.save(checkpoint, filename)
else:
self.print_to_log_file("No checkpoint written, checkpointing is disabled")
def load_checkpoint(self, filename_or_checkpoint):
self.print_to_log_file("Load checkpoint...")
if not self.was_initialized:
self.initialize()
if isinstance(filename_or_checkpoint, str):
checkpoint = torch.load(
filename_or_checkpoint, map_location=self.device, weights_only=False
)
# if state dict comes from nn.DataParallel but we use non-parallel model here then the state dict keys do not
# match. Use heuristic to make it match
new_state_dict = {}
for k, value in checkpoint["network_weights"].items():
key = k
if key not in self.network.state_dict().keys() and key.startswith(
"module."
):
key = key[7:]
new_state_dict[key] = value
if "checkpoint_final.pth" in os.listdir(self.logs_output_folder):
self.current_epoch = self.num_epochs
else:
self.current_epoch = checkpoint["current_epoch"]
self.logger.load_checkpoint(checkpoint["logging"])
self._best_ema = checkpoint["_best_ema"]
if isinstance(self.network, OptimizedModule):
self.network._orig_mod.load_state_dict(new_state_dict)
else:
self.network.load_state_dict(new_state_dict)
self.optimizer.load_state_dict(checkpoint["optimizer_state"])
if self.grad_scaler is not None:
if checkpoint["grad_scaler_state"] is not None:
self.grad_scaler.load_state_dict(checkpoint["grad_scaler_state"])
self.lr_scheduler.ctr = checkpoint["LRScheduler_step"]
def perform_actual_validation(self, save_probabilities: bool = False):
self.print_to_log_file("----------Perform actual validation----------")
dataset_path = os.path.join("./Dataset_preprocessed", self.dataset_name)
original_img_folder = os.path.join(
dataset_path, "preprocessed_datas_" + self.preprocess_config
)
predictions_save_folder = os.path.join(self.logs_output_folder, "validation")
if predictions_save_folder is not None and not os.path.exists(
predictions_save_folder
):
os.makedirs(predictions_save_folder)
model_path = os.path.join(self.logs_output_folder, "checkpoint_final.pth")
self.network = self.get_networks(self.config_dict["Network"])
load_pretrained_weights(self.network, model_path, load_all=True, verbose=True)
self.network.to(self.device)
self.print_to_log_file("Compiling network for actual validation...")
self.network = torch.compile(self.network)
best_saved_model = torch.load(
os.path.join(self.logs_output_folder, "checkpoint_best.pth"),
weights_only=False,
)
self.print_to_log_file("Pseudo Best Epoch:", best_saved_model["current_epoch"])
del best_saved_model
predict_configs = {
"dataset_name": self.dataset_name,
"modality": self.modality,
"fold": self.fold,
"split": "val",
"original_img_folder": original_img_folder,
"predictions_save_folder": predictions_save_folder,
"model_path": model_path,
"device": self.device_dict,
"overwrite": True,
"save_probabilities": save_probabilities,
"patch_size": self.patch_size,
"tile_step_size": 0.5,
"use_gaussian": True,
"perform_everything_on_gpu": True,
"use_mirroring": True,
"allowed_mirroring_axes": self.allow_mirroring_axes_during_inference,
"num_processes": self.num_processes,
}
self.config_dict["Inferring_settings"] = predict_configs
dataset_split = open_json(
os.path.join(
"./Dataset_preprocessed", self.dataset_name, "dataset_split.json"
)
)
data_path_list = [
i
for i in os.listdir(original_img_folder)
if i.endswith(".npy") and not i.endswith("_seg.npy")
]
validation_data_file = [
i
for i in data_path_list
if i.split(".")[0]
in dataset_split["0" if self.fold == "all" else self.fold]["val"]
]
validation_data_file.sort()
validation_data_path = [
os.path.join(original_img_folder, i) for i in validation_data_file
]
validation_pkl_path = [
os.path.join(original_img_folder, i.replace(".npy", ".pkl"))
for i in validation_data_file
]
predictions_save_path = [
os.path.join(predictions_save_folder, i.replace(".npy", ""))
for i in validation_data_file
]
iter_lst = []
for data, output_file, data_properites in zip(
validation_data_path, predictions_save_path, validation_pkl_path
):
iter_lst.append(
{
"data": data,
"output_file": output_file,
"data_properites": data_properites,
}
)
if self.natural_image_flag:
predictor = NaturalImagePredictor(
self.config_dict, allow_tqdm=True, verbose=False
)
predictor.manual_initialize(
self.network, self.config_dict["Network"]["out_channels"]
)
self.print_to_log_file("Start predicting using NaturalImagePredictor.")
start = time()
predictor.predict_from_data_iterator(
data_iterator=iter_lst,
save_vis_mask=True,
save_or_return_probabilities=save_probabilities,
)
self.print_to_log_file("Predicting ends. Cost: {}s".format(time() - start))
else:
predictor = PatchBasedPredictor(
self.config_dict, allow_tqdm=True, verbose=False
)
predictor.manual_initialize(
self.network, self.config_dict["Network"]["out_channels"]
)
self.print_to_log_file("Start predicting using PatchBasedPredictor.")
start = time()
predictor.predict_from_data_iterator(
data_iterator=iter_lst,
predict_way=self.preprocess_config,
save_or_return_probabilities=save_probabilities,
)
self.print_to_log_file("Predicting ends. Cost: {}s".format(time() - start))
ground_truth_folder = os.path.join(dataset_path, "gt_segmentations")
evaluator = Evaluator(
predictions_save_folder,
ground_truth_folder,
dataset_yaml_or_its_path=self.dataset_yaml,
num_processes=self.num_processes,
)
evaluator.run()
self.print_to_log_file("Evaluating ends.")