I would like to use this pre-trained model .
This is in theano layout, my code depends on the order in which the dimensions of the tensor flow are measured.
There is a guide for converting weights between formats .
But it seems broken. In the section for converting theano to tensor flow, the first instruction is to load the weights into the tensor flow model.
The Keras backend in this case should be TensorFlow. First load the Theano-trained weights into your TensorFlow model:
model.load_weights('my_weights_theano.h5')
This raises an exception, weight schemes would be incompatible. And if load_weights function took the weights for the tensor flow model, they do not need to be converted.
I looked at the convert_kernel function to find out if I could take the necessary steps myself.
The code is pretty simple - I don’t understand why the manual uses a tensor flow session. This seems unnecessary.
I copied the code from a pre-trained model to create a model with a tensor flow layer. It just meant changing the input form and backend.image_dim_ordering before adding any convolutions. Then I used this loop:
model is the original model created from the code that I linked at the beginning. model_tensorflow is exactly the same model, but with a tensor layout.
for i in range(len(model.layers)): layer_theano=model.layers[i] layer_tensorflow=model_tensorflow.layers[i] if layer_theano.__class__.__name__ in ['Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D']: weights_theano=layer_theano.get_weights() kernel=weights_theano[0] bias=weights_theano[1] converted_kernel=convert_kernel(kernel, "th") converted_kernel=converted_kernel.transpose((3,2,1,0)) weights_tensorflow=[converted_kernel, bias] layer_tensorflow.set_weights(weights_tensorflow) else: layer_tensorflow.set_weights(layer_theano.get_weights())
There is a test scenario in the source code: the prediction is performed on a cat image. I downloaded the cat image and tried a test case with the original model: 285. The converted model predicts 585.
I don’t know if mark 285 is the correct mark for a cat, but even if it isn’t, the two models should be broken the same way, I expect the same forecast.
How to convert weights between models?