1

我遇到了一个问题,比如Loading saved_model 导致“无法编译片段着色器”用于收集操作 在此处输入图像描述

const MODEL_URL = './web_model/tensorflowjs_model.pb';
const WEIGHTS_URL = './web_model/weights_manifest.json';
async function predict(){
  const model = await tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL);
  var input_x = tf.tensor([[2714,    0,    0,   0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0]],shape=[1,50],dtype='int32');
  var dropout_keep_prob = 1.0;
  var output = model.execute({dropout_keep_prob:dropout_keep_prob, input_x:input_x});
  console.log(output);
}
predict();

这是关于我的模型的一些事情:

self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name="input_x")
self.input_y = tf.placeholder(tf.float32, [None, num_classes], name="input_y")
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
self.scores = tf.nn.xw_plus_b(self.h_drop, W, b, name="scores")
self.predictions = tf.argmax(self.scores, 1, name="predictions")
tf.saved_model.simple_save(sess, "./saved_model",
                           inputs={"input_x": cnn.input_x, }, outputs={"predictions": cnn.predictions,"scores":cnn.scores,})

我只是像那个网页一样转换我的 saved_model 。

这是 Python tensorflow 输入,如你所知,模型曾经是 python 脚本,我想将其转换为 tfjs。

inputs_major = np.zeros(shape=[1,max_seq_length], dtype=np.int32)
for v in range(len(vec)):
    inputs_major[0][v] = vec[v]

我更新了 tf.js,但 loadFrozenModel 已被删除,我将其更改为 loadGraphModel,错误是“未捕获(承诺中)错误:无法解析来自 ./web_model/tensorflowjs_model.pb 的响应的模型 JSON。您的路径包含 . pb 文件扩展名。TensorFlow.js 1.0 中删除了对 .pb 模型的支持,转而支持 .json 模型。您可以使用 TensorFlow.js 1.0 转换脚本重新转换您的 Python TensorFlow 模型,也可以使用以下命令转换您的 .pb 模型tensorflow/tfjs-converter 存储库中的 'pb2json'NPM 脚本。”

所以我尝试使用 tensorflowjs 转换器 1.0.1 而不是 0.8,而我的 tf 版本是 1.13。

错误是“tensorflow.python.eager.lift_to_graph.UnliftableError:无法提升张量,因为它通过至少一条路径传递依赖于占位符,例如:IdentityN(IdentityN)<-scores(BiasAdd)<-scores/MatMul(MatMul) <- dropout/dropout/mul (Mul) <- dropout/dropout/Floor (Floor) <- dropout/dropout/add (Add) <- dropout_keep_prob (Placeholder)"

我认为是因为我错误的saved_model,

self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name="input_x")
self.input_y = tf.placeholder(tf.float32, [None, num_classes], name="input_y")
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
self.scores = tf.nn.xw_plus_b(self.h_drop, W, b, name="scores")
self.predictions = tf.argmax(self.scores, 1, name="predictions")
tf.saved_model.simple_save(sess, "./saved_model",
                           inputs={"input_x": cnn.input_x,},
                           outputs={"predictions": cnn.predictions, "scores": cnn.scores, })

所以我改变了我的代码。

tf.saved_model.simple_save(sess, "./saved_model",
                           inputs={"input_x": cnn.input_x, "dropout_keep_prob":cnn.dropout_keep_prob,},
                           outputs={"predictions": cnn.predictions, "scores": cnn.scores, })

保存时“警告:tensorflow:来自 D:\Python\Python35\lib\site-packages\tensorflow\python\saved_model\signature_def_utils_impl.py:205: build_tensor_info(来自 tensorflow.python.saved_model.utils_impl)已弃用并将被删除在未来的版本中。更新说明:此功能将仅通过 v1 兼容库作为 tf.compat.v1.saved_model.utils.build_tensor_info 或 tf.compat.v1.saved_model.build_tensor_info 提供。"

错误是“由于缺少 TensorBoard 安装而导致 tf.compat.v2.summary API 受限 2019-03-20 19:43:18.894836: I tensorflow/core/grappler/devices.cc:53] 符合条件的 GPU 数量(核心数 > = 8):0(注意:TensorFlow未在CUDA支持下编译)2019-03-20 19:43:18.909183:I tensorflow/core/grappler/clusters/single_machine.cc:359]开始新会话2019-03-20 19:43:18.931823:I tensorflow/core/platform/cpu_feature_guard.cc:142]您的 CPU 支持未编译此 TensorFlow 二进制文件以使用的指令:AVX2 2019-03-20 19:43:18.989768:E tensorflow/core/ grappler/grappler_item_builder.cc:636] Init node embedding/W/Assign 在图 Traceback 中不存在(最近一次调用最后一次):文件“d:\anaconda3\lib\site-packages\tensorflow\python\grappler\tf_optimizer. py",第 43 行,OptimizeGraph 详细,graph_id,状态)SystemError:返回NULL而不设置错误

在处理上述异常的过程中,又出现了一个异常:

Traceback(最近一次调用最后一次):文件“d:\anaconda3\lib\runpy.py”,第 193 行,在 _run_module_as_main“ main ”中", mod_spec) 文件 "d:\anaconda3\lib\runpy.py",第 85 行,在 _run_code exec(code, run_globals) 文件 "D:\Anaconda3\Scripts\tensorflowjs_converter.exe__main__.py",第 9 行,在文件中“d:\anaconda3\lib\site-packages\tensorflowjs\converters\converter.py”,第 358 行,在主 strip_debug_ops=FLAGS.strip_debug_ops 文件“d:\anaconda3\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2 .py”,第 271 行,convert_tf_saved_model 具体_func) 文件“d:\anaconda3\lib\site-packages\tensorflow\python\framework\convert_to_constants.py”,第 140 行,convert_variables_to_constants_v2 graph_def = _run_inline_graph_optimization(func) 文件“d: \anaconda3\lib\site-packages\tensorflow\python\framework\convert_to_constants.py”,第 59 行,在 _run_inline_graph_optimization 返回 tf_optimizer.OptimizeGraph(config, meta_graph) 文件“d:\anaconda3\lib\site-packages\tensorflow\python\grappler\tf_optimizer.py”,第 43 行,在 OptimizeGraph 详细,graph_id,状态)文件“d: \anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py”,第 548 行,在退出 c_api.TF_GetCode(self.status.status)) tensorflow.python.framework.errors_impl.InvalidArgumentError:导入元图失败,请查看错误日志以获取更多信息。”

我希望你能给我一些帮助,这个问题几乎让我发疯。谢谢!

4

1 回答 1

0

最后还是放弃了,因为最新版本去掉了loadFrozenModel,支持的很少。我尝试使用 keras 模型并且它有效。

于 2019-03-24T01:22:43.597 回答