簡體   English   中英

添加 MaxPooling 2D - ValueError:新數組的總大小必須不變

[英]Addition of MaxPooling 2D - ValueError: total size of new array must be unchanged

我創建了以下 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

在此之前,我的model如下,並沒有出現這個錯誤:

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層並消除了GlobalAveragePoolingDropout而發生的。

我應該如何修改我的代碼?

謝謝!

錯誤在這里backbone = Flatten()(model)

更正它

model = Flatten()(model)

這里是完整的代碼: https://colab.research.google.com/drive/12Fa-h12nCsPO1xkPEVnX99iE7jNDuo0A?usp=sharing

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM