diff --git a/deepfinder/losses.py b/deepfinder/losses.py index 1b6d71b883968fcf186c566585121556acabd4ac..cd7ffa1fdbba79117fa2b74e60f26eb1c2aa575a 100644 --- a/deepfinder/losses.py +++ b/deepfinder/losses.py @@ -5,7 +5,16 @@ # License: GPL v3.0. See <https://www.gnu.org/licenses/> # ============================================================================================= -from keras import backend as K +import tensorflow as tf +from tensorflow.keras import backend as K + +# had to replace sometimes K by tf, because else: TypeError: An op outside of the function building code is being passed +# a "Graph" tensor. It is possible to have Graph tensors +# leak out of the function building context by including a +# tf.init_scope in your function building code. +# Reason was: So the main issue here is that custom loss function is returning a Symbolic KerasTensor and not a Tensor. +# And this is happening because inputs to the custom loss function are in Symbolic KerasTensor form. +# ref: https://github.com/tensorflow/tensorflow/issues/43650 # Ref: salehi17, "Twersky loss function for image segmentation using 3D FCDN" # -> the score is computed for each class separately and then summed @@ -14,20 +23,18 @@ from keras import backend as K # alpha+beta=1 : produces set of F*-scores def tversky_loss(y_true, y_pred): alpha = 0.5 - beta = 0.5 - - ones = K.ones(K.shape(y_true)) - p0 = y_pred # proba that voxels are class i - p1 = ones-y_pred # proba that voxels are not class i + beta = 0.5 + + ones = tf.ones(tf.shape(y_true)) + p0 = y_pred # proba that voxels are class i + p1 = ones - y_pred # proba that voxels are not class i g0 = y_true - g1 = ones-y_true - - num = K.sum(p0*g0, (0,1,2,3)) - den = num + alpha*K.sum(p0*g1,(0,1,2,3)) + beta*K.sum(p1*g0,(0,1,2,3)) - - T = K.sum(num/den) # when summing over classes, T has dynamic range [0 Ncl] - - Ncl = K.cast(K.shape(y_true)[-1], 'float32') - return Ncl-T - - + g1 = ones - y_true + + num = K.sum(p0 * g0, (0, 1, 2, 3)) + den = num + alpha * K.sum(p0 * g1, (0, 1, 2, 3)) + beta * K.sum(p1 * g0, (0, 1, 2, 3)) + + T = K.sum(num / den) # when summing over classes, T has dynamic range [0 Ncl] + + Ncl = tf.cast(tf.shape(y_true)[-1], 'float32') + return Ncl - T diff --git a/deepfinder/models.py b/deepfinder/models.py index 2d3b1a5b8b0c71f3efbdf92762275c3d9ce7aa27..9635678c6e12d923fb49d693525c9a5873d8fde7 100644 --- a/deepfinder/models.py +++ b/deepfinder/models.py @@ -5,9 +5,9 @@ # License: GPL v3.0. See <https://www.gnu.org/licenses/> # ============================================================================================= -from keras.layers import Input, concatenate -from keras.models import Model -from keras.layers.convolutional import Conv3D, MaxPooling3D, UpSampling3D +from tensorflow.keras.layers import Input, concatenate +from tensorflow.keras.layers import Conv3D, MaxPooling3D, UpSampling3D +from tensorflow.keras.models import Model def my_model(dim_in, Ncl): diff --git a/deepfinder/training.py b/deepfinder/training.py index 61ff12f24e1eb41e62c467e4cf46f358b2c7e35a..3bca9dd02a775014572547757eec817aa33b6766 100644 --- a/deepfinder/training.py +++ b/deepfinder/training.py @@ -8,9 +8,9 @@ import h5py import numpy as np import time -from keras.optimizers import Adam -from keras.utils import to_categorical -import keras.backend as K +from tensorflow.keras.optimizers import Adam +from tensorflow.keras.utils import to_categorical +import tensorflow.keras.backend as K from sklearn.metrics import precision_recall_fscore_support diff --git a/requirements.txt b/requirements.txt index 0d9739050c58147eaed9237b782aab64af7d6d6d..f23b6e93cee969e39ab3dc70495e90641e357342 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ -tensorflow==1.14.0 -Keras==2.3.1 -numpy==1.16.4 -h5py==2.9.0 +tensorflow==2.6.0 +Keras==2.6.0 +numpy==1.19.5 +h5py==3.1.0 lxml==4.3.4 mrcfile==1.1.2 scikit-learn==0.22.2.post1 diff --git a/requirements_gpu.txt b/requirements_gpu.txt index 04414c8ce95862be24a129169dbee5e8cf9e4280..c642e1e998c656a1bd314dd25e9193c5d8dc5832 100644 --- a/requirements_gpu.txt +++ b/requirements_gpu.txt @@ -1,7 +1,7 @@ -tensorflow-gpu==1.14.0 -Keras==2.3.1 -numpy==1.16.4 -h5py==2.9.0 +tensorflow-gpu==2.6.0 +Keras==2.6.0 +numpy==1.19.5 +h5py==3.1.0 lxml==4.3.4 mrcfile==1.1.2 scikit-learn==0.22.2.post1