How to apply convolution using a Gaussian kernel within the loss function using keras backend

Viewed 899

I am building a CNN in keras for object labeling in electron microscopy images as contours. The labeled contours occupy a small percentage of the image. The output is a mask with ones labeling the object(s) contour(s).

I need to write my own loss function. My idea is to first apply Gaussian blurring on both y_pred and y_test. This way absolute overlapping of contours is not required to qualify as true positive.

blurring_kernel = np.array(( 1, 1, 1, 1, 3, 1, 1, 1, 1 ), dtype = "float32").reshape((3, 3))
blurring_kernel /= blurring_kernel.sum()

def loss(y_true, y_pred):
    #Apply Convolution bluring on y_true here
    #Apply Convolution bluring on y_true here
    y_true_f = K.flatten(y_true);
    y_pred_f = K.flatten(y_pred);
    intersection = K.sum(y_true_f * y_pred_f);
    return  -intersection / K.sum(y_true_f*y_true_f + y_pred_f*y_pred_f)

I tried conv2d from keras backend but encountered

ValueError: len(dilation_rate)=2 but should be 1

I also tried to multiply y_pred andy_test a numpy kernel but that was obviously a desperate try as they are different objects.

I still think the solution is with conv2d from keras backend but I do not have the experience to use it and the documentation has not helped.

0 Answers
Related