Python 添加MaxPooling 2D-ValueError:新数组的总大小必须保持不变

Python 添加MaxPooling 2D-ValueError:新数组的总大小必须保持不变,python,tensorflow,machine-learning,keras,max-pooling,Python,Tensorflow,Machine Learning,Keras,Max Pooling,我创建了以下模型: def create_model(input_shape = (224, 224, 3)): input_img = Input(shape=input_shape) model = efnB0_model (input_img) model = MaxPooling2D(pool_size=(2, 2), strides=2)(model) backbone = Flatten() (model) backbone = model

我创建了以下模型:

def create_model(input_shape = (224, 224, 3)):
    input_img = Input(shape=input_shape)
    model = efnB0_model (input_img)
    model = MaxPooling2D(pool_size=(2, 2), strides=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_360")(branches[i])
            branches[i] = Activation("relu") (branches[i])
            branches[i] = BatchNormalization()(branches[i])
            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
当我现在跑步时:

model = create_model()
我得到这个错误:

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

4 frames
/usr/local/lib/python3.6/dist-packages/keras/layers/core.py in _fix_unknown_dimension(self, input_shape, output_shape)
    385             output_shape[unknown] = original // known
    386         elif original != known:
--> 387             raise ValueError(msg)
    388 
    389         return tuple(output_shape)

ValueError: total size of new array must be unchanged
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 = model

    branches = []
    for i in range(7):
            branches.append(backbone)
            branches[i] = Dense(360, name="branch_"+str(i)+"_Dense_360")(branches[i])
            branches[i] = Activation("relu") (branches[i])
            branches[i] = BatchNormalization()(branches[i])
            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
因此,错误似乎是由于添加了
MaxPooling2D
层,并消除了
GlobalAveragePooling
Dropout

我应该如何修改我的代码


谢谢

错误在这里
backbone=flatte()(模型)

改正

model = Flatten()(model)

这里是完整的代码:

错误在这里
backbone=flatte()(model)

改正

model = Flatten()(model)

这里是完整的代码:

谢谢!我看到你通过
安装了EfficientNet!pip安装效率网
。这与我使用的方法不同吗?我
!pip安装git+https://github.com/qubvel/segmentation_models
导入efficientnet.keras作为efn
。模型与您从细分要求中看到的相同。\u模型谢谢!你以前使用过efficientnet吗?没有,我以前从未使用过它,因为我想知道如何将它应用于无分割车牌识别。谢谢!我看到你通过
安装了EfficientNet!pip安装效率网
。这与我使用的方法不同吗?我
!pip安装git+https://github.com/qubvel/segmentation_models
导入efficientnet.keras作为efn
。模型与您从细分要求中看到的相同。\u模型谢谢!你以前使用过efficientnet吗?没有,我以前从未使用过它,因为我想知道如何将它应用于无分割车牌识别。