【问题标题】:ValueError: cannot reshape array of size 921600 into shape (224,224,3)ValueError:无法将大小为 921600 的数组重塑为形状 (224,224,3)
【发布时间】:2022-06-12 09:47:48
【问题描述】:

我使用迁移学习 (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)

我应该怎么做才能解决这个问题?

谢谢!!!

【问题讨论】:

  • 完成回溯。然后你需要cv.resize,因为你的frame 不是(224,224,3) 的形状并且不能重新调整为那个形状,它必须调整为图像的大小
  • 如果 cap.read() 返回 numpy 数组,我应该如何将框架调整为图像? Github链接:github.com/Nehasatya/sign-language-translator.git
  • 224*224*3=150528,不是 921600

标签: python tensorflow opencv keras deep-learning


【解决方案1】:

您是否先尝试将图像转换为灰色?

detectMultiScal() 需要 CV_8U 格式的图像。

https://docs.opencv.org/3.4/d1/de5/classcv_1_1CascadeClassifier.html#aaf8181cb63968136476ec4204ffca498

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:
        frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        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)

【讨论】:

  • ValueError:调用层“inception_v3”(功能类型)时遇到异常。层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=2。收到完整形状:(32, 224) 收到调用参数: • inputs=tf.Tensor(shape=(32, 224), dtype=float32) • training=False • mask=None 我在转换为灰度时出现上述错误,
【解决方案2】:

将图像调整大小并重新调整为所需格式解决了我的问题:

while cap.isOpened():
    sts,frame=cap.read()
    frame1=cv.resize(frame,(224,224))
    frame1 = frame1.reshape(1,224,224,3)
    if sts:
        faces=facedetect.detectMultiScale(frame,1.3,5)
        for x,y,w,h in faces:
            y_pred=model.predict(frame)

【讨论】:

    【解决方案3】:

    我看到你提到了 TL,我假设你正在使用其中一个 VGG 模型,我使用其中一个进行嗜睡预测,当我尝试使用 28x28 尺寸的图像时遇到了同样的问题,我收到错误消息,通知我尺寸限制不允许我将其调整为输入尺寸 224x224,所以我做了一些事情,我使用 glob 函数将图像的尺寸从尺寸增加到 224x 224 尺寸,问题是如果您使用的图像具有敏感数据(尽管我猜您将它用于现实生活中的数据,因为您使用的是工作相机而不是图片加载)像素失真会破坏所有和任何类型的敏感数据。

    希望对你有帮助...

    【讨论】:

    • 您“使用 glob 函数来增加图像的大小”?请解释这有什么意义。
    • 我没有使用 glob 函数来增加大小,我使用函数来调整它们的大小,唯一的缺点是图像模糊,这对数据集没有用,因为我无法单独放大每个图像,我在 python 中使用了 glob 函数(在这种情况下,我可以从文件中获取图像并调整其大小并将其存储在另一个地方而不干扰数据集)以将其应用于一批图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2021-03-20
    • 2021-03-21
    • 2018-10-22
    • 2019-11-29
    • 2020-01-29
    相关资源
    最近更新 更多