How to do point-wise categorical crossentropy loss in Keras?

前端 未结 4 1205
栀梦
栀梦 2021-01-18 02:26

I have a network that produces a 4D output tensor where the value at each position in spatial dimensions (~pixel) is to be interpreted as the class probabilities for that po

4条回答
  •  花落未央
    2021-01-18 03:06

    Found this issue to confirm my intuition.

    In short : the softmax will take 2D or 3D inputs. If they are 3D keras will assume a shape like this (samples, timedimension, numclasses) and apply the softmax on the last one. For some weird reasons, it doesnt do that for 4D tensors.

    Solution : reshape your output to a sequence of pixels

    reshaped_output = Reshape((height*width, num_classes))(output_tensor)
    

    Then apply your softmax

    new_output = Activation('softmax')(reshaped_output) 
    

    And then either you reshape your target tensors to 2D or you just reshape that last layer into (width, height, num_classes).

    Otherwise, something I would try if I wasn't on my phone right now is to use a TimeDistributed(Activation('softmax')). But no idea if that would work... will try later

    I hope this helps :-)

提交回复
热议问题