dump weights of cnn in json using keras

て烟熏妆下的殇ゞ 提交于 2019-12-09 13:15:11

问题


I want to use the dumped weights and model architecture in other framework for testing.

I know that:

  • model.get_config() can give the configuration of the model
  • model.to_json returns a representation of the model as a JSON string, but that the representation does not include the weights, only the architecture
  • model.save_weights(filepath) saves the weights of the model as a HDF5 file

I want to save the architecture as well as weights in a json file.


回答1:


Keras does not have any built-in way to export the weights to JSON.

Solution 1:

For now you can easily do it by iterating over the weights and saving it to the JSON file.

weights_list = model.get_weights()

will return a list of all weight tensors in the model, as Numpy arrays.

Then, all you have to do next is to iterate over this list and write to the file:

for i, weights in enumerate(weights_list):
    writeJSON(weights)

Solution 2:

import json
weights_list = model.get_weights()
print json.dumps(weights_list.tolist())


来源:https://stackoverflow.com/questions/43971649/dump-weights-of-cnn-in-json-using-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!