Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert tensorflow model to caffe model? #59

Open
cyh24 opened this issue Sep 9, 2016 · 34 comments
Open

how to convert tensorflow model to caffe model? #59

cyh24 opened this issue Sep 9, 2016 · 34 comments

Comments

@cyh24
Copy link

cyh24 commented Sep 9, 2016

How to convert a tensorflow .ckpt to caffe model? Is that possible without .prototxt?

Any help is appreciated.

@Dagalaki
Copy link

Have the same problem. I want to convert tensorflow model to caffe model.
Have you found any way on how to do it?

@soldier828
Copy link

Same problem. If you @cyh24 @Dagalaki find some solutions, please inform me.

@catsdogone
Copy link

catsdogone commented Feb 14, 2017

@ethereon could you provide some advises?

@ethereon
Copy link
Owner

The reverse conversion is fairly similar:

  1. Map TensorFlow ops (or groups of ops) to Caffe layers
  2. Transform parameters to match Caffe's expected format

Things are slightly trickier for step 1 when going from tf to caffe, since the equivalent of a caffe layer might be split into multiple tf sub ops. So pattern matching against the op signatures / scopes might be one approach for tackling this.

For certain ops like convolutions, you can avoid the transformation in step 2 by specifying a Caffe compatible ordering (eg: data_format = NCHW)

@catsdogone
Copy link

catsdogone commented Feb 21, 2017

@ethereon @cyh24 Thank you for your help. I am trying to convert inception-resnet-v2 model to caffe, and not sure about the param of BatchNorm layer.
Is it right if I make:
tfLayer/weights:0 -> caffeLayer_weights, ...[0].data
tfLayer/BatchNorm/beta:0 -> caffeLayerScale_bias, ...[1].data
tfLayer/BatchNorm/moving_mean:0 -> caffeLayerBn_mean, ...[0].data
tfLayer/BatchNorm/moving_variance:0 -> caffeLayerBn_var ? ...[1].data
I copy the parameters while the produced caffe model show bad classification results.

@Jerryzcn
Copy link

Jerryzcn commented Mar 23, 2017

@catsdogone I tried the same thing and my activation is off, and cannot get the same accuracy. I also specified the scale parameter in scale layer to 1, and set BatchNorm's moving average factor to 1. :(

@sskgit
Copy link

sskgit commented Apr 5, 2017

Same issue. Want to convert Tensorflow Inception V3 and ResNet model to Caffe. That will be great!

@Jerryzcn
Copy link

Jerryzcn commented Apr 7, 2017

Okay, I was able to achieve similar performance after changing the padding for the 1x7 and 7x1 filter to (0,3) and (3,0) instead of (1,2) and (2,1)

@nyyznyyz1991
Copy link

nyyznyyz1991 commented May 19, 2017

@Jerryzcn @catsdogone
Could you share how to transfer model from tensorflow to caffe?
rewrite caffe's prototxt from scratch based on tensorflow or write a transfer.py script to achieve it?

@Jerryzcn
Copy link

@nyyznyyz1991 I use pycaffe to generate the prototxt based on tensorflow. I cannot share it though.

@neobarney
Copy link

@Jerryzcn is it hard to code the conversion script with pycaffe?

@Jerryzcn
Copy link

@neobarney took me about 1 week.

@neobarney
Copy link

@Jerryzcn wow that's long, you're not planning to release it on github ? would be helpful to lots of peoples ! :)

@Jerryzcn
Copy link

@neobarney it should only take u 2-3 days, I spend half a week on figuring out why my activation does not match the original network.

@neobarney
Copy link

neobarney commented Jun 21, 2017 via email

@zmlmanly
Copy link

zmlmanly commented Jul 3, 2017

@Jerryzcn If I use tf.contrib.layers.batch_norm(input, scale=False) in Tensorflow, the "scale =False" means whether the gamma is zero in "y = gamma*x+beta".
the definition of contrib.layers.batch_norm in tensorflow:
def batch_norm(inputs,
decay=0.999,
center=True,
scale=False,
epsilon=0.001,
activation_fn=None,
param_initializers=None,
param_regularizers=None,
updates_collections=ops.GraphKeys.UPDATE_OPS,
is_training=True,
reuse=None,
variables_collections=None,
outputs_collections=None,
trainable=True,
batch_weights=None,
fused=False,
data_format=DATA_FORMAT_NHWC,
zero_debias_moving_mean=False,
scope=None,
renorm=False,
renorm_clipping=None,
renorm_decay=0.99):
scale: If True, multiply by gamma. If False, gamma is
not used. When the next layer is linear (also e.g. nn.relu), this can be
disabled since the scaling can be done by the next layer.

And how to set the param in batchnormlayer in Caffe to make the result same between tensorflow and caffe.

@Jerryzcn
Copy link

Jerryzcn commented Jul 4, 2017

@zmlmanly set scale to 1 on caffe should work

@zmlmanly
Copy link

zmlmanly commented Jul 4, 2017

@Jerryzcn Thank you very much. I’m converting tensorflow to caffe. I use tf.contrib.layers.batch_norm(input, scale=False) in Tensorflow, so there is only beta param in checkpoint, in your view,
caffeLayer:scale_layer_gamma=1
caffeLayer:scale_layer_beta=tfLayer/BatchNorm/beta:0
But I can not find the mean and variance in checkpoint, so how to set the mean and variance in caffe?

@zmlmanly
Copy link

zmlmanly commented Jul 4, 2017

@catsdogone Hi, I want to know how to save the moving_mean and the moving_variance param in tensorflow/batchnormlayer. I have check the param in my trained model of tensorflow, but there is no mean and variance in batchnormlayer. Thank you for your help.

@Jerryzcn
Copy link

Jerryzcn commented Jul 4, 2017

@zmlmanly I think I set them either to zero or one. I forgot which exactly.

@MayankSingal
Copy link

@zmlmanly @neobarney Were you able to get it running?

@jzhaosc
Copy link

jzhaosc commented Oct 14, 2017

Convert batch normalization layer in tensorflow to caffe: 1 batchnorm layer in tf is equivalent to a successive of two layer : batchNorm + Scale:
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

@lhCheung1991
Copy link

@jzhaosc
Thx, it helps a lot.

Be careful of the epsilon guys.

@jiezhicheng
Copy link

@catsdogone May I learn if you have successfully converted inception_resnet_v2 from tensorflow to caffe? Thank you.

@AddASecond
Copy link

when you are using tf.nn.batch_normalization,
tf movingvariance + 0.001 ==> caffe batchnorm bias
tf movingmean ==> caffe batchnorm weights
tf gamma ==> caffe scale weights
tf beta ==> caffe scale bias

@zhongchengyong
Copy link

Maybe you can see another open source library called MMdnn by Microsoft,it is the link:https://github.com/Microsoft/MMdnn

@AddASecond
Copy link

AddASecond commented Mar 7, 2018

@giticaniup yes I‘ve already down it with MMdnn, which is much easier and intuitive. The only question exists is how to make the image pre-process in tensorflow equals to that in caffe?

@cjerry1243
Copy link

I'm now converting tf mobilenet-v2 to caffe model.
I use the protrotxt here (https://github.com/shicai/MobileNet-Caffe)
and have converted all the params correctly, but cannot get the same accuracy

Do I miss any detail?

@AddASecond
Copy link

@cjerry1243 I have tried that before thus strongly recommend not to waste time using this repository on training Mobilenet in caffe. This repository uses caffe built-in group convolution, where the depthwise convolution implementation is a non-parallel 'for loop', also do not have a good cuda/cudnn support. The training process was very slow, so that it will take a lot of time to tune hyperparameters. Maybe you should try https://github.com/yonghenglh6/DepthwiseConvolution or other implementations instead.

@cjerry1243
Copy link

@bobauditore Thanks for your advice.
I still want to convert movilenet-v2 ckpt to caffe model

Except for the different "depthwise convolution"in that repository(https://github.com/shicai/MobileNet-Caffe), I found another problem during conversion.

The first conv layer output values
(caffe: net.params['conv1'][0].data and tf: sess.graph.get_tensor_by_name('MobilenetV2/Conv/Conv2D:0')) are different when I feed in the same preprocessed image. The only difference of the image input is channel last and first for tf and caffe.

Besides, I use np.swapaxes to swap tf variables and feed into caffe variables:
tf_var_shape: (height, width, depth, channel)
caffe_var shape: (channel, depth, height, width)

Where's the mistake of my conversion ?

@KeyKy
Copy link

KeyKy commented May 20, 2018

@cjerry1243 Do you solve the problem?

@cjerry1243
Copy link

cjerry1243 commented May 20, 2018

@KeyKy I have solved it !
The padding method is different in tf and caffe. Use pad=2 and followed by 2 slice layers when the conv stride=2, pad=1 in tf.

@KeyKy
Copy link

KeyKy commented May 21, 2018

@cjerry1243 could you share your code with me? It is hard to understand.


update: I see! Thanks!

@yzsatgithub
Copy link

Does anyone have any update about new tools to convert tf models to caffemodel? It's been a long time since the question was posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests