Search code examples
pythontensorflowmachine-learningkerasmax-pooling

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


I created the following 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

When I now run:

model = create_model()

I get this error:

---------------------------------------------------------------------------
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

Before that, my model was the following and I did not have this error:

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

So, the error seems to occur due to the addition of the MaxPooling2D layer and the elimination of GlobalAveragePooling and Dropout.

How should I modify my code?

Thanks!


Solution

  • the error is here backbone = Flatten()(model)

    correct it with

    model = Flatten()(model)
    

    here the complete code: https://colab.research.google.com/drive/12Fa-h12nCsPO1xkPEVnX99iE7jNDuo0A?usp=sharing