简体   繁体   English

Keras转换神经网络问题:ValueError:检查输入时出错:预期conv2d_1_input具有4维

[英]Keras conv-neural-network issue: ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions

I'm new to Keras and try to build a convolutional neural network. 我是Keras的新手,并尝试构建卷积神经网络。 For the preprocessing I took 32 patches of each picture in my sample (batch size) with a size of 200 x 200 (patch size). 对于预处理,我在样本中(批次大小)对每个图片取了32个补丁,大小为200 x 200(补丁大小)。 I generated numpy arrays of it and saved them. 我生成了它的numpy数组并保存了它们。 I try to feed the model with these batches. 我尝试用这些批次来填充模型。

pg = PatchGenerator(image_folder, list_path, sheet_name, patch_size, 
batch_size, split_distribution)

# Load batches and labels
X_train = []
Y_train = []
c = 0
for x in range(2):
     X_train.append(np.load(train_batch_load_path + train_batch_name + 
     str(c+1) + '.npy'))
     Y_train.append(pg.hd_train_list[c])
     c = c + 1
X_train = np.array(X_train)

print(X_train.shape)  # shape: (2, 32, 200, 200, 3)

# Model
model.add(Convolution2D(filters=16, kernel_size=3, strides=(1, 1), 
bias_initializer='zeros', padding='same', data_format='channels_last', 
activation=None, batch_size=32, input_shape=(patch_size, patch_size, 3))) 

print(model.output_shape) # shape: (32, 200, 200, 16)

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),  padding='same', 
data_format='channels_last'))

print(model.output_shape) #shape: (32, 100, 100, 16)

model.add(Convolution2D(filters=32, kernel_size=3, strides=(2, 2), 
bias_initializer='zeros', padding='same', data_format='channels_last', 
activation='relu'))

print(model.output_shape) # shape: (32, 50, 50, 32)

model.add(AveragePooling2D(pool_size=(2, 2), strides=(5, 5),  
padding='same', data_format='channels_last'))

print(model.output_shape) # shape: (32, 10, 10, 32)

model.add(Convolution2D(filters=16, kernel_size=5, strides=(1, 1), 
bias_initializer='zeros', padding='valid', data_format='channels_last', 
activation='relu'))

print(model.output_shape) # shape: (32, 6, 6, 16)

model.add(MaxPooling2D(pool_size=(4, 4), strides=(1, 1),  padding='valid', 
data_format='channels_last'))

print(model.output_shape) # shape: (32, 3, 3, 16)

model.add(Convolution2D(filters=1, kernel_size=3, strides=(1, 1), 
bias_initializer='zeros', padding='valid', data_format='channels_last', 
activation='relu'))

print(model.output_shape) # shape: (32, 1, 1, 1)

model.add(Flatten())

print(model.output_shape) # shape: (32, 1)

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

model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1)

When I try to run the code I got the following error: 当我尝试运行代码时,出现以下错误:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (2, 32, 200, 200, 3) ValueError:检查输入时出错:预期conv2d_1_input具有4个维,但数组的形状为(2,32,200,200,3)

I know their are more then 4 dimensons, but when I do the same with only one batch that has a shape with 4 dimensons I got a error that I have to too little dimensions. 我知道它们的数量多于4个Dimenson,但是当我只对形状为4个Dimenson的一批批次执行相同操作时,我得到一个错误,即我的尺寸太小。 I think the output shapes are the right ones I need to run the model. 我认为输出形状是运行模型所需的正确形状。

Hope you can help me. 希望您能够帮助我。

No, your data should be 4D as the error mentions. 不,错误所提到的,您的数据应为4D。 You gave input shape equals to (200, 200, 3), meaning that the shape for input X should be (samples, 200, 200, 3). 您给定的输入形状等于(200,200,3),这意味着输入X的形状应为(samples,200,200,3)。 Samples can be any number, but the other two dimensions are fixed. 样本可以是任意数量,但其他两个维度是固定的。

If you got a different error when using a 4D array, you should also include it in your question. 如果在使用4D阵列时遇到其他错误,则还应将其包括在问题中。

暂无
暂无

声明:本站的技术帖子网页,遵循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 有 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 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) 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 有 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 具有形状 (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具有形状(28,28,1),但数组的形状为(58000,28,28) - ValueError: Error when checking : expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (58000, 28, 28)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM