yolo3 Keras中Lambda层的替代品 [英] Alternative for Lambda layer in yolo3 Keras
问题描述
我的目标
我想在Tensorflow(python)中训练自定义对象检测模型,并在挖掘了大量我发现
I want to train a custom object detection model in Tensorflow(python) and use it using Tensorflow js after digging lot of example I found this which is widely popular
我做了什么
我已经写了Tensorflow JS部分(从在线示例获得帮助)来从本地加载模型并获得预测.我使用了COCO预训练模型,它工作正常(因此,在这里不添加代码).
I have written ( taken help form online examples ) the Tensorflow JS part to load a model from local and get the get the predictions. I used with COCO pretrained model It is working fine (so not adding the code here).
我的问题是什么
我对python和Tensorflow非常陌生. 训练 qqwweee/keras-yolo3 的示例是在python中,它是Keras的Lamda
I am very new to python and Tensorflow. The example for training qqwweee/keras-yolo3 the model is in python and it is Lamda from Keras
from keras.layers import Input, Lambda
这个地方
model.compile(optimizer=Adam(lr=1e-3), loss={
# use custom yolo_loss Lambda layer.
'yolo_loss': lambda y_true, y_pred: y_pred})
还有
model.compile(optimizer=Adam(lr=1e-4), loss={'yolo_loss': lambda y_true, y_pred: y_pred}) # recompile to apply the change
还有
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
所以到目前为止,我了解到,Lambda主要用于计算损失函数,这在TFJS中引起了主要问题,因为到目前为止尚未实现Lambda层,我想使用一些替代方法来代替lambda层. 这是我在TFJS中使用经过训练的模型时遇到的错误
So What I understood so far, Lambda is mainly used for calculating the loss function, and this is causing main problem in TFJS because Lambda layer is not implemented till now I want to use some alternative instead of lambda layer. This is the error I am getting while using the trained model in TFJS
Error loading layer ValueError: Unknown layer: Lambda. This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().
这里也要问类似的问题在浏览器上的tensorflowjs中,它谈论编写自定义层,该示例不足以做到这一点,最终导致死胡同.
Similar question is also asked here "Unknown layer: Lambda" in tensorflowjs on browser, it talks about writing a custom layer , the example is not enough to do that, ultimately leads to dead end.
我想要的
- 是否有任何方法可以使用lambda指导的其他损失函数?怎么样?
- 有没有为lambda编写自定义层的示例
- 我的理解在哪里错了?
p.s:我花了很多时间来寻找解决方案,任何帮助将不胜感激,在此先感谢
p.s: I spent hell lot of time to find the solution, any help will be appreciated, Thanks in advance
在添加@edkeveked(感谢!)给出的空lambda层后,错误Error loading layer ValueError: Unknown layer: Lambda
消失了,但遇到了其他问题.
After adding the empty lambda layer given by @edkeveked (Thanks!), the error Error loading layer ValueError: Unknown layer: Lambda
is gone but ran into something else.
在此处
现在,在模型预热中,它会抛出该错误 热身代码
Now, In the model warmup itslef thorowing this error code for warmup
let zero = tfNode.zeros([1, 416, 416, 3]);
const result = await this.model.predict(zero)
result.map(async (t) => await t.data());
result.map(async (t) => t.dispose());
图像预测代码
batched = tfNode.tidy(() => {
if (!(img instanceof tfNode.Tensor)) {
img = tfNode.browser.fromPixels(img);
}
return img.expandDims(0);
});
result = await this.model.predict(batched);
我遇到错误
"Error: Error when checking model : the Array of Tensors that you are passing to your model is not the size the the model expected. Expected to see 4 Tensor(s), but instead got 1 Tensors(s).
at new ValueError (XXX\node_modules\@tensorflow\tfjs-layers\dist\errors.js:68:28)
at checkInputData (XXX\node_modules\@tensorflow\tfjs-layers\dist\engine\training.js:316:19)
at LayersModel.predict (XXX\node_modules\@tensorflow\tfjs-layers\dist\engine\training.js:981:9)
at ObjectDetection.warmUp (XXX\tensorflow_predownloaded_model.js:47:45)
at XXX\tensorflow_predownloaded_model.js:38:18"
推荐答案
由于尚不支持Lambda层,因此需要提供该层以使转换正常进行. 此外,加载的层不用于训练,因此lambda层可以为空. (代码未尝试)
Since the Lambda layer is not yet supported, it need to be provided for the conversion to work. Moreover, the loaded layer is not used for training, so the lambda layer can be empty. (code not tried)
class Lambda extends tf.layers.Layer {
constructor() {
super({})
}
static get className() {
return 'Lambda';
}
}
tf.serialization.SerializationMap.register(Lambda);
;
这篇关于yolo3 Keras中Lambda层的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!