0

when I try to load a model in Azure ML with below code I get an error. anyone know how to fix the issue with Azure?

from tensorflow import keras
keras.models.load_model('model.h5')
AttributeError                            Traceback (most recent call last)
    Input In [24], in <cell line: 2>()
          1 from tensorflow import keras
    ----> 2 keras.models.load_model('model_base-3.h5')
    
    File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py:184, in load_model(filepath, custom_objects, compile)
        181 with generic_utils.CustomObjectScope(custom_objects or {}):
        182   if (h5py is not None and (
        183       isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
    --> 184     return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
        186   if sys.version_info >= (3, 4) and isinstance(filepath, pathlib.Path):
        187     filepath = str(filepath)
    
    File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py:176, in load_model_from_hdf5(filepath, custom_objects, compile)
        174 if model_config is None:
        175   raise ValueError('No model found in config file.')
    --> 176 model_config = json.loads(model_config.decode('utf-8'))
        177 model = model_config_lib.model_from_config(model_config,
        178                                            custom_objects=custom_objects)
        180 # set weights
    
    AttributeError: 'str' object has no attribute 'decode'

1 Answers1

0

When we are calling the model we first need to consider tensorflow library and before that OS library.

import os

import tensorflow as tf
from tensorflow import keras

In the given question, tensorflow was not called and even if it was imported, to load the H5 model, we need to use tensorflow. Replace the existing code block of loading with the below code block.

import os

import tensorflow as tf
from tensorflow import keras

tf.keras.models.load_model(filepath, custom_objects=None, compile=True)

this is the procedure to call the model to solve the current issue.

Sairam Tadepalli
  • 1,563
  • 1
  • 3
  • 11