tensorflow model into a caffe model and predict call

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
2
3
4
5
6
7
8
9
10
11
12
13
layer {
name: "input"
type: "Input"
top: "data"
input_param {
shape {
dim: 1
dim: 32
dim: 1
dim: 1
}
}
}

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
2
3
4
5
6
7
8
9
layer{
name:"linear"
type:"InnerProduct"
bottom:"data"
top:"linear"
inner_product_param {
num_output: 1024
}
}

The next layer is the number of vectors num_output
(3) BatchNorm layer

1
2
3
4
5
6
7
8
9
layer {
bottom: "linear"
top: "bn1"
name: "bn1"
type: "BatchNorm"
batch_norm_param {
use_global_stats: true
}
}

This argument is true in the testing phase, the training phase is false
(4) Scale Layer

1
2
3
4
5
6
7
8
9
layer {
bottom: "bn1"
top: "bn1"
name: "scale1"
type: "Scale"
scale_param {
bias_term: true
}
}

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
2
3
4
5
6
layer {
bottom: "bn1"
top: "ReLU1"
name: "ReLU1"
type: "ReLU"
}

(6)Eltwise

1
2
3
4
5
6
7
layer {
bottom: "sum_up"
bottom: "ReLU5"
top: "sum_up2"
name: "sum_up2"
type:"Eltwise"
}

In this residual network layer need be added.

Written document with the model parameters python code

(1) read tensorflow model

1
2
3
4
5
6
7
8
9
10
import tensorflow as tf
sys.path.insert(0,"/path_to_caffe/python")
print(sys.path)
import caffe
import numpy as np
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
from tensorflow.python import pywrap_tensorflow
checkpoint_path = "./checkpoint-4874200"
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()

(在tensorflow较高版本中,模型文档三个 分别为xxx.meta xxxx.index xxx.0000-data-0001)
(2)创建caffe网络

1
2
cf_prototxt = "./2dto3d_deploy.prototxt"
net = caffe.Net(cf_prototxt, caffe.TEST)

(3)将参数读取写入
全解连接层的w和b

1
2
3
4
5
6
7
8
linear_w=np.squeeze(reader.get_tensor('linear_model/w1'))
tmp=np.linalg.norm(linear_w)
linear_w1=linear_w*1*(tmp>1)/tmp + linear_w*(tmp<=1)
net.params['linear'][0].data[:]=np.transpose(linear_w1, (1,0))


linear_b=np.squeeze(reader.get_tensor('linear_model/b1'))
net.params['linear'][1].data[:]=linear_b

BN层由于tensoflow和caffe有差异
在转化的时候记住caffe的bn+scale层等于tensorflow的bn层次
具体的转化有以下规则:

1
2
3
4
5
6
net.params[bn_name][0].data[:] = tf_movingmean
#epsilon 0.001 is the default value used by tf.contrib.layers.batch_norm!!
net.params[bn_name][1].data[:] = tf_movingvariance + 0.001
net.params[bn_name][2].data[:] = 1 # important, set it to be 1
net.params[scale_name][0].data[:] = tf_gamma
net.params[scale_name][1].data[:] = tf_beta

相关的具体讨论可以参考:bn层转化
下面给出bn层转化的一个试例

1
2
3
4
5
6
7
8
9
10
bn1_tf_movingmean=np.squeeze(reader.get_tensor('linear_model/batch_normalization/moving_mean'))
bn1_tf_movingvariance=np.squeeze(reader.get_tensor('linear_model/batch_normalization/moving_variance'))
bn1_tf_gamma=np.squeeze(reader.get_tensor('linear_model/batch_normalization/gamma'))
bn1_tf_beta=np.squeeze(reader.get_tensor('linear_model/batch_normalization/beta'))
net.params['bn1'][0].data[:] = bn1_tf_movingmean
# epsilon 0.001 is the default value used by tf.contrib.layers.batch_norm!!
net.params['bn1'][1].data[:] = bn1_tf_movingvariance+ 0.001
net.params['bn1'][2].data[:] = 1 # important, set it to be 1
net.params['scale1'][0].data[:] = bn1_tf_gamma
net.params['scale1'][1].data[:] = bn1_tf_beta

(3)最后写入文档

1
net.save('a.caffemodel')

调用caffe模型预测验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import sys
sys.path.insert(0,"/path_to_caffe/python")
print(sys.path)
import caffe
import numpy as np
a=[-1.59036,2.64035,-1.84236,2.63166,-1.60718,4.62976,-1.53916,5.21137,-1.26383,2.65532,-1.29271,4.93954,-1.38703,5.27324,-1.58944,0.633358,-1.59042,1.12005,-1.53584,0.696239,-1.08071,0.598781,-0.603272,-0.938632,0.0140431,-2.17247,-1.89872,0.648127,-2.23398,-0.798227,-2.80334,-1.86967]
p2d=np.array(a)
y=np.reshape(p2d,(1,32,1,1))
caffe.set_mode_gpu()
model_def = './2dto3d_deploy.prototxt'
model_pretrained ='./a.caffemodel'
net = caffe.Net(model_def,model_pretrained,caffe.TEST)
net.blobs['data'].data[...]=y
out = net.forward() #前像传播预测
print (out)

以上测试数据的正确预测结果为:
{‘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/

Guess you like

Origin www.cnblogs.com/chinatrump/p/11415135.html