"Flatten" 的输入形状未完全定义(得到 (None, None, 64)

Shape of input to "Flatten" is not fully defined (got (None, None, 64)

我正在为我的第一个图像分类应用程序使用预训练模型 GoogleNet。使用 Flatten 时出现此错误 -

ValueError: The shape of the input to "Flatten" is not fully defined got 
(None, None, 64). Make sure to pass a complete "input_shape" or 
"batch_input_shape" argument to the first layer in your model.

我在互联网上搜索了很多,但没有在任何地方找到解决方案。如果有人能帮助我,我将不胜感激。

下面是我的代码。

train_datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    'dir_path',
    target_size=(500, 500),
    batch_size=batch_size,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    'dir_path',
    target_size=(500, 500),
    batch_size=batch_size,
    class_mode='categorical')

base_model = InceptionV3(weights='imagenet', include_top=False)

x = base_model.output
x = Conv2D(32, (3, 3), use_bias=True, activation='relu', input_shape=
(500,500,3)) (x) #line2
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu') (x) #line3
x = Flatten()(x)
x = Dense(batch_size, activation='relu')(x) #line1
x = (Dropout(0.5))(x)
predictions = Dense(num_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

您应该将输入形状从您的第一个 Conv2D 层移动到您的基础模型,如下所示:

base_model = InceptionV3(weights='imagenet', input_shape=(500,500,3), include_top=False)

x = base_model.output
x = Conv2D(32, (3, 3), use_bias=True, activation='relu', ) (x) 
...