Azure ML Studio笔记本导入MobileNetV2模型触发AttributeError报错
Azure ML Studio 导入MobileNetV2报错'str' object has no attribute 'decode' 排查方案
问题现象
在Azure ML Studio笔记本环境中导入Keras内置的MobileNetV2模型时触发报错,完全相同的代码在本地机器Jupyter Notebook中可正常运行。
报错触发阶段为模型权重加载环节,核心异常为:AttributeError: 'str' object has no attribute 'decode'
复现代码
import tensorflow as tf from tensorflow import keras model = tf.keras.applications.MobileNetV2()
完整报错日志
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [23], in <cell line: 1>() ----> 1 model = tf.keras.applications.MobileNetV2() File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/applications/mobilenet_v2.py:405, in MobileNetV2(input_shape, alpha, include_top, weights, input_tensor, pooling, classes, classifier_activation, **kwargs) 402 weight_path = BASE_WEIGHT_PATH + model_name 403 weights_path = data_utils.get_file( 404 model_name, weight_path, cache_subdir='models') --> 405 model.load_weights(weights_path) 406 elif weights is not None: 407 model.load_weights(weights) File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:250, in Model.load_weights(self, filepath, by_name, skip_mismatch) 246 if (self._distribution_strategy.extended.steps_per_run > 1 and 247 (not network._is_hdf5_filepath(filepath))): # pylint: disable=protected-access 248 raise ValueError('Load weights is not yet supported with TPUStrategy ' 249 'with steps_per_run greater than 1.') --> 250 return super(Model, self).load_weights(filepath, by_name, skip_mismatch) File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py:1266, in Network.load_weights(self, filepath, by_name, skip_mismatch) 1263 hdf5_format.load_weights_from_hdf5_group_by_name( 1264 f, self.layers, skip_mismatch=skip_mismatch) 1265 else: -> 1266 hdf5_format.load_weights_from_hdf5_group(f, self.layers) File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py:659, in load_weights_from_hdf5_group(f, layers) 648 """Implements topological (order-based) weight loading. 649 650 Arguments: (...) 656 and weights file. 657 """ 658 if 'keras_version' in f.attrs: --> 659 original_keras_version = f.attrs['keras_version'].decode('utf8') 660 else: 661 original_keras_version = '1' AttributeError: 'str' object has no attribute 'decode'
故障原因
这是典型的依赖版本不兼容问题:
- Azure ML默认的
azureml_py38环境中预装的h5py版本为3.x系列,该版本读取HDF5文件属性时会直接返回字符串类型,不需要额外做字节转字符串的decode操作 - 环境中预装的TensorFlow为2.5及更早版本,这部分版本的Keras权重加载逻辑是适配
h5py2.x版本编写的,默认读取到的属性是字节类型,会主动调用.decode('utf8')做转码,遇到3.x版本返回的字符串时就会触发属性不存在的报错
本地环境要么h5py停留在2.x版本,要么TensorFlow版本已经升级到适配h5py3.x的新版本,所以不会出现该问题。
解决方法
二选一即可,优先推荐第一种,和Azure ML默认环境的其他依赖冲突最少:
- 方案1:降级h5py到兼容的2.x版本,在Notebook单元格中执行以下命令,安装完成后重启内核再运行代码即可
!pip install h5py==2.10.0 - 方案2:升级TensorFlow到2.6及以上版本,新版本已经修复了h5py 3.x的兼容问题,执行以下命令后重启内核即可
!pip install --upgrade tensorflow
内容的提问来源于stack exchange,提问作者Skyward
相关产品推荐
相关产品推荐

