celldetective 1.4.2__py3-none-any.whl → 1.5.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- celldetective/__init__.py +25 -0
- celldetective/__main__.py +62 -43
- celldetective/_version.py +1 -1
- celldetective/extra_properties.py +477 -399
- celldetective/filters.py +192 -97
- celldetective/gui/InitWindow.py +541 -411
- celldetective/gui/__init__.py +0 -15
- celldetective/gui/about.py +44 -39
- celldetective/gui/analyze_block.py +120 -84
- celldetective/gui/base/__init__.py +0 -0
- celldetective/gui/base/channel_norm_generator.py +335 -0
- celldetective/gui/base/components.py +249 -0
- celldetective/gui/base/feature_choice.py +92 -0
- celldetective/gui/base/figure_canvas.py +52 -0
- celldetective/gui/base/list_widget.py +133 -0
- celldetective/gui/{styles.py → base/styles.py} +92 -36
- celldetective/gui/base/utils.py +33 -0
- celldetective/gui/base_annotator.py +900 -767
- celldetective/gui/classifier_widget.py +6 -22
- celldetective/gui/configure_new_exp.py +777 -671
- celldetective/gui/control_panel.py +635 -524
- celldetective/gui/dynamic_progress.py +449 -0
- celldetective/gui/event_annotator.py +2023 -1662
- celldetective/gui/generic_signal_plot.py +1292 -944
- celldetective/gui/gui_utils.py +899 -1289
- celldetective/gui/interactions_block.py +658 -0
- celldetective/gui/interactive_timeseries_viewer.py +447 -0
- celldetective/gui/json_readers.py +48 -15
- celldetective/gui/layouts/__init__.py +5 -0
- celldetective/gui/layouts/background_model_free_layout.py +537 -0
- celldetective/gui/layouts/channel_offset_layout.py +134 -0
- celldetective/gui/layouts/local_correction_layout.py +91 -0
- celldetective/gui/layouts/model_fit_layout.py +372 -0
- celldetective/gui/layouts/operation_layout.py +68 -0
- celldetective/gui/layouts/protocol_designer_layout.py +96 -0
- celldetective/gui/pair_event_annotator.py +3130 -2435
- celldetective/gui/plot_measurements.py +586 -267
- celldetective/gui/plot_signals_ui.py +724 -506
- celldetective/gui/preprocessing_block.py +395 -0
- celldetective/gui/process_block.py +1678 -1831
- celldetective/gui/seg_model_loader.py +580 -473
- celldetective/gui/settings/__init__.py +0 -7
- celldetective/gui/settings/_cellpose_model_params.py +181 -0
- celldetective/gui/settings/_event_detection_model_params.py +95 -0
- celldetective/gui/settings/_segmentation_model_params.py +159 -0
- celldetective/gui/settings/_settings_base.py +77 -65
- celldetective/gui/settings/_settings_event_model_training.py +752 -526
- celldetective/gui/settings/_settings_measurements.py +1133 -964
- celldetective/gui/settings/_settings_neighborhood.py +574 -488
- celldetective/gui/settings/_settings_segmentation_model_training.py +779 -564
- celldetective/gui/settings/_settings_signal_annotator.py +329 -305
- celldetective/gui/settings/_settings_tracking.py +1304 -1094
- celldetective/gui/settings/_stardist_model_params.py +98 -0
- celldetective/gui/survival_ui.py +422 -312
- celldetective/gui/tableUI.py +1665 -1701
- celldetective/gui/table_ops/_maths.py +295 -0
- celldetective/gui/table_ops/_merge_groups.py +140 -0
- celldetective/gui/table_ops/_merge_one_hot.py +95 -0
- celldetective/gui/table_ops/_query_table.py +43 -0
- celldetective/gui/table_ops/_rename_col.py +44 -0
- celldetective/gui/thresholds_gui.py +382 -179
- celldetective/gui/viewers/__init__.py +0 -0
- celldetective/gui/viewers/base_viewer.py +700 -0
- celldetective/gui/viewers/channel_offset_viewer.py +331 -0
- celldetective/gui/viewers/contour_viewer.py +394 -0
- celldetective/gui/viewers/size_viewer.py +153 -0
- celldetective/gui/viewers/spot_detection_viewer.py +341 -0
- celldetective/gui/viewers/threshold_viewer.py +309 -0
- celldetective/gui/workers.py +403 -126
- celldetective/log_manager.py +92 -0
- celldetective/measure.py +1895 -1478
- celldetective/napari/__init__.py +0 -0
- celldetective/napari/utils.py +1025 -0
- celldetective/neighborhood.py +1914 -1448
- celldetective/preprocessing.py +1620 -1220
- celldetective/processes/__init__.py +0 -0
- celldetective/processes/background_correction.py +271 -0
- celldetective/processes/compute_neighborhood.py +894 -0
- celldetective/processes/detect_events.py +246 -0
- celldetective/processes/downloader.py +137 -0
- celldetective/processes/measure_cells.py +565 -0
- celldetective/processes/segment_cells.py +760 -0
- celldetective/processes/track_cells.py +435 -0
- celldetective/processes/train_segmentation_model.py +694 -0
- celldetective/processes/train_signal_model.py +265 -0
- celldetective/processes/unified_process.py +292 -0
- celldetective/regionprops/_regionprops.py +358 -317
- celldetective/relative_measurements.py +987 -710
- celldetective/scripts/measure_cells.py +313 -212
- celldetective/scripts/measure_relative.py +90 -46
- celldetective/scripts/segment_cells.py +165 -104
- celldetective/scripts/segment_cells_thresholds.py +96 -68
- celldetective/scripts/track_cells.py +198 -149
- celldetective/scripts/train_segmentation_model.py +324 -201
- celldetective/scripts/train_signal_model.py +87 -45
- celldetective/segmentation.py +844 -749
- celldetective/signals.py +3514 -2861
- celldetective/tracking.py +30 -15
- celldetective/utils/__init__.py +0 -0
- celldetective/utils/cellpose_utils/__init__.py +133 -0
- celldetective/utils/color_mappings.py +42 -0
- celldetective/utils/data_cleaning.py +630 -0
- celldetective/utils/data_loaders.py +450 -0
- celldetective/utils/dataset_helpers.py +207 -0
- celldetective/utils/downloaders.py +235 -0
- celldetective/utils/event_detection/__init__.py +8 -0
- celldetective/utils/experiment.py +1782 -0
- celldetective/utils/image_augmenters.py +308 -0
- celldetective/utils/image_cleaning.py +74 -0
- celldetective/utils/image_loaders.py +926 -0
- celldetective/utils/image_transforms.py +335 -0
- celldetective/utils/io.py +62 -0
- celldetective/utils/mask_cleaning.py +348 -0
- celldetective/utils/mask_transforms.py +5 -0
- celldetective/utils/masks.py +184 -0
- celldetective/utils/maths.py +351 -0
- celldetective/utils/model_getters.py +325 -0
- celldetective/utils/model_loaders.py +296 -0
- celldetective/utils/normalization.py +380 -0
- celldetective/utils/parsing.py +465 -0
- celldetective/utils/plots/__init__.py +0 -0
- celldetective/utils/plots/regression.py +53 -0
- celldetective/utils/resources.py +34 -0
- celldetective/utils/stardist_utils/__init__.py +104 -0
- celldetective/utils/stats.py +90 -0
- celldetective/utils/types.py +21 -0
- {celldetective-1.4.2.dist-info → celldetective-1.5.0b1.dist-info}/METADATA +1 -1
- celldetective-1.5.0b1.dist-info/RECORD +187 -0
- {celldetective-1.4.2.dist-info → celldetective-1.5.0b1.dist-info}/WHEEL +1 -1
- tests/gui/test_new_project.py +129 -117
- tests/gui/test_project.py +127 -79
- tests/test_filters.py +39 -15
- tests/test_notebooks.py +8 -0
- tests/test_tracking.py +232 -13
- tests/test_utils.py +123 -77
- celldetective/gui/base_components.py +0 -23
- celldetective/gui/layouts.py +0 -1602
- celldetective/gui/processes/compute_neighborhood.py +0 -594
- celldetective/gui/processes/downloader.py +0 -111
- celldetective/gui/processes/measure_cells.py +0 -360
- celldetective/gui/processes/segment_cells.py +0 -499
- celldetective/gui/processes/track_cells.py +0 -303
- celldetective/gui/processes/train_segmentation_model.py +0 -270
- celldetective/gui/processes/train_signal_model.py +0 -108
- celldetective/gui/table_ops/merge_groups.py +0 -118
- celldetective/gui/viewers.py +0 -1354
- celldetective/io.py +0 -3663
- celldetective/utils.py +0 -3108
- celldetective-1.4.2.dist-info/RECORD +0 -123
- {celldetective-1.4.2.dist-info → celldetective-1.5.0b1.dist-info}/entry_points.txt +0 -0
- {celldetective-1.4.2.dist-info → celldetective-1.5.0b1.dist-info}/licenses/LICENSE +0 -0
- {celldetective-1.4.2.dist-info → celldetective-1.5.0b1.dist-info}/top_level.txt +0 -0
|
@@ -11,75 +11,95 @@ from tqdm import tqdm
|
|
|
11
11
|
import numpy as np
|
|
12
12
|
import random
|
|
13
13
|
|
|
14
|
-
from celldetective.utils import
|
|
15
|
-
from celldetective.
|
|
16
|
-
from
|
|
14
|
+
from celldetective.utils.image_augmenters import augmenter
|
|
15
|
+
from celldetective.utils.image_loaders import load_image_dataset
|
|
16
|
+
from celldetective.utils.image_cleaning import interpolate_nan
|
|
17
|
+
from celldetective.utils.normalization import normalize_multichannel
|
|
18
|
+
from celldetective.utils.mask_cleaning import fill_label_holes
|
|
17
19
|
from art import tprint
|
|
18
20
|
from distutils.dir_util import copy_tree
|
|
19
|
-
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def save_json(data, fpath, **kwargs):
|
|
24
|
+
with open(fpath, "w") as f:
|
|
25
|
+
f.write(json.dumps(data, **kwargs))
|
|
26
|
+
|
|
20
27
|
|
|
21
28
|
tprint("Train")
|
|
22
29
|
|
|
23
|
-
parser = argparse.ArgumentParser(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
parser = argparse.ArgumentParser(
|
|
31
|
+
description="Train a signal model from instructions.",
|
|
32
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
33
|
+
)
|
|
34
|
+
parser.add_argument("-c", "--config", required=True, help="Training instructions")
|
|
35
|
+
parser.add_argument("-g", "--use_gpu", required=True, help="Use GPU")
|
|
27
36
|
|
|
28
37
|
args = parser.parse_args()
|
|
29
38
|
process_arguments = vars(args)
|
|
30
|
-
instructions = str(process_arguments[
|
|
31
|
-
use_gpu = bool(process_arguments[
|
|
39
|
+
instructions = str(process_arguments["config"])
|
|
40
|
+
use_gpu = bool(process_arguments["use_gpu"])
|
|
32
41
|
|
|
33
42
|
if os.path.exists(instructions):
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
with open(instructions, "r") as f:
|
|
44
|
+
training_instructions = json.load(f)
|
|
36
45
|
else:
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
print("Training instructions could not be found. Abort.")
|
|
47
|
+
os.abort()
|
|
39
48
|
|
|
40
|
-
model_name = training_instructions[
|
|
41
|
-
target_directory = training_instructions[
|
|
42
|
-
model_type = training_instructions[
|
|
43
|
-
pretrained = training_instructions[
|
|
49
|
+
model_name = training_instructions["model_name"]
|
|
50
|
+
target_directory = training_instructions["target_directory"]
|
|
51
|
+
model_type = training_instructions["model_type"]
|
|
52
|
+
pretrained = training_instructions["pretrained"]
|
|
44
53
|
|
|
45
|
-
datasets = training_instructions[
|
|
54
|
+
datasets = training_instructions["ds"]
|
|
46
55
|
|
|
47
|
-
target_channels = training_instructions[
|
|
48
|
-
normalization_percentile = training_instructions[
|
|
49
|
-
normalization_clip = training_instructions[
|
|
50
|
-
normalization_values = training_instructions[
|
|
51
|
-
spatial_calibration = training_instructions[
|
|
56
|
+
target_channels = training_instructions["channel_option"]
|
|
57
|
+
normalization_percentile = training_instructions["normalization_percentile"]
|
|
58
|
+
normalization_clip = training_instructions["normalization_clip"]
|
|
59
|
+
normalization_values = training_instructions["normalization_values"]
|
|
60
|
+
spatial_calibration = training_instructions["spatial_calibration"]
|
|
52
61
|
|
|
53
|
-
validation_split = training_instructions[
|
|
54
|
-
augmentation_factor = training_instructions[
|
|
62
|
+
validation_split = training_instructions["validation_split"]
|
|
63
|
+
augmentation_factor = training_instructions["augmentation_factor"]
|
|
55
64
|
|
|
56
|
-
learning_rate = training_instructions[
|
|
57
|
-
epochs = training_instructions[
|
|
58
|
-
batch_size = training_instructions[
|
|
65
|
+
learning_rate = training_instructions["learning_rate"]
|
|
66
|
+
epochs = training_instructions["epochs"]
|
|
67
|
+
batch_size = training_instructions["batch_size"]
|
|
59
68
|
|
|
60
69
|
|
|
61
70
|
# Load dataset
|
|
62
|
-
print(f
|
|
63
|
-
X,Y,filenames = load_image_dataset(
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
print(f"Datasets: {datasets}")
|
|
72
|
+
X, Y, filenames = load_image_dataset(
|
|
73
|
+
datasets,
|
|
74
|
+
target_channels,
|
|
75
|
+
train_spatial_calibration=spatial_calibration,
|
|
76
|
+
mask_suffix="labelled",
|
|
77
|
+
)
|
|
78
|
+
print("Dataset loaded...")
|
|
66
79
|
|
|
67
80
|
values = []
|
|
68
81
|
percentiles = []
|
|
69
82
|
for k in range(len(normalization_percentile)):
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
X = [
|
|
83
|
+
if normalization_percentile[k]:
|
|
84
|
+
percentiles.append(normalization_values[k])
|
|
85
|
+
values.append(None)
|
|
86
|
+
else:
|
|
87
|
+
percentiles.append(None)
|
|
88
|
+
values.append(normalization_values[k])
|
|
89
|
+
|
|
90
|
+
X = [
|
|
91
|
+
normalize_multichannel(
|
|
92
|
+
x, **{"percentiles": percentiles, "values": values, "clip": normalization_clip}
|
|
93
|
+
)
|
|
94
|
+
for x in X
|
|
95
|
+
]
|
|
78
96
|
|
|
79
97
|
for k in range(len(X)):
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
98
|
+
x = X[k].copy()
|
|
99
|
+
x_interp = np.moveaxis(
|
|
100
|
+
[interpolate_nan(x[:, :, c].copy()) for c in range(x.shape[-1])], 0, -1
|
|
101
|
+
)
|
|
102
|
+
X[k] = x_interp
|
|
83
103
|
|
|
84
104
|
Y = [fill_label_holes(y) for y in tqdm(Y)]
|
|
85
105
|
|
|
@@ -88,165 +108,268 @@ rng = np.random.RandomState()
|
|
|
88
108
|
ind = rng.permutation(len(X))
|
|
89
109
|
n_val = max(1, int(round(validation_split * len(ind))))
|
|
90
110
|
ind_train, ind_val = ind[:-n_val], ind[-n_val:]
|
|
91
|
-
X_val, Y_val = [X[i] for i in ind_val]
|
|
111
|
+
X_val, Y_val = [X[i] for i in ind_val], [Y[i] for i in ind_val]
|
|
92
112
|
X_trn, Y_trn = [X[i] for i in ind_train], [Y[i] for i in ind_train]
|
|
93
113
|
|
|
94
114
|
files_train = [filenames[i] for i in ind_train]
|
|
95
115
|
files_val = [filenames[i] for i in ind_val]
|
|
96
116
|
|
|
97
|
-
print(
|
|
98
|
-
print(
|
|
99
|
-
print(
|
|
100
|
-
|
|
101
|
-
if model_type==
|
|
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
|
-
|
|
117
|
+
print("number of images: %3d" % len(X))
|
|
118
|
+
print("- training: %3d" % len(X_trn))
|
|
119
|
+
print("- validation: %3d" % len(X_val))
|
|
120
|
+
|
|
121
|
+
if model_type == "cellpose":
|
|
122
|
+
|
|
123
|
+
# do augmentation in place
|
|
124
|
+
X_aug = []
|
|
125
|
+
Y_aug = []
|
|
126
|
+
n_val = max(1, int(round(augmentation_factor * len(X_trn))))
|
|
127
|
+
indices = random.choices(list(np.arange(len(X_trn))), k=n_val)
|
|
128
|
+
print("Performing image augmentation pre-training...")
|
|
129
|
+
for i in tqdm(indices):
|
|
130
|
+
x_aug, y_aug = augmenter(X_trn[i], Y_trn[i])
|
|
131
|
+
X_aug.append(x_aug)
|
|
132
|
+
Y_aug.append(y_aug)
|
|
133
|
+
|
|
134
|
+
# Channel axis in front for cellpose_utils
|
|
135
|
+
X_aug = [np.moveaxis(x, -1, 0) for x in X_aug]
|
|
136
|
+
X_val = [np.moveaxis(x, -1, 0) for x in X_val]
|
|
137
|
+
print("number of augmented images: %3d" % len(X_aug))
|
|
138
|
+
|
|
139
|
+
from cellpose.models import CellposeModel
|
|
140
|
+
from cellpose.io import logger_setup
|
|
141
|
+
import torch
|
|
142
|
+
|
|
143
|
+
if not use_gpu:
|
|
144
|
+
print("Using CPU for training...")
|
|
145
|
+
device = torch.device("cpu")
|
|
146
|
+
else:
|
|
147
|
+
print("Using GPU for training...")
|
|
148
|
+
|
|
149
|
+
diam_mean = 30.0
|
|
150
|
+
logger, log_file = logger_setup()
|
|
151
|
+
print(f"Pretrained model: ", pretrained)
|
|
152
|
+
if pretrained is not None:
|
|
153
|
+
if pretrained.endswith("CP_nuclei"):
|
|
154
|
+
diam_mean = 17.0
|
|
155
|
+
pretrained_path = os.sep.join([pretrained, os.path.split(pretrained)[-1]])
|
|
156
|
+
else:
|
|
157
|
+
pretrained_path = pretrained
|
|
158
|
+
|
|
159
|
+
model = CellposeModel(
|
|
160
|
+
gpu=use_gpu,
|
|
161
|
+
model_type=None,
|
|
162
|
+
pretrained_model=pretrained_path,
|
|
163
|
+
diam_mean=diam_mean,
|
|
164
|
+
nchan=X_aug[0].shape[0],
|
|
165
|
+
)
|
|
166
|
+
for name, module in model.net.named_children():
|
|
167
|
+
print(name, type(module))
|
|
168
|
+
|
|
169
|
+
# Freeze parts of the UNET (if we loaded a pretrained model)
|
|
170
|
+
if pretrained is not None:
|
|
171
|
+
for param in model.net.downsample.parameters():
|
|
172
|
+
param.requires_grad = False
|
|
173
|
+
|
|
174
|
+
# Optional: freeze style branch (recommended unless you are training on very different imaging domains)
|
|
175
|
+
for param in model.net.make_style.parameters():
|
|
176
|
+
param.requires_grad = False
|
|
177
|
+
|
|
178
|
+
# Keep decoder (upsampling path) trainable
|
|
179
|
+
for param in model.net.upsample.parameters():
|
|
180
|
+
param.requires_grad = True
|
|
181
|
+
|
|
182
|
+
# Keep output head trainable
|
|
183
|
+
for param in model.net.output.parameters():
|
|
184
|
+
param.requires_grad = True
|
|
185
|
+
|
|
186
|
+
# Unfreeze all output heads (version-safe)
|
|
187
|
+
output_heads = ["output", "output_conv", "flow", "prob"]
|
|
188
|
+
for head_name in output_heads:
|
|
189
|
+
if hasattr(model.net, head_name):
|
|
190
|
+
for param in getattr(model.net, head_name).parameters():
|
|
191
|
+
param.requires_grad = True
|
|
192
|
+
|
|
193
|
+
# Now train normally (Cellpose will internally skip frozen params)
|
|
194
|
+
model.train(
|
|
195
|
+
train_data=X_aug,
|
|
196
|
+
train_labels=Y_aug,
|
|
197
|
+
normalize=False,
|
|
198
|
+
channels=None,
|
|
199
|
+
batch_size=batch_size,
|
|
200
|
+
min_train_masks=1,
|
|
201
|
+
save_path=target_directory + os.sep + model_name,
|
|
202
|
+
n_epochs=epochs,
|
|
203
|
+
model_name=model_name,
|
|
204
|
+
learning_rate=learning_rate,
|
|
205
|
+
test_data=X_val,
|
|
206
|
+
test_labels=Y_val,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
file_to_move = glob(os.sep.join([target_directory, model_name, "models", "*"]))[0]
|
|
210
|
+
shutil.move(
|
|
211
|
+
file_to_move,
|
|
212
|
+
os.sep.join([target_directory, model_name, ""])
|
|
213
|
+
+ os.path.split(file_to_move)[-1],
|
|
214
|
+
)
|
|
215
|
+
os.rmdir(os.sep.join([target_directory, model_name, "models"]))
|
|
216
|
+
|
|
217
|
+
diameter = model.diam_labels
|
|
218
|
+
|
|
219
|
+
if pretrained is not None and os.path.split(pretrained)[-1] == "CP_nuclei":
|
|
220
|
+
standard_diameter = 17.0
|
|
221
|
+
else:
|
|
222
|
+
standard_diameter = 30.0
|
|
223
|
+
|
|
224
|
+
input_spatial_calibration = spatial_calibration # *diameter / standard_diameter
|
|
225
|
+
|
|
226
|
+
config_inputs = {
|
|
227
|
+
"channels": target_channels,
|
|
228
|
+
"diameter": standard_diameter,
|
|
229
|
+
"cellprob_threshold": 0.0,
|
|
230
|
+
"flow_threshold": 0.4,
|
|
231
|
+
"normalization_percentile": normalization_percentile,
|
|
232
|
+
"normalization_clip": normalization_clip,
|
|
233
|
+
"normalization_values": normalization_values,
|
|
234
|
+
"model_type": "cellpose",
|
|
235
|
+
"spatial_calibration": input_spatial_calibration,
|
|
236
|
+
"cell_size_um": round(diameter * input_spatial_calibration, 4),
|
|
237
|
+
"dataset": {"train": files_train, "validation": files_val},
|
|
238
|
+
}
|
|
239
|
+
json_input_config = json.dumps(config_inputs, indent=4)
|
|
240
|
+
with open(
|
|
241
|
+
os.sep.join([target_directory, model_name, "config_input.json"]), "w"
|
|
242
|
+
) as outfile:
|
|
243
|
+
outfile.write(json_input_config)
|
|
244
|
+
|
|
245
|
+
elif model_type == "stardist":
|
|
246
|
+
|
|
247
|
+
from stardist import calculate_extents, gputools_available
|
|
248
|
+
from stardist.models import Config2D, StarDist2D
|
|
249
|
+
|
|
250
|
+
n_rays = 32
|
|
251
|
+
print(gputools_available())
|
|
252
|
+
|
|
253
|
+
n_channel = X_trn[0].shape[-1]
|
|
254
|
+
|
|
255
|
+
# Predict on subsampled grid for increased efficiency and larger field of view
|
|
256
|
+
grid = (2, 2)
|
|
257
|
+
conf = Config2D(
|
|
258
|
+
n_rays=n_rays,
|
|
259
|
+
grid=grid,
|
|
260
|
+
use_gpu=use_gpu,
|
|
261
|
+
n_channel_in=n_channel,
|
|
262
|
+
train_learning_rate=learning_rate,
|
|
263
|
+
train_patch_size=(256, 256),
|
|
264
|
+
train_epochs=epochs,
|
|
265
|
+
train_reduce_lr={"factor": 0.1, "patience": 30, "min_delta": 0},
|
|
266
|
+
train_batch_size=batch_size,
|
|
267
|
+
train_steps_per_epoch=int(augmentation_factor * len(X_trn)),
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
if use_gpu:
|
|
271
|
+
from csbdeep.utils.tf import limit_gpu_memory
|
|
272
|
+
|
|
273
|
+
limit_gpu_memory(None, allow_growth=True)
|
|
274
|
+
|
|
275
|
+
if pretrained is None:
|
|
276
|
+
model = StarDist2D(conf, name=model_name, basedir=target_directory)
|
|
277
|
+
else:
|
|
278
|
+
|
|
279
|
+
os.rename(
|
|
280
|
+
instructions, os.sep.join([target_directory, model_name, "temp.json"])
|
|
281
|
+
)
|
|
282
|
+
copy_tree(pretrained, os.sep.join([target_directory, model_name]))
|
|
283
|
+
|
|
284
|
+
if os.path.exists(
|
|
285
|
+
os.sep.join([target_directory, model_name, "training_instructions.json"])
|
|
286
|
+
):
|
|
287
|
+
os.remove(
|
|
288
|
+
os.sep.join(
|
|
289
|
+
[target_directory, model_name, "training_instructions.json"]
|
|
290
|
+
)
|
|
291
|
+
)
|
|
292
|
+
if os.path.exists(
|
|
293
|
+
os.sep.join([target_directory, model_name, "config_input.json"])
|
|
294
|
+
):
|
|
295
|
+
os.remove(os.sep.join([target_directory, model_name, "config_input.json"]))
|
|
296
|
+
if os.path.exists(os.sep.join([target_directory, model_name, "logs" + os.sep])):
|
|
297
|
+
shutil.rmtree(os.sep.join([target_directory, model_name, "logs"]))
|
|
298
|
+
os.rename(
|
|
299
|
+
os.sep.join([target_directory, model_name, "temp.json"]),
|
|
300
|
+
os.sep.join([target_directory, model_name, "training_instructions.json"]),
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
# shutil.copytree(pretrained, os.sep.join([target_directory, model_name]))
|
|
304
|
+
model = StarDist2D(None, name=model_name, basedir=target_directory)
|
|
305
|
+
model.config.train_epochs = epochs
|
|
306
|
+
model.config.train_batch_size = min(len(X_trn), batch_size)
|
|
307
|
+
model.config.train_learning_rate = (
|
|
308
|
+
learning_rate # perf seems bad if lr is changed in transfer
|
|
309
|
+
)
|
|
310
|
+
model.config.use_gpu = use_gpu
|
|
311
|
+
model.config.train_reduce_lr = {"factor": 0.1, "patience": 10, "min_delta": 0}
|
|
312
|
+
print(f"{model.config=}")
|
|
313
|
+
|
|
314
|
+
save_json(
|
|
315
|
+
vars(model.config),
|
|
316
|
+
os.sep.join([target_directory, model_name, "config.json"]),
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
median_size = calculate_extents(list(Y_trn), np.mean)
|
|
320
|
+
fov = np.array(model._axes_tile_overlap("YX"))
|
|
321
|
+
print(f"median object size: {median_size}")
|
|
322
|
+
print(f"network field of view : {fov}")
|
|
323
|
+
if any(median_size > fov):
|
|
324
|
+
print(
|
|
325
|
+
"WARNING: median object size larger than field of view of the neural network."
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
if pretrained is not None:
|
|
329
|
+
|
|
330
|
+
mod = model.keras_model
|
|
331
|
+
encoder_depth = len(mod.layers) // 2
|
|
332
|
+
|
|
333
|
+
for layer in mod.layers[:encoder_depth]:
|
|
334
|
+
layer.trainable = False
|
|
335
|
+
|
|
336
|
+
# Keep decoder trainable
|
|
337
|
+
for layer in mod.layers[encoder_depth:]:
|
|
338
|
+
layer.trainable = True
|
|
339
|
+
|
|
340
|
+
if augmentation_factor == 1.0:
|
|
341
|
+
model.train(X_trn, Y_trn, validation_data=(X_val, Y_val))
|
|
342
|
+
else:
|
|
343
|
+
model.train(X_trn, Y_trn, validation_data=(X_val, Y_val), augmenter=augmenter)
|
|
344
|
+
model.optimize_thresholds(X_val, Y_val)
|
|
345
|
+
|
|
346
|
+
if isinstance(median_size, list):
|
|
347
|
+
median_size = np.mean(median_size)
|
|
348
|
+
|
|
349
|
+
config_inputs = {
|
|
350
|
+
"channels": target_channels,
|
|
351
|
+
"normalization_percentile": normalization_percentile,
|
|
352
|
+
"normalization_clip": normalization_clip,
|
|
353
|
+
"normalization_values": normalization_values,
|
|
354
|
+
"model_type": "stardist",
|
|
355
|
+
"spatial_calibration": spatial_calibration,
|
|
356
|
+
"cell_size_um": median_size * spatial_calibration,
|
|
357
|
+
"dataset": {"train": files_train, "validation": files_val},
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
def make_json_safe(obj):
|
|
361
|
+
if isinstance(obj, np.ndarray):
|
|
362
|
+
return obj.tolist() # convert to list
|
|
363
|
+
if isinstance(obj, (np.int64, np.int32)):
|
|
364
|
+
return int(obj)
|
|
365
|
+
if isinstance(obj, (np.float32, np.float64)):
|
|
366
|
+
return float(obj)
|
|
367
|
+
return str(obj) # fallback
|
|
368
|
+
|
|
369
|
+
json_input_config = json.dumps(config_inputs, indent=4, default=make_json_safe)
|
|
370
|
+
with open(
|
|
371
|
+
os.sep.join([target_directory, model_name, "config_input.json"]), "w"
|
|
372
|
+
) as outfile:
|
|
373
|
+
outfile.write(json_input_config)
|
|
374
|
+
|
|
375
|
+
print("Done.")
|