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)
4

1 回答 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) 
...
于 2018-03-01T10:47:40.000 回答