2017-04-18 65 views
0

我需要在具有65536行(代表每個圖像1個圖像),49列(7x7圖像)和二進制類(第50列)的數據集上建立CNN模型。 (7800行測試集)ValueError:CorrMM:不可能的輸出形狀

我參考使用mnist數據集執行CNN的示例,但我未能建立火車模型。

這裏是我的代碼:

from __future__ import print_function 
import keras 
from keras.models import Sequential 
from keras.layers import Dense, Dropout, Flatten 
from keras.layers import Conv2D, MaxPooling2D 
from keras.utils import np_utils 
from keras import backend as K 
K.set_image_dim_ordering('th') 
import pandas as pd 
import numpy as np 



# input image dimensions 
img_rows, img_cols = 7, 7 

# fix random seed for reproducibility 
seed = 7 
np.random.seed(seed) 

x_train = pd.read_csv('palm_3x3_test.csv') 
x_train.drop(['class'],axis=1,inplace=True) 

x_train = x_train.as_matrix().reshape(-1,7, 7) 

y_train = pd.read_csv('palm_3x3_test.csv') 
y_train = y_train[['class']] 


x_test = pd.read_csv('palm_3x3_data.csv') 
x_test.drop(['class'],axis=1,inplace=True) 
x_test = x_test.as_matrix().reshape(-1,7, 7) 

y_test = pd.read_csv('palm_3x3_data.csv') 
y_test = y_test[['class']] 


# reshape to be [samples][pixels][width][height] 
x_train_final = x_train.reshape(x_train.shape[0],1,7,7).astype('float32') 
x_test_final = x_test.reshape(x_test.shape[0],1,7, 7).astype('float32') 

print(x_train.shape) 
print(x_test.shape) 
print(x_train_final.shape) 
print(x_test_final.shape) 
print(y_train.shape) 
print(y_test.shape) 

# normalize inputs from 0-255 to 0-1 
x_train_final = x_train_final/255 
x_test_final = x_test_final/255 

# one hot encode outputs 
y_train = np_utils.to_categorical(y_train) 
y_test = np_utils.to_categorical(y_test) 
num_classes = y_test.shape[1] 

print (y_test.shape) 
print(y_train.shape) 

def baseline_model(): 
#  create model 

    model = Sequential() 
    model.add(Conv2D(30,(5,5), padding='valid', activation='relu',input_shape=(1,7,7))) 
    model.add(MaxPooling2D(pool_size=(2, 2))) 
    model.add(Conv2D(15 ,(3, 3), activation='relu')) 
    model.add(MaxPooling2D(pool_size=(2, 2))) 
    model.add(Dropout(0.2)) 
    model.add(Flatten()) 
    model.add(Dense(128, activation='relu')) 
    model.add(Dense(50, activation='relu')) 
    model.add(Dense(num_classes, activation='softmax')) 

    # Compile model 
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
    return model 

# build the model 
model = baseline_model() 
# Fit the model 
model.fit(x_train_final,y_train, validation_data=(x_test_final,y_test), nb_epoch=10, batch_size=200,verbose=2) 
# Final evaluation of the model 
scores = model.evaluate(x_test,y_test, verbose=0) 
print("CNN Error: %.2f%%" % (100-scores[1]*100)) 

我使用Theano和我的數據輸出形狀:

x_train.shape = (65536L, 7L, 7L) 
x_test.shape = (7800L, 7L, 7L) 
x_train_final.shape = (65536L, 1L, 7L, 7L) 
x_test_final.shape = (7800L, 1L, 7L, 7L) 
y_train.shape = (65536L, 2L) 
y_test.shape = (7800L, 2L) 

,而我運行它的錯誤提示:

ValueError: CorrMM: impossible output shape 
    bottom shape: 200 x 30 x 1 x 1 
    weights shape: 15 x 30 x 3 x 3 
    top shape: 200 x 15 x -1 x -1 

回答

1

您對第二個卷積圖層的輸入小於您希望應用的濾鏡。您的輸入圖像是(7,7);如果您使用valid填充與(5,5)篩選器,您將有一個篩選的圖像是(3,3)。應用最大池後,您將剩下一個(1,1)圖像,該圖像與您的輔助卷積圖層不兼容,該圖像的濾鏡尺寸爲(3,3)

編譯模型後,您可以運行model.summary()來查看每個圖層的輸出形狀。消極的形狀導致你看到的impossible output shape錯誤。在第一卷積層中改變padding='valid'padding='same'。這不會完全解決您的問題,您還需要更改第二個卷積圖層的填充以確保完全連接的圖層具有輸出。考慮到圖像的小尺寸,您可能需要重新考慮使用最大池。

This question是有效和相同填充之間差異的很好解釋。