Keras sparse_categorical_accuracy metric produces "Incompatible shapes" error

Viewed 943

I'm relatively new to Keras and am trying to implement a U-Net architecture with TensorFlow backend for pixel-wise, multi-class classification on many 256x256 images. Input shape and output shapes are both 256x256x1 (batch size is also 1).

I have over 30 classes so I'm trying to use sparse_categorical_crossentropy as my loss function (regular categorical cross entropy gives me a memory error), as many online forums suggested to do. When I change the "metric" parameter from 'accuracy' to 'sparse_categorical_accuracy', I get this error:

InvalidArgumentError: Incompatible shapes: [65536] vs. [1,256,256]   
[[{{node metrics_3/sparse_categorical_accuracy/Equal}} = 
Equal[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](metrics_3/sparse_categorical_accuracy/Reshape, metrics_3/sparse_categorical_accuracy/Cast)]]   
[[{{node metrics_3/sparse_categorical_accuracy/Mean/_749}} =
_Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_3000_metrics_3/sparse_categorical_accuracy/Mean", tensor_type=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

It seems it's expecting my output to be 1D instead of 2D? Do I need to change the format of my data? However when I try to flatten my output using a flatten layer followed by a dense layer (to produce a 65536 vector output) at the end of my U-Net I get another error saying the expected output dimension is 1. I've also tried changing the number of channels in the last layer to the total number of classes I'm working with (to make the output 256x256xnum_classes) but I get the same error.

Also when I just leave the "metric" parameter as 'accuracy' the code runs but the loss is always NaN and the accuracy converges very quickly to about 0.008. What does 'accuracy' default to in this case?

Are these any known issues with sparse_categorical_accuracy? I haven't been able to find much information online and the Keras documentation on this particular loss function is scarce as well. Any help is appreciated! Please let me know if other parts of my code are needed as well to help debug.

0 Answers
Related