RMSE computation incorporating neighboring pixels in tensorflow

Viewed 52

I am training a FCNN using tensorflow with Keras and I currently use RMSE for validation as:

import tensorflow
from tensorflow.python.keras import backend
def rmse(y_true, y_pred):
    return backend.sqrt(backend.mean(backend.square(y_pred - y_true)))

but instead of pixel by pixel comparison I would like to compute the RMSE incorporating neighboring pixels, meaning for each pixel the error would be calculated considering the same corresponding pixel in the y_true plus it's 3x3 or 5x5 neighboring pixels.

How do I implement this in keras?

1 Answers

Instead of doing this:

def rmse(y_true, y_pred):
    return backend.sqrt(backend.mean(backend.square(y_pred - y_true)))

you could try :

avg_pool = tf.keras.layers.AveragePooling2D(pool_size=(2, 2),
strides=(1, 1), padding='valid'))

def rmse(y_true, y_pred):
    return backend.sqrt(backend.mean(backend.square(avg_pool(y_pred) - avg_pool(y_true))))
Related