Core ML上具有两个参数功能的自定义层 [英] Custom layer with two parameters function on Core ML

查看:96
本文介绍了Core ML上具有两个参数功能的自定义层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢这篇出色的文章( http://machinethink.net/blog/coreml-自定义层/),我了解了如何使用带有Keras自定义层的coremltools和Lambda编写转换. 但是,我对这种情况不了解,有两个参数起作用.

Thanks to this great article(http://machinethink.net/blog/coreml-custom-layers/), I understood how to write converting using coremltools and Lambda with Keras custom layer. But, I cannot understand on the situation, function with two parameters.

#python
def scaling(x, scale):
    return x * scale

Keras层在这里.

Keras layer is here.

#python
up = conv2d_bn(mixed,
                   K.int_shape(x)[channel_axis],
                   1,
                   activation=None,
                   use_bias=True,
                   name=name_fmt('Conv2d_1x1'))
x = Lambda(scaling, # HERE !!
           output_shape=K.int_shape(up)[1:],
           arguments={'scale': scale})(up)
x = add([x, up])

在这种情况下,如何在Swift的custom MLCustomLayer class中写func evaluate(inputs: [MLMultiArray], outputs: [MLMultiArray])?我只了解一种这样的参数函数情况,

On this situation, how can I write func evaluate(inputs: [MLMultiArray], outputs: [MLMultiArray]) in custom MLCustomLayer class on Swift? I understand just in one parameter function situation, like this,

#swift 
func evaluate(inputs: [MLMultiArray], outputs: [MLMultiArray]) throws {
  for i in 0..<inputs.count {
    let input = inputs[i]
    let output = outputs[i]

    for j in 0..<input.count {
      let x = input[j].floatValue
      let y = x / (1 + exp(-x))
      output[j] = NSNumber(value: y)
    }
  }  
}

两个参数的功能如何,例如x * scale?

How about two parameters function, like x * scale?

完整代码在这里.

  • Converting to Core ML model with custom layer https://github.com/osmszk/dla_team14/blob/master/facenet/coreml/CoremlTest.ipynb
  • Network model by Keras https://github.com/osmszk/dla_team14/blob/master/facenet/code/facenet_keras_v2.py

谢谢.

推荐答案

看来scale是超参数,而不是可学习的参数,对吗?

It looks like scale is a hyperparameter, not a learnable parameter, is that correct?

在这种情况下,您需要将scale添加到自定义层的参数字典中.然后在您的Swift类中,scale还将位于传递到init(parameters)函数的参数字典中.将其存储在属性中,然后在evaluate(inputs, outputs)中再次读取该属性.

In that case, you need to add scale to the parameters dictionary for the custom layer. Then in your Swift class, scale will also be inside the parameters dictionary that is passed into your init(parameters) function. Store it inside a property and then in evaluate(inputs, outputs) read from that property again.

我的博客文章实际上显示了如何执行此操作. ;-)

My blog post actually shows how to do this. ;-)

这篇关于Core ML上具有两个参数功能的自定义层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆