简体   繁体   English

Python 神经网络 - 检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到形状为 (700, 128, 33) 的数组

[英]Python Neural Networks - Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (700, 128, 33)

So I am working on a "music genre classification" project and I am working with the GTZAN dataset to create a simple CNN.network to classify the genre for an audio file.所以我正在研究一个“音乐流派分类”项目,我正在使用 GTZAN 数据集创建一个简单的 CNN.network 来对音频文件的流派进行分类。

My code for the model training, validation and testing is below:我的 model 训练、验证和测试代码如下:

input_shape = (genre_features.train_X.shape[1], genre_features.train_X.shape[2],1)
print("Build CNN model ...")
model = Sequential()

model.add(Conv2D(24, (5, 5), strides=(1, 1), input_shape=input_shape))
model.add(AveragePooling2D((2, 2), strides=(2,2)))
model.add(Activation('relu'))

model.add(Conv2D(48, (5, 5), padding="same"))
model.add(AveragePooling2D((2, 2), strides=(2,2)))
model.add(Activation('relu'))

model.add(Conv2D(48, (5, 5), padding="same"))
model.add(AveragePooling2D((2, 2), strides=(2,2)))
model.add(Activation('relu'))

model.add(Flatten())
model.add(Dropout(rate=0.5))

model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(rate=0.5))

model.add(Dense(10))
model.add(Activation('softmax'))
print("Compiling ...")
opt = Adam()
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
model.summary()

print("Training ...")
batch_size = 35  # num of training examples per minibatch
num_epochs = 400
model.fit(
    genre_features.train_X,
    genre_features.train_Y,
    batch_size=batch_size,
    epochs=num_epochs
)

print("\nValidating ...")
score, accuracy = model.evaluate(
    genre_features.dev_X, genre_features.dev_Y, batch_size=batch_size, verbose=1
)
print("Dev loss:  ", score)
print("Dev accuracy:  ", accuracy)


print("\nTesting ...")
score, accuracy = model.evaluate(
    genre_features.test_X, genre_features.test_Y, batch_size=batch_size, verbose=1
)
print("Test loss:  ", score)
print("Test accuracy:  ", accuracy)

# Creates a HDF5 file 'lstm_genre_classifier.h5'
model_filename = "lstm_genre_classifier_lstm.h5"
print("\nSaving model: " + model_filename)
model.save(model_filename)

And when I try to train the file I get the following Error ( I also printed the Train, Validation and Test Shape before compiling model)当我尝试训练文件时出现以下错误(我还在编译模型之前打印了训练、验证和测试形状)

Training X shape: (700, 128, 33)
Training Y shape: (700, 10)
Dev X shape: (200, 128, 33)
Dev Y shape: (200, 10)
Test X shape: (100, 128, 33)
Test Y shape: (100, 10)
Build CNN model ...
2020-12-25 15:46:58.410663: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Compiling ...
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d_1 (Conv2D)            (None, 124, 29, 24)       624
_________________________________________________________________
average_pooling2d_1 (Average (None, 62, 14, 24)        0
_________________________________________________________________
activation_1 (Activation)    (None, 62, 14, 24)        0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 62, 14, 48)        28848
_________________________________________________________________
average_pooling2d_2 (Average (None, 31, 7, 48)         0
_________________________________________________________________
activation_2 (Activation)    (None, 31, 7, 48)         0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 31, 7, 48)         57648
_________________________________________________________________
average_pooling2d_3 (Average (None, 15, 3, 48)         0
_________________________________________________________________
activation_3 (Activation)    (None, 15, 3, 48)         0
_________________________________________________________________
flatten_1 (Flatten)          (None, 2160)              0
_________________________________________________________________
dropout_1 (Dropout)          (None, 2160)              0
_________________________________________________________________
dense_1 (Dense)              (None, 64)                138304
_________________________________________________________________
activation_4 (Activation)    (None, 64)                0
_________________________________________________________________
dropout_2 (Dropout)          (None, 64)                0
_________________________________________________________________
dense_2 (Dense)              (None, 10)                650
_________________________________________________________________
activation_5 (Activation)    (None, 10)                0
=================================================================
Total params: 226,074
Trainable params: 226,074
Non-trainable params: 0
_________________________________________________________________
Training ...
Traceback (most recent call last):
  File "cnn.py", line 82, in <module>
    epochs=400
  File "C:\Users\Bharat.000\miniconda3\lib\site-packages\keras\engine\training.py", line 1154, in fit
    batch_size=batch_size)
  File "C:\Users\Bharat.000\miniconda3\lib\site-packages\keras\engine\training.py", line 579, in _standardize_user_data
    exception_prefix='input')
  File "C:\Users\Bharat.000\miniconda3\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 conv2d_1_input to have 4 dimensions, but got array with shape (700, 128, 33)

I tried few solutions from some similar questions, but I could not understood much since I am new to this topic.我尝试了一些类似问题的解决方案,但由于我是这个主题的新手,所以我不太了解。 Any help apprecitated about what do I change to get proper output.任何关于我要更改什么以获得正确的 output 的帮助表示赞赏。

Your input dimension is wrong.你输入的尺寸是错误的。 Are you sure your data is 2D (like images) and not 1D (like sound waves)?你确定你的数据是二维的(比如图像)而不是一维的(比如声波)吗? If your data is 1D then you should be doing 1 dimensional convolutions.如果您的数据是一维的,那么您应该进行一维卷积。 The reason why an error occurs is because your train data has the shape (700 (how many datapoints), 128, 33).出现错误的原因是因为你的火车数据具有形状(700(多少个数据点),128, 33)。 In Conv2D in keras you need to have (batch size, image_height, image_width, channels) -- channels could be first or last but its not really relevant.在 keras 的 Conv2D 中,您需要具有(批量大小、图像高度、图像宽度、通道)——通道可以是第一个或最后一个,但它并不真正相关。 What I am trying to say is that instead of the (image_height, image_width) tuple required by 2Dconv you only provide the number 128. Maybe what you're looking for is 1 Dimensional conv.我想说的是,您只提供数字 128,而不是 2Dconv 所需的 (image_height, image_width) 元组。也许您正在寻找的是 1 Dimensional conv。

暂无
暂无

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

相关问题 ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的形状为数组(8020,1) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1) ValueError:检查输入时出错:预期 conv2d_1_input 具有形状 (128, 75, 1) 但得到形状为 (1, 128, 1) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have shape (128, 75, 1) but got array with shape (1, 128, 1) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (595、10083) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但在与 model 拟合时得到形状为 (999, 12, 1) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (999, 12, 1) while fitting with model 维度问题:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (26, 26, 1) 的数组 - Dimension problems: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (26, 26, 1) ValueError:检查输入时出错:预期conv2d_1_input具有4维,但数组的形状为(454,512,512) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (454, 512, 512) 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) ValueError:检查输入时出错:预期 conv2d_1_input 的形状为 (224, 224, 1) 但得到的数组的形状为 (224, 224, 8) - ValueError: Error when checking input: expected conv2d_1_input to have shape (224, 224, 1) but got array with shape (224, 224, 8) 检查输入时出错:预期conv2d_1_input具有形状(64、64、3),但数组具有形状(64、64、4) - Error when checking input: expected conv2d_1_input to have shape (64, 64, 3) but got array with shape (64, 64, 4) 检查输入时出错:预期conv2d_1_input具有形状(3、32、32),但数组的形状为(32、32、3) - Error when checking input: expected conv2d_1_input to have shape (3, 32, 32) but got array with shape (32, 32, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM