ValueError:无法将921600尺寸的阵列重塑为形状(224,224,3)
我使用传输学习(InceptionV3)培训了一个模型,当我尝试预测结果时:
ValueError: cannot reshape array of size 921600 into shape (224,224,3)
我用来训练模型的图像生成器是:
root_dir = 'G:/Dataset'
img_generator_flow_train = img_generator.flow_from_directory(
directory=root_dir,
target_size=(224,224),
batch_size=32,
shuffle=True,
subset="training")
img_generator_flow_valid = img_generator.flow_from_directory(
directory=root_dir,
target_size=(224,224),
batch_size=32,
shuffle=True,
subset="validation")
base_model = tf.keras.applications.InceptionV3(input_shape=(224,224,3),
include_top=False,
weights = "imagenet"
)
实现代码是:
cap=cv.VideoCapture(0)
facedetect=cv.CascadeClassifier(cv.data.haarcascades + 'haarcascade_frontalface_default.xml')
model=load_model('Signmodel.h5')
while cap.isOpened():
sts,frame=cap.read()
if sts:
faces=facedetect.detectMultiScale(frame,1.3,5)
for x,y,w,h in faces:
y_pred=model.predict(frame)
print(y_pred,"printing y_pred")
cv.putText(frame,y_pred,(x,y-30), cv.FONT_HERSHEY_COMPLEX, 0.75, (255,0,0),1, cv.LINE_AA)
我尝试调整帧大小:
frame=cv.resize(frame,(224,224),3)
但是当这样做时,我得到了:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)
我该怎么办解决这个问题?
谢谢!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试将图像首先转换为灰色?
DentectMultiscal()需要格式cv_8u的图像。
Did you try converting your image to grey first?
detectMultiScal() requires an image in format CV_8U.
https://docs.opencv.org/3.4/d1/de5/classcv_1_1CascadeClassifier.html#aaf8181cb63968136476ec4204ffca498
调整大小并重塑图像为所需的格式为我解决了问题:
Resizing and reshaping the image into required format solved the problem for me:
我看到您提到了TL,我将假设您正在使用其中一种VGG模型,我将其中一个用于嗜睡预测,当我尝试使用28x28尺寸的图像时,我遇到了同样的问题,我收到了错误通知我,尺寸约束不允许我将其调整到输入尺寸224x224,所以我做了一些我使用Glob函数将图像大小从大小增加到224x 224尺寸的事情,问题是问题所在您正在使用的图像具有敏感数据(尽管我想您正在使用它用于现实生活数据,因为您使用的是工作摄像机而不是图片加载)像素失真将破坏所有和任何类型的敏感数据。
希望它有帮助...
i see you have mentioned TL, i am going to assume you are using one of the VGG Models, i used one of them for drowsiness prediction, i got the same problem when i tried to use an image of 28x28 size, i got the error informing me that the size constraints won't allow me to adapt it to the input size 224x224, so i did something i used glob function to increase the size of the images from the size to enlarge them to 224x 224 size, the problem is if the image you are using has sensitive data(though i guess you are using it for real life data since you are using working camera instead of picture loading) the pixel distortion will destroy all and any kind of sensitive data.
Hope it helps...