【发布时间】:2020-07-24 09:59:25
【问题描述】:
我导入了以下包:
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
也许这有助于为我最初遇到的错误找到答案...
【问题讨论】:
标签: python tensorflow keras error-handling conv-neural-network