(AttributeError: 'NoneType' object has no attribute 'get' ) 在 tensorflow 2.1 中加载保存的 .h5 扩展名的 keras 模

     2023-02-23     207

关键词:

【中文标题】(AttributeError: \'NoneType\' object has no attribute \'get\' ) 在 tensorflow 2.1 中加载保存的 .h5 扩展名的 keras 模型时【英文标题】:(AttributeError: 'NoneType' object has no attribute 'get' ) while loading saved keras model with .h5 extension in tensorflow 2.1(AttributeError: 'NoneType' object has no attribute 'get' ) 在 tensorflow 2.1 中加载保存的 .h5 扩展名的 keras 模型时 【发布时间】:2020-05-30 00:30:02 【问题描述】:

我有一个使用 tensorflow 的 feature_column api 的 keras 模型,我可以将模型保存为 .h5 扩展名,但在加载保存的模型时在 colab 中出现以下错误。

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-38-8a2d51a054f6> in <module>()
----> 1 new_model = tf.keras.models.load_model('model.h5')

13 frames

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    318       obj = _GLOBAL_CUSTOM_OBJECTS[object_name]
    319     else:
--> 320       obj = module_objects.get(object_name)
    321       if obj is None:
    322         raise ValueError('Unknown ' + printable_module_name + ':' + object_name)

AttributeError: 'NoneType' object has no attribute 'get'

以下是我的代码,我使用的是 tensorlow 2.1

import pandas as pd
import tensorflow as tf 
import numpy as np


feature_layer = tf.keras.layers.DenseFeatures(feature_columns)

def model_builder():


      tf.keras.backend.clear_session()
      model = tf.keras.Sequential()
      model.add(feature_layer)
      model.add(tf.keras.layers.Dense(32, activation='relu'))
      model.add(tf.keras.layers.Dense(32, activation='relu'))
      model.add(tf.keras.layers.Dense(1))


      model.compile(optimizer = tf.keras.optimizers.RMSprop(0.001),
                    loss=tf.keras.losses.Huber(),
                    metrics=['mae'])


      return model
model = model_builder()
model.fit(train_ds , epochs=5,verbose=1)

model.save('model.h5')


new_model = tf.keras.models.load_model('model.h5')

在本地环境中运行代码时出现以下错误,即在 conda 环境中使用 jupyter notebook。但是本地环境有 tf 2.0


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-fbbaf2c35e78> in <module>
----> 1 new_model = tf.keras.models.load_model('model1.h5')

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
    144   if (h5py is not None and (
    145       isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 146     return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    147 
    148   if isinstance(filepath, six.string_types):

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
    166     model_config = json.loads(model_config.decode('utf-8'))
    167     model = model_config_lib.model_from_config(model_config,
--> 168                                                custom_objects=custom_objects)
    169 
    170     # set weights

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/model_config.py in model_from_config(config, custom_objects)
     53                     '`Sequential.from_config(config)`?')
     54   from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
---> 55   return deserialize(config, custom_objects=custom_objects)
     56 
     57 

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/serialization.py in deserialize(config, custom_objects)
    100       module_objects=globs,
    101       custom_objects=custom_objects,
--> 102       printable_module_name='layer')

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    189             custom_objects=dict(
    190                 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 191                 list(custom_objects.items())))
    192       with CustomObjectScope(custom_objects):
    193         return cls.from_config(cls_config)

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py in from_config(cls, config, custom_objects)
    367     for layer_config in layer_configs:
    368       layer = layer_module.deserialize(layer_config,
--> 369                                        custom_objects=custom_objects)
    370       model.add(layer)
    371     if not model.inputs and build_input_shape:

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/serialization.py in deserialize(config, custom_objects)
    100       module_objects=globs,
    101       custom_objects=custom_objects,
--> 102       printable_module_name='layer')

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    189             custom_objects=dict(
    190                 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 191                 list(custom_objects.items())))
    192       with CustomObjectScope(custom_objects):
    193         return cls.from_config(cls_config)

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/feature_column/feature_column_v2.py in from_config(cls, config, custom_objects)
    450     config_cp = config.copy()
    451     config_cp['feature_columns'] = serialization.deserialize_feature_columns(
--> 452         config['feature_columns'], custom_objects=custom_objects)
    453 
    454     return cls(**config_cp)

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/feature_column/serialization.py in deserialize_feature_columns(configs, custom_objects)
    188   return [
    189       deserialize_feature_column(c, custom_objects, columns_by_name)
--> 190       for c in configs
    191   ]

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/feature_column/serialization.py in <listcomp>(.0)
    188   return [
    189       deserialize_feature_column(c, custom_objects, columns_by_name)
--> 190       for c in configs
    191   ]

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/feature_column/serialization.py in deserialize_feature_column(config, custom_objects, columns_by_name)
    141       cls_config,
    142       custom_objects=custom_objects,
--> 143       columns_by_name=columns_by_name)
    144 
    145   # If the name already exists, re-use the column from columns_by_name,

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/feature_column/feature_column_v2.py in _from_config(cls, config, custom_objects, columns_by_name)
   2871     kwargs = _standardize_and_copy_config(config)
   2872     kwargs['normalizer_fn'] = generic_utils.deserialize_keras_object(
-> 2873         config['normalizer_fn'], custom_objects=custom_objects)
   2874     kwargs['dtype'] = dtypes.as_dtype(config['dtype'])
   2875 

~/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    206       obj = _GLOBAL_CUSTOM_OBJECTS[object_name]
    207     else:
--> 208       obj = module_objects.get(object_name)
    209       if obj is None:
    210         raise ValueError('Unknown ' + printable_module_name + ':' + object_name)

AttributeError: 'NoneType' object has no attribute 'get'

【问题讨论】:

有时会在导入 tensorflow 和 keras 时出现一些混淆,您能说明如何在代码中导入 keras 和 tensorflow 我已经添加了导入代码,请检查 4 是否相同。 尝试用这种方式导入import tensorflow as tffrom tensorflow import keras 按照你说的尝试过,但结果相同。 检查过tf的版本是2.1吗? 【参考方案1】:

此错误可能是由于您的模型中有自定义逻辑,而在调用 load_modelmodel_from_json 等时未在 custom_objects 参数中提供自定义逻辑。

在我的例子中,这是一个传递给layers.Lambda()的函数:

@tf.function()
def shift_cols(x):
    """
    Transforms (32, 32, 3) to (3, 32, 32) 
    """
    return tf.transpose(x, [0, 3, 1, 2])

input_layer = Input(shape=(32, 32, 3), dtype='float32', name='features')
x = Lambda(shift_cols, input_shape=(32, 32, 3))(input_layer)  # shape=(3, 32, 32)
x = Flatten()(x) 
x = Dense(64, activation='softmax')(x) 
model = Model(inputs=input_layer, outputs='output': x)
model.save('./model_path/')

要加载没有指定错误的模型,您必须在加载调用的custom_objects 参数中提供shift_cols 函数:

model = tf.keras.models.load_model('./model_path/', custom_objects=
    'shift_cols': shift_cols
)

【讨论】:

【参考方案2】:

这就是我解决问题的方法。 我查看了以下网址:- https://github.com/tensorflow/tensorflow/issues/31927

我将我的功能列、模型全部放在一个单元格中,并且完全按照上述网址中上一篇文章中的人所做的那样。 但是在上面的 url 中,指标列中的 dtype 是 float 但我用 int32 替换了它,因为我遇到了其他错误并且它有效


#**************************** our main model  *********************************

keras.backend.clear_session()





feature_columns = []
feature_layer_inputs =  
feature_columns.append(tf.feature_column.numeric_column('col_name1'))  
feature_layer_inputs['col_name1'] = tf.keras.Input(shape=(1,), name='col_name1')  

feature_columns.append(tf.feature_column.numeric_column('col_name2')) 
feature_layer_inputs['col_name2'] = tf.keras.Input(shape=(1,), name='col_name2')  

feature_columns.append(tf.feature_column.numeric_column('col_name3'))    
feature_layer_inputs['col_name3'] = tf.keras.Input(shape=(1,), name='col_name3')  

feature_columns.append(tf.feature_column.numeric_column('col_name4'))    
feature_layer_inputs['col_name4'] = tf.keras.Input(shape=(1,), name='col_name4')  

feature_columns.append(tf.feature_column.numeric_column('col_name5'))    
feature_layer_inputs['col_name5'] = tf.keras.Input(shape=(1,), name='col_name5')  

feature_columns.append(tf.feature_column.numeric_column('col_name6'))    
feature_layer_inputs['col_name6'] = tf.keras.Input(shape=(1,), name='col_name6')  

col_name7  = tf.feature_column.categorical_column_with_identity('col_name7',10,default_value=None)
feature_columns.append(tf.feature_column.indicator_column(col_name7))
feature_layer_inputs['col_name7'] = tf.keras.Input(shape=(1,), name='col_name7', dtype= tf.int32)


col_name8  = tf.feature_column.categorical_column_with_identity('col_name8',50,default_value=None)
feature_columns.append(tf.feature_column.indicator_column(col_name8))
feature_layer_inputs['col_name8'] = tf.keras.Input(shape=(1,), name='col_name8', dtype= tf.int32)

col_name9  = tf.feature_column.categorical_column_with_identity('col_name9',12,default_value=None)
feature_columns.append(tf.feature_column.indicator_column(col_name9))
feature_layer_inputs['col_name9'] = tf.keras.Input(shape=(1,), name='col_name9', dtype= tf.int32)

feature_layer = tf.keras.layers.DenseFeatures(feature_columns)
feature_layer_outputs = feature_layer(feature_layer_inputs)


x = keras.layers.Dense(32, activation='relu')(feature_layer_outputs)
x = keras.layers.Dense(32, activation='relu')(x)
output = keras.layers.Dense(1)(x)

model = keras.Model(inputs=[v for v in feature_layer_inputs.values()], outputs=output)

model.compile(optimizer='adam',loss='mae')

model.fit(train_ds , epochs=50,verbose=1)

现在保存或加载模型时没有错误

【讨论】:

AttributeError:“NumpyArrayIterator”对象没有属性“类”

】AttributeError:“NumpyArrayIterator”对象没有属性“类”【英文标题】:AttributeError:\'NumpyArrayIterator\'objecthasnoattribute\'classes\'【发布时间】:2020-02-2516:26:51【问题描述】:我收到此错误:AttributeError:\'NumpyArrayIterator\'对象没有属性\'cl... 查看详情

sklearn 中的 AttributeError

】sklearn中的AttributeError【英文标题】:AttributeErrroratsklearn【发布时间】:2019-04-1210:15:11【问题描述】:我刚刚使用pip安装了sklearn。当我尝试导入任何东西时,我得到一个AttributeError。importsklearn#Noerrorfromsklearnimportsvm#AttributeErrorfrom... 查看详情

AttributeError:“字节”对象没有属性“告诉”

】AttributeError:“字节”对象没有属性“告诉”【英文标题】:AttributeError:\'bytes\'objecthasnoattribute\'tell\'【发布时间】:2021-11-2912:11:34【问题描述】:我正在使用email.message和smtplib使用python发送电子邮件。当图像作为附件发送时,... 查看详情

AttributeError: 'RDD' 对象没有属性 'show'

】AttributeError:\\\'RDD\\\'对象没有属性\\\'show\\\'【英文标题】:AttributeError:\'RDD\'objecthasnoattribute\'show\'AttributeError:\'RDD\'对象没有属性\'show\'【发布时间】:2019-05-0605:40:57【问题描述】:frompysparkimportSparkContext,SparkConf,sqlfrompys 查看详情

AttributeError:模块 'dbus' 没有属性 'lowlevel'

】AttributeError:模块\\\'dbus\\\'没有属性\\\'lowlevel\\\'【英文标题】:AttributeError:module\'dbus\'hasnoattribute\'lowlevel\'AttributeError:模块\'dbus\'没有属性\'lowlevel\'【发布时间】:2021-12-2321:53:32【问题描述】:错误信息:Traceback(mostrecentcalllast... 查看详情

AttributeError:模块'keras'没有属性'initializers'

】AttributeError:模块\\\'keras\\\'没有属性\\\'initializers\\\'【英文标题】:AttributeError:module\'keras\'hasnoattribute\'initializers\'AttributeError:模块\'keras\'没有属性\'initializers\'【发布时间】:2017-09-0116:57:02【问题描述】:我正在尝试将keras.init... 查看详情

AttributeError:“列表”对象没有属性“numpy”

】AttributeError:“列表”对象没有属性“numpy”【英文标题】:AttributeError:\'list\'objecthasnoattribute\'numpy\'【发布时间】:2022-01-2309:47:00【问题描述】:我正在尝试使用以下代码将列表转换为数组:[t.numpy()fortinsuper_final_predictions]但是... 查看详情

AttributeError:“模块”对象没有属性“WebSocketApp”

】AttributeError:“模块”对象没有属性“WebSocketApp”【英文标题】:AttributeError:\'module\'objecthasnoattribute\'WebSocketApp\'【发布时间】:2017-06-2718:10:22【问题描述】:我正在尝试使用WebSocketApp使用python连接到API,但我似乎无法做到。无... 查看详情

AttributeError:“会话”对象没有属性“会话”

】AttributeError:“会话”对象没有属性“会话”【英文标题】:AttributeError:\'Session\'objecthasnoattribute\'session\'【发布时间】:2022-01-1604:31:20【问题描述】:我正在创建一个端点,它将使用带有python的fastApi实现反转。我有一个端点,... 查看详情

AttributeError:模块'asyncio'没有属性'run'

】AttributeError:模块\\\'asyncio\\\'没有属性\\\'run\\\'【英文标题】:AttributeError:module\'asyncio\'hasnoattribute\'run\'AttributeError:模块\'asyncio\'没有属性\'run\'【发布时间】:2019-03-2511:20:20【问题描述】:我正在尝试使用asyncio运行以下程序:... 查看详情

初学者 Python:AttributeError:'list' 对象没有属性

】初学者Python:AttributeError:\\\'list\\\'对象没有属性【英文标题】:BeginnerPython:AttributeError:\'list\'objecthasnoattribute初学者Python:AttributeError:\'list\'对象没有属性【发布时间】:2015-06-0219:57:45【问题描述】:错误提示:AttributeError:\'... 查看详情

AttributeError:“字节”对象没有属性“编码”

】AttributeError:“字节”对象没有属性“编码”【英文标题】:AttributeError:\'bytes\'objecthasnoattribute\'encode\'【发布时间】:2019-02-0209:20:32【问题描述】:我正在尝试构建一个用户登录系统,并且我已经成功构建了用户注册页面,但... 查看详情

AttributeError:“测试”对象没有属性“a”

】AttributeError:“测试”对象没有属性“a”【英文标题】:AttributeError:\'Test\'objecthasnoattribute\'a\'【发布时间】:2020-08-2901:33:42【问题描述】:我收到AttributeError:\'Test\'objecthasnoattribute\'a\'。我不明白代码有什么问题。请指教。classT... 查看详情

AttributeError:模块 'urllib' 没有属性 'parse'

】AttributeError:模块\\\'urllib\\\'没有属性\\\'parse\\\'【英文标题】:AttributeError:module\'urllib\'hasnoattribute\'parse\'AttributeError:模块\'urllib\'没有属性\'parse\'【发布时间】:2017-05-2023:26:02【问题描述】:python3.5.2代码1importurllibs=urllib.par 查看详情

AttributeError: 'float' 对象没有属性 'split'

】AttributeError:\\\'float\\\'对象没有属性\\\'split\\\'【英文标题】:AttributeError:\'float\'objecthasnoattribute\'split\'AttributeError:\'float\'对象没有属性\'split\'【发布时间】:2017-07-0215:46:18【问题描述】:我正在拨打此行:lang_modifiers=[keyw.strip()f... 查看详情

AttributeError:模块'mysql'没有属性'connector'

】AttributeError:模块\\\'mysql\\\'没有属性\\\'connector\\\'【英文标题】:AttributeError:module\'mysql\'hasnoattribute\'connector\'AttributeError:模块\'mysql\'没有属性\'connector\'【发布时间】:2018-03-1205:47:45【问题描述】:我正在运行Ubuntu16.04,试图... 查看详情

AttributeError: 'NoneType' 对象没有属性 'loader'

】AttributeError:\\\'NoneType\\\'对象没有属性\\\'loader\\\'【英文标题】:AttributeError:\'NoneType\'objecthasnoattribute\'loader\'AttributeError:\'NoneType\'对象没有属性\'loader\'【发布时间】:2019-07-0813:12:21【问题描述】:今天我启动笔记本电脑(Ubuntu18.4... 查看详情

AttributeError:模块“cupy”没有属性“cupyx”

】AttributeError:模块“cupy”没有属性“cupyx”【英文标题】:AttributeError:module\'cupy\'hasnoattribute\'cupyx\'【发布时间】:2019-12-2010:43:11【问题描述】:当我运行它时,我有这个python代码,它说AttributeError:模块\'cupy\'没有属性\'cupyx\'代... 查看详情