deepblink 0.1.5__py2.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.
- deepblink/__init__.py +33 -0
- deepblink/__main__.py +6 -0
- deepblink/_wandb.py +179 -0
- deepblink/augment.py +165 -0
- deepblink/cli/__init__.py +7 -0
- deepblink/cli/_argparse.py +482 -0
- deepblink/cli/_check.py +50 -0
- deepblink/cli/_config.py +105 -0
- deepblink/cli/_create.py +299 -0
- deepblink/cli/_download.py +92 -0
- deepblink/cli/_logger.py +26 -0
- deepblink/cli/_main.py +117 -0
- deepblink/cli/_parseutil.py +184 -0
- deepblink/cli/_predict.py +200 -0
- deepblink/cli/_train.py +85 -0
- deepblink/cli/_util.py +29 -0
- deepblink/cli/_visualize.py +114 -0
- deepblink/cli/models.txt +1 -0
- deepblink/data.py +142 -0
- deepblink/datasets/__init__.py +7 -0
- deepblink/datasets/_datasets.py +34 -0
- deepblink/datasets/sequence.py +77 -0
- deepblink/datasets/spots.py +83 -0
- deepblink/inference.py +132 -0
- deepblink/io.py +147 -0
- deepblink/losses.py +151 -0
- deepblink/metrics.py +310 -0
- deepblink/models/__init__.py +6 -0
- deepblink/models/_models.py +125 -0
- deepblink/models/spots.py +41 -0
- deepblink/networks/__init__.py +11 -0
- deepblink/networks/_networks.py +138 -0
- deepblink/networks/unet.py +93 -0
- deepblink/optimizers.py +22 -0
- deepblink/training.py +122 -0
- deepblink/util.py +164 -0
- deepblink-0.1.5.dist-info/METADATA +60 -0
- deepblink-0.1.5.dist-info/RECORD +42 -0
- deepblink-0.1.5.dist-info/WHEEL +6 -0
- deepblink-0.1.5.dist-info/entry_points.txt +2 -0
- deepblink-0.1.5.dist-info/licenses/LICENSE +9 -0
- deepblink-0.1.5.dist-info/top_level.txt +1 -0
deepblink/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""deepBlink for spot detection and localization.
|
|
2
|
+
|
|
3
|
+
Modules are arranged as follows:
|
|
4
|
+
- augment: Data augmentation to artificially increase dataset size.
|
|
5
|
+
- cli: Command line interface for inferencing.
|
|
6
|
+
- data: Data manipulation. Mainly to properly format for training.
|
|
7
|
+
- datasets: Unique data import functions.
|
|
8
|
+
- inference: Prediction related functions.
|
|
9
|
+
- io: File-manipulation-related functions.
|
|
10
|
+
- losses: Simple functions returning model losses.
|
|
11
|
+
- metrics: Quantitative output of training / model performance.
|
|
12
|
+
- models: Training loop containing classes for each type of model.
|
|
13
|
+
- networks: Architecture / building of model structure.
|
|
14
|
+
- optimizers: Simple functions returning model optimizers.
|
|
15
|
+
- training: Core training loop and callbacks.
|
|
16
|
+
- util: Basic utility functions not fitting into a category.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
__version__ = "0.1.5"
|
|
20
|
+
|
|
21
|
+
from . import augment
|
|
22
|
+
from . import cli
|
|
23
|
+
from . import data
|
|
24
|
+
from . import datasets
|
|
25
|
+
from . import inference
|
|
26
|
+
from . import io
|
|
27
|
+
from . import losses
|
|
28
|
+
from . import metrics
|
|
29
|
+
from . import models
|
|
30
|
+
from . import networks
|
|
31
|
+
from . import optimizers
|
|
32
|
+
from . import training
|
|
33
|
+
from . import util
|
deepblink/__main__.py
ADDED
deepblink/_wandb.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Logging callbacks for wandb."""
|
|
2
|
+
# pylint: disable=no-member,missing-function-docstring
|
|
3
|
+
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pandas as pd
|
|
7
|
+
import tensorflow as tf
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
import wandb
|
|
11
|
+
|
|
12
|
+
if wandb.__version__ <= "0.10.03":
|
|
13
|
+
raise AssertionError
|
|
14
|
+
except (ModuleNotFoundError, AttributeError, AssertionError):
|
|
15
|
+
raise ImportError(
|
|
16
|
+
(
|
|
17
|
+
"To support conda packages we don't ship deepBlink with wandb. "
|
|
18
|
+
"Please install any using pip: 'pip install \"wandb>=0.10.3\"'"
|
|
19
|
+
)
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
from .data import get_coordinate_list
|
|
24
|
+
from .datasets import Dataset
|
|
25
|
+
from .metrics import compute_metrics
|
|
26
|
+
from .models import Model
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def wandb_callback():
|
|
30
|
+
"""Callback function to avoid double imports."""
|
|
31
|
+
return wandb.keras.WandbCallback()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class WandbImageLogger(tf.keras.callbacks.Callback):
|
|
35
|
+
"""Custom image prediction logger callback in wandb.
|
|
36
|
+
|
|
37
|
+
Expects segmentation images and the model class to have a predict_on_image method.
|
|
38
|
+
|
|
39
|
+
Attributes:
|
|
40
|
+
model_wrapper: Model used for predictions.
|
|
41
|
+
dataset: Dataset class containing data.
|
|
42
|
+
n_examples: Number of examples saved for display.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self, model_wrapper: Model, dataset: Dataset, n_examples: int = 4,
|
|
47
|
+
):
|
|
48
|
+
super().__init__()
|
|
49
|
+
self.model_wrapper = model_wrapper
|
|
50
|
+
self.valid_images = dataset.x_valid[:n_examples] # type: ignore[index]
|
|
51
|
+
self.train_images = dataset.x_train[:n_examples] # type: ignore[index]
|
|
52
|
+
self.train_masks = dataset.y_train[:n_examples] # type: ignore[index]
|
|
53
|
+
self.valid_masks = dataset.y_valid[:n_examples] # type: ignore[index]
|
|
54
|
+
self.image_size = dataset.x_train[0].shape[0] # type: ignore[index]
|
|
55
|
+
|
|
56
|
+
def plot_scatter(
|
|
57
|
+
self, title: str, images: np.ndarray, masks: np.ndarray = None
|
|
58
|
+
) -> None:
|
|
59
|
+
"""Plot one set of images to wandb."""
|
|
60
|
+
plots = []
|
|
61
|
+
for i, image in enumerate(images):
|
|
62
|
+
if masks is not None:
|
|
63
|
+
mask = masks[i]
|
|
64
|
+
else:
|
|
65
|
+
mask = self.model_wrapper.predict_on_image(image) # type: ignore[attr-defined]
|
|
66
|
+
coords = get_coordinate_list(mask, image_size=self.image_size)
|
|
67
|
+
|
|
68
|
+
plt.figure()
|
|
69
|
+
plt.imshow(image)
|
|
70
|
+
plt.scatter(coords[..., 1], coords[..., 0], marker="+", color="r", s=10)
|
|
71
|
+
plots.append(wandb.Image(plt, caption=f"{title}: {i}"))
|
|
72
|
+
wandb.log({title: plots}, commit=False)
|
|
73
|
+
plt.close(fig="all")
|
|
74
|
+
|
|
75
|
+
# pylint: disable=W0613,W0221
|
|
76
|
+
def on_train_begin(self, epochs, logs=None): # noqa: D102
|
|
77
|
+
self.plot_scatter("Train ground truth", self.train_images, self.train_masks)
|
|
78
|
+
self.plot_scatter("Valid ground truth", self.valid_images, self.valid_masks)
|
|
79
|
+
|
|
80
|
+
def on_epoch_end(self, epoch, logs=None): # noqa: ignore=D102
|
|
81
|
+
self.plot_scatter("Train data predictions", self.train_images)
|
|
82
|
+
self.plot_scatter("Valid data predictions", self.valid_images)
|
|
83
|
+
|
|
84
|
+
# pylint: enable=W0613,W0221
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class WandbComputeMetrics(tf.keras.callbacks.Callback):
|
|
88
|
+
"""Compute the final metrics once training is complete."""
|
|
89
|
+
|
|
90
|
+
def __init__(self, model: tf.keras.models.Model, dataset: Dataset, mdist: int):
|
|
91
|
+
super().__init__()
|
|
92
|
+
self.model = model
|
|
93
|
+
self.train_images = dataset.x_train
|
|
94
|
+
self.train_labels = dataset.y_train
|
|
95
|
+
self.valid_images = dataset.x_valid
|
|
96
|
+
self.valid_labels = dataset.y_valid
|
|
97
|
+
self.mdist = mdist
|
|
98
|
+
|
|
99
|
+
def log_scores(
|
|
100
|
+
self, name: str, images: np.ndarray, labels: np.ndarray
|
|
101
|
+
) -> pd.DataFrame:
|
|
102
|
+
"""Prediction and logging function for one set of images and labels."""
|
|
103
|
+
df = pd.DataFrame()
|
|
104
|
+
mdist = self.mdist
|
|
105
|
+
|
|
106
|
+
for idx, (image, true) in enumerate(zip(images, labels)):
|
|
107
|
+
pred = self.model.predict(image[None, ..., None]).squeeze()
|
|
108
|
+
curr_df = compute_metrics(
|
|
109
|
+
pred=get_coordinate_list(pred, image_size=image.shape[0]),
|
|
110
|
+
true=get_coordinate_list(true, image_size=image.shape[0]),
|
|
111
|
+
mdist=mdist,
|
|
112
|
+
)
|
|
113
|
+
curr_df["image"] = idx # for downstream groupby's
|
|
114
|
+
df = df.append(curr_df)
|
|
115
|
+
|
|
116
|
+
# Log single summary values to wandb
|
|
117
|
+
values = {
|
|
118
|
+
f"{name} f1@{mdist} mean": df[df["cutoff"] == mdist]["f1_score"].mean(),
|
|
119
|
+
f"{name} f1@{mdist} std": df[df["cutoff"] == mdist]["f1_score"].std(),
|
|
120
|
+
f"{name} integral mean": df["f1_integral"].mean(),
|
|
121
|
+
f"{name} integral std": df["f1_integral"].std(),
|
|
122
|
+
f"{name} euclidean mean": df["mean_euclidean"].mean(),
|
|
123
|
+
f"{name} euclidean std": df["mean_euclidean"].std(),
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
for k, v in values.items():
|
|
127
|
+
wandb.run.summary[k] = v
|
|
128
|
+
|
|
129
|
+
# Barplot with all metrics
|
|
130
|
+
try:
|
|
131
|
+
wandb.log(
|
|
132
|
+
{
|
|
133
|
+
f"{name} metrics": wandb.plot.bar(
|
|
134
|
+
wandb.Table(
|
|
135
|
+
data=list(values.items()), columns=["label", "value"]
|
|
136
|
+
),
|
|
137
|
+
"label",
|
|
138
|
+
"value",
|
|
139
|
+
title=f"{name} metrics",
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
except TypeError:
|
|
144
|
+
print(list(values.items()))
|
|
145
|
+
|
|
146
|
+
return df
|
|
147
|
+
|
|
148
|
+
def log_plots(self) -> None:
|
|
149
|
+
"""Create matplotlib plots and log to wandb."""
|
|
150
|
+
# F1 score vs. cutoff
|
|
151
|
+
cutoffs = self.df_train["cutoff"].unique()
|
|
152
|
+
plt.errorbar(
|
|
153
|
+
x=cutoffs,
|
|
154
|
+
y=self.df_train.groupby("cutoff")["f1_score"].mean().values,
|
|
155
|
+
yerr=self.df_train.groupby("cutoff")["f1_score"].std().values / 2,
|
|
156
|
+
label="Train",
|
|
157
|
+
)
|
|
158
|
+
plt.errorbar(
|
|
159
|
+
x=cutoffs,
|
|
160
|
+
y=self.df_valid.groupby("cutoff")["f1_score"].mean().values,
|
|
161
|
+
yerr=self.df_valid.groupby("cutoff")["f1_score"].std().values / 2,
|
|
162
|
+
label="Valid",
|
|
163
|
+
)
|
|
164
|
+
plt.legend(loc="lower right")
|
|
165
|
+
wandb.log({"F1 score vs. cutoff": plt})
|
|
166
|
+
|
|
167
|
+
# F1 Integral distribution
|
|
168
|
+
plt.hist(x=self.df_train["f1_integral"], label="Train")
|
|
169
|
+
plt.hist(x=self.df_valid["f1_integral"], label="Valid")
|
|
170
|
+
plt.legend(loc="upper left")
|
|
171
|
+
wandb.log({"F1 integral histogram": plt})
|
|
172
|
+
|
|
173
|
+
# pylint: disable=W0613
|
|
174
|
+
def on_train_end(self, logs=None): # noqa: D102
|
|
175
|
+
self.df_train = self.log_scores("Train", self.train_images, self.train_labels)
|
|
176
|
+
self.df_valid = self.log_scores("Valid", self.valid_images, self.valid_labels)
|
|
177
|
+
self.log_plots()
|
|
178
|
+
|
|
179
|
+
# pylint: enable=W0613
|
deepblink/augment.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""Model utility functions for augmentation."""
|
|
2
|
+
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
import warnings
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def augment_batch_baseline(
|
|
10
|
+
images: np.ndarray,
|
|
11
|
+
masks: np.ndarray,
|
|
12
|
+
flip_: bool = False,
|
|
13
|
+
illuminate_: bool = False,
|
|
14
|
+
gaussian_noise_: bool = False,
|
|
15
|
+
rotate_: bool = False,
|
|
16
|
+
translate_: bool = False,
|
|
17
|
+
cell_size: int = 4,
|
|
18
|
+
) -> Tuple[np.ndarray, np.ndarray]:
|
|
19
|
+
"""Baseline augmentation function.
|
|
20
|
+
|
|
21
|
+
Probability of augmentations is determined in the corresponding functions
|
|
22
|
+
and not in this baseline.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
images: Batch of input image to be augmented with shape (n, x, y).
|
|
26
|
+
masks: Batch of corresponding prediction matrix with ground truth values with shape (n, x, y).
|
|
27
|
+
flip_: If True, images might be flipped.
|
|
28
|
+
illuminate_: If True, images might be altered in illumination.
|
|
29
|
+
gaussian_noise_: If True, gaussian noise might be added.
|
|
30
|
+
rotate_: If True, images might be rotated.
|
|
31
|
+
translate_: If True, images might be translated.
|
|
32
|
+
cell_size: Size of one cell in the prediction matrix.
|
|
33
|
+
"""
|
|
34
|
+
if images.shape.index(min(images.shape)) != 0:
|
|
35
|
+
warnings.warn(
|
|
36
|
+
f"Images have shape {images.shape} and might not be in the format (n, x, y)!",
|
|
37
|
+
UserWarning,
|
|
38
|
+
)
|
|
39
|
+
if masks.shape.index(min(masks.shape)) != 0:
|
|
40
|
+
warnings.warn(
|
|
41
|
+
f"Masks have shape {masks.shape} and might not be in the format (n, x, y)!",
|
|
42
|
+
UserWarning,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
aug_images = []
|
|
46
|
+
aug_masks = []
|
|
47
|
+
|
|
48
|
+
for image, mask in zip(images, masks):
|
|
49
|
+
aug_image = image.copy()
|
|
50
|
+
aug_mask = mask.copy()
|
|
51
|
+
|
|
52
|
+
if flip_:
|
|
53
|
+
aug_image, aug_mask = flip(aug_image, aug_mask)
|
|
54
|
+
if illuminate_:
|
|
55
|
+
aug_image, aug_mask = illuminate(aug_image, aug_mask)
|
|
56
|
+
if gaussian_noise_:
|
|
57
|
+
aug_image, aug_mask = gaussian_noise(aug_image, aug_mask)
|
|
58
|
+
if rotate_:
|
|
59
|
+
aug_image, aug_mask = rotate(aug_image, aug_mask)
|
|
60
|
+
if translate_:
|
|
61
|
+
aug_image, aug_mask = translate(aug_image, aug_mask, cell_size=cell_size)
|
|
62
|
+
|
|
63
|
+
aug_images.append(aug_image)
|
|
64
|
+
aug_masks.append(aug_mask)
|
|
65
|
+
|
|
66
|
+
aug_images = np.array(aug_images, dtype=np.float32)
|
|
67
|
+
aug_masks = np.array(aug_masks, dtype=np.float32)
|
|
68
|
+
|
|
69
|
+
return aug_images, aug_masks
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def flip(image: np.ndarray, mask: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
|
73
|
+
"""Augment through horizontal/vertical flipping."""
|
|
74
|
+
rand_flip = np.random.randint(low=0, high=2)
|
|
75
|
+
|
|
76
|
+
image = np.flip(image.copy(), rand_flip)
|
|
77
|
+
mask = np.flip(mask.copy(), rand_flip)
|
|
78
|
+
|
|
79
|
+
# Horizontal flip / change along x axis
|
|
80
|
+
if rand_flip == 0:
|
|
81
|
+
mask[..., 1] = np.where(mask[..., 0], 1 - mask[..., 1], mask[..., 1])
|
|
82
|
+
|
|
83
|
+
# Vertical flip / change along y axis
|
|
84
|
+
if rand_flip == 1:
|
|
85
|
+
mask[..., 2] = np.where(mask[..., 0], 1 - mask[..., 2], mask[..., 2])
|
|
86
|
+
|
|
87
|
+
return image, mask
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def illuminate(image: np.ndarray, mask: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
|
91
|
+
"""Augment through changing illumination."""
|
|
92
|
+
rand_illumination = 1 + np.random.uniform(-0.75, 0.75)
|
|
93
|
+
image = image.copy()
|
|
94
|
+
image = np.multiply(image, rand_illumination)
|
|
95
|
+
return image, mask
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def gaussian_noise(
|
|
99
|
+
image: np.ndarray, mask: np.ndarray, mean: int = 0
|
|
100
|
+
) -> Tuple[np.ndarray, np.ndarray]:
|
|
101
|
+
"""Augment through the addition of gaussian noise.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
image: Image to be augmented.
|
|
105
|
+
mask: Corresponding prediction matrix with ground truth values.
|
|
106
|
+
mean: Average noise pixel values added. Zero means no net difference occurs.
|
|
107
|
+
"""
|
|
108
|
+
sigma = np.random.uniform(0.0001, 0.01)
|
|
109
|
+
noise = np.random.normal(mean, sigma, image.shape)
|
|
110
|
+
image = image.copy()
|
|
111
|
+
|
|
112
|
+
def _gaussian_noise(image: np.ndarray) -> np.ndarray:
|
|
113
|
+
"""Gaussian noise helper."""
|
|
114
|
+
mask_overflow_upper = image + noise >= 1.0
|
|
115
|
+
mask_overflow_lower = image + noise < 0
|
|
116
|
+
noise[mask_overflow_upper] = 1.0
|
|
117
|
+
noise[mask_overflow_lower] = 0
|
|
118
|
+
image = np.add(image, noise)
|
|
119
|
+
return image
|
|
120
|
+
|
|
121
|
+
image = _gaussian_noise(image)
|
|
122
|
+
|
|
123
|
+
return image, mask
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def rotate(image: np.ndarray, mask: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
|
127
|
+
"""Augment through rotation."""
|
|
128
|
+
rand_rotate = np.random.randint(low=0, high=4)
|
|
129
|
+
image = image.copy()
|
|
130
|
+
mask = mask.copy()
|
|
131
|
+
for _ in range(rand_rotate):
|
|
132
|
+
|
|
133
|
+
image = np.rot90(image) # rotate image -90 degrees
|
|
134
|
+
mask = np.rot90(mask) # rotate mask -90 degrees
|
|
135
|
+
r_coord = mask[..., 1].copy()
|
|
136
|
+
c_coord = mask[..., 2].copy()
|
|
137
|
+
mask[..., 1] = 1 - c_coord # rotation coordinates +90 degrees + translation
|
|
138
|
+
mask[..., 2] = r_coord # rotation coordinates +90 degrees
|
|
139
|
+
mask[..., 1][mask[..., 0] == 0] = 0
|
|
140
|
+
mask[..., 2][mask[..., 0] == 0] = 0
|
|
141
|
+
|
|
142
|
+
return image, mask
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def translate(
|
|
146
|
+
image: np.ndarray, mask: np.ndarray, cell_size: int = 4
|
|
147
|
+
) -> Tuple[np.ndarray, np.ndarray]:
|
|
148
|
+
"""Augment through translation along all axes.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
image: Image to be augmented.
|
|
152
|
+
mask: Corresponding prediction matrix with ground truth values.
|
|
153
|
+
cell_size: Size of one cell in the prediction matrix.
|
|
154
|
+
"""
|
|
155
|
+
direction = np.random.choice([0, 1])
|
|
156
|
+
image = image.copy()
|
|
157
|
+
mask = mask.copy()
|
|
158
|
+
|
|
159
|
+
shift_mask = np.random.choice(range(len(image) // cell_size))
|
|
160
|
+
shift_image = shift_mask * cell_size
|
|
161
|
+
|
|
162
|
+
image = np.roll(image, shift_image, direction)
|
|
163
|
+
mask = np.roll(mask, shift_mask, direction)
|
|
164
|
+
|
|
165
|
+
return image, mask
|