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

<hr>

<p><a href="https://www.youtube.com/watch?v=9iJheyF7x4Y" rel="noreferrer">Part 1</a> covers the creation of the architecture of <a href="https://arxiv.org/pdf/1409.1556.pdf" rel="noreferrer">VGG-19</a> in Caffe and <a href="http://tflearn.org/" rel="noreferrer">tflearn</a> (higher level API for TensorFlow, with some changes to the code native TensorFlow should also work).</p>

<hr>

<p>In <a href="https://www.youtube.com/watch?v=LNsEfZV_24c" rel="noreferrer">Part 2</a> 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:  </p>

<pre><code>#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])
</code></pre>

<p>For a convolutional layer, the layer_name is <code>Conv_2D</code>. Fully-Connected layers are called <code>FullyConnected</code>. 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 <code>Conv_2D_1</code>). 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.  </p>

<p>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. </p>

<hr>

<p><a href="https://www.youtube.com/watch?v=kvXHOIn3-8s" rel="noreferrer">Part 3</a> 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 <code>[height, width, depth, number of filters]</code> (<a href="https://www.tensorflow.org/extend/tool_developers/" rel="noreferrer">TensorFlow docs, at the bottom</a>), Caffe uses <code>[number of filters, depth, height, width]</code> (<a href="http://caffe.berkeleyvision.org/tutorial/net_layer_blob.html" rel="noreferrer">Caffe docs, chapter 'Blob storage and communication'</a>). To convert between the formats you can use the <code>transpose</code> function (for example: <code>weights_of_first_conv_layer.transpose((3,2,0,1))</code>. 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.).<br>
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.</p>

<pre><code>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))
</code></pre>

<p>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 <code>[entries in the tensor, units in the fc-layer]</code> (here: <code>[8192, 4096]</code>). 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, <code>number of filters</code> being the <code>number of units in the fc-layer</code>). 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 <code>[number of inputs, number of outputs]</code>, Caffe does it the other way around.<br>
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 (<code>fc_layer_weights.transpose((1,0))</code>)</p>

<p>You can then set the parameters of the network using</p>

<pre><code>net.params['layer_name_in_prototxt'][0].data[...] = weights
net.params['layer_name_in_prototxt'][1].data[...] = biases
</code></pre>

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

<hr>

<p>Cheers,<br>
Fatality</p>
Convert Tensorflow model to Caffe model
<p>I would like to be able to convert a Tensorflow model to Caffe model.</p>

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

<p>Does anyone have an idea on how to do it?</p>

<p>Thanks,
Evi</p>