anchor_target_layer 預備編程知識

由於我自己之前編程很少利用np,這個算一個補習吧。如果想要嘗試理解一個np函數什麼時候用,可以看anchor_target_layer解析裏面的應用。

 

---------------------  

1. np.where(x>4)

返回一個行向量,一個列向量。

所以大多後面加一個[0] np.where(x>4)[0]

---------------------  

2.np.meshgrid

類似於座標軸一樣的感覺,所以rpn生成anchor平移的時候,就可以用到座標軸,也就是np.meshgrid。

z,s=np.meshgrid(x,y)  z就是x軸,z的每一列就是x軸的平移。s類似

x=np.array([-2,1,0,1])  4
y=np.array([0,1,2])   3
z,s=np.meshgrid(x,y)   3*4
              列數,行數
>>> z=array([[-2,  1,  0,  1],
             [-2,  1,  0,  1],
             [-2,  1,  0,  1]])
>>> s= array([[0, 0, 0, 0],
              [1, 1, 1, 1],
              [2, 2, 2, 2]])

meshgrid is very useful to evaluate functions on a grid.

>>> x = np.arange(-5, 5, 0.1) 
>>> y = np.arange(-5, 5, 0.1) 
>>> xx, yy = np.meshgrid(x, y, sparse=True) 
>>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2) 
>>> h = plt.contourf(x,y,z)

---------------------  

3. np.ravel( ) vs np.flatten()

相同點都是返回一維度。

np.ravel( )  返回view(視圖),共享內存。之前的變了,他也變。更節省!

np.flatten() 返回copy,開闢內存。

--------------------- 

4. x.reshape( )

注意:返回的是view。

c = a.reshape(1,3)
>>> a = array([[1],[2],[3]])
>>> a[2] = 4
>>> a
array([[1],[2],[4]])
>>> c
array([[1, 2, 4]])

首先看幾個常用的 :

    x.reshape( -1) 變成維度爲1的數組。ndim = (n,)

    x.reshape( 1,-1) _>ndim = (1,n) ,-1 可以用到增加維度上。也可以將ndim = (n,)的數組,變成ndim = (1,n) or(n,1)的向量時。

--------------------- 

5. np.squeeze(a,axis=none) & tensor.squeeze()

首先np中沒有unsqueeze。 squeeze的作用,是把axis軸的維度壓縮,去掉,前提是axis的維度爲 1,如果不是就報錯。

a=array([[[1],[2],[3]]])
a.shape
>>>(1,3,1)
np.squeeze(a,axis=0)
>>>array([[1],[2],[3]])
np.squeeze(a,axis=1)
>>>報錯
np.squeeze(a,axis=2)
array([[1,2,3]])

但是在pytorch中,有unsqueeze,相當於增加維度。squeeze相當於壓縮維度。

tensor.squeeze(0)把第0個軸壓縮,如果維度是1則壓縮,不是不壓縮,也不報錯!

--------------------- 

6. np.view & tensor.view

np.view是一個視圖,但是在torch中你經常看到x = x.view(x.size(0), -1) 作用是改變維度[n,c,h,w]_>[n,c*h*w]

--------------------- 

7. np.argmax(a,axis = none) & torch.max(a,1)

np.argmax返回的索引 如果axis=1,二維表示行找最大

torch.max(a,1)[0]返回最大值

torch.max(a,1)[1]返回索引

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章