0

I have a real problem with converting Keras model's convolutional kernels and most preferably dense weights (but that is not my priority) from theano to tensorflow. I made some research but I just can't handle it. First I used this script: Keras: convert pretrained weights between theano and tensorflow

But than my accuracy fell from 85% to worse than random (12% in 8 classes) so I guess something is wrong with it.

Than I tried to use titu1994 code aka: https://github.com/titu1994/Keras-Classification-Models/blob/master/weight_conversion_theano.py

Although I just can't get it done because I get error during the script about running OOM on my paperspace virtual machine and I can't get access to better one. The weights are vgg16_bn.h5 from fast.ai deep learning course. I would highly appreciate any help if you had similiar problem before or maybe if some1 had converted these weights to tensorflow format I would love if you decide to share.

Or if you know other weights and models I can succesfully perform transfer learning from I would appreciate if you share as well.

1 Answers1

1

In Keras 2, the conversion of convolutional kernels is performed automatically in load_weights(). In your case, the automatic conversion failed because you're trying to load a Keras 1 weight file, and there's no backend information saved in Keras 1 weight files.

So all you need to do is to put an attribute named backend into the weight file, and let Keras take care of the rest.

import h5py
with h5py.File(weight_file, 'a') as f:
    f.attrs['backend'] = 'theano'.encode('utf8')
Yu-Yang
  • 14,539
  • 2
  • 55
  • 62