【问题标题】:Reshaping Target Array size for 3DCNN on MNIST dataset - ValueError在 MNIST 数据集上重塑 3DCNN 的目标数组大小 - ValueError
【发布时间】:2018-09-25 16:45:19
【问题描述】:

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

【问题讨论】:

  • 您在哪一行收到错误消息?发布完整的回溯
  • 刚刚发布了 Traceback
  • all 重塑完成后打印出 x_train、y_train、x_test、y_test 的形状并将其添加到问题中。
  • 刚刚添加了形状的打印输出
  • 这个问题解决了吗?

标签: python machine-learning keras mnist


【解决方案1】:
(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)

【讨论】:

  • 但这能让我在上面运行 3D ConvNet 吗?我认为你需要一个 5D 张量来做到这一点?
  • 我需要输入的尺寸为 5,而不是 4 才能使用 Conv3D。甚至可以使用 MNIST 数据吗?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2022-01-08
    • 2018-05-22
    • 2020-12-11
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 2020-02-26
    • 2021-05-08
    相关资源
    最近更新 更多