2017-04-17 52 views
0

我試圖在MNIST數據集上運行3DCNN。我的代碼像這樣拋出一個ValueError:重塑MNIST數據集上的3DCNN的目標數組大小 - ValueError

ValueError:輸入數組應該具有與目標數組相同數量的樣本。發現15000個輸入樣本和60000個目標樣本。

這裏是我的代碼:

import tensorflow 
import keras 
from keras.datasets import mnist 
from keras.models import Sequential 
from keras.layers import Dense, Dropout, Flatten 
from keras.layers import Conv3D, MaxPooling3D 
from keras import backend as K 

batch_size = 128 
num_classes = 10 
epochs = 12 

img_rows, img_cols, img_dep = 28, 28, 4 

(x_train, y_train), (x_test, y_test) = mnist.load_data() 

print y_train.shape[0] 

if K.image_data_format() == 'channels_first': 
    x_train = x_train.reshape(15000, 1, img_dep, img_rows, img_cols) 
    x_test = x_test.reshape(2500, 1, img_dep, img_rows, img_cols) 
    input_shape = (1, img_dep, img_rows, img_cols) 
else: 
    x_train = x_train.reshape(15000, img_dep, img_rows, img_cols, 1) 
    x_test = x_test.reshape(2500, img_dep, img_rows, img_cols, 1) 
    input_shape = (img_dep, img_rows, img_cols, 1) 

x_train = x_train.astype('float32') 
x_test = x_test.astype('float32') 
x_train /= 255 
x_test /= 255 
print('x_train shape:', x_train.shape) 
print(x_train.shape[0], 'train samples') 
print(x_test.shape[0], 'test samples') 

y_train.reshape(15000, img_dep) 
y_test.reshape(2500, img_dep) 

print('x_train shape', x_train.shape) 
print('x_test shape', x_test.shape) 
print('y_train shape', y_train.shape) 
print('y_test shape', y_test.shape) 

y_train = keras.utils.to_categorical(y_train, num_classes) 
y_test = keras.utils.to_categorical(y_test, num_classes) 

model = Sequential() 
model.add(Conv3D(32, kernel_size=(3, 3, 3), 
     activation='relu', 
     input_shape=input_shape)) 
model.add(Conv3D(64, (2, 2, 2), activation='relu')) 
#model.add(MaxPooling3D(pool_size=(2, 2, 2))) 
model.add(Dropout(0.25)) 
model.add(Flatten()) 
model.add(Dense(128, activation='relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(num_classes, activation='softmax')) 

model.compile(loss=keras.losses.categorical_crossentropy, 
      optimizer=keras.optimizers.Adadelta(), 
      metrics=['accuracy']) 

model.fit(x_train, y_train, 
     batch_size=batch_size, 
     epochs=epochs, 
     verbose=1, 
     validation_data=(x_test, y_test)) 

score = model.evaluate(x_test, y_test, verbose=0) 
print('Test loss:', score[0]) 
print('Test accuracy:', score[1]) 

任何想法,我要去哪裏錯了嗎?我對ML很陌生,因此這可能是一個簡單的錯誤,只需一個簡單的修復。我在網上找不到任何人使用MNIST數據運行3DCNN。謝謝!

的回溯信息是:

Traceback (most recent call last): 
    File "3dconvnet.py", line 67, in <module> 
    validation_data=(x_test, y_test)) 
    File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/models.py", line 853, in fit 
    initial_epoch=initial_epoch) 
    File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1406, in fit 
    batch_size=batch_size) 
    File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1308, in _standardize_user_data 
    _check_array_lengths(x, y, sample_weights) 
    File "/home/jackson/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 229, in _check_array_lengths 
    'and ' + str(list(set_y)[0]) + ' target samples.') 
ValueError: Input arrays should have the same number of samples as target arrays. Found 15000 input samples and 60000 target samples. 

印刷形狀的輸出是:

('x_train shape', (15000, 4, 28, 28, 1)) 
('x_test shape', (2500, 4, 28, 28, 1)) 
('y_train shape', (60000,)) 
('y_test shape', (10000,)) 

編輯* 1:加入回溯

編輯** 2:加入打印輸出形狀

+0

論哪一行你有錯誤信息? Post full tr​​aceback – kvorobiev

+0

剛剛發佈Traceback –

+0

在完成__all__整形並將其添加到問題後,打印出x_train,y_train,x_test,y_test的形狀。 – putonspectacles

回答

0
(x_train, y_train), (x_test, y_test) = mnist.load_data() 
x_train.shape 
Out[3]: (60000, 28, 28) 

y_train.shape 
Out[4]: (60000,) 

MNIST數據集由60000個28x28圖像和一個(不是四個)通道組成。代替這個代碼片段的

if K.image_data_format() == 'channels_first': 
    x_train = x_train.reshape(15000, 1, img_dep, img_rows, img_cols) 
    x_test = x_test.reshape(2500, 1, img_dep, img_rows, img_cols) 
    input_shape = (1, img_dep, img_rows, img_cols) 
else: 
    x_train = x_train.reshape(15000, img_dep, img_rows, img_cols, 1) 
    x_test = x_test.reshape(2500, img_dep, img_rows, img_cols, 1) 
    input_shape = (img_dep, img_rows, img_cols, 1) 

需要

if K.image_data_format() == 'channels_first': 
    x_train = x_train.reshape(-1, 1, img_rows, img_cols) 
    x_test = x_test.reshape(-1, 1, img_rows, img_cols) 
    input_shape = (1, img_rows, img_cols) 
else: 
    x_train = x_train.reshape(-1, img_rows, img_cols, 1) 
    x_test = x_test.reshape(-1, img_rows, img_cols, 1) 
    input_shape = (img_rows, img_cols, 1) 

因爲圖像深度(顏色通道的計數)= 1。此外,你並不需要

y_train.reshape(15000, img_dep) 
y_test.reshape(2500, img_dep) 
+0

但是,我可以運行一個3D ConvNet嗎?我以爲你需要一個5D張量來做到這一點? –

+0

我需要的輸入尺寸是5,而不是4使用Conv3D。 MNIST數據甚至可能嗎? –