在MNIST数据集上重塑3DCNN的目标阵列大小 - ValueError

时间:2017-04-17 16:27:43

标签: python machine-learning keras mnist

我正在尝试在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:添加打印形状的输出

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

据我所知3d CNN用于体积数据或时间图像数据。 要在mnist数据集中使用,您需要以某种方式将其转换为5d张量。您的主要错误是x火车的大小为25000,y火车的大小为60000。 这就是问题。希望它能解决。