yolo loss 将图像标注的真实事坐标转换到anchor坐标

本文深入探讨YOLOv3的损失函数计算,特别是真值框坐标如何从原始标注转换到锚点坐标,并解释了在损失计算中使用obj_mask的重要性,确保只计算有标记的anchor box的损失。通过对真实坐标与预测坐标的平方差求和,计算中点坐标的损失,同时利用obj_mask将未标记点的损失设为0。
最近在看yolov3 的源码,在看yolo_loss的时候遇到了一个卡点,就是将真是标注的box终点坐标转换
到anchor点的坐标
        true_xy = true_xy * tf.cast(grid_size, tf.float32) - tf.cast(grid, tf.float32)
        raw_true_xy = y_true[l][..., :2] * grid_shapes[l][:] - grid
import tensorflow as tf


import numpy as np



#anchor box=13*13
grid_size=13
grid = tf.meshgrid(tf.range(grid_size), tf.range(grid_size))
grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)


#batch_size=8 box=13*13 ,每一种规格的anchor box 对应3个box ,中点坐标是2维
T_xy=np.zeros([8, 13, 13, 3, 2])*1.0

T_xy[6,4,2]=[0.309,0.46212122]


T_xy=tf.constant(T_xy,dtype=tf.float32)


true_xy = T_xy * tf.cast(grid_size, tf.float32) - tf.cast(grid, tf.float32)

print(true_xy)


[[[[  0.   0.]
    [  0.   0.]
    [  0.   0.]]

   [[ -1.   0.]
    [ -1.   0.]
    [ -1.   0.]]

   [[ -2.   0.]
    [ -2.   0.]
    [ -2.   0.]]

   ...


   ...

   [[-10. -12.]
    [-10. -12.]
    [-10. -12.]]

   [[-11. -12.]
    [-11. -12.]
    [-11. -12.]]

   [[-12. -12.]
    [-12. -12.]
    [-12. -12.]]]]], shape=(8, 13, 13, 3, 2), dtype=float32)
        xy_loss = obj_mask * box_loss_scale * \
            tf.reduce_sum(tf.square(true_xy - pred_xy), axis=-1)
obj_mask=np.zeros([8, 13, 13, 3])

obj_mask[0,6,4,2]=1

obj_mask=tf.constant(obj_mask,dtype=tf.float32)
发现这样处理后计算loss,有大量常量数值1,…11,12等,感觉会有问题因为用了tf.reduce_sum(tf.square(true_xy - pred_xy)
实际上没关系,因为在前面乘以了obj_mask ,对于yolo_loss中,中点坐标的loss ,只会计算有标记的对应anchor 的loss其余点loss会设置为0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值