Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in load_model function #1

Open
shriyavanvari opened this issue Dec 2, 2019 · 8 comments
Open

Problem in load_model function #1

shriyavanvari opened this issue Dec 2, 2019 · 8 comments

Comments

@shriyavanvari
Copy link

I get the following error on running:

Traceback (most recent call last):
File "main.py", line 30, in
main(arg_parser.parse_args())
File "main.py", line 13, in main
yolo = YOLO(config)
File "/Users/shriyavanvari/Documents/SJSU/Semester 1/CMPE 257/CARLA/Code/traffic-light-detection-module/yolo.py", line 113, in init
self.feature_extractor = TinyYoloFeature(self.image_h, config).feature_extractor
File "/Users/shriyavanvari/Documents/SJSU/Semester 1/CMPE 257/CARLA/Code/traffic-light-detection-module/yolo.py", line 56, in init
pretrained = load_model('checkpoints/' + config['model']['saved_model_name'], custom_objects={'custom_loss': dummy_loss, 'tf': tf})
File "/Users/shriyavanvari/anaconda3/lib/python3.7/site-packages/keras/engine/saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "/Users/shriyavanvari/anaconda3/lib/python3.7/site-packages/keras/engine/saving.py", line 584, in load_model
model = _deserialize_model(h5dict, custom_objects, compile)
File "/Users/shriyavanvari/anaconda3/lib/python3.7/site-packages/keras/engine/saving.py", line 270, in _deserialize_model
model_config = h5dict['model_config']
File "/Users/shriyavanvari/anaconda3/lib/python3.7/site-packages/keras/utils/io_utils.py", line 318, in getitem
raise ValueError('Cannot create group in read-only mode.')
ValueError: Cannot create group in read-only mode.
Can you please help out?

@filipbasara0
Copy link
Contributor

filipbasara0 commented Dec 2, 2019

Hi,

Can you please specify which Tensorflow and Keras versions you are using?

@shriyavanvari
Copy link
Author

Keras 2.3.1
Tensorflow 1.14.0

@filipbasara0
Copy link
Contributor

filipbasara0 commented Dec 2, 2019

Can you try using Keras 2.2.4 and Tensorflow 1.12.0? I tested it out with those versions and it seemed to be working fine.

@spadesM
Copy link

spadesM commented Dec 30, 2019

this error happened to me because there is no pretrained model.
"pretrained = load_model('checkpoints\' + config['model']['saved_model_name'], custom_objects={'custom_loss': dummy_loss, 'tf': tf})"

you can find from description.

@Kangsan-Jeon
Copy link

Kangsan-Jeon commented Jan 9, 2020

When I ran 'detect_test_images.py' with Windows10, keras(2.2.4) and tensorflow(1.15.0), error occurred 'load_model' operation. The error was 'ValueError: Cannot create group in read only mode.'. In my case, I made 'checkpoints' directory and put your 'traffic-light-detection.h5' file in 'checkpoints' folder. How can I solve this problem?

I also modified json file like below.

{
"model" :
{
"image_h": 416,
"image_w": 416,
"anchors": [0.24,0.79, 0.80,2.12],
"max_obj": 5,
"grid_h": 13,
"grid_w": 13,
"num_classes": 2,
"classes": ["go", "stop"],
"obj_thresh": 0.20,
"nms_thresh": 0.01,
"saved_model_name": "traffic-light-detection.h5",
"obj_scale": 85,
"no_obj_scale": 75,
"coord_scale": 70,
"class_scale": 70
},

"plot_model":           true,

"train": {
    "enabled":              false,
    "annot_file_name":      "carla_all.csv",
    "batch_size":           8,
    "learning_rate":        1e-4,
    "nb_epochs":            500,
    "warmup_batches":       0,
    "debug":                true
}

}

@masskro0
Copy link

masskro0 commented Aug 9, 2020

I have the same problem as @Kangsan-Jeon .

@masskro0
Copy link

masskro0 commented Aug 10, 2020

Ok I could fix this issue. You need to access the model attribute of the YOLO class, e.g.
model = YOLO(config)
and then you can call
yolo.model.load_weights("checkpoints\\traffic-light-detection.h5")
That is how you load the pretrained model. But you have to make several changes to the code.
In yolo.py in the TinyYoloFeature class, i removed the following lines:
pretrained = load_model('checkpoints\\' + config['model']['saved_model_name'], custom_objects={'custom_loss': dummy_loss, 'tf': tf})
pretrained = pretrained.get_layer('model_1')
idx = 0
for layer in self.feature_extractor.layers:
print(layer.name)
layer.set_weights(pretrained.get_layer(index=idx).get_weights())
idx += 1

as well as in the YOLO class the following lines:
pretrained = load_model('checkpoints\\' + config['model']['saved_model_name'], custom_objects={'custom_loss': self.custom_loss, 'tf': tf})
self.model.get_layer('DetectionLayer').set_weights(pretrained.get_layer('DetectionLayer').get_weights())

Now none of these classes loads the model and you can load the pretrained model as I described above. Hope this helps.

@aeermis
Copy link

aeermis commented Nov 21, 2020

hello, I get the following error, could you please help me about it?

This function in the predict.py file;

1- def get_model_from_file(config):
2- path = os.path.join(BASE_DIR, 'checkpoints', config['model']['saved_model_name'])
3- model = load_model(path, custom_objects={'custom_loss': dummy_loss})
4- return model

I get the error:

SavedModel file does not exist at: C:\Users\90531\Source\Repos\traffic-light-detection-module\checkpoints\model.09-2.01.h5/{saved_model.pbtxt|saved_model.pb}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants