Load in Python a CNN keras model saved in R

135 views
Skip to first unread message

khawla....@gmail.com

unread,
Feb 7, 2020, 5:26:49 PM2/7/20
to Keras-users


I want to upload a CNN keras model saved from an R session with save_model_hdf5(model, "model.h5") in a Python session with load_model('model.h5')


I am able to load in R a saved model in Python but when I load an R model in Python I am getting this error:


Traceback
(most recent call last): File "<stdin>", line 1, in <module> File "//lib/python3.6/site-packages/keras/engine/saving.py", line 492, in load_wrapper return load_function(*args, **kwargs) File "//lib/python3.6/site-packages/keras/engine/saving.py", line 584, in load_model model = _deserialize_model(h5dict, custom_objects, compile) File "//lib/python3.6/site-packages/keras/engine/saving.py", line 274, in _deserialize_model model = model_from_config(model_config, custom_objects=custom_objects) File "//lib/python3.6/site-packages/keras/engine/saving.py", line 627, in model_from_config return deserialize(config, custom_objects=custom_objects) File "//lib/python3.6/site-packages/keras/layers/__init__.py", line 168, in deserialize printable_module_name='layer') File "//lib/python3.6/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object list(custom_objects.items()))) File "//lib/python3.6/site-packages/keras/engine/sequential.py", line 302, in from_config model.add(layer) File "//lib/python3.6/site-packages/keras/engine/sequential.py", line 182, in add output_tensor = layer(self.outputs[0]) File "//lib/python3.6/site-packages/keras/engine/base_layer.py", line 463, in __call__ self.build(unpack_singleton(input_shapes)) File "//lib/python3.6/site-packages/keras/layers/normalization.py", line 94, in build dim = input_shape[self.axis] TypeError: tuple indices must be integers or slices, not list

Lance Norskog

unread,
Feb 7, 2020, 9:15:03 PM2/7/20
to khawla....@gmail.com, Keras-users
Which version of Keras is this?

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/9d81938d-ac31-4e6a-9b71-41054954a653%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA

khawla....@gmail.com

unread,
Feb 10, 2020, 9:45:49 AM2/10/20
to Keras-users
Keras version in R: keras_2.2.5.0    tensorflow_2.0.0

Keras version in Python: keras_2.3.1    tensorflow_1.14.0








On Friday, February 7, 2020 at 9:15:03 PM UTC-5, Lance Norskog wrote:
Which version of Keras is this?

To unsubscribe from this group and stop receiving emails from it, send an email to keras...@googlegroups.com.

Lance Norskog

unread,
Feb 10, 2020, 12:29:48 PM2/10/20
to khawla....@gmail.com, Keras-users
Try Tensorflow 2.x in Python. This may be a version discrepancy.

To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/a7dd45de-bf97-4709-a50e-03d98e303a70%40googlegroups.com.

khawla....@gmail.com

unread,
Feb 10, 2020, 1:17:57 PM2/10/20
to Keras-users


I updated tensorflow version:


Keras version in R: keras_2.2.5.0    tensorflow_2.0.0



Keras version in Python: keras_2.3.1    tensorflow_2.1.0


I am getting an other error :

 


Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/lib/python3.6/site-packages/keras/engine/saving.py", line 492, in load_wrapper

return load_function(*args, **kwargs)

File "/lib/python3.6/site-packages/keras/engine/saving.py", line 584, in load_model

model = _deserialize_model(h5dict, custom_objects, compile)

File "/lib/python3.6/site-packages/keras/engine/saving.py", line 274, in _deserialize_model

model = model_from_config(model_config, custom_objects=custom_objects)

File "/lib/python3.6/site-packages/keras/engine/saving.py", line 627, in model_from_config

return deserialize(config, custom_objects=custom_objects)

File "/lib/python3.6/site-packages/keras/layers/__init__.py", line 168, in deserialize

printable_module_name='layer')

File "/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object

list(custom_objects.items())))

File "/lib/python3.6/site-packages/keras/engine/sequential.py", line 301, in from_config

custom_objects=custom_objects)

File "/lib/python3.6/site-packages/keras/layers/__init__.py", line 168, in deserialize

printable_module_name='layer')

File "/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 149, in deserialize_keras_object

return cls.from_config(config['config'])

File "/lib/python3.6/site-packages/keras/engine/base_layer.py", line 1179, in from_config

return cls(**config)

File "/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper

return func(*args, **kwargs)

File "/lib/python3.6/site-packages/keras/layers/convolutional.py", line 353, in __init__

**kwargs)

File "/lib/python3.6/site-packages/keras/layers/convolutional.py", line 117, in __init__

self.kernel_initializer = initializers.get(kernel_initializer)

File "/lib/python3.6/site-packages/keras/initializers.py", line 515, in get

return deserialize(identifier)

File "/lib/python3.6/site-packages/keras/initializers.py", line 510, in deserialize

printable_module_name='initializer')

File "/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 149, in deserialize_keras_object

return cls.from_config(config['config'])

File "/lib/python3.6/site-packages/keras/initializers.py", line 30, in from_config

return cls(**config)

File "/lib/python3.6/site-packages/keras/initializers.py", line 204, in __init__

'but got', distribution)

ValueError: ('Invalid `distribution` argument: expected one of {"normal", "uniform"} but got', 'truncated_normal')



On Monday, February 10, 2020 at 12:29:48 PM UTC-5, Lance Norskog wrote:
Try Tensorflow 2.x in Python. This may be a version discrepancy.

Lance Norskog

unread,
Feb 12, 2020, 10:27:21 PM2/12/20
to khawla....@gmail.com, Keras-users
> ValueError: ('Invalid `distribution` argument: expected one of {"normal", "uniform"} but got', 'truncated_normal')

Aha! This is a discrepancy between R and Python in the "custom object" defaults. It is added in Tensorflow 2.1:

You can also fix this by registering a 'truncated_normal' initializer object. This is only used in training, so if you only want to run predictions, this does not actually have to be an initializer object.

But this does bring up a problem: Is it worth adding a "read-only" mode to load_model()? For the sake of resilience across platforms?

You can also resolve this by building the model in Python, then using load_weights() to read the R version. This really should work.

Cheers,

Lance



To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/99e332e7-642b-40a0-a591-85087892de65%40googlegroups.com.
Message has been deleted
Message has been deleted
Message has been deleted

khawla....@gmail.com

unread,
Feb 13, 2020, 11:26:38 AM2/13/20
to Keras-users

Thank you for your help,

I tried to use Tensorflow 2.1 to identify if the problem comes from the version, but it does not contain get_session function (even tf.compat.v1.Session() didn’t solve the problem). So, I downgraded Tensorflow version to 1.14


I didn’t find the ‘read-only’ argument in load_model (load_model_weights_hdf5(object, filepath, by_name = FALSE, skip_mismatch = FALSE, reshape = FALSE))


When using load_model_weights_hdf5('my_weights.h5') in R I am getting this error :

Erreur : $ operator is invalid for atomic vectors


Best,

Khawla

Lance Norskog

unread,
Feb 13, 2020, 4:22:03 PM2/13/20
to khawla....@gmail.com, Keras-users
I thought that you were trying to load an R-created model file in Python?

I would try load_weights() under Tensorflow 2.0 in Python.

If that does not work, you have found a fundamental problem in Keras cross-compatibility.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.

Lance Norskog

unread,
Feb 13, 2020, 4:23:51 PM2/13/20
to khawla....@gmail.com, Keras-users
This is starting to sound like an "x, y" problem:
"Please help me do X"
"Why do you want to do X?"
"Because I want to achieve Y"
"Y is not a good idea"

What is the target goal here?

Cheers,

Lance

pablo....@gmail.com

unread,
Jun 20, 2020, 9:52:11 PM6/20/20
to Keras-users
I am submitting an assignment for a course on Deep Learning, the TA uses Python but I feel more comfortable using Keras/Tensorflow from R/Anaconda/RStudio, specially parallelization works better on my multiple CPU-only laptop when running R than python. So after all my development and testing I provide a Jupiter Python notebook where the TA can run my final model, load the weights from the hdf5 file saved from my RStudio Notebook or Jupyter/R notebook and voilà, minimal chance of incompatibility issues. Please test as extensively as possible before handing your job to others.
Pablo
Reply all
Reply to author
Forward
0 new messages