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

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

我的一些机器学习代码需要帮助。 我正在使用 google 预训练模型的基础,并添加我自己的最后一个展平层和输出层。

训练好模型并保存后,我把训练部分的代码注释掉,加载使用保存的模型。

我的代码:

import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib.pyplot as plt

import tensorflow_datasets as tfds
tfds.disable_progress_bar()

class_names = ['cat','dog']

#load the images
(raw_train, raw_validation, raw_test), metadata = tfds.load(
    'cats_vs_dogs',
    split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
    with_info=True,
    as_supervised=True,
)

get_label_name = metadata.features['label'].int2str

#function to resize image
IMG_SIZE=160
def format_example(image, label):
    image = tf.cast(image, tf.float32) #cast to convert integer values in pixels to float32
    image = (image/127.5) - 1
    image = tf.image.resize(image,  (IMG_SIZE, IMG_SIZE))
    return image,label

#.map is to apply a function to all raw images
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)

BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000

train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)

IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
#Create base model from pretrained model MobileNetV2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                                include_top=False,
                                                weights='imagenet')

#freezing the base (preventing any more training)
base_model.trainable = False

#Flattening it down
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
#Adding dense layer of only 1 neuron as only 2 possibilities(cat/dog)
prediction_layer = keras.layers.Dense(1)

model = tf.keras.Sequential([
    base_model,
    global_average_layer,
    prediction_layer
])

#slow learning rate since it is a pretrained model
base_learning_rate = 0.0001
#Compiling the model
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=base_learning_rate),
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])

initial_epochs = 3
validation_steps=20

loss0,accuracy0 = model.evaluate(validation_batches, steps = validation_steps)

print('training...')
history = model.fit(train_batches,
                    epochs=initial_epochs,
                    validation_data = validation_batches)

acc = history.history['accuracy']
print(acc)

model.save("dogs_vs_cats.h5")

model = tf.keras.models.load_model('dogs_vs_cats.h5', compile=True)
print('predicting... ')
prediction = model.predict(test, verbose=1)
print(prediction)

我收到以下错误:

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

我的理解是否正确,您仅在评论训练代码和从 model = tf.keras.models.load_model('dogs_vs_cats.h5', compile=True) 开始的 运行 行后才会收到此错误?如果是,我认为你在这一行中有错误:

prediction = model.predict(test, verbose=1)

您对 3D 图像使用 test,但未对 4D 图像进行批处理 test_batches,这就是您获得 3D/4D 形状错误的原因。