[Keras]我创建模型时出了点问题

时间:2018-10-20 07:29:57

标签: python keras

当我使用kears创建模型时,发生此错误。 Scale3 Scale2 Scale1是相同的操作。 代码是:

Scale3 = myconv2d(Scale3,255,1,1,'valid')
model = Model(inputs = X_input, outputs = [Scale3,Scale2,Scale1], name='YOLOV3')

错误是:error_image

myconv2d函数是

def myconv2d(X,f,size,s,stage,p = 'same'):
    X = Conv2D(f,kernel_size = (size,size),strides = (s,s),padding = 'same',name = 'conv2d_'+ str(stage))(X)
    X = BatchNormalization(axis=3,name ='bn_' + str(stage))(X)
    X = K.relu(X,0.1)
    return X

我该如何解决?

2 个答案:

答案 0 :(得分:0)

您不能直接在Keras张量上应用后端函数,您需要为此使用Lambda层:

def myconv2d(X,f,size,s,stage,p = 'same'):
    X = Conv2D(f,kernel_size = (size,size),strides = (s,s),padding = 'same',name = 'conv2d_'+ str(stage))(X)
    X = BatchNormalization(axis=3,name ='bn_' + str(stage))(X)
    X = Lambda(lambda x: K.relu(x,0.1)(X)
    return X

答案 1 :(得分:0)

您可以使用layers.advanced_activations.LeakyReLU(0.1)(X)替换代码:K.relu(X,0.1)

Matias Valdenegro也正确