Skip to content

Commit

Permalink
Finished UNet + launcher + started widget
Browse files Browse the repository at this point in the history
  • Loading branch information
c-h-benedetti committed Nov 18, 2024
1 parent eddbf5e commit 9b6fdb5
Show file tree
Hide file tree
Showing 16 changed files with 1,104 additions and 236 deletions.
8 changes: 4 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

# -- Project information -----------------------------------------------------

project = 'Protoplasts swelling analyzer'
project = 'Microglia Analyzer'
copyright = '2024, Clément H. Benedetti'
author = 'Clément H. BENEDETTI'
author = 'Clément H. BENEDETTI'

# The full version, including alpha/beta/rc tags
release = '1.0.0'
release = '0.0.1'


# -- General configuration ---------------------------------------------------
Expand Down Expand Up @@ -69,4 +69,4 @@

html_favicon = 'icon.png'

autosummary_generate = True
autosummary_generate = True
2 changes: 1 addition & 1 deletion src/microglia_analyzer/_tests/workflow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tifffile
import os
from microglia_analyzer.microglia_analyzer import MicrogliaAnalyzer
from microglia_analyzer.ma_worker import MicrogliaAnalyzer
from microglia_analyzer.tiles import tiler


Expand Down
2 changes: 1 addition & 1 deletion src/microglia_analyzer/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import re

from microglia_analyzer import TIFF_REGEX
from microglia_analyzer.microglia_analyzer import MicrogliaAnalyzer
from microglia_analyzer.ma_worker import MicrogliaAnalyzer


_IMAGE_LAYER_NAME = "µ-Image"
Expand Down
2 changes: 1 addition & 1 deletion src/microglia_analyzer/_widget_yolo_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def clear_mask(self):
Clears the mask layer.
"""
if _MASKS_LAYER in self.viewer.layers:
self.viewer.layers[_MASKS_LAYER].data = np.zeros_like(self.viewer.layers[_IMAGE_LAYER].data)
self.viewer.layers[_MASKS_LAYER].data = np.zeros_like(self.viewer.layers[_MASKS_LAYER].data)

def fill_current_label(self):
if not _MASKS_LAYER in self.viewer.layers:
Expand Down
60 changes: 60 additions & 0 deletions src/microglia_analyzer/dl/losses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import tensorflow as tf

def jaccard_loss(y_true, y_pred):
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.cast(y_pred, tf.float32)
intersection = tf.reduce_sum(y_true * y_pred)
union = tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) - intersection
return 1 - (intersection + 1) / (union + 1)

def dice_loss(y_true, y_pred):
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.cast(y_pred, tf.float32)
intersection = tf.reduce_sum(y_true * y_pred)
return 1 - (2. * intersection + 1) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + 1)

def bce_dice_loss(bce_coef=0.5):
def bcl(y_true, y_pred):
bce = tf.keras.losses.binary_crossentropy(y_true, y_pred)
dice = dice_loss(y_true, y_pred)
return bce_coef * bce + (1.0 - bce_coef) * dice
return bcl

def focal_loss(gamma=2.0, alpha=5.75):
def focal_loss_fixed(y_true, y_pred):
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.cast(y_pred, tf.float32)
alpha_t = y_true * alpha + (1 - y_true) * (1 - alpha)
p_t = y_true * y_pred + (1 - y_true) * (1 - y_pred)
fl = - alpha_t * (1 - p_t) ** gamma * tf.math.log(p_t + 1e-5)
return tf.reduce_mean(fl)
return focal_loss_fixed

def tversky_loss(alpha=0.5):
beta = 1 - alpha
def loss(y_true, y_pred):
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.cast(y_pred, tf.float32)
true_pos = tf.reduce_sum(y_true * y_pred)
false_neg = tf.reduce_sum(y_true * (1 - y_pred))
false_pos = tf.reduce_sum((1 - y_true) * y_pred)
return 1 - (true_pos + 1) / (true_pos + alpha * false_neg + beta * false_pos + 1)
return loss

def skeleton_recall(y_true, y_pred):
intersection = tf.reduce_sum(y_true * y_pred)
recall = intersection / (tf.reduce_sum(y_true) + 1e-8)
return 1 - recall

def dice_skeleton_loss(skeleton_coef=0.5, bce_coef=0.5):
bdl = bce_dice_loss(bce_coef)
def dsl(y_true, y_pred):
y_pred = tf.square(y_pred)
return (1.0 - skeleton_coef) * bdl(y_true, y_pred) + skeleton_coef * skeleton_recall(y_true, y_pred)
return dsl

def skeleton_loss(y_true, y_pred):
inter = tf.reduce_sum(y_true * y_pred) / tf.reduce_sum(y_true)
mse_score = tf.reduce_mean(tf.square(y_true - y_pred))
mean_constraint = tf.abs(tf.reduce_mean(y_pred) - tf.reduce_mean(y_true))
return 1.0 - inter + mse_score + 0.1 * mean_constraint
Loading

0 comments on commit 9b6fdb5

Please sign in to comment.