2017-06-04 10 views
0

tensorflowの関数はtf.space_to_depthとなります。 Tensorflowのソースコードでこの関数を実装するのはとても難しいです。 numpyを使用して実装するのを手伝ってください。numpyでtf.space_to_depthを実装する方法は?

この機能の仕組みを視覚化するためのコードです。ところで、すべての前に、テンソルフロー関数への入力は入力形状でなければならないことは言うまでもない。[batch, height, width, depth]

このコードを前提とする。まず、テンソルを定義する必要があります。ここでは

norm = tf.reshape(tf.range(0,72),(1,6,6,2)) 

は、深さ1(norm[0,:,:,0])の値である:ここで

[[ 0, 2, 4, 6, 8, 10], 
[12, 14, 16, 18, 20, 22], 
[24, 26, 28, 30, 32, 34], 
[36, 38, 40, 42, 44, 46], 
[48, 50, 52, 54, 56, 58], 
[60, 62, 64, 66, 68, 70]] 

は、深さ2(norm[0,:,:,1])の値である:

[[ 1, 3, 5, 7, 9, 11], 
[13, 15, 17, 19, 21, 23], 
[25, 27, 29, 31, 33, 35], 
[37, 39, 41, 43, 45, 47], 
[49, 51, 53, 55, 57, 59], 
[61, 63, 65, 67, 69, 71]] 

次のステップでは、tf.space_to_depth関数を適用したいと思います。

(1,3,3,8)と、ここで、この関数の出力です::210
trans = tf.space_to_depth(norm,2) 

出力形状がある

trans[0,:,:,0] 
[[ 0, 4, 8], 
[24, 28, 32], 
[48, 52, 56]] 
trans[0,:,:,1] 
[[ 1, 5, 9], 
[25, 29, 33], 
[49, 53, 57]] 
trans[0,:,:,2] 
[[ 2, 6, 10], 
[26, 30, 34], 
[50, 54, 58]] 
trans[0,:,:,3] 
[[ 3, 7, 11], 
[27, 31, 35], 
[51, 55, 59]] 
trans[0,:,:,4] 
[[12, 16, 20], 
[36, 40, 44], 
[60, 64, 68]] 
trans[0,:,:,5] 
[[13, 17, 21], 
[37, 41, 45], 
[61, 65, 69]] 
trans[0,:,:,6] 
[[14, 18, 22], 
[38, 42, 46], 
[62, 66, 70]] 
trans[0,:,:,7] 
[[15, 19, 23], 
[39, 43, 47], 
[63, 67, 71]] 

誰かが私を助けるかもしれないどのように私はnumpyの中で、この関数のベクトル化バージョンを実装することができます?

応答を事前に評価してください!

答えて

2

あなたはreshape()swapaxes()機能への適切な呼び出しでspace_to_depthを実装することができます

ここ
import numpy as np 

def space_to_depth(x, block_size): 
    x = np.asarray(x) 
    batch, height, width, depth = x.shape 
    reduced_height = height // block_size 
    reduced_width = width // block_size 
    y = x.reshape(batch, reduced_height, block_size, 
         reduced_width, block_size, depth) 
    z = np.swapaxes(y, 2, 3).reshape(batch, reduced_height, reduced_width, -1) 
    return z 

the documentation of tf.space_to_depthからの例は以下のとおりです。

In [328]: x = [[[[1], [2]], 
    ...:  [[3], [4]]]] 
    ...: 

In [329]: space_to_depth(x, 2) 
Out[329]: array([[[[1, 2, 3, 4]]]]) 

In [330]: x = [[[[1, 2, 3], [4, 5, 6]], 
    ...:  [[7, 8, 9], [10, 11, 12]]]] 
    ...: 

In [331]: space_to_depth(x, 2) 
Out[331]: array([[[[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]]]) 

In [332]: x = [[[[1], [2], [5], [6]], 
    ...:  [[3], [4], [7], [8]], 
    ...:  [[9], [10], [13], [14]], 
    ...:  [[11], [12], [15], [16]]]] 
    ...: 

In [333]: space_to_depth(x, 2) 
Out[333]: 
array([[[[ 1, 2, 3, 4], 
     [ 5, 6, 7, 8]], 

     [[ 9, 10, 11, 12], 
     [13, 14, 15, 16]]]]) 

そして、ここではあなたの例である:

In [334]: norm = np.arange(72).reshape(1, 6, 6, 2) 

In [335]: trans = space_to_depth(norm, 2) 

In [336]: trans[0, :, :, 0] 
Out[336]: 
array([[ 0, 4, 8], 
     [24, 28, 32], 
     [48, 52, 56]]) 

In [337]: trans[0, :, :, 1] 
Out[337]: 
array([[ 1, 5, 9], 
     [25, 29, 33], 
     [49, 53, 57]]) 

In [338]: trans[0, :, :, 7] 
Out[338]: 
array([[15, 19, 23], 
     [39, 43, 47], 
     [63, 67, 71]]) 
+0

お返事ありがとうございます!私はnumpyの小さな初心者で、そのような機能をどのように実装するかについて考えていません。 –

+0

もう一つ重要なのは、入力の形状を[バッチ、深度、高さ、幅]に変更すると、上記のコードをどのように変更して同じ結果が得られるのかを教えてください。 @ウォーレン・ヴェッカーセラー –

関連する問題