2016-01-13 36 views
0

我有一個3通道5×5的圖像是這樣的:Theano中的卷積是否旋轉濾波器?

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 

和3-通道3×3過濾器是這樣的:

10 20 30 0.1 0.2 0.3 1 2 3 
40 50 60 0.4 0.5 0.6 4 5 6 
70 80 90 0.7 0.8 0.9 7 8 9 

當卷積與圖像過濾器,我期待這樣的輸出:

369.6 514.8 316.8 
435.6 594. 356.4 
211.2 277.2 158.4 

然而,Theano(使用keras)給了我這樣的輸出:

158.4 277.2 211.2 
356.4 594. 435.6 
316.8 514.8 369.6 

看來輸出旋轉180度,我不知道爲什麼會發生這種情況,我怎麼才能得到正確的答案。這裏是我的測試代碼:

def SimpleNet(weight_array,biases_array): 
    model = Sequential() 
    model.add(ZeroPadding2D(padding=(1,1),input_shape=(3,5,5))) 
    model.add(Convolution2D(1, 3, 3, weights=[weight_array,biases_array],border_mode='valid',subsample=(2,2))) 

    return model 
im = np.asarray([ 
     1,1,1,1,1, 
     1,1,1,1,1, 
     1,1,1,1,1, 
     1,1,1,1,1, 
     1,1,1,1,1, 
     2,2,2,2,2, 
     2,2,2,2,2, 
     2,2,2,2,2, 
     2,2,2,2,2, 
     2,2,2,2,2, 
     3,3,3,3,3, 
     3,3,3,3,3, 
     3,3,3,3,3, 
     3,3,3,3,3, 
     3,3,3,3,3]) 

weight_array = np.asarray([ 
       10,20,30, 
       40,50,60, 
       70,80,90, 
       0.1,0.2,0.3, 
       0.4,0.5,0.6, 
       0.7,0.8,0.9, 
       1,2,3, 
       4,5,6, 
       7,8,9]) 

im = np.reshape(im,[1,3,5,5]) 
weight_array = np.reshape(weight_array,[1,3,3,3]) 
biases_array = np.zeros(1) 

model = SimpleNet(weight_array,biases_array) 

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) 
model.compile(optimizer=sgd, loss='categorical_crossentropy') 
out = model.predict(im) 
print out.shape 
print out 

回答

1

這是卷積的定義。它的優點是,如果您將僅包含零的圖像進行卷積,卷積將在該位置放置一個過濾器的副本。

Theano完成這些卷積,如數學定義。這意味着在使用圖像補丁獲取點產品之前翻轉過濾器(操作是filter[:, :, ::-1, ::-1])。請注意,這些不是180度旋轉,至少不是一般的旋轉。

看來你正在尋找的是互相關,它是在圖像的每個點上採用點產品與非翻轉版本的濾鏡。

另請參閱this answer其中theano.tensor.nnet.conv2d顯示與scipy對應物完全相同。

+0

謝謝。你對互相關是正確的,這正是我正在尋找的。我通過反轉權重來解決這個問題。例如,一個頻道中的一個權重被逆轉爲[90,80,70,60,50,40,30,20,10],這樣它就給我提供了互相關。 – Demonedge

相關問題