我在实施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的其他实现中的代码,大多数丢失函数只是让我感到困惑。它们要么太复杂,要么无法遵循或实施与原始论文所概述的完全不同的东西。