def weights_map(ys):
"""Compute corresponding weight map when use cross entropy loss.
Argument:
ys: [depth, height, width]
Return:
weights_map: [depth, height, width]
"""
weights = ys.astype(np.float64)
# Balance class frequencies.
cls_ratio = np.sum(1 - ys) / np.sum(ys)
weights *= cls_ratio
# Generate boundaries using morphological operation.
se = generate_binary_structure(3, 1)
bigger = binary_dilation(ys, structure=se).astype(np.float64)
small = binary_erosion(ys, structure=se).astype(np.float64)
edge = bigger - small
# Balance edge frequencies.
edge_ratio = np.sum(bigger) / np.sum(edge)
edge *= np.exp(edge_ratio) * 10
# `weights` should > 0
# `targets * -log(sigmoid(logits)) * pos_weight + (1 - targets) * -log(1 - sigmoid(logits))`
return weights + edge + 1
评论列表
文章目录