ValueError:无法将921600尺寸的阵列重塑为形状(224,224,3)

发布于 2025-01-25 08:24:00 字数 1680 浏览 3 评论 0 原文

我使用传输学习(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)

我该怎么办解决这个问题?

谢谢!!!

I trained a model using Transfer Learning(InceptionV3) and when I tried to predict the results it shows:

ValueError: cannot reshape array of size 921600 into shape (224,224,3)

The image generator I used to train the model is:

    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"
                                               )

The implementation code is:

  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)

I tried to resize the frame:

frame=cv.resize(frame,(224,224),3)

but when doing so I got:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(32, 224, 3)

What should I do to resolve this?

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘你却要生生世世 2025-02-01 08:24:00

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

DentectMultiscal()需要格式cv_8u的图像。

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)

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

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)
℉服软 2025-02-01 08:24:00

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

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)

Resizing and reshaping the image into required format solved the problem for me:

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)
忱杏 2025-02-01 08:24:00

我看到您提到了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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文