我导入了以下软件包:

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
也许可以帮助找到我最初遇到的错误的答案...

最佳答案

我可能是错的,但我认为问题出在output = Reshape((7, 35))(output)语句中。
我了解您要输出(7,35)尺寸,但是错误是指需要附加尺寸。
由于无法测试网络,我认为您可以尝试以下语句:output = Reshape((1, 7, 35))(output)

关于python - 展平图层ValueError : Input 0 is incompatible with layer flatten_5: expected min_ndim=3, found ndim=2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63071277/

10-13 07:22