简体   繁体   English

检查输入时出错:预期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)

My model for classifying images is shown below.When using the predict function it is giving the below error: 我的图像分类模型如下所示,当使用预测函数时会出现以下错误:

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

The model is as shown below: 模型如下图所示:

# Importing the Keras libraries and packages
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 = (64, 64, 3), 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
import pickle
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('Dataset/training_data',
target_size = (64, 64),batch_size = 32,class_mode = 'binary')
test_set = test_datagen.flow_from_directory('Dataset/test_data',
target_size = (64, 64),batch_size = 32,class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 350,epochs = 2,validation_data = test_set,validation_steps 
= 101)

The predict function is shown below: I have used requests package since I want to use a url of an image for prediction. 预测函数如下所示:我使用了请求包,因为我想使用图像的URL进行预测。

import requests
from io import BytesIO
from PIL import Image
import numpy as np
from keras.preprocessing import image
import cv2

 url='http://answers.opencv.org/upfiles/logo_2.png'
 response = requests.get(url)
 img = Image.open(BytesIO(response.content))
 #file = cv2.imread(img)
 img = img.resize((64,64))
 x = image.img_to_array(img)
 x = np.expand_dims(x, axis=0)
 result = classifier.predict(x)
 #training_set.class_indices
 if result[0][0] == 1:
      prediction = 'signature'
 else:
      prediction = 'nonsignature'



 print(prediction)

Is there an alternate way using only one package instead of PIL and keras 是否有一种仅使用一个软件包而不是PIL和keras的替代方法

Thanks for help!! 感谢帮助!!

The error message speaks for itself. 错误消息说明一切。 Your network expects images with 3 color channels ( RGB ), but you are providing it images with 4 channels. 您的网络需要具有3个颜色通道( RGB )的图像,但是您正在为其提供具有4个通道的图像。 You are working with a png image, so the image is probably in RGBA format and the fourth channel is a transparancy channel. 您正在处理png图像,因此该图像可能是RGBA格式,第四个通道是透明通道。

PIL images can be converted to RGB format as follows: PIL图像可以转换为RGB格式,如下所示:

img = img.convert(mode='RGB')
x = image.img_to_array(img)

x.shape
> (64, 64, 3)

However, this conversion might not give the desired result (the background will be black for instance). 但是,此转换可能无法提供所需的结果(例如,背景为黑色)。 You can refer to this question for other ways of converting to RGB . 您可以参考此问题以了解其他转换为RGB

You can also use the load_img function from the Keras preprocessing module: 您还可以使用load_img预处理模块中的load_img函数:

import keras
img = keras.preprocessing.image.load_img(io.BytesIO(response.content))

This will load the image in RGB format. 这将以RGB格式加载图像。 This function uses PIL under the hood and produces the same result as above. 此功能在引擎盖下使用PIL并产生与上述相同的结果。

暂无
暂无

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

相关问题 imutils.resize 将无法获得正确的形状:预期 conv2d_1_input 的形状为 (64, 64, 3) 但得到的数组形状为 (91, 64, 3) - imutils.resize will not get the right shape: expected conv2d_1_input to have shape (64, 64, 3) but got array with shape (91, 64, 3) ValueError:检查输入时出错:预期 conv2d_9_input 的形状为 (64, 64, 3) 但得到的数组的形状为 (32, 32, 1) - ValueError: Error when checking input: expected conv2d_9_input to have shape (64, 64, 3) but got array with shape (32, 32, 1) 预期conv2d_1_input具有4个维度,但数组的形状为(15936,64) - expected conv2d_1_input to have 4 dimensions, but got array with shape (15936, 64) 检查输入时出错:预期 conv1d_57_input 有 3 个维度,但得到了形状为 (152, 64) 的数组 - Error when checking input: expected conv1d_57_input to have 3 dimensions, but got array with shape (152, 64) 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_3的形状为(1,58,58),但数组的形状为(1,64,64) - ValueError: Error when checking target: expected conv2d_3 to have shape (1,58,58), but got array with shape (1,64,64) 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) 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) 检查输入时出错:预期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) 检查输入时出错:预期conv2d_1_input具有形状(28,28,1),但具有形状(3,224,224)的数组 - Error when checking input: expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (3, 224, 224)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM