numpy使用(二)

numpy记录(二)绘制网格模块

1、np.clip(a, a_min, a_max, out=None)
将数组中的元素限制在a_min, a_max之间
2、x,y = np.meshgrid(np.arange(f),np.arange(f))
绘制网格,可利用x,y来实现对各网格中心的遍历循环
numpy使用(二)_第1张图片
3、列表加、乘运算

>>a=[1,1,1,1]
>>b=[1,2,3,4]
>>print(a+b)
[1, 1, 1, 1, 1, 2, 3, 4]

>>a=[1,2,3]
>>b=3
>>a*b
[1, 2, 3, 1, 2, 3, 1, 2, 3]

4、for i, j in zip(y,x):
循环列表y和列表x,将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表

>>>a = [1,2,3]
>>> b = [4,5,6]
>>>> zipped = zip(a,b)     # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]

>>> zip(*zipped)          # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]

5、numpy.linspace(start,stop,num)
start:参数表示这个序列的开始值
stop:序列的终止数值
num:要生成的样本数,必须是非负值。

>>np.linspace(50,250,3)
array([ 50., 150., 250.])

6、整体代码

import numpy as np
from utils.config import Config
from math import sqrt as sqrt
from itertools import product as product
import matplotlib.pyplot as plt

mean = []
for k, f in enumerate(Config["feature_maps"]):
    x,y = np.meshgrid(np.arange(f),np.arange(f))
    # x为numpy.ndarray的N维数组
    # print(type(x))
    x = x.reshape(-1)
    # print('x==', x)

    y = y.reshape(-1)
    # print('y==', y)

    for i, j in zip(y,x):
        # print(i,j)
        # 300/8
        f_k = Config["min_dim"] / Config["steps"][k]
        # print('%s=%s', ([k], f_k))
        # 计算网格的中心
        cx = (j + 0.5) / f_k
        cy = (i + 0.5) / f_k

        # 求短边
        s_k = Config["min_sizes"][k]/Config["min_dim"]
        mean += [cx, cy, s_k, s_k]
        # print('mean=', mean)

        # 求长边
        s_k_prime = sqrt(s_k * (Config["max_sizes"][k]/Config["min_dim"]))
        mean += [cx, cy, s_k_prime, s_k_prime]

        # 获得长方形
        for ar in Config["aspect_ratios"][k]:
            mean += [cx, cy, s_k*sqrt(ar), s_k/sqrt(ar)]
            mean += [cx, cy, s_k/sqrt(ar), s_k*sqrt(ar)]

mean = np.clip(mean,0,1)
mean = np.reshape(mean,[-1,4])*Config["min_dim"]

linx = np.linspace(0.5 * Config["steps"][4], Config["min_dim"] - 0.5 * Config["steps"][4],
                    Config["feature_maps"][4])
liny = np.linspace(0.5 * Config["steps"][4], Config["min_dim"] - 0.5 * Config["steps"][4],
                    Config["feature_maps"][4])


print("linx:",linx)
print("liny:",liny)
centers_x, centers_y = np.meshgrid(linx, liny)

fig = plt.figure()
ax = fig.add_subplot(111)
plt.ylim(-100,500)
plt.xlim(-100,500)
plt.scatter(centers_x,centers_y)

step_start = 8707
step_end = 8711
# step_start = 8728
# step_end = 8732
box_widths = mean[step_start:step_end,2]
print(box_widths)
box_heights = mean[step_start:step_end,3]


prior_boxes = np.zeros_like(mean[step_start:step_end,:])
print(prior_boxes)
prior_boxes[:,0] = mean[step_start:step_end,0]
print(prior_boxes[:,0])
prior_boxes[:,1] = mean[step_start:step_end,1]
# prior_boxes[:,2] = mean[step_start:step_end,2]
# prior_boxes[:,3] = mean[step_start:step_end,3]
print(prior_boxes)

# 获得先验框的左上角和右下角
prior_boxes[:, 0] -= box_widths/2
prior_boxes[:, 1] -= box_heights/2
# prior_boxes[:, 2] += box_widths/2
# prior_boxes[:, 3] += box_heights/2
print(prior_boxes)
print(np.shape(mean))
rect1 = plt.Rectangle([prior_boxes[0, 0],prior_boxes[0, 1]],box_widths[0],box_heights[0],color="r",fill=False)
rect2 = plt.Rectangle([prior_boxes[1, 0],prior_boxes[1, 1]],box_widths[1],box_heights[1],color="g",fill=False)
rect3 = plt.Rectangle([prior_boxes[2, 0],prior_boxes[2, 1]],box_widths[2],box_heights[2],color="b",fill=False)
rect4 = plt.Rectangle([prior_boxes[3, 0],prior_boxes[3, 1]],box_widths[3],box_heights[3],color="r",fill=False)
rect5 = plt.Rectangle([25,25],100,50,color='b',fill=False)

ax.add_patch(rect1)
ax.add_patch(rect2)
ax.add_patch(rect3)
ax.add_patch(rect4)
ax.add_patch(rect5)

plt.show()


你可能感兴趣的:(numpy,目标检测,numpy)