将Tensorflow模型转换为Caffe模型 [英] Convert Tensorflow model to Caffe model

查看:229
本文介绍了将Tensorflow模型转换为Caffe模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将Tensorflow模型转换为Caffe模型.

I would like to be able to convert a Tensorflow model to Caffe model.

我在Google上进行了搜索,但是我只能找到从caffe到tensorflow的转换器,而没有找到相反的东西.

I searched on google but I was able to find only converters from caffe to tensorflow but not the opposite.

有人知道怎么做吗?

谢谢,艾维(Evi)

推荐答案

我遇到了同样的问题,并找到了解决方案.可以在这里找到代码( https://github.com/lFatality/tensorflow2caffe ),我已经还记录了一些Youtube视频中的代码.

I've had the same problem and found a solution. The code can be found here (https://github.com/lFatality/tensorflow2caffe) and I've also documented the code in some Youtube videos.

第1部分涵盖了 tflearn (用于TensorFlow的高级API,对本机TensorFlow的代码进行一些更改也应适用).

Part 1 covers the creation of the architecture of VGG-19 in Caffe and tflearn (higher level API for TensorFlow, with some changes to the code native TensorFlow should also work).

第2部分中,将权重和偏差从TensorFlow模型导出到描述了一个numpy文件.在tflearn中,您可以像这样获得图层的权重:

In Part 2 the export of the weights and biases out of the TensorFlow model into a numpy file is described. In tflearn you can get the weights of a layer like this:

#get parameters of a certain layer
conv2d_vars = tflearn.variables.get_layer_variables_by_name(layer_name)
#get weights out of the parameters
weights = model.get_weights(conv2d_vars[0])
#get biases out of the parameters
biases = model.get_weights(conv2d_vars[1])

对于卷积层,layer_name是 Conv_2D .完全连接的层称为 FullyConnected .如果您使用了某种类型的多个图层,则会使用带有下划线的前导整数(例如,第二个conv图层称为 Conv_2D_1 ).我已经在TensorBoard的图表中找到了这些名称.如果您在体系结构定义中命名图层,则这些layer_names可能会更改为您定义的名称.

For a convolutional layer, the layer_name is Conv_2D. Fully-Connected layers are called FullyConnected. If you use more than one layer of a certain type, a raising integer with a preceding underscore is used (e.g. the 2nd conv layer is called Conv_2D_1). I've found these names in the graph of the TensorBoard. If you name the layers in your architecture definition, then these layer_names might change to the names you defined.

在原生TensorFlow中,导出将需要不同的代码,但是参数的格式应该相同,因此后续步骤仍然适用.

In native TensorFlow the export will need different code but the format of the parameters should be the same so subsequent steps should still be applicable.

第3部分涵盖了实际的转化.至关重要的是在创建caffemodel时权重的转换(偏差可以无变化地保留下来).保存过滤器时,TensorFlow和Caffe使用不同的格式.虽然TensorFlow使用 [高度,宽度,深度,过滤器数量] ( TensorFlow文档,位于底部),Caffe使用 [过滤器数量,深度,高度,宽度] ().要在格式之间进行转换,可以使用 transpose 函数(例如: weights_of_first_conv_layer.transpose((3,2,0,1)).3,2,0通过枚举TensorFlow格式(原始),然后将其切换为Caffe格式(目标格式),同时将数字保持在其特定变量,可以获得1个序列.)
如果要将张量输出连接到完全连接的层,则有些棘手.如果您使用输入大小为112x112的VGG-19,则它看起来像这样.

Part 3 covers the actual conversion. What's critical is the conversion of the weights when you create the caffemodel (the biases can be carried over without change). TensorFlow and Caffe use different formats when saving a filter. While TensorFlow uses [height, width, depth, number of filters] (TensorFlow docs, at the bottom), Caffe uses [number of filters, depth, height, width] (Caffe docs, chapter 'Blob storage and communication'). To convert between the formats you can use the transpose function (for example: weights_of_first_conv_layer.transpose((3,2,0,1)). The 3,2,0,1 sequence can be obtained by enumerating the TensorFlow format (origin) and then switching it to the Caffe format (target format) while keeping the numbers at their specific variable.).
If you want to connect a tensor output to a fully-connected layer, things get a little tricky. If you use VGG-19 with an input size of 112x112 it looks like this.

fc1_weights = data_file[16][0].reshape((4,4,512,4096))
fc1_weights = fc1_w.transpose((3,2,0,1))
fc1_weights = fc1_w.reshape((4096,8192))

如果在张量和完全连接的层之间的连接处导出参数,则从TensorFlow得到的结果是一个数组,其形状为 [张量中的项,fc层中的单位] (此处: [8192,4096] ).您必须找出输出张量的形状,然后重新调整数组的形状,使其适合TensorFlow格式(请参见上文,过滤器数是fc中的单位数-layer ).之后,您可以使用之前使用的转置转换,然后再次对数组进行整形,但反之亦然.TensorFlow将fc层权重保存为 [输入数量,输出数量] ,而Caffe则相反.如果您将两个fc层相互连接,则不必执行前面描述的复杂过程,但是您必须通过再次转置来解决不同的fc层格式( fc_layer_weights.transpose(((1,0)))

What you get from TensorFlow if you export the parameters at the connection between tensor and fully-connected layer is an array with the shape [entries in the tensor, units in the fc-layer] (here: [8192, 4096]). You have to find out what the shape of your output tensor is and then reshape the array so that it fits the TensorFlow format (see above, number of filters being the number of units in the fc-layer). After that you use the transpose-conversion you've used previously and then reshape the array again, but the other way around. While TensorFlow saves fc-layer weights as [number of inputs, number of outputs], Caffe does it the other way around.
If you connect two fc-layers to each other, you don't have to do the complex process previously described but you will have to account for the different fc-layer format by transposing again (fc_layer_weights.transpose((1,0)))

然后您可以使用设置网络参数

You can then set the parameters of the network using

net.params['layer_name_in_prototxt'][0].data[...] = weights
net.params['layer_name_in_prototxt'][1].data[...] = biases

这是一个快速概述.如果您需要所有代码,它们都在我的github存储库中.希望对您有所帮助.:)

This was a quick overview. If you want all the code, it's in my github repository. I hope it helps. :)

干杯,
死亡

这篇关于将Tensorflow模型转换为Caffe模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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