123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import paddle
- def compute_mean_covariance(img):
- batch_size = img.shape[0]
- channel_num = img.shape[1]
- height = img.shape[2]
- width = img.shape[3]
- num_pixels = height * width
-
- mu = img.mean(2, keepdim=True).mean(3, keepdim=True)
-
- img_hat = img - mu.expand_as(img)
- img_hat = img_hat.reshape([batch_size, channel_num, num_pixels])
-
- img_hat_transpose = img_hat.transpose([0, 2, 1])
-
- covariance = paddle.bmm(img_hat, img_hat_transpose)
- covariance = covariance / num_pixels
- return mu, covariance
- def dice_coefficient(y_true_cls, y_pred_cls, training_mask):
- eps = 1e-5
- intersection = paddle.sum(y_true_cls * y_pred_cls * training_mask)
- union = paddle.sum(y_true_cls * training_mask) + paddle.sum(
- y_pred_cls * training_mask) + eps
- loss = 1. - (2 * intersection / union)
- return loss
|