keras_to_tensorflow icon indicating copy to clipboard operation
keras_to_tensorflow copied to clipboard

Couldn't load model from json

Open tunicashashi opened this issue 6 years ago • 8 comments

I am using anaconda and python 3 for converting model but getting issue like:

E1108 14:25:02.245650 139965222659840 keras_to_tensorflow.py:77] Couldn't load model from json. Traceback (most recent call last): File "keras_to_tensorflow.py", line 61, in load_model model = keras.models.load_model(input_model_path) File "/home/intel_ecrd3/anaconda3/envs/my_env/lib/python3.6/site-packages/keras/engine/saving.py", line 419, in load_model model = _deserialize_model(f, custom_objects, compile) File "/home/intel_ecrd3/anaconda3/envs/my_env/lib/python3.6/site-packages/keras/engine/saving.py", line 221, in _deserialize_model model_config = f['model_config'] File "/home/intel_ecrd3/anaconda3/envs/my_env/lib/python3.6/site-packages/keras/utils/io_utils.py", line 302, in getitem raise ValueError('Cannot create group in read only mode.') ValueError: Cannot create group in read only mode.

Any suggestion about what could be solution.

tunicashashi avatar Nov 08 '18 14:11 tunicashashi

@tunicashashi I got the same problem, do you have solution now?

NorthLatitudeOne avatar Jan 16 '19 05:01 NorthLatitudeOne

Nop, I didn't. I worked with another model after the issue.

tunicashashi avatar Jan 16 '19 07:01 tunicashashi

Please share the command you are using to run the keras_to_tensorflow script.

amir-abdi avatar Jan 16 '19 21:01 amir-abdi

I knew the problem where I got, I converted MASK RCNN model to tensorflow model, but the MASK RCNN model only saved weights not model all, it could not be loaded model successful.

NorthLatitudeOne avatar Jan 22 '19 02:01 NorthLatitudeOne

For Keras 2.2.4 and Tensorflow 1.12.0 after research I found a solution.

Save model weights & architecture like:

model_json = model.to_json()
open('architecture.json', 'w').write(model_json)
model.save_weights('weights.h5', overwrite=True)

And for converting a model to CoreML .mlmodel I use:

import coremltools

from keras.layers import DepthwiseConv2D, ReLU
from pathlib import Path
from keras.models import model_from_json
from tensorflow.python.keras.utils.generic_utils import CustomObjectScope

model_architecture = './Networks/architecture.json'
model_weights = './Networks/weights.h5'

model_structure = Path(model_architecture).read_text()

with CustomObjectScope({'relu6': ReLU ,'DepthwiseConv2D': DepthwiseConv2D}):
    model = model_from_json(model_structure)
    model.load_weights(model_weights)

    output_labels = ['0', '1', '2', '3', '4', '5', '6']
    coreml_model = coremltools.converters.keras.convert(
        model, input_names=['image'], output_names=['output'],
        class_labels=output_labels, image_input_names='image')

    coreml_model.save('ModelX.mlmodel')

relax94 avatar Jan 31 '19 18:01 relax94

The kears_to_tensorflow tool covers both

  • separate json and weight files by calling model_from_json and subsequently load_weights,
  • and also loading the entire model as a single file using load_model.

So, however you call the tool, as long as you set the right FLAGS (arguments), it should work. Please read the README file, and also share the command you are using to load your models.

amir-abdi avatar Feb 01 '19 20:02 amir-abdi

I got the same problem when I do this tool on windows,the python's version is 3.6.8, tensorflow's version is 1.12.0 ,tf.keras's version is 2.1.6-tf . then,when I do this tool on ubuntu,It's OK.the python's version is 3.6.7,tensorflow's version is 1.13.1,tf.keras'version is 2.2.4-tf.

jinhshi avatar Mar 20 '19 03:03 jinhshi

Can someone please help me with the error I am getting "Cannot create group in read only mode"?

ashhasthantra avatar May 02 '19 22:05 ashhasthantra