简体   繁体   English

ValueError:检查输入时出错:预期 conv2d_3_input 具有形状 (100, 100, 1) 但得到形状为 (100, 100, 3) 的数组

[英]ValueError: Error when checking input: expected conv2d_3_input to have shape (100, 100, 1) but got array with shape (100, 100, 3)

When I try to code a neural network for image recognition, I get the error:当我尝试编写用于图像识别的神经网络时,出现错误:

ValueError: Error when checking input: expected conv2d_3_input to have shape (100, 100, 1) but got array with shape (100, 100, 3). ValueError:检查输入时出错:预期 conv2d_3_input 具有形状 (100, 100, 1) 但得到形状为 (100, 100, 3) 的数组。

All my images are greyscale with size 100x100 pixels.我所有的图像都是greyscale ,大小为100x100像素。 Here the code:这里的代码:

 # Importing the Keras libraries and packages import tensorflow as tf from keras.models import Sequential from keras.layers import Conv2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense # Initialising the CNN classifier = Sequential() # Step 1 - Convolution classifier.add(Conv2D(32, (3, 3), input_shape = (100, 100, 1), activation = 'relu')) # Step 2 - Pooling classifier.add(MaxPooling2D(pool_size = (2, 2))) # Adding a second convolutional layer classifier.add(Conv2D(32, (3, 3), activation = 'relu')) classifier.add(MaxPooling2D(pool_size = (2, 2))) # Step 3 - Flattening classifier.add(Flatten()) # Step 4 - Full connection classifier.add(Dense(units = 128, activation = 'relu')) classifier.add(Dense(units = 1, activation = 'sigmoid')) # Compiling the CNN classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) # Part 2 - Fitting the CNN to the images from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True) test_datagen = ImageDataGenerator(rescale = 1./255) training_set = train_datagen.flow_from_directory('E:/exercise/dataset/train', target_size = (100, 100), batch_size = 32, color_mode = "grayscale", class_mode = 'binary') test_set = test_datagen.flow_from_directory('E:/exercise/dataset/test', target_size = (100, 100), color_mode = "grayscale", batch_size = 32, class_mode = 'binary') classifier.fit_generator(training_set, steps_per_epoch = 40, epochs = 10, validation_data = test_set, validation_steps = 8) import numpy as np from keras.preprocessing import image test_image = image.load_img('E:/exercise/predict_2.jpg', target_size = (100, 100)) test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image, axis = 0) result = classifier.predict(test_image) training_set.class_indices if result[0][0] >= 0.5: prediction = 'happy' else: prediction = 'sad' print(prediction)

Anyone can show me how to fix this problem.任何人都可以告诉我如何解决这个问题。 Thank you all!谢谢你们!

Try adding the parameter preprocessing_function=gray_to_rgb to ImageDataGenerator for both train_datagen & test_datagen , as:尝试为train_datagentest_datagen添加参数preprocessing_function=gray_to_rgbImageDataGenerator ,如:

def rgb_to_gray(rgb):     # Using the luminosity formula: grayscale =  0.21 R + 0.72 G + 0.07 B
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
preprocessing_function=rgb_to_gray)
test_datagen = ImageDataGenerator(rescale = 1./255, preprocessing_function=rgb_to_gray)

暂无
暂无

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

相关问题 ValueError:检查输入时出错:期望conv1d_81_input具有形状(177,100)但是具有形状的数组(1,177) - ValueError: Error when checking input: expected conv1d_81_input to have shape (177, 100) but got array with shape (1, 177) 检查输入时出错:期望flatten_input具有3维,但数组的形状为(无,100、100、1) - Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (None, 100, 100, 1) 检查输入时出错:预期 conv2d_3_input 的形状为 (32, 32, 1) 但得到的数组的形状为 (32, 32, 3) - Error when checking input: expected conv2d_3_input to have shape (32, 32, 1) but got array with shape (32, 32, 3) Keras检查时发生错误:预期embedding_1_input具有形状(无,100),但数组的形状为(1,3) - Keras Error when checking : expected embedding_1_input to have shape (None, 100) but got array with shape (1, 3) Tensorflow js:错误:检查时出错:预期 conv2d_13_input 有 4 个维度,但得到形状为 [100,120,3] 的数组 - Tensorflow js: Error: Error when checking : expected conv2d_13_input to have 4 dimension(s), but got array with shape [100,120,3] ValueError:检查目标时出错:预期activation_5 的形状为(1,),但数组的形状为(100,) - ValueError: Error when checking target: expected activation_5 to have shape (1,) but got array with shape (100,) Keras 3D Convolution:检查模型输入时出错:预期covolution3d_input_1有5个维度,但得到阵列形状(1,90,100,100) - Keras 3D Convolution: Error when checking model input: expected covolution3d_input_1 to have 5 dimensions, but got array shape (1, 90, 100, 100) ValueError:检查目标时出错:预期 avg_pool 有 4 个维度,但得到了形状为 (100, 2) 的数组 - ValueError: Error when checking target: expected avg_pool to have 4 dimensions, but got array with shape (100, 2) ValueError:检查输入时出错:预期conv3d_1_input具有5个维,但数组的形状为(7,9,384,1) - ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (7, 9, 384, 1) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM