xmxoxo / BERT-train2deploy

BERT模型从训练到部署
517 stars 165 forks source link

关于压缩pb #3

Open tian231825 opened 5 years ago

tian231825 commented 5 years ago

您好,在freeze_graph那一步中,您的line17 使用了import modeling,请问这个是项目提供的吗?为什么一直报找不到modeling

xmxoxo commented 5 years ago

这个modeling就是BERT的源码,请到 https://github.com/google-research/bert 后面我把modeling目录补充更新上去下。

xmxoxo commented 5 years ago

modeling.py文件已经补充更新。

tian231825 commented 5 years ago

后续在python freeze_graph的过程中设置num——labels后 UnboundLocalError: local variable 'num_labels' referenced before assignment

E:CLASSIFICATION_MODEL, Lodding...:[fre:opt:200]:fail to optimize the graph! local variable 'num_labels' referenced before assignment Traceback (most recent call last): File "freeze_graph.py", line 176, in optimize_class_model input_ids=input_ids, input_mask=input_mask, segment_ids=None, labels=None, num_labels=num_labels) UnboundLocalError: local variable 'num_labels' referenced before assignment 想请教一下这个怎么改

tian231825 commented 5 years ago

如果num——labels使用默认则会出现 E:CLASSIFICATION_MODEL, Lodding...:[fre:opt:200]:fail to optimize the graph! init_predict_var() missing 1 required positional argument: 'path' Traceback (most recent call last): File "freeze_graph.py", line 164, in optimize_class_model num_labels, label2id, id2label = init_predict_var() TypeError: init_predict_var() missing 1 required positional argument: 'path'

tian231825 commented 5 years ago

是label2id.pkl的路径?但是在参数里也无法进行设置啊难道是直接在函数里面改吗

xmxoxo commented 5 years ago

未指定num_labels参数时报错的bug已经处理,会从model_dir目录下的”label2id.pkl"文件读取

tian231825 commented 5 years ago

NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key output_bias not found in checkpoint

Key output_bias 具体是指什么找不到呢

xmxoxo commented 5 years ago

检查下各个参数是否指定正确:

tian231825 commented 5 years ago

错误疑似发生在对于预训练模型的识别上,我直接选定的是原有的含config.json、vocab.txt等5个文件的那个目录(我把那个chinese_L-12_H-768_A-12的5个文件放入一个叫做checkpoint的目录下)然后好像就会报如下的错误:

2019-05-24 15:07:48.180114: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: Key output_bias not found in checkpoint E:CLASSIFICATION_MODEL, Lodding...:[fre:opt:207]:fail to optimize the graph! Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key output_bias not found in checkpoint [[node save/RestoreV2 (defined at freeze_graph.py:189) ]]

Caused by op 'save/RestoreV2', defined at: File "freeze_graph.py", line 240, in optimize_class_model(args, logger=None) File "freeze_graph.py", line 189, in optimize_class_model saver = tf.train.Saver() File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 832, in init self.build() File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 844, in build self._build(self._filename, build_save=True, build_restore=True) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 881, in _build build_save=build_save, build_restore=build_restore) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 513, in _build_internal restore_sequentially, reshape) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 332, in _AddRestoreOps restore_sequentially) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 580, in bulk_restore return io_ops.restore_v2(filename_tensor, names, slices, dtypes) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_io_ops.py", line 1572, in restore_v2 name=name) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper op_def=op_def) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op op_def=op_def) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1801, in init self._traceback = tf_stack.extract_stack()

NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key output_bias not found in checkpoint [[node save/RestoreV2 (defined at freeze_graph.py:189) ]] Traceback (most recent call last): File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call return fn(*args) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn options, feed_dict, fetch_list, target_list, run_metadata) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.NotFoundError: Key output_bias not found in checkpoint [[{{node save/RestoreV2}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1276, in restore {self.saver_def.filename_tensor_name: save_path}) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run run_metadata_ptr) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _run feed_dict_tensor, options, run_metadata) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run run_metadata) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.NotFoundError: Key output_bias not found in checkpoint [[node save/RestoreV2 (defined at freeze_graph.py:189) ]]

Caused by op 'save/RestoreV2', defined at: File "freeze_graph.py", line 240, in optimize_class_model(args, logger=None) File "freeze_graph.py", line 189, in optimize_class_model saver = tf.train.Saver() File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 832, in init self.build() File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 844, in build self._build(self._filename, build_save=True, build_restore=True) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 881, in _build build_save=build_save, build_restore=build_restore) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 513, in _build_internal restore_sequentially, reshape) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 332, in _AddRestoreOps restore_sequentially) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 580, in bulk_restore return io_ops.restore_v2(filename_tensor, names, slices, dtypes) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_io_ops.py", line 1572, in restore_v2 name=name) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper op_def=op_def) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op op_def=op_def) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1801, in init self._traceback = tf_stack.extract_stack()

NotFoundError (see above for traceback): Key output_bias not found in checkpoint [[node save/RestoreV2 (defined at freeze_graph.py:189) ]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1286, in restore names_to_keys = object_graph_key_mapping(save_path) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1591, in object_graph_key_mapping checkpointable.OBJECT_GRAPH_PROTO_KEY) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 370, in get_tensor status) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in exit c_api.TF_GetCode(self.status.status)) tensorflow.python.framework.errors_impl.NotFoundError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "freeze_graph.py", line 195, in optimize_class_model saver.restore(sess,latest_checkpoint ) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1292, in restore err, "a Variable name or other graph key that is missing") tensorflow.python.framework.errors_impl.NotFoundError: Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key output_bias not found in checkpoint [[node save/RestoreV2 (defined at freeze_graph.py:189) ]]

Caused by op 'save/RestoreV2', defined at: File "freeze_graph.py", line 240, in optimize_class_model(args, logger=None) File "freeze_graph.py", line 189, in optimize_class_model saver = tf.train.Saver() File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 832, in init self.build() File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 844, in build self._build(self._filename, build_save=True, build_restore=True) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 881, in _build build_save=build_save, build_restore=build_restore) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 513, in _build_internal restore_sequentially, reshape) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 332, in _AddRestoreOps restore_sequentially) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 580, in bulk_restore return io_ops.restore_v2(filename_tensor, names, slices, dtypes) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_io_ops.py", line 1572, in restore_v2 name=name) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper op_def=op_def) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op op_def=op_def) File "/home/junruit/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1801, in init self._traceback = tf_stack.extract_stack()

NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key output_bias not found in checkpoint [[node save/RestoreV2 (defined at freeze_graph.py:189) ]]

tian231825 commented 5 years ago

是不是freeze.graph 第193行那个路径是不是应该是bert-model_dir预训练模型路径?而不是model_dir(output_dir)

wangzhe258369 commented 5 years ago

既然用了freeze_graph,把tensorflow的optimize_for_inference也用一下吧

zwd13122889 commented 4 years ago

@tian231825 我遇到了和你一样的问题,请问您最后是怎么解决的呢