Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions EmbedSeg/criterions/my_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ def forward(
instance_ids = np.arange(instances.size(1))
else:
instance_ids = instance.unique()
instance_ids = instance_ids[instance_ids != 0]
instance_ids = instance_ids[instance_ids > 0]

# pixels labelled -1 carry no information and are excluded from the loss
if self.one_hot:
ignore = (instance < 0).any(1) # 1 x h x w
else:
ignore = instance < 0 # 1 x h x w
n_valid = (~ignore).sum().clamp(min=1)

# regress bg to zero
bg_mask = label == 0
bg_mask = (label == 0) & ~ignore

if bg_mask.sum() > 0:
seed_loss += torch.sum(torch.pow(seed_map[bg_mask] - 0, 2))
Expand Down Expand Up @@ -117,7 +124,11 @@ def forward(
)

# apply lovasz-hinge loss
instance_loss = instance_loss + lovasz_hinge(dist * 2 - 1, in_mask)
lovasz_labels = in_mask.long()
lovasz_labels[ignore] = -1
instance_loss = instance_loss + lovasz_hinge(
dist * 2 - 1, lovasz_labels, ignore=-1
)
# seed loss
seed_loss += self.foreground_weight * torch.sum(
torch.pow(seed_map[in_mask] - dist[in_mask].detach(), 2)
Expand All @@ -133,7 +144,7 @@ def forward(
instance_loss /= obj_count
var_loss /= obj_count

seed_loss = seed_loss / (height * width)
seed_loss = seed_loss / n_valid

loss += w_inst * instance_loss + w_var * var_loss + w_seed * seed_loss

Expand Down
16 changes: 12 additions & 4 deletions EmbedSeg/criterions/my_loss_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ def forward(
label = labels[b].unsqueeze(0) # 1 x d x h x w
center_image = center_images[b].unsqueeze(0) # 1 x d x h x w
instance_ids = instance.unique()
instance_ids = instance_ids[instance_ids != 0]
instance_ids = instance_ids[instance_ids > 0]

# pixels labelled -1 carry no information and are excluded from the loss
ignore = instance < 0 # 1 x d x h x w
n_valid = (~ignore).sum().clamp(min=1)

# regress bg to zero
bg_mask = label == 0
bg_mask = (label == 0) & ~ignore

if bg_mask.sum() > 0:
seed_loss += torch.sum(torch.pow(seed_map[bg_mask] - 0, 2))
Expand Down Expand Up @@ -127,7 +131,11 @@ def forward(
)

# apply lovasz-hinge loss
instance_loss = instance_loss + lovasz_hinge(dist * 2 - 1, in_mask)
lovasz_labels = in_mask.long()
lovasz_labels[ignore] = -1
instance_loss = instance_loss + lovasz_hinge(
dist * 2 - 1, lovasz_labels, ignore=-1
)

# seed loss
seed_loss += self.foreground_weight * torch.sum(
Expand All @@ -144,7 +152,7 @@ def forward(
instance_loss /= obj_count
var_loss /= obj_count

seed_loss = seed_loss / (depth * height * width)
seed_loss = seed_loss / n_valid

loss += w_inst * instance_loss + w_var * var_loss + w_seed * seed_loss

Expand Down
9 changes: 6 additions & 3 deletions EmbedSeg/datasets/ThreeDimensionalDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def __getitem__(self, index):

@classmethod
def decode_instance(cls, pic, one_hot, bg_id=None):
pic = np.array(pic, copy=False, dtype=np.uint16)
pic = np.asarray(pic, dtype=np.int32)
instance_map = np.zeros(
(pic.shape[0], pic.shape[1], pic.shape[2]), dtype=np.int16
)
Expand All @@ -253,6 +253,9 @@ def decode_instance(cls, pic, one_hot, bg_id=None):
else:
class_map[mask] = 1

# pixels labelled -1 carry no information and are ignored in the loss
instance_map[pic < 0] = -1

return instance_map, class_map

@classmethod
Expand All @@ -271,11 +274,11 @@ def rle_decode(cls, filename, one_hot=False, center=False):
else:
if center:
mask_decoded = np.zeros(
ast.literal_eval(df_numpy[0][2]), dtype=np.bool
ast.literal_eval(df_numpy[0][2]), dtype=bool
) # obtain size by reading first row of csv file
else:
mask_decoded = np.zeros(
ast.literal_eval(df_numpy[0][2]), dtype=np.uint16
ast.literal_eval(df_numpy[0][2]), dtype=np.int32
) # obtain size by reading first row of csv file
for row in df_numpy:
d["counts"] = ast.literal_eval(row[1])
Expand Down
10 changes: 7 additions & 3 deletions EmbedSeg/datasets/TwoDimensionalDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def __getitem__(self, index):

@classmethod
def decode_instance(cls, pic, one_hot, bg_id=None):
pic = np.array(pic, copy=False, dtype=np.uint16)
pic = np.asarray(pic, dtype=np.int32)
if one_hot:
instance_map = np.zeros(
(pic.shape[0], pic.shape[1], pic.shape[2]), dtype=np.uint8
Expand All @@ -221,6 +221,10 @@ def decode_instance(cls, pic, one_hot, bg_id=None):
else:
class_map[mask] = 1

if not one_hot:
# pixels labelled -1 carry no information and are ignored in the loss
instance_map[pic < 0] = -1

return instance_map, class_map

@classmethod
Expand All @@ -239,11 +243,11 @@ def rle_decode(cls, filename, one_hot=False, center=False):
else:
if center:
mask_decoded = np.zeros(
ast.literal_eval(df_numpy[0][2]), dtype=np.bool
ast.literal_eval(df_numpy[0][2]), dtype=bool
) # obtain size by reading first row of csv file
else:
mask_decoded = np.zeros(
ast.literal_eval(df_numpy[0][2]), dtype=np.uint16
ast.literal_eval(df_numpy[0][2]), dtype=np.int32
) # obtain size by reading first row of csv file
for row in df_numpy:
d["counts"] = ast.literal_eval(row[1])
Expand Down
24 changes: 12 additions & 12 deletions EmbedSeg/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from EmbedSeg.utils.metrics import matching_dataset, obtain_APdsb_one_hot
from EmbedSeg.utils.utils import Cluster, Cluster_3d
import numpy as np
from tifffile import imsave
from tifffile import imwrite
from EmbedSeg.utils.test_time_augmentation import apply_tta_2d, apply_tta_3d
from scipy.ndimage import zoom
from scipy.optimize import minimize_scalar, linear_sum_assignment
Expand Down Expand Up @@ -1138,15 +1138,15 @@ def test(fg_thresh, *args):
# save predictions
base, _ = os.path.splitext(os.path.basename(sample["im_name"][0]))
instances_file = os.path.join(save_dir, "predictions/", base + ".tif")
imsave(
imwrite(
instances_file,
instance_map.cpu().detach().numpy().astype(np.uint16),
)
seeds_file = os.path.join(save_dir, "seeds/", base + ".tif")
imsave(seeds_file, seed_map.cpu().detach().numpy())
imwrite(seeds_file, seed_map.cpu().detach().numpy())
if "instance" in sample:
gt_file = os.path.join(save_dir, "ground-truth/", base + ".tif")
imsave(gt_file, sample["instance"].squeeze().cpu().detach().numpy())
imwrite(gt_file, sample["instance"].squeeze().cpu().detach().numpy())

if save_results and "instance" in sample:
if not os.path.exists(os.path.join(save_dir, "results/")):
Expand Down Expand Up @@ -1471,14 +1471,14 @@ def test_3d(fg_thresh, *args):
image_file_names.append(base)

instances_file = os.path.join(save_dir, "predictions/", base + ".tif")
imsave(instances_file, instance_map)
imwrite(instances_file, instance_map)

seeds_file = os.path.join(save_dir, "seeds/", base + ".tif")
imsave(seeds_file, seed_map)
imwrite(seeds_file, seed_map)

if "instance" in sample:
gt_file = os.path.join(save_dir, "ground-truth/", base + ".tif")
imsave(gt_file, sample["instance"].squeeze().cpu().detach().numpy())
imwrite(gt_file, sample["instance"].squeeze().cpu().detach().numpy())

if save_results and "instance" in sample:
if not os.path.exists(os.path.join(save_dir, "results/")):
Expand Down Expand Up @@ -1783,19 +1783,19 @@ def test_3d_sliced(fg_thresh, *args):
image_file_names.append(base)

prediction_file = os.path.join(save_dir, "predictions/", base + ".tif")
imsave(
imwrite(
prediction_file,
instance_map.cpu().detach().numpy().astype(np.uint16),
)

seeds_file = os.path.join(save_dir, "seeds/", base + ".tif")
imsave(
imwrite(
seeds_file, torch.sigmoid(output[0, -1, ...]).cpu().detach().numpy()
)

if "instance" in sample:
gt_file = os.path.join(save_dir, "ground-truth/", base + ".tif")
imsave(gt_file, sample["instance"].squeeze().cpu().detach().numpy())
imwrite(gt_file, sample["instance"].squeeze().cpu().detach().numpy())

if save_results and "instance" in sample:
if not os.path.exists(os.path.join(save_dir, "results/")):
Expand Down Expand Up @@ -2203,10 +2203,10 @@ def test_3d_ilp(fg_thresh, *args):
# save predictions
base, _ = os.path.splitext(os.path.basename(sample["im_name"][0]))
instances_file = os.path.join(save_dir, "predictions/", base + ".tif")
imsave(instances_file, instance_map)
imwrite(instances_file, instance_map)

seeds_file = os.path.join(save_dir, "seeds/", base + ".tif")
imsave(seeds_file, np.asarray(seed_z).astype(float))
imwrite(seeds_file, np.asarray(seed_z).astype(float))

if save_results and "instance" in sample:
if not os.path.exists(os.path.join(save_dir, "results/")):
Expand Down
8 changes: 4 additions & 4 deletions EmbedSeg/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def train_vanilla(
instances[0].cpu(), key="groundtruth", title="Ground Truth"
) # TODO
instance_ids = instances[0].unique()
instance_ids = instance_ids[instance_ids != 0]
instance_ids = instance_ids[instance_ids > 0]

if display_embedding:
(
Expand Down Expand Up @@ -392,7 +392,7 @@ def train_vanilla_3d(
title="Ground Truth",
) # TODO
instance_ids = instances[0].unique()
instance_ids = instance_ids[instance_ids != 0]
instance_ids = instance_ids[instance_ids > 0]

visualizer.display(
predictions.cpu()[zslice, ...], key="prediction", title="Prediction"
Expand Down Expand Up @@ -542,7 +542,7 @@ def val_vanilla(
instances[0].cpu(), key="groundtruth", title="Ground Truth"
) # TODO
instance_ids = instances[0].unique()
instance_ids = instance_ids[instance_ids != 0]
instance_ids = instance_ids[instance_ids > 0]
if display_embedding:
(
center_x,
Expand Down Expand Up @@ -773,7 +773,7 @@ def val_vanilla_3d(
title="Ground Truth",
) # TODO
instance_ids = instances[0].unique()
instance_ids = instance_ids[instance_ids != 0]
instance_ids = instance_ids[instance_ids > 0]

visualizer.display(
predictions.cpu()[zslice, ...],
Expand Down
Loading