Hi,
I am trying to load a model architecture from a JSON file. The file accompanies a paper for this published model, but I am unable to load it into R via the keras::model_from_json() function. I have reached out to the author, but have yet to hear back.
I can't tell if there is an issue with the JSON file itself, or perhaps it is on the R/keras side of things.
library(reticulate)
use_condaenv('r-reticulate', conda = '/opt/anaconda3/bin/conda')
keras::model_from_json("CNN21.json")
Error in py_call_impl(callable, dots$args, dots$keywords) :
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Detailed traceback:
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/saving/model_config.py", line 114, in model_from_json
config = json.loads(json_string)
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
md5-879b610884bd637171fc37b2e229e772
$class_name
[1] "Sequential"
$keras_version
[1] "1.1.1"
$config
class_name config.b_regularizer config.W_constraint
config.b_constraint config.name
config.activity_regularizer config.trainable
config.dim_ordering config.nb_col config.subsample
config.init config.bias config.nb_filter config.input_dtype
config.border_mode config.batch_input_shape
config.W_regularizer config.activation config.nb_row
config.pool_size config.strides config.p config.input_dim
config.output_dim
[ reached getOption("max.print") -- omitted 5 rows ]
[ reached 'max' / getOption("max.print") -- omitted 24 rows ]
```
@mattwarkentin, keras::model_from_json expects json string as argument, not the filename. Please add readLines() in your code to get the json string, then call model_from_json. Can you please try below and check if it works for you?
library(reticulate)
use_condaenv('r-reticulate', conda = '/opt/anaconda3/bin/conda')
json_string <- readLines("CNN21.json")
keras::model_from_json(json_string)
Ahh, I now see that. When reading in the JSON via readLines() I now get the following error when trying to load the model from JSON:
Error in py_call_impl(callable, dots$args, dots$keywords) :
AttributeError: 'list' object has no attribute 'items'
Detailed traceback:
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/saving/model_config.py", line 116, in model_from_json
return deserialize(config, custom_objects=custom_objects)
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 109, in deserialize
printable_module_name='layer')
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 362, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "/opt/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 325, in class_and_config_for_serialized_keras_object
for key, item in cls_config.items():
I think the problem here is that this file was generated with an older version of Keras and there were broken changes that made the file unloadable with a recent version.