重新排列 numpy 数组以适合其网格单元

问题描述

我从头开始编写了一个 YOLO 模型,并有一个 numpy 数组,如下所示:

[
 [[1 0 1 0.4 0.3 0.2 0.1]
 [1 1 0 0.2 0.3 0.4 0.5]
 [0 0 0 0 0 0 0]]
 ...]

这就是它在 Pandas 对象中的样子:

Obj_score c1 c2 x y h w 
1 0 1 0.4 0.3 0.2 0.1
1 1 0 0.2 0.3 0.4 0.5
0 0 0 0 0 0 0

这是一些用于将 numpy 数组转换为 7*7*17 矩阵的代码

label_matrix = np.zeros([7,7,17])
    for i in range(y.shape[0]):
        for l in range(y.shape[1]):
            Obj_score = y[i][l][0] 
            c1 = y[i][l][1]
            c2 = y[i][l][2] 
            xmax = y[i][l][3]
            ymax = y[i][l][4]
            xmin = y[i][l][5]
            ymin = y[i][l][6]
            
            x = (xmin + xmax) / 2 
            y1 = (ymin + ymax) / 2
            w = (xmax - xmin)
            h = (ymax - ymin)
            loc = [7 * x,7 * y1]
            loc_i = int(loc[1])
            loc_j = int(loc[0])
            y1 = loc[1] - loc_i
            x = loc[0] - loc_j

我想做的是,如果元素 obj_score = 0(对于那个网格单元),那么整个

enter image description here

框(我不知道如何描述它,所以我把那个图像)用1*1*17 矩阵为 0,如果该网格单元的 obj_score = 1,则应填充相应的矩阵。

这里有一些代码(我发现)可以做到这一点(我认为),我将如何重写它以满足我的需要:

   if label_matrix[loc_i,loc_j,24] == 0:
        label_matrix[loc_i,cls] = 1
        label_matrix[loc_i,20:24] = [x,y,w,h]
        label_matrix[loc_i,24] = 1  # response
            

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)