首页 > 解决方案 > ValueError:未知初始化程序:GlorotUniform

问题描述

我正在尝试将我的模型转换,而不是加载到 CoreML,但它一直给我这个错误,我不知道如何修复它。

这是我的代码:

import keras
import coremltools

import tensorflow as tf

model = tf.keras.models.load_model('machine')

print(model.summary())

output_labels = ['0', '1']

model = coremltools.converters.keras.convert('machine', input_names= 
['text'], output_names=['output'])

model.author = 'Aarush'

model.short_description = 'My model'

model.input_description['text'] = 'Takes text as an input'

model.output_description['output'] = 'Prediction of CyberBullying 
sentiment'

model.save('xcodeModel.mlmodel')

ERROR:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-62-5bde9de3c9f8> in <module>()
      1 output_labels = ['0', '1']
      2 
----> 3 model = coremltools.converters.keras.convert('machine', input_names=['text'], output_names=['output'])
      4 
      5 model.author = 'Aarush'

21 frames
/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    138             if cls is None:
    139                 raise ValueError('Unknown ' + printable_module_name +
--> 140                                  ': ' + class_name)
    141         if hasattr(cls, 'from_config'):
    142             custom_objects = custom_objects or {}

ValueError: Unknown initializer: GlorotUniform

附言

我试过做import keras,而且tf.kerascreateObject但他们都没有工作。任何帮助将不胜感激。

谢谢

标签: tensorflowkeras

解决方案


转换后的kerascoremltools 只支持keras模型,不支持tf.keras. 对于后一个库,您需要使用tensorflow转换器,并使用以下功能:

coremltools.converters.tensorflow.convert

更多信息在源代码的注释这里。您还需要转换参数,因为它采用模型文件名 (a .h5),而不是模型实例。例如:

import coremltools
from tensorflow.keras.applications import ResNet50

model = coremltools.converters.tensorflow.convert(
    './model.h5',
     input_name_shape_dict={'input_1': (1, 224, 224, 3)},
     output_feature_names=['Identity']
)

推荐阅读