Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于压缩pb #3

Open
tian231825 opened this issue May 23, 2019 · 12 comments
Open

关于压缩pb #3

tian231825 opened this issue May 23, 2019 · 12 comments

Comments

@tian231825
Copy link

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

@xmxoxo
Copy link
Owner

xmxoxo commented May 23, 2019

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

@xmxoxo xmxoxo closed this as completed May 23, 2019
@xmxoxo
Copy link
Owner

xmxoxo commented May 23, 2019

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

@xmxoxo xmxoxo reopened this May 23, 2019
@tian231825
Copy link
Author

后续在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
Copy link
Author

如果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
Copy link
Author

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

@xmxoxo
Copy link
Owner

xmxoxo commented May 24, 2019

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

@tian231825
Copy link
Author

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
Copy link
Owner

xmxoxo commented May 24, 2019

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

  • bert_model_dir 参数是BERT预训练模型
  • model_dir 就是训练好的.ckpt文件所在的目录
  • model_pb_dir 参数是转换完成的pb文件保存的目录,默认为model_dir
  • max_seq_len 要与原来一致;
  • num_labels 是分类标签的个数,本例中是3个,如果省略这个参数,会自动从model_dir指定的目录下寻找"label2id.pkl"文件读取

@tian231825
Copy link
Author

错误疑似发生在对于预训练模型的识别上,我直接选定的是原有的含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
Copy link
Author

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

@wangzhe258369
Copy link

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

@zwd13122889
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants