“Flatten”的输入形状未完全定义(got(None,None,64)

问题描述 投票:0回答:1

我使用预先训练的模型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)
neural-network deep-learning keras conv-neural-network pre-trained-model
1个回答
2
投票

您应该将输入形状从第一个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) 
...
© www.soinside.com 2019 - 2024. All rights reserved.