You could count the number of positive and negative pixels for your complete training dataset and use the average of these counts to calculate the pos_weight or alternatively you could calculate the pos_weigth for the current batch by counting the positive and negative pixels and use the functional API via F.binary_cross_entropy_with_logits.