tensorflow model into a caffe model and predict call
Deploy.prototxt article is divided into three parts written first caffe according to the network structure of the code tensorflow, and then write the code XXXX.caffemodel python documentation, last call caffe model predictions.
Write caffe according to the network structure of the code tensorflow of deploy.prototxt
After writing the code can be entered here (tool) to detect the wording is correct:
verification tools
writing rules below, I give every type of its parameters must be written argument, if you want to know more detailed parameters for each layer, can refer to: Caffe Detailed download network structure
name is the name of each layer, top layers is that the layer is subjected to the legend, bottom layer of the hierarchy data that
(1) the input layer input:
1 |
layer { |
Be sure to specify the shape layer, first as a dim batchsize is, how much can one-time data processing, dim as the second channel, if the process is an image that is the number of channels of the picture. If the process is a vector, this is the vector the length, the height of the third image is dim, dim is the fourth width of the image. If the third and fourth vectors are 1.
(2) full connection layer InnerProduct
1 |
layer{ |
The next layer is the number of vectors num_output
(3) BatchNorm layer
1 |
layer { |
This argument is true in the testing phase, the training phase is false
(4) Scale Layer
1 |
layer { |
When the conversion model, tensorflow a normalization layer corresponds to a continuous caffe + Scale batchNorm:
the Convert to BATCH normalization layer in tensorflow caffe: TF in IS. 1 batchnorm equivalent to Layer A Layer Successive of TWO: batchNorm + Scale:
(. 5 ) RELU
1 |
layer { |
(6)Eltwise
1 |
layer { |
In this residual network layer need be added.
Written document with the model parameters python code
(1) read tensorflow model
1 |
import tensorflow as tf |
(在tensorflow较高版本中,模型文档三个 分别为xxx.meta xxxx.index xxx.0000-data-0001)
(2)创建caffe网络
1 |
cf_prototxt = "./2dto3d_deploy.prototxt" |
(3)将参数读取写入
全解连接层的w和b
1 |
linear_w=np.squeeze(reader.get_tensor('linear_model/w1')) |
BN层由于tensoflow和caffe有差异
在转化的时候记住caffe的bn+scale层等于tensorflow的bn层次
具体的转化有以下规则:
1 |
net.params[bn_name][0].data[:] = tf_movingmean |
相关的具体讨论可以参考:bn层转化
下面给出bn层转化的一个试例
1 |
bn1_tf_movingmean=np.squeeze(reader.get_tensor('linear_model/batch_normalization/moving_mean')) |
(3)最后写入文档
1 |
net.save('a.caffemodel') |
调用caffe模型预测验证
1 |
import sys |
以上测试数据的正确预测结果为:
{‘linear6’: array([[-1.1093297 , 0.13423912, -0.71337676, -0.3413014 , 0.60594785,
-0.503688 , -0.44055757, 0.61926687, -0.3807128 , 1.1093313 ,
-0.13424 , 0.7133761 , 0.57371044, 0.56901824, -0.12641048,
0.05005788, 0.47413373, 0.14192167, -0.3304365 , -0.7288994 ,
0.4668903 , 0.06698397, -0.6077603 , 0.33841628, 0.02581951,
-0.36136347, -0.09946679, 0.07204475, -0.29733503, 0.13283135,
0.85128194, -1.0867463 , 0.6229229 , 1.1905218 , -3.798849 ,
0.13268307, 1.8069856 , -3.7732291 , -0.5529848 , -0.9289112 ,
-0.9280683 , 0.14142033, -1.392531 , -2.9245243 , -0.55180794,
-1.8918784 , -2.9869053 , -1.0472283 ]], dtype=float32)}
附件下载
最后我给出了一个示例转换的所有文档附件下载地址
其中有如下内容:
(1) linear_model.py 为网络结构的定义文档可以参考这个文档的代码按照规则写出
(2) 2dto3d_deploy.prototxt
(3)checkpoint*文档是tensorflow模型文档
(4)caffe_script.py为写模型参数文档运行之后生产a.caffemodel
(5)predict.py根据2dto3d_deploy.prototxt和a.caffemodel进行预测看结果是否正确已经给出一组测试数据
(6)caffe网络模型深入理解各层详解.pdf
原文链接 大专栏 https://www.dazhuanlan.com/2019/08/19/5d59ba6a9af46/