Weighted loss for segmentation

I have 2 classes (1 representing positive class and 0 representing negative class). I calculate weights for my BCE loss as follows.

Weights for Zero = 1 / (number of zeros in the entire dataset)
Weights for ones = 1 / (number of ones in the entire dataset)
but the problem is that the weights of zeros and ones becomes very small like 1e-6 and 1e-4 respectively.

I further change it to
Weights for Zero = 1 - (number of zeros in the entire dataset / total number of pixels in the dataset)
Weights for ones = 1 - (number of ones in the entire dataset / total number of pixels in the dataset)
By this way i can get the weights in range [0-1] with zeros getting close to zero and ones getting close to 1.

I would like to know if this is the correct way to do or is there any suggested way? I saw many reference discussions but they focus on classification and not segmenting pixels.

It seems your dataset is skewed towards 1.
What about the weights as below:

Weight for 0 : Avg number of 1s / Avg number of 0s
Weight for 1 : 1

The intuition here is to give a higher weight to 0.

Do we need to give larger weight to zero class which has more occurrence in the mask ? or the other way round ? because in this reference it says we to need to give more weights to classes which are less.

oh yes. you are right. its the other way around.

Weight for 0 : 1
Weight for 1 : Avg number of 0s / Avg number of 1s

The intuition here is to give a higher weight to 1.