-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmodel_workers.py
More file actions
1382 lines (1132 loc) · 47.1 KB
/
Copy pathmodel_workers.py
File metadata and controls
1382 lines (1132 loc) · 47.1 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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import platform
from pathlib import Path
import importlib.util
from typing import Optional
import numpy as np
from tifffile import imwrite
import torch
from tqdm import tqdm
# MONAI
from monai.data import CacheDataset
from monai.data import DataLoader
from monai.data import Dataset
from monai.data import decollate_batch
from monai.data import pad_list_data_collate
from monai.data import PatchDataset
from monai.inferers import sliding_window_inference
from monai.metrics import DiceMetric
from monai.transforms import AddChannel
from monai.transforms import AsDiscrete
from monai.transforms import Compose
from monai.transforms import EnsureChannelFirstd
from monai.transforms import EnsureType
from monai.transforms import EnsureTyped
from monai.transforms import LoadImaged
from monai.transforms import Orientationd
from monai.transforms import Rand3DElasticd
from monai.transforms import RandAffined
from monai.transforms import RandFlipd
from monai.transforms import RandRotate90d
from monai.transforms import RandShiftIntensityd
from monai.transforms import RandSpatialCropSamplesd
from monai.transforms import SpatialPad
from monai.transforms import SpatialPadd
from monai.transforms import ToTensor
from monai.transforms import Zoom
from monai.utils import set_determinism
# threads
from napari.qt.threading import GeneratorWorker
from napari.qt.threading import WorkerBaseSignals
# Qt
from qtpy.QtCore import Signal
from napari_cellseg3d import utils
from napari_cellseg3d import log_utility
# local
from napari_cellseg3d.model_instance_seg import binary_connected
from napari_cellseg3d.model_instance_seg import binary_watershed
from napari_cellseg3d.model_instance_seg import volume_stats
"""
Writing something to log messages from outside the main thread is rather problematic (plenty of silent crashes...)
so instead, following the instructions in the guides below to have a worker with custom signals, I implemented
a custom worker function."""
# FutureReference():
# https://python-forum.io/thread-31349.html
# https://www.pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/
# https://napari-staging-site.github.io/guides/stable/threading.html
WEIGHTS_DIR = os.path.dirname(os.path.realpath(__file__)) + str(
Path("/models/pretrained")
)
class WeightsDownloader:
"""A utility class the downloads the weights of a model when needed."""
def __init__(self, log_widget: Optional[log_utility.Log] = None):
"""
Creates a WeightsDownloader, optionally with a log widget to display the progress.
Args:
log_widget (log_utility.Log): a Log to display the progress bar in. If None, uses print()
"""
self.log_widget = log_widget
def download_weights(self, model_name: str, model_weights_filename: str):
"""
Downloads a specific pretrained model.
This code is adapted from DeepLabCut with permission from MWMathis.
Args:
model_name (str): name of the model to download
model_weights_filename (str): name of the .pth file expected for the model
"""
import json
import tarfile
import urllib.request
def show_progress(count, block_size, total_size):
pbar.update(block_size)
cellseg3d_path = os.path.split(
importlib.util.find_spec("napari_cellseg3d").origin
)[0]
pretrained_folder_path = os.path.join(
cellseg3d_path, "models", "pretrained"
)
json_path = os.path.join(
pretrained_folder_path, "pretrained_model_urls.json"
)
check_path = os.path.join(
pretrained_folder_path, model_weights_filename
)
if os.path.exists(check_path):
message = f"Weight file {model_weights_filename} already exists, skipping download"
if self.log_widget is not None:
self.log_widget.print_and_log(message, printing=False)
print(message)
return
with open(json_path) as f:
neturls = json.load(f)
if model_name in neturls.keys():
url = neturls[model_name]
response = urllib.request.urlopen(url)
start_message = f"Downloading the model from the M.W. Mathis Lab server {url}...."
total_size = int(response.getheader("Content-Length"))
if self.log_widget is None:
print(start_message)
pbar = tqdm(unit="B", total=total_size, position=0)
else:
self.log_widget.print_and_log(start_message)
pbar = tqdm(
unit="B",
total=total_size,
position=0,
file=self.log_widget,
)
filename, _ = urllib.request.urlretrieve(
url, reporthook=show_progress
)
with tarfile.open(filename, mode="r:gz") as tar:
tar.extractall(pretrained_folder_path)
else:
raise ValueError(
f"Unknown model: {model_name}. Should be one of {', '.join(neturls)}"
)
class LogSignal(WorkerBaseSignals):
"""Signal to send messages to be logged from another thread.
Separate from Worker instances as indicated `here`_""" # TODO link ?
log_signal = Signal(str)
"""qtpy.QtCore.Signal: signal to be sent when some text should be logged"""
warn_signal = Signal(str)
"""qtpy.QtCore.Signal: signal to be sent when some warning should be emitted in main thread"""
# Should not be an instance variable but a class variable, not defined in __init__, see
# https://stackoverflow.com/questions/2970312/pyqt4-qtcore-pyqtsignal-object-has-no-attribute-connect
def __init__(self):
super().__init__()
# TODO : use dataclass for config instead ?
class InferenceWorker(GeneratorWorker):
"""A custom worker to run inference jobs in.
Inherits from :py:class:`napari.qt.threading.GeneratorWorker`"""
def __init__(
self,
device,
model_dict,
weights_dict,
results_path,
filetype,
transforms,
instance,
use_window,
window_infer_size,
window_overlap,
keep_on_cpu,
stats_csv,
images_filepaths=None,
layer=None, # FIXME
):
"""Initializes a worker for inference with the arguments needed by the :py:func:`~inference` function.
Args:
* device: cuda or cpu device to use for torch
* model_dict: the :py:attr:`~self.models_dict` dictionary to obtain the model name, class and instance
* weights_dict: dict with "custom" : bool to use custom weights or not; "path" : the path to weights if custom or name of the file if not custom
* results_path: the path to save the results to
* filetype: the file extension to use when saving,
* transforms: a dict containing transforms to perform at various times.
* instance: a dict containing parameters regarding instance segmentation
* use_window: use window inference with specific size or whole image
* window_infer_size: size of window if use_window is True
* keep_on_cpu: keep images on CPU or no
* stats_csv: compute stats on cells and save them to a csv file
* images_filepaths: the paths to the images of the dataset
* layer: the layer to run inference on
Note: See :py:func:`~self.inference`
"""
super().__init__(self.inference)
self._signals = LogSignal() # add custom signals
self.log_signal = self._signals.log_signal
self.warn_signal = self._signals.warn_signal
###########################################
###########################################
self.device = device
self.model_dict = model_dict
self.weights_dict = weights_dict
self.results_path = results_path
self.filetype = filetype
self.transforms = transforms
self.instance_params = instance
self.use_window = use_window
self.window_infer_size = window_infer_size
self.window_overlap_percentage = window_overlap
self.keep_on_cpu = keep_on_cpu
self.stats_to_csv = stats_csv
############################################
############################################
self.layer = layer
self.images_filepaths = images_filepaths
############################################
############################################
"""These attributes are all arguments of :py:func:~inference, please see that for reference"""
self.downloader = WeightsDownloader()
"""Download utility"""
@staticmethod
def create_inference_dict(images_filepaths):
"""Create a dict for MONAI with "image" keys with all image paths in :py:attr:`~self.images_filepaths`
Returns:
dict: list of image paths from loaded folder"""
data_dicts = [{"image": image_name} for image_name in images_filepaths]
return data_dicts
def set_download_log(self, widget):
self.downloader.log_widget = widget
def log(self, text):
"""Sends a signal that ``text`` should be logged
Args:
text (str): text to logged
"""
self.log_signal.emit(text)
def warn(self, warning):
"""Sends a warning to main thread"""
self.warn_signal.emit(warning)
def log_parameters(self):
self.log("-" * 20)
self.log("\nParameters summary :")
self.log(f"Model is : {self.model_dict['name']}")
if self.transforms["thresh"][0]:
self.log(
f"Thresholding is enabled at {self.transforms['thresh'][1]}"
)
if self.use_window:
status = "enabled"
else:
status = "disabled"
self.log(f"Window inference is {status}\n")
if self.keep_on_cpu:
self.log(f"Dataset loaded to CPU")
else:
self.log(f"Dataset loaded on {self.device}")
if self.transforms["zoom"][0]:
self.log(f"Scaling factor : {self.transforms['zoom'][1]} (x,y,z)")
if self.instance_params["do_instance"]:
self.log(
f"Instance segmentation enabled, method : {self.instance_params['method']}\n"
f"Probability threshold is {self.instance_params['threshold']:.2f}\n"
f"Objects smaller than {self.instance_params['size_small']} pixels will be removed\n"
)
self.log("-" * 20)
def load_folder(self):
images_dict = self.create_inference_dict(self.images_filepaths)
# TODO : better solution than loading first image always ?
data_check = LoadImaged(keys=["image"])(images_dict[0])
check = data_check["image"].shape
self.log("\nChecking dimensions...")
pad = utils.get_padding_dim(check)
# dims = self.model_dict["model_input_size"]
#
# if self.model_dict["name"] == "SegResNet":
# model = self.model_dict["class"].get_net(
# input_image_size=[
# dims,
# dims,
# dims,
# ]
# )
# elif self.model_dict["name"] == "SwinUNetR":
# model = self.model_dict["class"].get_net(
# img_size=[dims, dims, dims],
# use_checkpoint=False,
# )
# else:
# model = self.model_dict["class"].get_net()
#
# self.log_parameters()
#
# model.to(self.device)
# print("FILEPATHS PRINT")
# print(self.images_filepaths)
if self.use_window:
load_transforms = Compose(
[
LoadImaged(keys=["image"]),
# AddChanneld(keys=["image"]), #already done
EnsureChannelFirstd(keys=["image"]),
# Orientationd(keys=["image"], axcodes="PLI"),
# anisotropic_transform,
EnsureTyped(keys=["image"]),
]
)
else:
load_transforms = Compose(
[
LoadImaged(keys=["image"]),
# AddChanneld(keys=["image"]), #already done
EnsureChannelFirstd(keys=["image"]),
# Orientationd(keys=["image"], axcodes="PLI"),
# anisotropic_transform,
SpatialPadd(keys=["image"], spatial_size=pad),
EnsureTyped(keys=["image"]),
]
)
self.log("\nLoading dataset...")
inference_ds = Dataset(data=images_dict, transform=load_transforms)
inference_loader = DataLoader(
inference_ds, batch_size=1, num_workers=2
)
self.log("Done")
return inference_loader
def load_layer(self):
data = np.squeeze(self.layer.data)
volume = np.array(data, dtype=np.int16)
volume_dims = len(volume.shape)
if volume_dims != 3:
raise ValueError(
f"Data array is not 3-dimensional but {volume_dims}-dimensional,"
f" please check for extra channel/batch dimensions"
)
volume = np.swapaxes(
volume, 0, 2
) # for anisotropy to be monai-like, i.e. zyx # FIXME rotation not always correct
print("Loading layer\n")
dims_check = volume.shape
self.log("\nChecking dimensions...")
pad = utils.get_padding_dim(dims_check)
# print(volume.shape)
# print(volume.dtype)
if self.use_window:
load_transforms = Compose(
[
ToTensor(),
# anisotropic_transform,
AddChannel(),
# SpatialPad(spatial_size=pad),
AddChannel(),
EnsureType(),
],
map_items=False,
log_stats=True,
)
else:
load_transforms = Compose(
[
ToTensor(),
# anisotropic_transform,
AddChannel(),
SpatialPad(spatial_size=pad),
AddChannel(),
EnsureType(),
],
map_items=False,
log_stats=True,
)
self.log("\nLoading dataset...")
input_image = load_transforms(volume)
self.log("Done")
return input_image
def model_output(
self,
inputs,
model,
post_process_transforms,
post_process=True,
aniso_transform=None,
):
inputs = inputs.to("cpu")
model_output = lambda inputs: post_process_transforms(
self.model_dict["class"].get_output(model, inputs)
)
if self.keep_on_cpu:
dataset_device = "cpu"
else:
dataset_device = self.device
if self.use_window:
window_size = self.window_infer_size
window_overlap = self.window_overlap_percentage
else:
window_size = None
window_overlap = 0.25
outputs = sliding_window_inference(
inputs,
roi_size=window_size,
sw_batch_size=1,
predictor=model_output,
sw_device=self.device,
device=dataset_device,
overlap=window_overlap,
)
out = outputs.detach().cpu()
if aniso_transform is not None:
out = aniso_transform(out)
if post_process:
out = np.array(out).astype(np.float32)
out = np.squeeze(out)
return out
else:
return out
def create_result_dict(
self,
semantic_labels,
instance_labels,
from_layer: bool,
original=None,
data_dict=None,
i=0,
):
if not from_layer and original is None:
raise ValueError(
"If the image is not from a layer, an original should always be available"
)
if from_layer:
if i != 0:
raise ValueError(
"A layer's ID should always be 0 (default value)"
)
semantic_labels = np.swapaxes(semantic_labels, 0, 2)
return {
"image_id": i + 1,
"original": original,
"instance_labels": instance_labels,
"object stats": data_dict,
"result": semantic_labels,
"model_name": self.model_dict["name"],
}
def get_original_filename(self, i):
return os.path.basename(self.images_filepaths[i]).split(".")[0]
def get_instance_result(self, semantic_labels, from_layer=False, i=-1):
if not from_layer and i == -1:
raise ValueError(
"An ID should be provided when running from a file"
)
if self.instance_params["do_instance"]:
instance_labels = self.instance_seg(
semantic_labels,
i + 1,
)
if from_layer:
instance_labels = np.swapaxes(instance_labels, 0, 2)
data_dict = self.stats_csv(instance_labels)
else:
instance_labels = None
data_dict = None
return instance_labels, data_dict
def save_image(
self,
image,
from_layer=False,
i=0,
):
if not from_layer:
original_filename = "_" + self.get_original_filename(i) + "_"
else:
original_filename = "_"
time = utils.get_date_time()
file_path = (
self.results_path
+ "/"
+ f"Prediction_{i+1}"
+ original_filename
+ self.model_dict["name"]
+ f"_{time}_"
+ self.filetype
)
imwrite(file_path, image)
filename = os.path.split(file_path)[1]
if from_layer:
self.log(f"\nLayer prediction saved as : {filename}")
else:
self.log(f"\nFile n°{i+1} saved as : {filename}")
def aniso_transform(self, image):
zoom = self.transforms["zoom"][1]
anisotropic_transform = Zoom(
zoom=zoom,
keep_size=False,
padding_mode="empty",
)
return anisotropic_transform(image[0])
def instance_seg(self, to_instance, image_id=0, original_filename="layer"):
if image_id is not None:
self.log(f"\nRunning instance segmentation for image n°{image_id}")
threshold = self.instance_params["threshold"]
size_small = self.instance_params["size_small"]
method_name = self.instance_params["method"]
if method_name == "Watershed":
def method(image):
return binary_watershed(image, threshold, size_small)
elif method_name == "Connected components":
def method(image):
return binary_connected(image, threshold, size_small)
else:
raise NotImplementedError(
"Selected instance segmentation method is not defined"
)
instance_labels = method(to_instance)
instance_filepath = (
self.results_path
+ "/"
+ f"Instance_seg_labels_{image_id}_"
+ original_filename
+ "_"
+ self.model_dict["name"]
+ f"_{utils.get_date_time()}_"
+ self.filetype
)
imwrite(instance_filepath, instance_labels)
self.log(
f"Instance segmentation results for image n°{image_id} have been saved as:"
)
self.log(os.path.split(instance_filepath)[1])
return instance_labels
def inference_on_folder(self, inf_data, i, model, post_process_transforms):
self.log("-" * 10)
self.log(f"Inference started on image n°{i + 1}...")
inputs = inf_data["image"]
out = self.model_output(
inputs,
model,
post_process_transforms,
aniso_transform=self.aniso_transform,
)
self.save_image(out, i=i)
instance_labels, data_dict = self.get_instance_result(out, i=i)
original = np.array(inf_data["image"]).astype(np.float32)
self.log(f"Inference completed on layer")
return self.create_result_dict(
out,
instance_labels,
from_layer=False,
original=original,
data_dict=data_dict,
i=i,
)
def stats_csv(self, instance_labels):
if self.stats_to_csv:
# try:
data_dict = volume_stats(
instance_labels
) # TODO test with area mesh function
return data_dict
# except ValueError as e:
# self.log(f"Error occurred during stats computing : {e}")
# return None
else:
return None
def inference_on_layer(self, image, model, post_process_transforms):
self.log("-" * 10)
self.log(f"Inference started on layer...")
image = image.type(torch.FloatTensor)
out = self.model_output(
image,
model,
post_process_transforms,
aniso_transform=self.aniso_transform,
)
self.save_image(out, from_layer=True)
instance_labels, data_dict = self.get_instance_result(
out, from_layer=True
)
return self.create_result_dict(
out, instance_labels, from_layer=True, data_dict=data_dict
)
def inference(self):
"""
Requires:
* device: cuda or cpu device to use for torch
* model_dict: the :py:attr:`~self.models_dict` dictionary to obtain the model name, class and instance
* weights: the loaded weights from the model
* images_filepaths: the paths to the images of the dataset
* results_path: the path to save the results to
* filetype: the file extension to use when saving,
* transforms: a dict containing transforms to perform at various times.
* use_window: use window inference with specific size or whole image
* window_infer_size: size of window if use_window is True
* keep_on_cpu: keep images on CPU or no
* stats_csv: compute stats on cells and save them to a csv file
Yields:
dict: contains :
* "image_id" : index of the returned image
* "original" : original volume used for inference
* "result" : inference result
"""
sys = platform.system()
print(f"OS is {sys}")
if sys == "Darwin":
torch.set_num_threads(1) # required for threading on macOS ?
self.log("Number of threads has been set to 1 for macOS")
try:
dims = self.model_dict["model_input_size"]
self.log(f"MODEL DIMS : {dims}")
self.log(self.model_dict["name"])
if self.model_dict["name"] == "SegResNet":
model = self.model_dict["class"].get_net(
input_image_size=[
dims,
dims,
dims,
], # TODO FIX ! find a better way & remove model-specific code
)
elif self.model_dict["name"] == "SwinUNetR":
model = self.model_dict["class"].get_net(
img_size=[dims, dims, dims],
use_checkpoint=False,
)
else:
model = self.model_dict["class"].get_net()
model = model.to(self.device)
self.log_parameters()
model.to(self.device)
# load_transforms = Compose(
# [
# LoadImaged(keys=["image"]),
# # AddChanneld(keys=["image"]), #already done
# EnsureChannelFirstd(keys=["image"]),
# # Orientationd(keys=["image"], axcodes="PLI"),
# # anisotropic_transform,
# SpatialPadd(keys=["image"], spatial_size=pad),
# EnsureTyped(keys=["image"]),
# ]
# )
if not self.transforms["thresh"][0]:
post_process_transforms = EnsureType()
else:
t = self.transforms["thresh"][1]
post_process_transforms = Compose(
AsDiscrete(threshold=t), EnsureType()
)
self.log("\nLoading weights...")
if self.weights_dict["custom"]:
weights = self.weights_dict["path"]
else:
self.downloader.download_weights(
self.model_dict["name"],
self.model_dict["class"].get_weights_file(),
)
weights = os.path.join(
WEIGHTS_DIR, self.model_dict["class"].get_weights_file()
)
model.load_state_dict(
torch.load(
weights,
map_location=self.device,
)
)
self.log("Done")
is_folder = self.images_filepaths is not None
is_layer = self.layer is not None
if is_layer and is_folder:
raise ValueError(
"Both a layer and a folder have been specified, please specify only one of the two. Aborting."
)
elif is_folder:
inference_loader = self.load_folder()
##################
##################
# DEBUG
# from monai.utils import first
#
# check_data = first(inference_loader)
# image = check_data[0][0]
# print(image.shape)
##################
##################
elif is_layer:
input_image = self.load_layer()
else:
raise ValueError("No data has been provided. Aborting.")
model.eval()
with torch.no_grad():
################################
################################
################################
if is_folder:
for i, inf_data in enumerate(inference_loader):
yield self.inference_on_folder(
inf_data, i, model, post_process_transforms
)
elif is_layer:
yield self.inference_on_layer(
input_image, model, post_process_transforms
)
model.to("cpu")
except Exception as e:
self.log(f"Error : {e}")
self.quit()
finally:
self.quit()
class TrainingWorker(GeneratorWorker):
"""A custom worker to run training jobs in.
Inherits from :py:class:`napari.qt.threading.GeneratorWorker`"""
def __init__(
self,
device,
model_dict,
weights_path,
data_dicts,
validation_percent,
max_epochs,
loss_function,
learning_rate,
val_interval,
batch_size,
results_path,
sampling,
num_samples,
sample_size,
do_augmentation,
deterministic,
):
"""Initializes a worker for inference with the arguments needed by the :py:func:`~train` function. Note: See :py:func:`~train`
Args:
* device : device to train on, cuda or cpu
* model_dict : dict containing the model's "name" and "class"
* weights_path : path to weights files if transfer learning is to be used
* data_dicts : dict from :py:func:`Trainer.create_train_dataset_dict`
* validation_percent : percentage of images to use as validation
* max_epochs : the amout of epochs to train for
* loss_function : the loss function to use for training
* learning_rate : the learning rate of the optimizer
* val_interval : the interval at which to perform validation (e.g. if 2 will validate once every 2 epochs.) Also determines frequency of saving, depending on whether the metric is better or not
* batch_size : the batch size to use for training
* results_path : the path to save results in
* sampling : whether to extract patches from images or not
* num_samples : the number of samples to extract from an image for training
* sample_size : the size of the patches to extract when sampling
* do_augmentation : whether to perform data augmentation or not
* deterministic : dict with "use deterministic" : bool, whether to use deterministic training, "seed": seed for RNG
"""
super().__init__(self.train)
self._signals = LogSignal()
self.log_signal = self._signals.log_signal
self.warn_signal = self._signals.warn_signal
self._weight_error = False
#############################################
self.device = device
self.model_dict = model_dict
self.weights_path = weights_path
self.data_dicts = data_dicts
self.validation_percent = validation_percent
self.max_epochs = max_epochs
self.loss_function = loss_function
self.learning_rate = learning_rate
self.val_interval = val_interval
self.batch_size = batch_size
self.results_path = results_path
self.num_samples = num_samples
self.sampling = sampling
self.sample_size = sample_size
self.do_augment = do_augmentation
self.seed_dict = deterministic
self.train_files = []
self.val_files = []
#######################################
self.downloader = WeightsDownloader()
def set_download_log(self, widget):
self.downloader.log_widget = widget
def log(self, text):
"""Sends a signal that ``text`` should be logged
Args:
text (str): text to logged
"""
self.log_signal.emit(text)
def warn(self, warning):
"""Sends a warning to main thread"""
self.warn_signal.emit(warning)
def log_parameters(self):
self.log("-" * 20)
self.log("Parameters summary :\n")
self.log(
f"Percentage of dataset used for validation : {self.validation_percent * 100}%"
)
self.log("-" * 10)
self.log("Training files :\n")
[
self.log(f"{os.path.basename(str(train_file)[:-2])}\n")
for train_file in self.train_files
]
self.log("-" * 10)
self.log("Validation files :\n")
[
self.log(f"{os.path.basename(str(val_file)[:-2])}\n")
for val_file in self.val_files
]
self.log("-" * 10)
if self.seed_dict["use deterministic"]:
self.log(f"Deterministic training is enabled")
self.log(f"Seed is {self.seed_dict['seed']}")
self.log(f"Training for {self.max_epochs} epochs")
self.log(f"Loss function is : {str(self.loss_function)}")
self.log(f"Validation is performed every {self.val_interval} epochs")
self.log(f"Batch size is {self.batch_size}")
self.log(f"Learning rate is {self.learning_rate}")
if self.sampling:
self.log(
f"Extracting {self.num_samples} patches of size {self.sample_size}"
)
else:
self.log("Using whole images as dataset")
if self.do_augment:
self.log("Data augmentation is enabled")
if self.weights_path is not None:
self.log(f"Using weights from : {self.weights_path}")
if self._weight_error:
self.log(
">>>>>>>>>>>>>>>>>\n"
"WARNING:\nChosen weights were incompatible with the model,\n"
"the model will be trained from random weights\n"
"<<<<<<<<<<<<<<<<<\n"
)