2
$\begingroup$

Is there a way to scale the output of a CNN before computing the loss function in keras. It would have to work for batches as well. Specifically I am training images and the output is a probability map of each pixels for the entire batch. Then I compute the loss function with the ground truth data. The problem is that the predicted output during training at some intermediate stage lies between 0.02 to 1e-4 and the ground truth pixels are between 0 and 1 so the loss function doesnt penalize pixels which dont look similar to the ground truth. I get this by visualizing the squared error between the output at some intermediate stage and the ground truth data. enter image description here

$\endgroup$
3
  • 2
    $\begingroup$ Keras has Lambda Layers if you want to do any processing inside the graph. You can write a function inside this Lambda layer to scale your values using whatever scaling method you want. $\endgroup$ Commented Apr 16, 2020 at 21:17
  • $\begingroup$ yeah thanks i found that solution $\endgroup$
    – arrhhh
    Commented Apr 17, 2020 at 16:45
  • $\begingroup$ Great! Upvote my comment..trying to build some reputation😅 $\endgroup$ Commented Apr 18, 2020 at 12:53

1 Answer 1

1
$\begingroup$

Use Lambda layer

def scale(tensor):
    tensor = (tensor - K.min(tensor)+1e-20)/(K.max(tensor)-K.min(tensor)+1e-10)
    return tensor

scaled_density_pred = Lambda(scale, name="scale_layer")(density_pred)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.