The binary cross-entropy loss should be used with sigmod activation in the last layer, and this severely punishes the opposite predictions. He does not take into account that the output is one hot code, and the sum of the predictions should be 1. But since erroneous predictions are severely punished, the model is somewhat learning to classify correctly.
Now, to force the use of the previous hot code, softmax activation with categorical cross-entropy is used. This is what you should use.
Now the problem is using softmax in your case, since Keras does not support softmax on every pixel.
The easiest way is to resize (n_rows, n_cols, 7) using the Permute layer, and then change it to (n_rows * n_cols, 7) using the Reshape layer. Then you can add a softmax activation softmax and use crossentopia. Data must also be modified accordingly.
Another way to do this would be to implement depth-softmax:
def depth_softmax(matrix): sigmoid = lambda x: 1 / (1 + K.exp(-x)) sigmoided_matrix = sigmoid(matrix) softmax_matrix = sigmoided_matrix / K.sum(sigmoided_matrix, axis=0) return softmax_matrix
and use it as a lambda layer:
model.add(Deconvolution2D(7, 1, 1, border_mode='same', output_shape=(7,n_rows,n_cols))) model.add(Permute(2,3,1)) model.add(BatchNormalization()) model.add(Lambda(depth_softmax))
If tf image_dim_ordering , you can do this using Permute layers.
See here for more details.
indraforyou
source share