简体   繁体   English

ValueError:检查输入时出错:预期 input_1 有 5 个维度,但得到了形状为 (10000, 32, 3, 32) 的数组

[英]ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (10000, 32, 3, 32)

I am trying to train a 3d cnn network on Cifar 10 dataset, but I got the following error:我正在尝试在 Cifar 10 数据集上训练 3d cnn 网络,但出现以下错误:

Traceback (most recent call last):
  File "kI3DV2s.py", line 286, in <module>
    callbacks=[])
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_generator.py", line 150, in fit_generator
    val_x, val_y, val_sample_weight)
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 579, in _standardize_user_data
    exception_prefix='input')
  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_utils.py", line 135, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (10000, 32, 3, 32)

I think the issue has to do with the input shape and the x_train dimensions.我认为问题与输入形状和 x_train 尺寸有关。

The relevant part of the code is as follows:代码的相关部分如下:

nb_classes = 10

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# reorder dimensions for tensorflow
x_train = np.transpose(x_train.astype('float32') / 255., (0, 2, 3, 1))
x_test = np.transpose(x_test.astype('float32') / 255., (0, 2, 3, 1))
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)


img_rows, img_cols, img_rc = 32, 32, 32
img_channels = 3


inputs = Input(shape=(img_rows, img_cols, img_rc, img_channels))

# 3d cnn model
...
...
...

model = Model(input=inputs, output=predictions)

model.summary()

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

batch_size = 128
nb_epoch = 10
data_augmentation = True

# Model saving callback
#checkpointer = ModelCheckpoint(filepath='stochastic_depth_cifar10.hdf5', verbose=1, save_best_only=True)

if not data_augmentation:
    print('Not using data augmentation.')
    history = model.fit(x_train, y_train, 
                        batch_size=batch_size, nb_epoch=nb_epoch, verbose=1,
                        validation_data=(x_test, y_test), shuffle=True,
                        callbacks=[])
else:
    print('Using real-time data augmentation.')

    # realtime data augmentation
    datagen_train = ImageDataGenerator(
        featurewise_center=False,
        samplewise_center=False,
        featurewise_std_normalization=False,
        samplewise_std_normalization=False,
        zca_whitening=False,
        rotation_range=0,
        width_shift_range=0.125,
        height_shift_range=0.125,
        horizontal_flip=True,
        vertical_flip=False)
    datagen_train.fit(x_train)

    # fit the model on the batches generated by datagen.flow()
    history = model.fit_generator(datagen_train.flow(x_train, y_train, batch_size=batch_size, shuffle=True),
                                  samples_per_epoch=x_train.shape[0], 
                                  nb_epoch=nb_epoch, verbose=1,
                                  validation_data=(x_test, y_test),
                                  callbacks=[])

Can anyone please tell me what I'm doing wrong and how can I get the dimensions right.任何人都可以告诉我我做错了什么,我怎样才能得到正确的尺寸。 Thank you.谢谢你。

Note:笔记:

x_train shape: (50000, 32, 3, 32) 
50000 train samples 
10000 test samples
Window 10
Python 3.7.6
Tensorflow-gpu==1.14
Keras==2.3.1

I was able to reshape it using the following code and testing it on 3d mnst dataset instead.我能够使用以下代码重塑它并在 3d mnst 数据集上对其进行测试。

xtrain = np.ndarray((x_train.shape[0], 4096, 3))
xtest = np.ndarray((x_test.shape[0], 4096, 3))

## iterate in train and test, add the rgb dimension 
def add_rgb_dimension(array):
    scaler_map = cm.ScalarMappable(cmap="Oranges")
    array = scaler_map.to_rgba(array)[:, : -1]
    return array
for i in range(x_train.shape[0]):
    xtrain[i] = add_rgb_dimention(x_train[i])
for i in range(x_test.shape[0]):
    xtest[i] = add_rgb_dimention(x_test[i])

xtrain = xtrain.reshape(x_train.shape[0], 16, 16, 16, 3)
xtest = xtest.reshape(x_test.shape[0], 16, 16, 16, 3)

## convert target variable into one-hot
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

y_train.shape

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“检查输入时出错:预期 input_1 有 2 个维度,但得到的数组形状为 (32, 168, 5)” - How to fix 'Error when checking input: expected input_1 to have 2 dimensions, but got array with shape (32, 168, 5)' ValueError:检查输入时出错:期望input_1有4个维度,但得到的数组有形状(6243,256,256) - ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (6243, 256, 256) ValueError:检查输入时出错:预期 input_1 有 5 个维度,但得到形状为 (1221, 50, 50, 1) 的数组 - ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (1221, 50, 50, 1) ValueError:检查输入时出错:预期 input_1 有 4 个维度,但得到了形状为(无、无、无)的数组 - ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (None, None, None) ValueError:检查输入时出错:预期 input_58 有 3 个维度,但得到了形状为 (10000, 10020) 的数组 - ValueError: Error when checking input: expected input_58 to have 3 dimensions, but got array with shape (10000, 10020) ValueError:检查输入时出错:预期 input_13 有 3 个维度,但得到了形状为 (50000, 32, 32, 3) 的数组 - ValueError: Error when checking input: expected input_13 to have 3 dimensions, but got array with shape (50000, 32, 32, 3) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (117, 1, 32, 32, 3) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (117, 1, 32, 32, 3) Keras LSTM输入-ValueError:检查输入时出错:预期input_1具有3维,但数组的形状为(1745,1) - Keras LSTM Input - ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (1745, 1) ValueError:检查输入时出错:预期 input_1 的形状为 (168, 5),但数组的形状为 (5808, 5) - ValueError: Error when checking input: expected input_1 to have shape (168, 5) but got array with shape (5808, 5) 检查输入时出错:预期 conv2d_4_input 有 4 个维度,但得到了形状为 (32, 32) 的数组 - Error when checking input: expected conv2d_4_input to have 4 dimensions, but got array with shape (32, 32)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM