Keras如何自定义IOU-创新互联
小编这次要给大家分享的是Keras如何自定义IOU,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

我就废话不多说了,大家还是直接看代码吧!
def iou(y_true, y_pred, label: int):
"""
Return the Intersection over Union (IoU) for a given label.
Args:
y_true: the expected y values as a one-hot
y_pred: the predicted y values as a one-hot or softmax output
label: the label to return the IoU for
Returns:
the IoU for the given label
"""
# extract the label values using the argmax operator then
# calculate equality of the predictions and truths to the label
y_true = K.cast(K.equal(K.argmax(y_true), label), K.floatx())
y_pred = K.cast(K.equal(K.argmax(y_pred), label), K.floatx())
# calculate the |intersection| (AND) of the labels
intersection = K.sum(y_true * y_pred)
# calculate the |union| (OR) of the labels
union = K.sum(y_true) + K.sum(y_pred) - intersection
# avoid divide by zero - if the union is zero, return 1
# otherwise, return the intersection over union
return K.switch(K.equal(union, 0), 1.0, intersection / union)
def mean_iou(y_true, y_pred):
"""
Return the Intersection over Union (IoU) score.
Args:
y_true: the expected y values as a one-hot
y_pred: the predicted y values as a one-hot or softmax output
Returns:
the scalar IoU value (mean over all labels)
"""
# get number of labels to calculate IoU for
num_labels = K.int_shape(y_pred)[-1] - 1
# initialize a variable to store total IoU in
mean_iou = K.variable(0)
# iterate over labels to calculate IoU for
for label in range(num_labels):
mean_iou = mean_iou + iou(y_true, y_pred, label)
# divide total IoU by number of labels to get mean IoU
return mean_iou / num_labels 网页题目:Keras如何自定义IOU-创新互联
浏览路径:http://www.jxjierui.cn/article/pssgp.html


咨询
建站咨询
