Skip to content

sparse_categorical_crossentropy with ignore_class fails for 4D inputs #19825

@enricoschroeder

Description

@enricoschroeder

Using ignore_class with keras.losses.sparse_categorical_crossentropy and 4D inputs (Batch x Height x Width x Class) fails with a ValueError indicating wrong shapes.

Minimal example to reproduce:

import numpy as np
import tensorflow as tf

y_true = np.zeros((1, 224, 224, 1))
y_true[0, 0, 0, 0] = 255
y_pred = np.ones((1, 224, 224, 21))
tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred, ignore_class=255)

--> "ValueError: Arguments target and output must have the same shape up until the last dimension: target.shape=(1, 224, 224, 1), output.shape=(1, 224, 224, 224, 21)"

This expand_dims seems to be the culprit:

ops.expand_dims(valid_mask, -1), y_pred.dtype

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions