代码改变世界

np.mgrid 用法

2018-06-27 12:42  Time皇族  阅读(2483)  评论(0编辑  收藏  举报
import numpy as np

dtype=np.float32
num_anchors = 6

y, x = np.mgrid[0:4, 0:4]
print(y)
print(x)
y = np.expand_dims(y, axis=-1) 
x = np.expand_dims(x, axis=-1)  
h = np.ones((num_anchors, ), dtype=dtype)  
w = np.ones((num_anchors, ), dtype=dtype)  
xmin = x - h / 2.
ymin = y - h / 2.
print('xmin')
print(xmin)
print('ymin')
print(ymin)
print(ymin.shape)

'''
[[0 0 0 0]
 [1 1 1 1]
 [2 2 2 2]
 [3 3 3 3]]
[[0 1 2 3]
 [0 1 2 3]
 [0 1 2 3]
 [0 1 2 3]]
xmin
[[[-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]
  [ 0.5  0.5  0.5  0.5  0.5  0.5]
  [ 1.5  1.5  1.5  1.5  1.5  1.5]
  [ 2.5  2.5  2.5  2.5  2.5  2.5]]

 [[-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]
  [ 0.5  0.5  0.5  0.5  0.5  0.5]
  [ 1.5  1.5  1.5  1.5  1.5  1.5]
  [ 2.5  2.5  2.5  2.5  2.5  2.5]]

 [[-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]
  [ 0.5  0.5  0.5  0.5  0.5  0.5]
  [ 1.5  1.5  1.5  1.5  1.5  1.5]
  [ 2.5  2.5  2.5  2.5  2.5  2.5]]

 [[-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]
  [ 0.5  0.5  0.5  0.5  0.5  0.5]
  [ 1.5  1.5  1.5  1.5  1.5  1.5]
  [ 2.5  2.5  2.5  2.5  2.5  2.5]]]
ymin
[[[-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]
  [-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]
  [-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]
  [-0.5 -0.5 -0.5 -0.5 -0.5 -0.5]]

 [[ 0.5  0.5  0.5  0.5  0.5  0.5]
  [ 0.5  0.5  0.5  0.5  0.5  0.5]
  [ 0.5  0.5  0.5  0.5  0.5  0.5]
  [ 0.5  0.5  0.5  0.5  0.5  0.5]]

 [[ 1.5  1.5  1.5  1.5  1.5  1.5]
  [ 1.5  1.5  1.5  1.5  1.5  1.5]
  [ 1.5  1.5  1.5  1.5  1.5  1.5]
  [ 1.5  1.5  1.5  1.5  1.5  1.5]]

 [[ 2.5  2.5  2.5  2.5  2.5  2.5]
  [ 2.5  2.5  2.5  2.5  2.5  2.5]
  [ 2.5  2.5  2.5  2.5  2.5  2.5]
  [ 2.5  2.5  2.5  2.5  2.5  2.5]]]
(4, 4, 6)
'''