neural-network - 如何在 Keras 中为 Sequential 模型实现损失函数(Intersection over Union)?
问题描述
IoU 是一个损失函数,需要最大化而不是最小化。这是我尝试过的,但我无法访问 max 函数中的参数值:
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0]) #error in THESE lines
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
# return the intersection over union value
return iou
这是我在 Internet 上找到的代码:(这行得通吗?看起来不像 IoU。
import numpy as np
def computeIoU(y_pred_batch, y_true_batch):
return np.mean(np.asarray([pixelAccuracy(y_pred_batch[i], y_true_batch[i])
for i in range(len(y_true_batch))]))
def pixelAccuracy(y_pred, y_true):
y_pred = np.argmax(np.reshape(y_pred,
[N_CLASSES_PASCAL,img_rows,img_cols]),axis=0)
y_true = np.argmax(np.reshape(y_true,
[N_CLASSES_PASCAL,img_rows,img_cols]),axis=0)
y_pred = y_pred * (y_true>0)
return 1.0 * np.sum((y_pred==y_true)*(y_true>0)) / np.sum(y_true>0)
解决方案
如果您有像素(不是框)的预测,以及 0 到 1 之间的值。(对于单类,存在/不存在)
def iou(true, pred):
intersection = true * pred
notTrue = 1 - true
union = true + (notTrue * pred)
return K.sum(intersection)/K.sum(union)
您的函数已调整(我不知道它是否正常工作,存在风险或由于交叉点 = 常数 0 而没有梯度)
def iou(boxA,boxB):
xA = K.stack([boxA[:,0], boxB[:,0]], axis=-1)
yA = K.stack([boxA[:,1], boxB[:,1]], axis=-1)
xB = K.stack([boxA[:,2], boxB[:,2]], axis=-1)
yB = K.stack([boxA[:,3], boxB[:,3]], axis=-1)
xA = K.max(xA, axis=-1)
yA = K.max(yA, axis=-1)
xB = K.min(xB, axis=-1)
yB = K.min(yB, axis=-1)
interX = K.zeros_like(xB)
interY = K.zeros_like(yB)
interX = K.stack([interX, xB-xA + 1], axis=-1)
interY = K.stack([interY, yB-yA + 1], axis=-1)
#because of these "max", interArea may be constant 0, without gradients, and you may have problems with no gradients.
interX = K.max(interX, axis=-1)
interY = K.max(interY, axis=-1)
interArea = interX * interY
boxAArea = (boxA[:,2] - boxA[:,0] + 1) * (boxA[:,3] - boxA[:,1] + 1)
boxBArea = (boxB[:,2] - boxB[:,0] + 1) * (boxB[:,3] - boxB[:,1] + 1)
iou = interArea / (boxAArea + boxBArea - interArea)
return iou
推荐阅读
- oracle - 创建 PL/SQL 块以将新记录插入到 Department 表中
- python - 在 sqlite 中存储更新或插入
- kubernetes - Kubernetes Ingress Object 和 Ingress Controller 的内部工作和连接
- flutter - Flutter SQFLITE:请解释一下什么工厂和这个数据类型Future
- > 意味着
- symfony - 使用 SoftDeleatable 扩展时,Mercure 不会更新 DELETE 方法
- android - 在 Play 商店外分发 apk(到非 gms 设备)
- python - 基于现有模型对象创建 django 用户
- pyspark - 使用 pyspark 同时编写 parquet 文件
- reactjs - 无效的挂钩调用。当我在 useEffect 中调用 useQuery 时,只能在函数组件的主体内部调用钩子
- linux - 如果在使用 shell 脚本的作业监控中找不到文件,如何触发电子邮件?