【问题标题】:Flatten Layer ValueError: Input 0 is incompatible with layer flatten_5: expected min_ndim=3, found ndim=2Flatten Layer ValueError: Input 0 is in compatible with layer flatten_5: expected min_ndim=3, found ndim=2
【发布时间】:2020-07-24 09:59:25
【问题描述】:

我导入了以下包:

import tensorflow as tf
import keras
from keras.models import Sequential, Model
from keras.layers import Conv2D, Flatten, MaxPooling2D, Dense, Input, Reshape, Concatenate, GlobalAveragePooling2D, BatchNormalization, Dropout
from keras.utils import Sequence
import efficientnet.keras as efn

我取了EfficientNetB0并排除了顶部以使用定制的,我取了imagenet的权重。

efnB0_model = efn.EfficientNetB0(include_top=False, weights="imagenet", input_shape=(224, 224, 3))
efnB0_model.trainable = False

并创建了以下模型:

def create_model(input_shape = (224, 224, 3)):
    input_img = Input(shape=input_shape)
    model = efnB0_model (input_img)
    model = GlobalAveragePooling2D(name='avg_pool')(model)
    model = Dropout(0.3)(model)
    backbone = Flatten() (model)
    backbone = model
branches = []
for i in range(7):
        branches.append(backbone)
        branches[i] = Dense(360, name="branch_"+str(i)+"_Dense_16000")(branches[i])
        branches[i] = BatchNormalization()(branches[i])
        branches[i] = Activation("relu") (branches[i])
        branches[i] = Dropout(0.3)(branches[i])
        branches[i] = Dense(128, name="branch_"+str(i)+"_Dense_128")(branches[i])
        branches[i] = BatchNormalization()(branches[i])
        branches[i] = Activation("relu")
        branches[i] = Dropout(0.3)(branches[i])            
        branches[i] = Dense(35, activation = "softmax", name="branch_"+str(i)+"_output")(branches[i])
    
output = Concatenate(axis=1)(branches)
output = Reshape((7, 35))(output)
model = Model(input_img, output)

return model

执行时:

model = create_model()

我收到此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-91-834f03506210> in <module>()
----> 1 model = create_model()

3 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
    356                                      self.name + ': expected min_ndim=' +
    357                                      str(spec.min_ndim) + ', found ndim=' +
--> 358                                      str(K.ndim(x)))
    359             # Check dtype.
    360             if spec.dtype is not None:

ValueError: Input 0 is incompatible with layer flatten_5: expected min_ndim=3, found ndim=2

Flatten() 层似乎不兼容。我应该如何修改我的代码?如果没有这一层,我不会收到此错误。

编辑:

我修改了网络并注释掉了一些层,现在它可以工作了:

def create_model(input_shape = (224, 224, 3)):
    input_img = Input(shape=input_shape)
    model = efnB0_model (input_img)
    model = GlobalAveragePooling2D(name='avg_pool')(model)
    model = Dropout(0.2)(model)
    #backbone = Flatten() (model)
    backbone = model

    branches = []
    for i in range(7):
            branches.append(backbone)
            branches[i] = Dense(360, name="branch_"+str(i)+"_Dense_16000")(branches[i])
            branches[i] = BatchNormalization()(branches[i])
            branches[i] = Activation("relu") (branches[i])
            branches[i] = Dropout(0.2)(branches[i])
            # branches[i] = Dense(128, name="branch_"+str(i)+"_Dense_128")(branches[i])
            # branches[i] = BatchNormalization()(branches[i])
            # branches[i] = Activation("relu")
            # branches[i] = Dropout(0.2)(branches[i])            
            branches[i] = Dense(35, activation = "softmax", name="branch_"+str(i)+"_output")(branches[i])
        
    output = Concatenate(axis=1)(branches)
    output = Reshape((7, 35))(output)
    model = Model(input_img, output)

return model

也许这有助于为我最初遇到的错误找到答案...

【问题讨论】:

    标签: python tensorflow keras error-handling conv-neural-network


    【解决方案1】:

    我可能错了,但我认为问题出在output = Reshape((7, 35))(output) 语句中。

    我了解您想要 (7, 35) 维度输出,但错误是指需要额外维度。

    由于我无法测试网络,我想你可以试试下面的语句:

    output = Reshape((1, 7, 35))(output)

    【讨论】:

    • 不幸的是,这不是错误。更新我的问题并展示它现在是如何工作的......
    猜你喜欢
    • 2022-11-06
    • 1970-01-01
    • 2017-10-26
    • 2019-07-19
    • 1970-01-01
    • 2017-07-28
    • 2020-01-29
    • 2018-05-19
    • 2019-01-03
    相关资源
    最近更新 更多