【问题标题】:Correctly building the Classifier model (Darknet19) of YOLOv2 in Keras在 Keras 中正确构建 YOLOv2 的 Classifier 模型(Darknet19)
【发布时间】:2019-07-23 19:24:03
【问题描述】:

我正在尝试阅读一篇论文并从头开始构建分类器部分,但我似乎遇到了一个错误,并且不确定我是否正确构建它:

这是论文: https://pjreddie.com/media/files/papers/YOLO9000.pdf

import keras
from keras.layers import Conv2D, Input, concatenate
from keras.layers import LeakyReLU, MaxPooling2D, BatchNormalization,GlobalAveragePooling2D
from keras.models import Model
from keras.activations import softmax
from functools import partial

#First train body for image classification
#Then train head

new_conv = partial(Conv2D ,padding = "same")

def _base_block(out,x):
    "(3,3), Leaky, Batch"
    x =new_conv(out, (3,3))(x)
    x =LeakyReLU(alpha=0.1)(x)
    x =BatchNormalization()(x) 
    return x

def _block_1(out, x):     
    """
    output follows:
    out//2, out

    """
    x =  new_conv(out//2, (1,1))(x)
    x =LeakyReLU(alpha=0.1)(x)
    x = BatchNormalization()(x)
    x = _base_block(out,x)
    return x

def _block_2(out, x):    
    """
    output follows:
    out, out//2, out

    """  
    x =_base_block(out,x)
    x = _block_1(out, x)
    return x


def Darknet19():
    input_layer = Input((img_size, img_size, 3))
    x = _base_block(32,input_layer)
    x =  MaxPooling2D((2,2),strides = 2)(x)
    x = _base_block(64,x)
    x = MaxPooling2D((2,2),strides = 2)(x)
    x = _block_2(128, x)
    x = MaxPooling2D((2,2),strides = 2)(x)
    x = _block_2(256, x)
    x = MaxPooling2D((2,2),strides = 2)(x)
    x = _block_2(512, x)
    x = _block_1(512, x)
    x = MaxPooling2D((2,2),strides = 2)(x)
    x =_block_2(1024, x)
    x = _block_1(512, x)
    x = new_conv(1, (1,1), activation = "linear")(x)
    model = Model(inputs = input_layer, outputs = x)  
    return model

def Darknet_classifier():
    base_model = Darknet19()
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    output = softmax(x)
    model = Model(inputs = base_model.inputs, outputs = output)

    return model

img_size = 426 #multiple of 30
model = Darknet19()
model =Darknet_classifier()
print(model.summary())

我收到的错误:

'找到:' + str(x)) ValueError:模型的输出张量必须是 Keras Layer 的输出(因此保存过去的层元数据)。找到:张量(“Softmax:0”,形状=(?,1), dtype=float32)

看来我不能从 GAP 转到 softmax。我对模型的解释不正确吗?

【问题讨论】:

    标签: python keras deep-learning conv-neural-network


    【解决方案1】:

    您没有正确应用 softmax,您可以为此使用 Activation 层:

    from keras.layers import Activation
    
    output = Activation("softmax")(x)
    

    【讨论】:

      【解决方案2】:

      您可以通过使用Lambda 层来消除上述错误,该层基本上将任意表达式包装为Layer 对象。

      将上述代码中的Darknet_classifier函数修改为如下所示:

      from keras.layers import Lambda
      
      def apply_soft(x): 
          output = softmax(x)
          return output
      
      def Darknet_classifier():
          base_model = Darknet19()
          x = base_model.output
          x = GlobalAveragePooling2D()(x)
          output = Lambda(apply_soft)(x)
          model = Model(inputs = base_model.inputs, outputs = output)
          return model
      

      或者,将output = softmax(x) 替换为output = Lambda(lambda x: softmax(x))(x)

      【讨论】:

      • 不建议盲目地从 keras 切换到 tf.keras,这可能会引入错误和与 OPs 代码库的不兼容性。
      • 我的错。我的代码中有一个错误,我刚刚更正了。感谢您的宝贵反馈。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 2011-12-04
      • 2020-05-28
      • 2018-12-14
      • 2019-03-14
      • 2020-08-16
      相关资源
      最近更新 更多