我的输入形状为(20,10,1)
我的非工作模型如下所示:
num_classes = 2
model.add(Conv2D(32, (5, 5),
padding='same',
data_format='channels_last',
input_shape=input_shape))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Conv2D(32, (5, 5)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (5, 5), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Conv2D(64, (5, 5)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
self.model = model
这给了我以下错误:
Negative dimension size caused by subtracting 5 from 2 for 'conv2d_4/convolution' (op: 'Conv2D') with input shapes: [?,5,2,64], [5,5,64,64].
然而,如果我做了以下两件事之一,那么错误就会消失:
1.删除所有三个model.add(Conv2D(64, (5, 5)))
或
2.从(5,5)到(3,3)改变^三个Conv2D层并更改全部
pool_size(2,2)
据我所知,第4层末端的尺寸造成了麻烦。
如何让^模型在当前状态下工作?
基本上我想比较这个模型的性能(过滤器大小5x5和pool_size(3,3)与另一个使用3x3过滤器和pool_size(2,2)的模型。谢谢
答案 0 :(得分:3)
问题是层conv2d_4
的输出变为零或负。
要解决此问题,您必须设计网络,以便不会对输入数据进行高度下采样。</ p>
以下是一些可能的解决方案:
pool_size=(2, 2)
,导致下采样一半。Conv2D
图层,在卷积步骤中不会导致下采样。</ li>
答案 1 :(得分:1)
将步幅值更改为1,1,如下所示。这解决了我的问题
model.add(MaxPooling2D(pool_size =(2,2),strides =(1,1),padding =&#39; same&#39;,name =&#39; pool2&#39;) )强>
我的代码的完整图层是
def getModel():
optim = Adam(lr= LR, decay=0)
model =Sequential()
model.add(Conv2D(32,(3,3),activation = 'relu',input_shape =[28,28,1],kernel_initializer='he_uniform',
kernel_regularizer=regularizers.l2(l2_labmda),data_format='channels_last',name='1st'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2),strides=(1,1),padding='same',name = 'pool1'))
model.add(Dropout(0.2,name='2'))
model.add(Conv2D(64,(3,3),activation = 'relu',kernel_initializer='he_uniform',
kernel_regularizer=regularizers.l2(l2_labmda),name='3'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2),strides=(1,1),padding='same',name = 'pool2'))
model.add(Dropout(0.2,name='4'))
model.add(Conv2D(64,(5,5),activation = 'relu',kernel_initializer='he_uniform',
kernel_regularizer=regularizers.l2(l2_labmda),name='5'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2),strides=(1,1),padding='same',name = 'pool3'))
model.add(Dropout(0.2, name='6'))
# =============================================================================
model.add(Conv2D(128,(3,3),activation = 'relu',kernel_initializer='he_uniform',
kernel_regularizer=regularizers.l2(l2_labmda),name='7'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2),strides=(2,2),padding='same',name = 'pool5'))
model.add(Dropout(0.2,name='8'))
#
# =============================================================================
model.add(Conv2D(128,(3,3),activation = 'relu',kernel_initializer='he_uniform',
kernel_regularizer=regularizers.l2(l2_labmda),name='9'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2),strides=(2,2),padding='same',name = 'pool6'))
model.add(Dropout(0.2))
model.add(Flatten(name='12'))
model.add(Dense(512,activation='relu',kernel_initializer='he_uniform',
kernel_regularizer=regularizers.l2(l2_labmda), name='13'))
model.add(Dropout(0.2,name='14'))
model.add(Dense(512,activation='relu',kernel_initializer='he_uniform',
kernel_regularizer=regularizers.l2(l2_labmda),name='15'))
model.add(Dropout(0.2,name='16'))
model.add(Dense(40,activation='softmax',kernel_initializer='glorot_uniform ',
kernel_regularizer=regularizers.l2(l2_labmda),name='17'))
model.compile(optimizer=optim,loss='categorical_crossentropy',metrics=['accuracy'])
print(model.summary())
return model
model = getModel()