Ввод 0 несовместим со слоем functional_3: ожидаемая форма = (Нет, 224, 224, 3), найденная форма = (Нет, 240, 240, 3)

#python #tensorflow #deep-learning #conv-neural-network #vgg-net

#питон #тензорный поток #глубокое обучение #conv-нейронная сеть #vgg-net

Вопрос:

Я новичок в VGG19 и обработке изображений на python. Я пытаюсь протестировать свою обученную модель VGG19 для прогнозирования изображения. Я получаю эту ошибку:-

 ValueError: Input 0 is incompatible with layer functional_3: expected shape=(None, 224, 224, 3), found shape=(None, 240, 240, 3)
 

Мой код tensorflow для прогнозирования выглядит следующим образом:-

 import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.models import load_model
model = load_model('VGG19.h5')
CATEGORIES = ["Pneumonia", "Non-Pneumonia"]
img = cv2.imread('person1_bacteria_1.jpeg')
img = cv2.resize(img,(240,240))     # resize image to match model's expected sizing
img = np.reshape(img,[1,240,240,3]) # return the image with shaping that TF wants.
prediction = model.predict(img)
prediction
 

Но в случае с файлом .ipynb я просто получаю предупреждение об этом:-

Этот образ

Ответ №1:

Вы resizing to wrong shape . Вместо 240,240

 img = cv2.resize(img,(240,240))     # resize image to match model's expected sizing
img = img.reshape(1,240,240,3) # return the image with shaping that TF wants.
 

Использовать 224,224

 img = cv2.resize(img,(224,224))     # resize image to match model's expected sizing
img = img.reshape(1,224,224,3) # return the image with shaping that TF wants.
 

Ответ №2:

Ваша предварительно подготовленная модель ожидает ввода формы (224,224,3), а вы вводите ее (240,240,3), отсюда и жалоба.
Так что просто сделайте:

img = img.reshape(1,224,224,3)

И все готово!