Dice Coefficient from Integer Labels

For a segmentation task, I have the predictions and the target as tensors of shape [batch_size, h, w] consisting of values in [0, num_classes - 1].
How can I compute the dice coefficient from these?
One way that I can think of is to one-hot encode the tensors and then compute the dice. Is there a better way?