Addition of MaxPooling 2D - ValueError: total size of new array must be unchanged
Addition of MaxPooling 2D - 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 = 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 = Flatten()(model)
更正为
model = Flatten()(model)
这里是完整的代码:https://colab.research.google.com/drive/12Fa-h12nCsPO1xkPEVnX99iE7jNDuo0A?usp=sharing
我创建了以下模型:
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 = Flatten()(model)
更正为
model = Flatten()(model)
这里是完整的代码:https://colab.research.google.com/drive/12Fa-h12nCsPO1xkPEVnX99iE7jNDuo0A?usp=sharing