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

Issue

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

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)

Answered By – Neha Satya

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published