【问题标题】:How to convert tf-js to tensorflow pb model如何将 tf-js 转换为 tensorflow pb 模型
【发布时间】:2020-08-19 14:30:04
【问题描述】:

我正在尝试加载 tfjs toxicity classifier 以在 Python 中加载

我试过tfjs-converter这种方式(我下载了json和shard文件)

tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras_saved_model model.json convert.h5

我已经从 tfjs-converter 尝试了 JavaScript-to-Python 部分的两个参数,这会引发:

2020-05-05 01:14:03.148408: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2300000000 Hz
2020-05-05 01:14:03.148659: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1c98840 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-05-05 01:14:03.148696: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
  File "/usr/local/bin/tensorflowjs_converter", line 8, in <module>
    sys.exit(pip_main())
  File "/usr/local/lib/python3.6/dist-packages/tensorflowjs/converters/converter.py", line 671, in pip_main
    main([' '.join(sys.argv[1:])])
  File "/usr/local/lib/python3.6/dist-packages/tensorflowjs/converters/converter.py", line 675, in main
    convert(argv[0].split(' '))
  File "/usr/local/lib/python3.6/dist-packages/tensorflowjs/converters/converter.py", line 636, in convert
    args.output_path)
  File "/usr/local/lib/python3.6/dist-packages/tensorflowjs/converters/converter.py", line 264, in dispatch_tensorflowjs_to_keras_saved_model_conversion
    model = keras_tfjs_loader.load_keras_model(config_json_path)
  File "/usr/local/lib/python3.6/dist-packages/tensorflowjs/converters/keras_tfjs_loader.py", line 217, in load_keras_model
    use_unique_name_scope=use_unique_name_scope)
  File "/usr/local/lib/python3.6/dist-packages/tensorflowjs/converters/keras_tfjs_loader.py", line 64, in _deserialize_keras_model
    model = tf.keras.models.model_from_json(json.dumps(model_topology_json))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/saving/model_config.py", line 96, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/layers/serialization.py", line 98, in deserialize
    layer_class_name = config['class_name']
KeyError: 'class_name'

我尝试的另一个 repo 是:tfjs-to-tf

还有以下内容: tfjs_graph_converter model.json convert/model.pb

加注:

TensorFlow.js Graph Model Converter

Graph model:    model.json
Output:         convert/model.pb
Target format:  tf_frozen_model

Converting.... Traceback (most recent call last):
  File "/usr/local/bin/tfjs_graph_converter", line 8, in <module>
    sys.exit(pip_main())
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/converter.py", line 127, in pip_main
    main([' '.join(sys.argv[1:])])
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/converter.py", line 137, in main
    convert(argv[0].split(' '))
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/converter.py", line 107, in convert
    api.graph_model_to_frozen_graph(args.input_path, args.output_path)
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/api.py", line 237, in graph_model_to_frozen_graph
    graph = load_graph_model(model_dir)
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/api.py", line 224, in load_graph_model
    return _convert_graph_model_to_graph(model_json, model_path)
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/api.py", line 202, in _convert_graph_model_to_graph
    graph_def = _convert_graph_def(topology)
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/api.py", line 139, in _convert_graph_def
    message_dict = _convert_attr_values(message_dict)
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/api.py", line 124, in _convert_attr_values
    _convert_string_attrs(node)
  File "/usr/local/lib/python3.6/dist-packages/tfjs_graph_converter/api.py", line 72, in _convert_string_attrs
    attrs = _find_if_has_key(node[attr_key], key=str_key, of_type=list)
KeyError: 'attr'

对此的任何帮助或关于如何将 tfjs 模型加载到 python 中的建议表示赞赏:)

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0 tensorflow.js


    【解决方案1】:

    最好使用https://github.com/patlevin/tfjs-to-tf 将您的 tfjs 模型转换为 tensorflow 模型。 然后你可以像普通的 tensorflow 模型一样使用 python

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 2020-03-31
      • 2018-09-12
      • 2020-05-23
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      相关资源
      最近更新 更多