'ValueError: cannot reshape array of size 921600 into shape (224,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!!!



Solution 1:[1]

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)

Solution 2:[2]

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...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shawn Ramirez
Solution 2 H99C