2

我在实现 YOLO 损失函数时遇到了一些问题。每次我训练我的模型时,我的损失都会减少,但我的准确性也会降低。这让我相信我的损失函数不正确。并且考虑到 YOLO 论文非常模糊,我想知道是否有人可以帮助我。

这是我目前正在尝试遵循论文功能的损失函数。我试图找到一个单一的类对象,因此我的 y_true = [P,w,h,x,y] 对象,没有其他边界框。

def get_coord(X,Y,H,W):
x2 = X + W
y2 = Y + H

return x2,y2


def loss_function2(y_true,y_pred):
object_true = y_true[:,:,:,0]
object_pred = y_pred[:,:,:,0]

width_true = y_true[:,:,:,1]
width_pred = y_pred[:,:,:,1]

height_true = y_true[:,:,:,2]
height_pred =y_pred[:,:,:,2]

X_true = y_true[:,:,:,3]
X_pred =y_pred[:,:,:,3]

Y_true = y_true[:,:,:,4]
Y_pred = y_pred[:,:,:,4]

x2_true,y2_true = get_coord(X_true,Y_true,height_true,width_true)
x2_pred,y2_pred = get_coord(X_pred,Y_pred,height_pred,width_pred)

xi1 = tf.maximum(X_true,X_pred)
yi1 = tf.maximum(Y_true,X_true)
xi2 = tf.minimum(x2_true,x2_pred)
yi2 = tf.minimum(y2_true,y2_pred)


zeros = tf.zeros([10,19,19])

inter_area = tf.maximum(zeros,xi2-xi1)*tf.maximum(zeros,yi2-yi1)
box_area1 = (x2_true-X_true)*(y2_true-Y_true)
box_area2 = (x2_pred-X_pred)*(y2_pred-Y_pred)

union_area = box_area1 + box_area2 - inter_area
iou = inter_area/union_area

loss1 = tf.reduce_sum(tf.multiply(object_true,(tf.squared_difference(X_pred,X_true) + tf.squared_difference(Y_pred,Y_true))))
loss2 = tf.reduce_sum(tf.multiply(object_true,(tf.squared_difference(tf.sqrt(width_pred),tf.sqrt(width_true)) + tf.squared_difference(tf.sqrt(height_pred),tf.sqrt(height_true)))))
loss3 = tf.reduce_sum(tf.multiply(object_true,(tf.squared_difference(object_pred,iou))))
loss4 = tf.scalar_mul(0.5,(tf.reduce_sum(tf.multiply(((object_true*-1)+1),(tf.squared_difference(object_pred,object_true))))))

return loss1+loss2+loss3+loss4

我将图像网格分割为 19x19,因此我的预测以 10x19x19x5 张量的批次出现。

我在 python 中查看了其他 YOLO 实现的代码,大多数损失函数都让我感到困惑。它们要么太复杂,我无法遵循,要么实现与原始论文概述的完全不同的东西。

4

0 回答 0