2017-02-11 42 views
2

我讀了Xception論文(甚至有描述NN的Keras Model),它討論了可分離的卷積。 我想了解他們如何計算完全是。我沒有把它放在不精確的單詞上,而是包含了下面這段代碼,它總結了我的理解。從特徵映射18x18x728到18x18x1024之一的代碼映射:如何計算Xception紙張中的可分卷積?

XSIZE = 18; 
YSIZE = 18; 
ZSIZE = 728; 
ZSIXE2 = 1024; 

float mapin[XSIZE][YSIZE][ZSIZE]; // Input map 
float imap[XSIZE][YSIZE][ZSIZE2]; // Intermediate map 
float mapout[XSIZE][YSIZE][ZSIZE2]; // Output map 

float wz[ZSIZE][ZSIZE2]; // Weights for 1x1 convs 
float wxy[3][3][ZSIZE2]; // Weights for 3x3 convs 

// Apply 1x1 convs 
for(y=0;y<YSIZE;y++) 
    for(x=0;x<XSIZE;x++) 
    for(o=0;o<ZSIZE2;o++){ 
     s=0.0; 
     for(z=0;z<ZSIZE;z++) 
     s+=mapin[x][y][z]*wz[z][o]; 
     imap[x][y][o]=s; 
    } 

// Apply 2D 3x3 convs 
for(o=0;o<ZSIZE2;o++) 
    for(y=0y<YSIZE;y++) 
    for(x=0;x<XSIZE;x++){ 
     s=0.0; 
     for(i=-1;i<2;i++) 
     for(j=-1;j<2;j++) 
      s+=imap[x+j][y+i][o]*wxy[j+1][i+1][o]; // This value is 0 if falls off the edge 
     mapout[x][y][o]=s; 
    } 

這是正確的嗎?如果不是的話,你能否建議類似地用C或僞C語言編寫的修復程序?

非常感謝你提前。

回答

0

我在Tensorflow中發現了tf.nn.separable_conv2d,它確實如此。所以我創建了一個非常簡單的圖表,並且在隨機數的幫助下,我試圖獲得上面的代碼以匹配結果。正確的代碼是:

XSIZE = 18; 
YSIZE = 18; 
ZSIZE = 728; 
ZSIXE2 = 1024; 

float mapin[XSIZE][YSIZE][ZSIZE]; // Input map 
float imap[XSIZE][YSIZE][ZSIZE]; // Intermediate map 
float mapout[XSIZE][YSIZE][ZSIZE2]; // Output map 

float wxy[3][3][ZSIZE]; // Weights for 3x3 convs 
float wz[ZSIZE][ZSIZE2]; // Weights for 1x1 convs 


// Apply 2D 3x3 convs 
for(o=0;o<ZSIZE;o++) 
    for(y=0y<YSIZE;y++) 
    for(x=0;x<XSIZE;x++){ 
     s=0.0; 
     for(i=-1;i<2;i++) 
     for(j=-1;j<2;j++) 
      s+=mapin[x+j][y+i][o]*wxy[j+1][i+1][o]; // This value is 0 if falls off the edge 
     imap[x][y][o]=s; 
    } 

// Apply 1x1 convs 
for(y=0;y<YSIZE;y++) 
    for(x=0;x<XSIZE;x++) 
    for(o=0;o<ZSIZE2;o++){ 
     s=0.0; 
     for(z=0;z<ZSIZE;z++) 
     s+=imap[x][y][z]*wz[z][o]; 
     mapout[x][y][o]=s; 
    } 

主要區別在於執行兩組卷積的順序。 令我驚訝的是,即使在ZSIZE == ZSIZE2時,順序也很重要。