前提・実現したいこと
kerasで作成した学習データをandroidで動かしてみたいと思っています。
kerasのh5ファイルからtensorflowのpbファイル変換しようと思ったですが
AssertionError: dense_2/Sigmoid is not in graph
が出ていて解決ができません。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "C:\Users\xxx\Desktop\python\tinn\keras_to_tensorflow.py", line 29, in <module> main() File "C:\Users\xxx\Desktop\python\tinn\keras_to_tensorflow.py", line 26, in main freeze_graph(session, [out.op.name for out in model.outputs], save_pb_dir=save_pb_dir) File "C:\Users\xxx\Desktop\python\tinn\keras_to_tensorflow.py", line 12, in freeze_graph graphdef_frozen = tf.compat.v1.graph_util.convert_variables_to_constants(session, graphdef_inf, output) File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func return func(*args, **kwargs) File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\graph_util_impl.py", line 359, in convert_variables_to_constants inference_graph = extract_sub_graph(input_graph_def, output_node_names) File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func return func(*args, **kwargs) File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\graph_util_impl.py", line 205, in extract_sub_graph _assert_nodes_are_present(name_to_node, dest_nodes) File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\graph_util_impl.py", line 160, in _assert_nodes_are_present assert d in name_to_node, "%s is not in graph" % d AssertionError: dense_2/Sigmoid is not in graph
該当のソースコード
import tensorflow as tf from keras.models import load_model from keras import backend as K from tensorflow.python.framework import graph_io from tensorflow.python.framework import graph_util def freeze_graph(session, output, save_pb_dir='.', save_pb_name='frozen_model.pb', save_pb_as_text=False): graph = session.graph with graph.as_default(): graphdef_inf = tf.compat.v1.graph_util.remove_training_nodes(graph.as_graph_def()) graphdef_frozen = tf.compat.v1.graph_util.convert_variables_to_constants(session, graphdef_inf, output) graph_io.write_graph(graphdef_frozen, save_pb_dir, save_pb_name, as_text=save_pb_as_text) return graphdef_frozen def main(): save_pb_dir = 'dddd.pb' keras_model = 'best.h5' K.clear_session() # This line must be executed before loading Keras model. K.set_learning_phase(0) model = load_model(keras_model) session = tf.compat.v1.keras.backend.get_session() freeze_graph(session, [out.op.name for out in model.outputs], save_pb_dir=save_pb_dir) if __name__ == '__main__': main()
###自分で調べたことや試したこと
kerasのモデル構築部分を
model.add(layers.Dense(7,activation="dense_2/sigmoid"))
したりしてみました
補足情報(FW/ツールのバージョンなど)
tensorflow 2.2.0
Keras 2.3.1
windows 10
あなたの回答
tips
プレビュー