新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编这次要给大家分享的是Keras如何自定义IOU,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
创新互联建站-专业网站定制、快速模板网站建设、高性价比岳阳楼网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式岳阳楼网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖岳阳楼地区。费用合理售后完善,10余年实体公司更值得信赖。我就废话不多说了,大家还是直接看代码吧!
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