我已经实现了此处描述的 UNET 网络
网络运行良好,但在论文中,他们提到在网络中添加加权图以更好地分离边界。据我了解,权重图是这样计算的
def unet_weight_map(y, wc=None, w0 = 10, sigma = 5):
"""
Parameters
----------
mask: Numpy array
2D array of shape (image_height, image_width) representing binary mask
of objects.
wc: dict
Dictionary of weight classes.
w0: int
Border weight parameter.
sigma: int
Border width parameter.
Returns
-------
Numpy array
Training weights. A 2D array of shape (image_height, image_width).
"""
labels = label(y)
no_labels = labels == 0
label_ids = sorted(np.unique(labels))[1:]
if len(label_ids) > 1:
distances = np.zeros((y.shape[0], y.shape[1], len(label_ids)))
for i, label_id in enumerate(label_ids):
distances[:,:,i] = distance_transform_edt(labels != label_id)
distances = np.sort(distances, axis=2)
d1 = distances[:,:,0]
d2 = distances[:,:,1]
w = w0 * np.exp(-1/2*((d1 + d2) / sigma)**2) * no_labels
else:
w = np.zeros_like(y)
if wc:
class_weights = np.zeros_like(y)
for k, v in wc.items():
class_weights[y == k] = v
w = w + class_weights
return w
直到这里一切都很好。但是,我的问题是如何在网络中使用这些权重图。我有一个加权二元交叉熵损失定义如下
def weighted_binary_crossentropy( y_true, y_pred, weight=[1.,2.]):
y_true = K.clip(y_true, K.epsilon(), 1-K.epsilon())
y_pred = K.clip(y_pred, K.epsilon(), 1-K.epsilon())
logloss = -(y_true * K.log(y_pred) * weight[0] + (1 - y_true) * K.log(1 - y_pred)*weight[1])
return K.mean( logloss, axis=-1)
但是,在这里,我将权重作为 [a, b] 数组输入到类权重的损失中,然后在编译时将此损失提供给网络。我的问题是我应该将这些地图输入到这个定制的损失函数中吗?如果是这样,怎么做?如果没有,我可以在 Keras 中使用什么其他方式?请帮忙。我已经阅读了许多与此问题相关的堆栈溢出问题,但我无法得到答案。如果需要,我可以提供有关我的网络的任何信息。