For a project that I'm working on, I have created a simple model in TensorFlow that consists of a dense features layer followed by three dense layers.
def build_model(arguments):
model = tf.keras.Sequential([
tf.keras.layers.DenseFeatures(arguments),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(5, activation='sigmoid')
])
return model
I am unable to go into more detail about the parameter arguments
, but the above model function works perfectly fine and can train and save a .h5
file perfectly fine using the code below.
# Create a path for the saving location of the model
model_dir = log_dir + "\model.h5"
# Save the model
model.save(model_dir)
However, when I try to load the model back from the .h5
file,
model = tf.keras.models.load_model(model_path)
I get the following error message.
File "sampleModel.py", line 342, in <module>
model = tf.keras.models.load_model(model_path)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\saving\save.py", line 1
82, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, c
ompile)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\saving\hdf5_format.py",
line 178, in load_model_from_hdf5
custom_objects=custom_objects)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\saving\model_config.py"
, line 55, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\layers\serialization.py
", line 175, in deserialize
printable_module_name='layer')
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py"
, line 358, in deserialize_keras_object
list(custom_objects.items())))
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\engine\sequential.py",
line 487, in from_config
custom_objects=custom_objects)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\layers\serialization.py
", line 175, in deserialize
printable_module_name='layer')
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py"
, line 358, in deserialize_keras_object
list(custom_objects.items())))
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\keras\feature_column\base_fea
ture_layer.py", line 141, in from_config
config['feature_columns'], custom_objects=custom_objects)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\feature_column\serialization.
py", line 186, in deserialize_feature_columns
for c in configs
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\feature_column\serialization.
py", line 186, in <listcomp>
for c in configs
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\feature_column\serialization.
py", line 138, in deserialize_feature_column
columns_by_name=columns_by_name)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\feature_column\feature_column
_v2.py", line 2622, in from_config
config['normalizer_fn'], custom_objects=custom_objects)
File "C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Python
\Python37\site-packages\tensorflow\python\feature_column\serialization.
py", line 273, in _deserialize_keras_object
obj = module_objects.get(object_name)
AttributeError: 'NoneType' object has no attribute 'get'
Looking around, I suspect it has something to do with the custom_objects
tag for the load_model
function, but I am not 100% sure of how to implement it.