Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
copy chainer's layer weights to Keras' layer
def copy_weights_deconvolution(chainer_model, keras_model, layer_name):
deconv_chainer = chainer_model[layer_name]
W, b = (deconv_chainer.W.data, deconv_chainer.b.data)
keras_model.get_layer(layer_name).set_weights([numpy.transpose(W, (2, 3, 0, 1)), b])
def copy_weights_convolution(chainer_model, keras_model, layer_name):
conv_chainer = chainer_model[layer_name]
W, b = (conv_chainer.W.data, conv_chainer.b.data)
keras_model.get_layer(layer_name).set_weights([numpy.transpose(W, (2, 3, 1, 0)), b])
def copy_weights_bn(chainer_model, keras_model, layer_name):
bn_chainer = chainer_model[layer_name]
w = [bn_chainer.gamma.data, bn_chainer.beta.data, bn_chainer.avg_mean, bn_chainer.avg_var]
keras_model.get_layer(layer_name).set_weights(w)
@Jannick-v
Copy link

Jannick-v commented Aug 31, 2017

This is great!
However, have you tested this on any pretrained model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment