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.
$\begingroup$
$\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$– ashutosh singhCommented Apr 16, 2020 at 21:17
-
$\begingroup$ yeah thanks i found that solution $\endgroup$– arrhhhCommented Apr 17, 2020 at 16:45
-
$\begingroup$ Great! Upvote my comment..trying to build some reputation😅 $\endgroup$– ashutosh singhCommented Apr 18, 2020 at 12:53
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
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)