YOLO3的具体实现过程

fun1:

y_true = list()
y_true.append(np.zeros((batch_size, 52, 52, 3, 4 + 1 + len(labels))))  # 预测矩阵1
y_true.append(np.zeros((batch_size, 26, 26, 3, 4 + 1 + len(labels))))  # 预测矩阵2
y_true.append(np.zeros((batch_size, 13, 13, 3, 4 + 1 + len(labels))))  # 预测矩阵3

anchors = [10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326]

如上代码所示,每张图片的y_true是一个列表,包含3个预测矩阵。分别对应不同尺度。每个预测矩阵都分配了3个anchor。如上所示,第一个anchor框的长宽分别是10、13(单位是像素),第二个anchor框的长宽是16、30,以此类推,所有anchor框都是从小到大排序的。第一个预测矩阵分配的anchor框是第一第二第三个anchor框。第二个预测矩阵分配的anchor框是第四第五第六个anchor框。以此类推。

上面是搭好了全0的框架,然后我们需要把数据label填到正确的位置,需要确定3步:

1.确定放到3个预测矩阵中的哪一个,

2.确定放到13*13中的哪个格子(假如确定是放到第三个预测矩阵),

3.确定是3个anchor位置中的哪一个。

1和3都是根据label框和每个anchor框的iou确定(这里的iou只考虑面积不考虑位置)。如果label框和第5个anchor框的iou最大,那么这个label就分配到第二个预测矩阵的第二个anchor位置。

2是根据label的中心点确定。这个很好理解。

然后就是放的xywh、confident、class的数据形式

center_x= .5 * (obj['xmin'] + obj['xmax'])/ float(net_w)

center_y = .5 * (obj['ymin'] + obj['ymax']) / float(net_h)

w = (obj['xmax'] - obj['xmin']) / float(net_w)

h = (obj['ymax'] - obj['ymin']) / float(net_h)

confident = 1

class_1 =  1(代表这个box是狗)

class_2 =  0(代表这个box不是猫)

fun2:

网络的输出经过reshape后得到的y_pre.shape=[[batch_size,52,52,3,4+1+2],[batch_size,26,26,3,4+1+2],[batch_size,13,13,3,4+1+2]],y_true的shape也一样。3个矩阵我们只看一个,其他两个同理

y_pre_3.shape=[batch_size,13,13,3,4+1+2]

y_true_3.shape=[batch_size,13,13,3,4+1+2]

grid_factor.shape = [batch_size,13,13,3,2] #所有的元素的值都是13
grid.shape = [batch_size,13,13,3,2] #元素分布见图
anchor.shape = [batch_size,13,13,3,2] 
img_factor.shape = [batch_size,13,13,3,2]

adjusted_true_xy = y_true_3[..., :2] * grid_factor - grid
adjusted_true_wh = tf.log(y_true_3[..., 2:4] / anchor * img_factor + 1e-9)  # 1e-9 just avoid log(0) = -inf
adjusted_true_c = y_true_3[..., 4:5]
adjusted_true_class = y_true_3[..., 5:]

xywh_scale = 2 - y_true[i][..., 2:3] * y_true[i][..., 3:4]

loss_xy = tf.reduce_sum(object_mask * xywh_scale *tf.nn.sigmoid_cross_entropy_with_logits(logits=net_out_reshape[..., :2],
                                                                   labels=adjusted_true_xy)) / batch_size
loss_wh = tf.reduce_sum(
    object_mask * xywh_scale * 0.5 * tf.square(net_out_reshape[..., 2:4] - adjusted_true_wh)) / batch_size
loss_c = tf.reduce_sum(
    object_mask * tf.nn.sigmoid_cross_entropy_with_logits(logits=net_out_reshape[..., 4:5],
                                                          labels=adjusted_true_c) + (
            1 - object_mask) * tf.nn.sigmoid_cross_entropy_with_logits(logits=net_out_reshape[..., 4:5],
                                                                       labels=adjusted_true_c) * ignore_masks) / batch_size
loss_class = tf.reduce_sum(
    object_mask * tf.nn.sigmoid_cross_entropy_with_logits(logits=net_out_reshape[..., 5:],
                                                          labels=adjusted_true_class)) / batch_size

检测

fun3

猜你喜欢

转载自blog.csdn.net/hsqyc/article/details/82378556