Class Balanced Dice Loss

I am currently doing a 3-d segmentation task. It has 31 classes, with class 0 as the background. but the dataset is heavily imbalanced. How to design a Dice Loss for this??