【问题标题】:Shape of input to "Flatten" is not fully defined (got (None, None, 64)“Flatten”的输入形状未完全定义(得到(无,无,64)
【发布时间】:2018-03-01 06:39:31
【问题描述】:

我将预训练模型 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】:

    您应该将输入形状从您的第一个 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) 
    ...
    

    【讨论】:

    • 我为这个错误苦苦挣扎了一整天。而你救了我。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 2019-08-23
    • 2019-04-01
    • 2019-07-06
    • 2019-06-22
    • 1970-01-01
    • 2019-01-24
    • 2021-07-15
    相关资源
    最近更新 更多