[英]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.