Keras:在theano和tensorflow之间转换预训练的权重 [英] Keras: convert pretrained weights between theano and tensorflow

查看:202
本文介绍了Keras:在theano和tensorflow之间转换预训练的权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用此预训练模型.

它在theano布局中,我的代码取决于tensorflow图像尺寸顺序.

It is in theano layout, my code depends on tensorflow image dimension ordering.

关于但这似乎坏了.在将theano转换为张量流的部分中,第一条指令是将权重加载到张量流模型中.

But this seems broken. In the section to convert theano to tensorflow, the first instruction is to load the weights into the tensorflow model.

在这种情况下,Keras后端应为TensorFlow. 首先,将Theano训练后的权重加载到TensorFlow模型中:

Keras backend should be TensorFlow in this case. First, load the Theano-trained weights into your TensorFlow model:

model.load_weights('my_weights_theano.h5')

这引发了一个异常,权重布局将不兼容. 而且,如果load_weights函数对张量流模型采用theano权重,则无需进行任何转换.

This raises an exception, the weight layouts would be incompatible. And if the load_weights function would take theano weights for a tensorflow model, there wouldn't be any need to convert them.

我看了 convert_kernel 查看我是否可以自己执行必要步骤的功能.

I took a look at the convert_kernel function to see, whether I could do the necessary steps myself.

代码非常简单-我不明白为什么该指南使用了tensorflow会话.似乎没有必要.

The code is rather simple - I don't understand why the guide makes use of a tensorflow session. That seems unnecessary.

我已经从预训练的模型中复制了代码以创建具有tensorflow层的模型.这只是意味着在添加任何卷积之前更改输入形状和backend.image_dim_ordering. 然后我使用了这个循环:

I've copied the code from the pretrained model to create a model with tensorflow layer. This just meant changing the input shape and the backend.image_dim_ordering before adding any Convolutions. Then I used this loop:

model是原始模型,是根据我开头链接的代码创建的. model_tensorflow是完全相同的模型,但具有张量流布局.

model is the original model, created from the code I linked at the beginning. model_tensorflow is the exact same model, but with tensorflow layout.

for i in range(len(model.layers)):
    layer_theano=model.layers[i]
    layer_tensorflow=model_tensorflow.layers[i]

    if layer_theano.__class__.__name__ in ['Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D']:
        weights_theano=layer_theano.get_weights()

        kernel=weights_theano[0]
        bias=weights_theano[1]

        converted_kernel=convert_kernel(kernel, "th")
        converted_kernel=converted_kernel.transpose((3,2,1,0))

        weights_tensorflow=[converted_kernel, bias]

        layer_tensorflow.set_weights(weights_tensorflow)

    else:
        layer_tensorflow.set_weights(layer_theano.get_weights())

在原始代码中,有一个测试用例:预测是基于猫的图像进行的. 我已经下载了一张猫图片,并尝试使用原始模型285进行测试. 转换后的模型预测为585.

In the original code, there is a testcase: Prediction ran on the image of a cat. I've downloaded a cat image and tried the testcase with the original model: 285. The converted model predicts 585.

我不知道285是否是猫的正确标签,但是即使不是,也应该以相同的方式破坏这两个模型,我期望得出相同的预测.

I don't know whether 285 is the correct label for a cat, but even if it isn't, the two models should be broken in the same way, I would expect the same prediction.

在模型之间转换权重的正确方法是什么?

What is the correct way of converting weights between models ?

推荐答案

您是对的.该代码已损坏.到目前为止,此问题已有解决方法,解决方案在这里.

You are right. The code is broken. As of now, there is a work around for this issue and the solution is described here.

我已经对其进行了测试,并且对我有用.

I have tested it myself and it worked for me.

如果您认为答案是有用的,请对其进行投票.谢谢!

If you feel the answer is useful, please upvote it. Thanks!

这篇关于Keras:在theano和tensorflow之间转换预训练的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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