Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read only mode in keras

Tags:

I have cloned human pose estimation keras model from this link human pose estimation keras

When I try to load the model on google colab, I get the following error

code

from keras.models import load_model model = load_model('model.h5') 

error

ValueError                                Traceback (most recent call   last) <ipython-input-29-bdcc7d8d338b> in <module>()       1 from keras.models import load_model ----> 2 model = load_model('model.h5')  /usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)     417     f = h5dict(filepath, 'r')     418     try: --> 419         model = _deserialize_model(f, custom_objects, compile)     420     finally:     421         if opened_new_file:  /usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in _deserialize_model(f, custom_objects, compile)     219         return obj     220  --> 221     model_config = f['model_config']     222     if model_config is None:     223         raise ValueError('No model found in config.')  /usr/local/lib/python3.6/dist-packages/keras/utils/io_utils.py in __getitem__(self, attr)     300             else:     301                 if self.read_only: --> 302                     raise ValueError('Cannot create group in read only mode.')     303                 val = H5Dict(self.data.create_group(attr))     304         return val  ValueError: Cannot create group in read only mode. 

Can someone please help me understand this read-only mode? How do I load this model?

like image 917
Debadri Chowdhury Avatar asked Nov 08 '18 17:11

Debadri Chowdhury


2 Answers

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

like image 196
Konstantin Grigorov Avatar answered Sep 24 '22 06:09

Konstantin Grigorov


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') 
like image 43
Akhilesh_IN Avatar answered Sep 20 '22 06:09

Akhilesh_IN