Read only mode in keras

Here is an example Git gist created on Google Collab for you: https://gist.github.com/kolygri/835ccea6b87089fbfd64395c3895c01f

As far as I understand:

You have to set and define the architecture of your model and then use model.load_weights('alexnet_weights.h5').

Here is a useful Github conversation link, which hopefully will help you understand the issue better: https://github.com/keras-team/keras/issues/6937


I used callbacks.ModelCheckpoint to save the weights and I had a similar error. I found out that there is a parameter called save_weights_only

If I set save_weights_only=True, then when I use load_model() to load the model in another process, it will raise the 'Cannot create group in read only mode.' error.

If I set save_weights_only=False(which is the default), then I can use load_model() to load the model and use it to do prediction, without compiling the model first.


I had a similar issue and solved this way

store the graph\architecture in JSON format and weights in h5 format

import json

# lets assume `model` is main model 
model_json = model.to_json()
with open("model_in_json.json", "w") as json_file:
    json.dump(model_json, json_file)

model.save_weights("model_weights.h5")

then need to load model first to create graph\architecture and load_weights in model

from keras.models import load_model
from keras.models import model_from_json
import json

with open('model_in_json.json','r') as f:
    model_json = json.load(f)

model = model_from_json(model_json)
model.load_weights('model_weights.h5')