Skip to content

Instantly share code, notes, and snippets.

@Hiroshiba
Created December 23, 2016 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hiroshiba/549052c175356949cba5162151916307 to your computer and use it in GitHub Desktop.
Save Hiroshiba/549052c175356949cba5162151916307 to your computer and use it in GitHub Desktop.
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

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