lvapeab / nmt-keras

Neural Machine Translation with Keras
http://nmt-keras.readthedocs.io
MIT License
533 stars 130 forks source link

score.py fails #112

Closed vinodjoseph closed 4 years ago

vinodjoseph commented 4 years ago

I am trying to use the nmt-keras library and am testing the score.py method. However, score fails to execute since load_model in cnn_model.py does not load the .h5 model even if path variable to the load_model is given correctly. How do I create the json model to share it to this path if that is the expected model/weight file ?

<<< Loading model from /home/XYZ/epoch_19_structure.json' ... >>> Traceback (most recent call last): File "/home/XYZ/site-packages/keras_wrapper/cnn_model.py", line 168, in loadModel model = load_model(model_name + '.h5', custom_objects=custom_objects, compile=compile_model) File "/home/XYZ/site-packages/keras/engine/saving.py", line 419, in load_model model = _deserialize_model(f, custom_objects, compile) File "/home/XYZ/site-packages/keras/engine/saving.py", line 225, in _deserialize_model model = model_from_config(model_config, custom_objects=custom_objects) File "/home/XYZ/site-packages/keras/engine/saving.py", line 458, in model_from_config return deserialize(config, custom_objects=custom_objects) File "/home/XYZ/site-packages/keras/layers/init.py", line 55, in deserialize printable_module_name='layer') File "/home/XYZ/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object list(custom_objects.items()))) File "/home/XYZ/site-packages/keras/engine/network.py", line 1022, in from_config process_layer(layer_data) File "/home/XYZ/site-packages/keras/engine/network.py", line 1008, in process_layer custom_objects=custom_objects) File "/home/XYZ/site-packages/keras/layers/init.py", line 55, in deserialize printable_module_name='layer') File "/home/XYZ/site-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object ': ' + class_name) ValueError: Unknown layer: RemoveMask

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/XYZ/nmt-keras-complex/score.py", line 61, in score_corpus(args, params) File "/home/XYZ/nmt-keras-complex/nmt_keras/apply_model.py", line 213, in score_corpus models = [loadModel(m, -1, full_path=True) for m in args.models] File "/home/XYZ/nmt-keras-complex/nmt_keras/apply_model.py", line 213, in models = [loadModel(m, -1, full_path=True) for m in args.models] File "/home/XYZ/site-packages/keras_wrapper/cnn_model.py", line 173, in loadModel model = model_from_json(open(model_name + '_structure.json').read(), custom_objects=custom_objects) FileNotFoundError: [Errno 2] No such file or directory: '/home/XYZ/nmt-keras-complex/trained_models/*/epoch_19_structure.json'

Process finished with exit code 1

lvapeab commented 4 years ago

Hello, can you please check that you are using our Keras' extended version?

vinodjoseph commented 4 years ago

Thank you. I used the Keras extended version and this works. Apologies for the error. I used the extended version previously and probably changed something when i could not input weights to the sample_ensemble.py. Anyways, the scoring works.

vinodjoseph commented 4 years ago

Please suggest how you can append weights using --weights for the models when compiling sample_ensemble.py since I have this error when trying to input weights (corresponding .h5 file) for inferencing. Do you have to build the weights.h5 file when building the model to overcome this error ?

I gave --weights with model.h5 file for every model given in the input and tested with just one model and weight as well.

----- Error with sample_ensemble.py in line 114 of apply_model.py --------------- File "sample_ensemble.py", line 62, in sample_ensemble(args, params) File "/home/XYZ/nmt-keras-complex/nmt_keras/apply_model.py", line 114, in sample_ensemble if len(model_weights) > 1: TypeError: object of type 'map' has no len()

Tried typecasting the map with list but to no avail

lvapeab commented 4 years ago

I was able to reproduce your error and indeed, it was a bug. I fixed it (https://github.com/lvapeab/nmt-keras/commit/c1c3eee2d4eb4a9d8fb1233c1fe08d88256fb49c). Updating the repo (git pull) should work.

Thanks for spotting this out!